IPCWorker.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "../IO/Log.h"
  2. #include "IPCWorker.h"
  3. #include "IPCMessage.h"
  4. #ifdef ATOMIC_PLATFORM_WINDOWS
  5. #include "IPCWindows.h"
  6. #else
  7. #include "IPCUnix.h"
  8. #include <unistd.h>
  9. #endif
  10. namespace Atomic
  11. {
  12. IPCWorker::IPCWorker(Context* context, IPCHandle clientRead, IPCHandle clientWrite, unsigned id) : IPCChannel(context, id)
  13. {
  14. #ifndef ATOMIC_PLATFORM_WINDOWS
  15. assert(0); // wrong constructor
  16. #else
  17. otherProcess_ = new IPCProcess(context_, clientRead, clientWrite, INVALID_IPCHANDLE_VALUE);
  18. if (!transport_.OpenClient(clientRead, clientWrite))
  19. {
  20. LOGERRORF("Unable to open IPC transport clientRead = %i", clientRead);
  21. shouldRun_ = false;
  22. return;
  23. }
  24. LOGERRORF("Opened IPC transport fd = %i", clientRead);
  25. #endif
  26. }
  27. IPCWorker::IPCWorker(Context* context, IPCHandle fd, unsigned id) : IPCChannel(context, id),
  28. clientRead_(fd),
  29. clientWrite_(fd)
  30. {
  31. #ifdef ATOMIC_PLATFORM_WINDOWS
  32. assert(0); // wrong constructor
  33. #else
  34. otherProcess_ = new IPCProcess(context_, -1, clientRead_, getppid());
  35. if (!transport_.OpenClient(clientRead_))
  36. {
  37. LOGERRORF("Unable to open IPC transport fd = %i", clientRead_);
  38. shouldRun_ = false;
  39. return;
  40. }
  41. LOGERRORF("Opened IPC transport fd = %i", clientRead_);
  42. #endif
  43. }
  44. IPCWorker::~IPCWorker()
  45. {
  46. }
  47. bool IPCWorker::Update()
  48. {
  49. if (otherProcess_.Null())
  50. return false;
  51. if (!shouldRun_)
  52. {
  53. Stop();
  54. return false;
  55. }
  56. return true;
  57. }
  58. void IPCWorker::ThreadFunction()
  59. {
  60. while (shouldRun_)
  61. {
  62. if (!otherProcess_->IsRunning())
  63. {
  64. break;
  65. }
  66. if (!Receive())
  67. {
  68. break;
  69. }
  70. }
  71. shouldRun_ = false;
  72. }
  73. }