cmConnection.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmConnection.h"
  4. #include "cmServer.h"
  5. #include "cm_uv.h"
  6. #include <cassert>
  7. #include <cstring>
  8. struct write_req_t
  9. {
  10. uv_write_t req;
  11. uv_buf_t buf;
  12. };
  13. void cmEventBasedConnection::on_alloc_buffer(uv_handle_t* handle,
  14. size_t suggested_size,
  15. uv_buf_t* buf)
  16. {
  17. (void)(handle);
  18. char* rawBuffer = new char[suggested_size];
  19. *buf = uv_buf_init(rawBuffer, static_cast<unsigned int>(suggested_size));
  20. }
  21. void cmEventBasedConnection::on_read(uv_stream_t* stream, ssize_t nread,
  22. const uv_buf_t* buf)
  23. {
  24. auto conn = static_cast<cmEventBasedConnection*>(stream->data);
  25. if (conn) {
  26. if (nread >= 0) {
  27. conn->ReadData(std::string(buf->base, buf->base + nread));
  28. } else {
  29. conn->OnDisconnect(static_cast<int>(nread));
  30. }
  31. }
  32. delete[](buf->base);
  33. }
  34. void cmEventBasedConnection::on_close(uv_handle_t* /*handle*/)
  35. {
  36. }
  37. void cmEventBasedConnection::on_write(uv_write_t* req, int status)
  38. {
  39. (void)(status);
  40. // Free req and buffer
  41. write_req_t* wr = reinterpret_cast<write_req_t*>(req);
  42. delete[](wr->buf.base);
  43. delete wr;
  44. }
  45. void cmEventBasedConnection::on_new_connection(uv_stream_t* stream, int status)
  46. {
  47. (void)(status);
  48. auto conn = static_cast<cmEventBasedConnection*>(stream->data);
  49. if (conn) {
  50. conn->Connect(stream);
  51. }
  52. }
  53. bool cmEventBasedConnection::IsOpen() const
  54. {
  55. return this->WriteStream != nullptr;
  56. }
  57. void cmEventBasedConnection::WriteData(const std::string& _data)
  58. {
  59. #ifndef NDEBUG
  60. auto curr_thread_id = uv_thread_self();
  61. assert(this->Server);
  62. assert(uv_thread_equal(&curr_thread_id, &this->Server->ServeThreadId));
  63. #endif
  64. auto data = _data;
  65. assert(this->WriteStream.get());
  66. if (BufferStrategy) {
  67. data = BufferStrategy->BufferOutMessage(data);
  68. }
  69. auto ds = data.size();
  70. write_req_t* req = new write_req_t;
  71. req->req.data = this;
  72. req->buf = uv_buf_init(new char[ds], static_cast<unsigned int>(ds));
  73. memcpy(req->buf.base, data.c_str(), ds);
  74. uv_write(reinterpret_cast<uv_write_t*>(req), this->WriteStream, &req->buf, 1,
  75. on_write);
  76. }
  77. void cmEventBasedConnection::ReadData(const std::string& data)
  78. {
  79. this->RawReadBuffer += data;
  80. if (BufferStrategy) {
  81. std::string packet = BufferStrategy->BufferMessage(this->RawReadBuffer);
  82. while (!packet.empty()) {
  83. ProcessRequest(packet);
  84. packet = BufferStrategy->BufferMessage(this->RawReadBuffer);
  85. }
  86. } else {
  87. ProcessRequest(this->RawReadBuffer);
  88. this->RawReadBuffer.clear();
  89. }
  90. }
  91. cmEventBasedConnection::cmEventBasedConnection(
  92. cmConnectionBufferStrategy* bufferStrategy)
  93. : BufferStrategy(bufferStrategy)
  94. {
  95. }
  96. void cmEventBasedConnection::Connect(uv_stream_t* server)
  97. {
  98. (void)server;
  99. Server->OnConnected(nullptr);
  100. }
  101. void cmEventBasedConnection::OnDisconnect(int onerror)
  102. {
  103. (void)onerror;
  104. this->OnConnectionShuttingDown();
  105. if (this->Server) {
  106. this->Server->OnDisconnect(this);
  107. }
  108. }
  109. cmConnection::~cmConnection()
  110. {
  111. }
  112. bool cmConnection::OnConnectionShuttingDown()
  113. {
  114. this->Server = nullptr;
  115. return true;
  116. }
  117. void cmConnection::SetServer(cmServerBase* s)
  118. {
  119. Server = s;
  120. }
  121. void cmConnection::ProcessRequest(const std::string& request)
  122. {
  123. Server->ProcessRequest(this, request);
  124. }
  125. bool cmConnection::OnServeStart(std::string* errString)
  126. {
  127. (void)errString;
  128. return true;
  129. }
  130. bool cmEventBasedConnection::OnConnectionShuttingDown()
  131. {
  132. if (this->WriteStream.get()) {
  133. this->WriteStream->data = nullptr;
  134. }
  135. WriteStream.reset();
  136. return true;
  137. }