IPCWindows.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Object.h"
  25. #include "IPCTypes.h"
  26. namespace Atomic
  27. {
  28. class PipePair {
  29. public:
  30. PipePair();
  31. IPCHandle serverRead() const { return srvRead_; }
  32. IPCHandle serverWrite() const { return srvWrite_; }
  33. IPCHandle clientRead() const { return clnRead_; }
  34. IPCHandle clientWrite() const { return clnWrite_; }
  35. static IPCHandle OpenPipeServer(const wchar_t* name, bool read);
  36. static IPCHandle OpenPipeClient(const wchar_t* name, bool read);
  37. private:
  38. IPCHandle srvRead_;
  39. IPCHandle srvWrite_;
  40. IPCHandle clnRead_;
  41. IPCHandle clnWrite_;
  42. };
  43. class PipeWin {
  44. public:
  45. PipeWin();
  46. ~PipeWin();
  47. bool OpenClient(IPCHandle pipeRead, IPCHandle pipeWrite);
  48. bool OpenServer(IPCHandle pipeRead, IPCHandle pipeWrite);
  49. bool Write(const void* buf, size_t sz);
  50. bool Read(void* buf, size_t* sz);
  51. bool IsConnected() const { return pipeRead_ != INVALID_IPCHANDLE_VALUE && pipeWrite_ != INVALID_IPCHANDLE_VALUE; }
  52. private:
  53. IPCHandle pipeRead_;
  54. IPCHandle pipeWrite_;
  55. };
  56. class PipeTransport : public PipeWin {
  57. public:
  58. static const size_t kBufferSz = 4096;
  59. bool Send(const void* buf, size_t sz) {
  60. return Write(buf, sz);
  61. }
  62. char* Receive(size_t* size);
  63. private:
  64. PODVector<char> buf_;
  65. };
  66. class IPCProcess : public Object
  67. {
  68. OBJECT(IPCProcess)
  69. public:
  70. IPCProcess(Context* context, IPCHandle clientRead, IPCHandle clientWrite, IPCHandle pid = INVALID_IPCHANDLE_VALUE);
  71. virtual ~IPCProcess();
  72. bool IsRunning();
  73. IPCHandle clientRead() const { return clientRead_; }
  74. IPCHandle clientWrite() const { return clientWrite_; }
  75. bool Launch(const String& command, const Vector<String>& args, const String& initialDirectory);
  76. private:
  77. IPCHandle pid_;
  78. IPCHandle clientRead_;
  79. IPCHandle clientWrite_;
  80. };
  81. }
  82. #endif