IPCWorker.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "../IO/Log.h"
  23. #include "IPCWorker.h"
  24. #include "IPCMessage.h"
  25. #ifdef ATOMIC_PLATFORM_WINDOWS
  26. #include "IPCWindows.h"
  27. #else
  28. #include "IPCUnix.h"
  29. #include <unistd.h>
  30. #endif
  31. namespace Atomic
  32. {
  33. IPCWorker::IPCWorker(Context* context, IPCHandle clientRead, IPCHandle clientWrite, unsigned id) : IPCChannel(context, id)
  34. {
  35. #ifndef ATOMIC_PLATFORM_WINDOWS
  36. assert(0); // wrong constructor
  37. #else
  38. otherProcess_ = new IPCProcess(context_, clientRead, clientWrite, INVALID_IPCHANDLE_VALUE);
  39. if (!transport_.OpenClient(clientRead, clientWrite))
  40. {
  41. LOGERRORF("Unable to open IPC transport clientRead = %i", clientRead);
  42. shouldRun_ = false;
  43. return;
  44. }
  45. LOGERRORF("Opened IPC transport fd = %i", clientRead);
  46. #endif
  47. }
  48. IPCWorker::IPCWorker(Context* context, IPCHandle fd, unsigned id) : IPCChannel(context, id),
  49. clientRead_(fd),
  50. clientWrite_(fd)
  51. {
  52. #ifdef ATOMIC_PLATFORM_WINDOWS
  53. assert(0); // wrong constructor
  54. #else
  55. otherProcess_ = new IPCProcess(context_, -1, clientRead_, getppid());
  56. if (!transport_.OpenClient(clientRead_))
  57. {
  58. LOGERRORF("Unable to open IPC transport fd = %i", clientRead_);
  59. shouldRun_ = false;
  60. return;
  61. }
  62. LOGERRORF("Opened IPC transport fd = %i", clientRead_);
  63. #endif
  64. }
  65. IPCWorker::~IPCWorker()
  66. {
  67. }
  68. bool IPCWorker::Update()
  69. {
  70. if (otherProcess_.Null())
  71. return false;
  72. if (!shouldRun_)
  73. {
  74. Stop();
  75. return false;
  76. }
  77. return true;
  78. }
  79. void IPCWorker::ThreadFunction()
  80. {
  81. while (shouldRun_)
  82. {
  83. if (!otherProcess_->IsRunning())
  84. {
  85. break;
  86. }
  87. if (!Receive())
  88. {
  89. break;
  90. }
  91. }
  92. shouldRun_ = false;
  93. }
  94. }