IPCWindows.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #pragma once
  24. #include "../Core/Mutex.h"
  25. #include "../Core/Thread.h"
  26. #include "../Core/Object.h"
  27. #include "IPCTypes.h"
  28. namespace Atomic
  29. {
  30. #define ATOMIC_WINDOWS_IPC_BUFFER_SIZE 1048576
  31. class PipePair {
  32. public:
  33. PipePair();
  34. IPCHandle serverRead() const { return srvRead_; }
  35. IPCHandle serverWrite() const { return srvWrite_; }
  36. IPCHandle clientRead() const { return clnRead_; }
  37. IPCHandle clientWrite() const { return clnWrite_; }
  38. static IPCHandle OpenPipeServer(const wchar_t* name, bool read);
  39. static IPCHandle OpenPipeClient(const wchar_t* name, bool read);
  40. private:
  41. IPCHandle srvRead_;
  42. IPCHandle srvWrite_;
  43. IPCHandle clnRead_;
  44. IPCHandle clnWrite_;
  45. };
  46. class PipeWin {
  47. public:
  48. PipeWin();
  49. ~PipeWin();
  50. bool OpenClient(IPCHandle pipeRead, IPCHandle pipeWrite);
  51. bool OpenServer(IPCHandle pipeRead, IPCHandle pipeWrite);
  52. bool Write(const void* buf, size_t sz);
  53. bool Read(void* buf, size_t* sz);
  54. bool IsConnected() const { return pipeRead_ != INVALID_IPCHANDLE_VALUE && pipeWrite_ != INVALID_IPCHANDLE_VALUE; }
  55. private:
  56. class ReaderThread : public Thread
  57. {
  58. public:
  59. ReaderThread(PipeWin* pipeWin) : pipeWin_(pipeWin), readSize_(0)
  60. {
  61. buf_.Resize(ATOMIC_WINDOWS_IPC_BUFFER_SIZE);
  62. }
  63. void Kill();
  64. void ThreadFunction();
  65. Mutex mutex_;
  66. PipeWin* pipeWin_;
  67. PODVector<char> buf_;
  68. unsigned readSize_;
  69. };
  70. IPCHandle pipeRead_;
  71. IPCHandle pipeWrite_;
  72. ReaderThread readerThread_;
  73. };
  74. class PipeTransport : public PipeWin {
  75. public:
  76. static const size_t kBufferSz = ATOMIC_WINDOWS_IPC_BUFFER_SIZE;
  77. bool Send(const void* buf, size_t sz) {
  78. return Write(buf, sz);
  79. }
  80. char* Receive(size_t* size);
  81. private:
  82. PODVector<char> buf_;
  83. };
  84. class ATOMIC_API IPCProcess : public Object
  85. {
  86. ATOMIC_OBJECT(IPCProcess, Object)
  87. public:
  88. IPCProcess(Context* context, IPCHandle clientRead, IPCHandle clientWrite, IPCHandle pid = INVALID_IPCHANDLE_VALUE);
  89. virtual ~IPCProcess();
  90. bool IsRunning();
  91. bool Terminate();
  92. IPCHandle clientRead() const { return clientRead_; }
  93. IPCHandle clientWrite() const { return clientWrite_; }
  94. bool Launch(const String& command, const Vector<String>& args, const String& initialDirectory);
  95. private:
  96. IPCHandle pid_;
  97. IPCHandle clientRead_;
  98. IPCHandle clientWrite_;
  99. };
  100. }
  101. #endif