IPCWorker.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Timer.h"
  23. #include "../IO/Log.h"
  24. #include "IPCWorker.h"
  25. #include "IPCMessage.h"
  26. #ifdef ATOMIC_PLATFORM_WINDOWS
  27. #include "IPCWindows.h"
  28. #else
  29. #include "IPCUnix.h"
  30. #include <unistd.h>
  31. #endif
  32. namespace Atomic
  33. {
  34. IPCWorker::IPCWorker(Context* context, IPCHandle clientRead, IPCHandle clientWrite, unsigned id) : IPCChannel(context, id)
  35. {
  36. #ifndef ATOMIC_PLATFORM_WINDOWS
  37. assert(0); // wrong constructor
  38. #else
  39. otherProcess_ = new IPCProcess(context_, clientRead, clientWrite, INVALID_IPCHANDLE_VALUE);
  40. if (!transport_.OpenClient(clientRead, clientWrite))
  41. {
  42. ATOMIC_LOGERRORF("Unable to open IPC transport clientRead = %i", clientRead);
  43. shouldRun_ = false;
  44. return;
  45. }
  46. ATOMIC_LOGERRORF("Opened IPC transport fd = %i", clientRead);
  47. #endif
  48. }
  49. IPCWorker::IPCWorker(Context* context, IPCHandle fd, unsigned id) : IPCChannel(context, id),
  50. clientRead_(fd),
  51. clientWrite_(fd)
  52. {
  53. #ifdef ATOMIC_PLATFORM_WINDOWS
  54. assert(0); // wrong constructor
  55. #else
  56. otherProcess_ = new IPCProcess(context_, -1, clientRead_, getppid());
  57. if (!transport_.OpenClient(clientRead_))
  58. {
  59. ATOMIC_LOGERRORF("Unable to open IPC transport fd = %i", clientRead_);
  60. shouldRun_ = false;
  61. return;
  62. }
  63. ATOMIC_LOGERRORF("Opened IPC transport fd = %i", clientRead_);
  64. #endif
  65. }
  66. IPCWorker::~IPCWorker()
  67. {
  68. }
  69. bool IPCWorker::Update()
  70. {
  71. if (otherProcess_.Null())
  72. return false;
  73. if (!shouldRun_)
  74. {
  75. Stop();
  76. return false;
  77. }
  78. return true;
  79. }
  80. void IPCWorker::ThreadFunction()
  81. {
  82. while (shouldRun_)
  83. {
  84. // On windows we use a job object to control process lifetime, we don't have a
  85. // parent pid (these change and are reused on Windows, so we would need to DuplicateHandle and pass
  86. // to child on command line
  87. #ifndef ATOMIC_PLATFORM_WINDOWS
  88. if (!otherProcess_->IsRunning())
  89. {
  90. break;
  91. }
  92. #endif
  93. if (!Receive())
  94. {
  95. break;
  96. }
  97. Time::Sleep(10);
  98. }
  99. shouldRun_ = false;
  100. }
  101. }