IPCChannel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "IPCChannel.h"
  24. namespace Atomic
  25. {
  26. IPCChannel::IPCChannel(Context* context, unsigned id) : Object(context),
  27. id_(id)
  28. {
  29. ipc_ = GetSubsystem<IPC>();
  30. currentHeader_.messageType_ = IPC_MESSAGE_UNDEFINED;
  31. }
  32. IPCChannel::~IPCChannel()
  33. {
  34. }
  35. void IPCChannel::PostMessage(StringHash eventType, VariantMap &eventData)
  36. {
  37. IPCMessageEvent msgEvent;
  38. msgEvent.DoSend(transport_, id_, eventType, eventData);
  39. }
  40. bool IPCChannel::Receive()
  41. {
  42. size_t sz = 0;
  43. const char* data = transport_.Receive(&sz);
  44. if (!data)
  45. {
  46. // error
  47. return false;
  48. }
  49. if (!sz)
  50. return true;
  51. dataBuffer_.Seek(dataBuffer_.GetSize());
  52. dataBuffer_.Write(data, (unsigned) sz);
  53. dataBuffer_.Seek(0);
  54. while (true)
  55. {
  56. if (currentHeader_.messageType_ == IPC_MESSAGE_UNDEFINED &&
  57. dataBuffer_.GetSize() - dataBuffer_.GetPosition() < sizeof(IPCMessageHeader))
  58. {
  59. return true;
  60. }
  61. if (currentHeader_.messageType_ == IPC_MESSAGE_UNDEFINED)
  62. {
  63. dataBuffer_.Read(&currentHeader_, sizeof(IPCMessageHeader));
  64. }
  65. if (currentHeader_.messageSize_ <= dataBuffer_.GetSize() - dataBuffer_.GetPosition())
  66. {
  67. MemoryBuffer buffer(dataBuffer_.GetData() + dataBuffer_.GetPosition(), currentHeader_.messageSize_);
  68. dataBuffer_.Seek( dataBuffer_.GetPosition() + currentHeader_.messageSize_);
  69. currentHeader_.messageType_ = IPC_MESSAGE_UNDEFINED;
  70. IPCMessageEvent event;
  71. StringHash eventType;
  72. VariantMap eventData;
  73. unsigned id;
  74. event.DoRead(buffer, id, eventType, eventData);
  75. ipc_->QueueEvent(id, eventType, eventData);
  76. }
  77. if (dataBuffer_.IsEof())
  78. {
  79. dataBuffer_.Clear();
  80. }
  81. if (dataBuffer_.GetPosition() == 0)
  82. break;
  83. VectorBuffer newBuffer;
  84. newBuffer.SetData(dataBuffer_.GetData() + dataBuffer_.GetPosition(), dataBuffer_.GetSize() - dataBuffer_.GetPosition());
  85. newBuffer.Seek(0);
  86. dataBuffer_ = newBuffer;
  87. }
  88. return true;
  89. }
  90. }