net.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <iostream>
  2. #include "config.hpp"
  3. #include "net.hpp"
  4. NodeIO::NodeIO(tcp::socket &&socket) : sock(std::move(socket))
  5. {
  6. }
  7. uint8_t *NodeIO::request_frame()
  8. {
  9. if (frames_available.empty()) {
  10. // Allocate a new frame. Note that this memory will (at this
  11. // time) never get deallocated. In theory, we could deallocate
  12. // it in return_frame, but if a certain number of frames were
  13. // allocated here, it means we had that much data in flight
  14. // (queued but not accepted for sending by the OS), and we're
  15. // likely to need that much again. Subsequent messages will
  16. // _reuse_ the allocated data, though, so the used memory won't
  17. // grow forever, and will be limited to the amount of in-flight
  18. // data needed.
  19. return new uint8_t[MAXCHUNKSIZE];
  20. }
  21. // Copy the pointer to the frame out of the deque and remove it from
  22. // the deque. Note this is _not_ taking the address of the element
  23. // *in* the deque (and then popping it, which would invalidate that
  24. // pointer).
  25. frame_deque_lock.lock();
  26. uint8_t *frame = frames_available.back();
  27. frames_available.pop_back();
  28. frame_deque_lock.unlock();
  29. return frame;
  30. }
  31. void NodeIO::return_frame(uint8_t *frame)
  32. {
  33. if (!frame) return;
  34. // We push the frame back on to the end of the deque so that it will
  35. // be the next one used. This may lead to better cache behaviour?
  36. frame_deque_lock.lock();
  37. frames_available.push_back(frame);
  38. frame_deque_lock.unlock();
  39. }
  40. void NodeIO::send_header_data(uint64_t header, uint8_t *data, size_t len)
  41. {
  42. std::vector<boost::asio::const_buffer> tosend;
  43. // Put the header into the deque so it's in memory at a stable
  44. // address during the async write
  45. header_deque_lock.lock();
  46. headers_inflight.push_back(header);
  47. uint64_t *headerp = &(headers_inflight.back());
  48. header_deque_lock.unlock();
  49. tosend.push_back(boost::asio::buffer(headerp, 5));
  50. if (data != NULL && len > 0) {
  51. tosend.push_back(boost::asio::buffer(data, len));
  52. }
  53. boost::asio::async_write(sock, tosend,
  54. [this, headerp, data](boost::system::error_code, std::size_t){
  55. // When the write completes, pop the header from the deque
  56. // (which should now be in the front)
  57. header_deque_lock.lock();
  58. assert(!headers_inflight.empty() &&
  59. &(headers_inflight.front()) == headerp);
  60. headers_inflight.pop_front();
  61. header_deque_lock.unlock();
  62. // And return the frame
  63. return_frame(data);
  64. });
  65. }
  66. void NodeIO::send_epoch(uint32_t epoch_num)
  67. {
  68. uint64_t header = (uint64_t(epoch_num) << 8) + 0x00;
  69. send_header_data(header, NULL, 0);
  70. }
  71. void NodeIO::send_message_header(uint32_t tot_message_len)
  72. {
  73. uint64_t header = (uint64_t(tot_message_len) << 8) + 0x01;
  74. send_header_data(header, NULL, 0);
  75. // If we're sending a new message header, we have to have finished
  76. // sending the previous message.
  77. assert(chunksize_inflight == msgsize_inflight);
  78. msgsize_inflight = tot_message_len;
  79. chunksize_inflight = 0;
  80. }
  81. void NodeIO::send_chunk(uint8_t *data, uint32_t chunk_len)
  82. {
  83. assert(chunk_len <= MAXCHUNKSIZE);
  84. uint64_t header = (uint64_t(chunk_len) << 8) + 0x02;
  85. send_header_data(header, data, chunk_len);
  86. chunksize_inflight += chunk_len;
  87. assert(chunksize_inflight <= msgsize_inflight);
  88. }
  89. bool NodeIO::recv_header(uint64_t &header)
  90. {
  91. header = 0;
  92. try {
  93. boost::asio::read(sock, boost::asio::buffer(&header, 5));
  94. } catch (...) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. bool NodeIO::recv_chunk(uint64_t header, uint8_t *&data, size_t &len)
  100. {
  101. len = 0;
  102. data = NULL;
  103. assert((header & 0xff) == 0x02);
  104. size_t datalen = header >> 8;
  105. if (datalen > MAXCHUNKSIZE) {
  106. return false;
  107. }
  108. try {
  109. boost::asio::read(sock,
  110. boost::asio::buffer(receive_frame, datalen));
  111. } catch (...) {
  112. return false;
  113. }
  114. data = receive_frame;
  115. len = datalen;
  116. return true;
  117. }
  118. NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
  119. : conf(config), myconf(config.nodes[config.my_node_num])
  120. {
  121. num_nodes = conf.nodes.size();
  122. nodeios.resize(num_nodes);
  123. me = conf.my_node_num;
  124. // Node number n will accept connections from nodes 0, ..., n-1 and
  125. // make connections to nodes n+1, ..., num_nodes-1. This is all
  126. // single threaded, but it doesn't deadlock because node 0 isn't
  127. // waiting for any incoming connections, so it immediately makes
  128. // outgoing connections. When it connects to node 1, that node
  129. // accepts its (only) incoming connection, and then starts making
  130. // its outgoing connections, etc.
  131. tcp::resolver resolver(io_context);
  132. tcp::acceptor acceptor(io_context,
  133. resolver.resolve(myconf.listenhost, myconf.listenport)->endpoint());
  134. for(size_t i=0; i<me; ++i) {
  135. #ifdef VERBOSE_NET
  136. std::cerr << "Accepting number " << i << "\n";
  137. #endif
  138. tcp::socket nodesock = acceptor.accept();
  139. #ifdef VERBOSE_NET
  140. std::cerr << "Accepted number " << i << "\n";
  141. #endif
  142. // Read 2 bytes from the socket, which will be the
  143. // connecting node's node number
  144. unsigned short node_num;
  145. boost::asio::read(nodesock,
  146. boost::asio::buffer(&node_num, sizeof(node_num)));
  147. if (node_num >= num_nodes) {
  148. std::cerr << "Received bad node number\n";
  149. } else {
  150. nodeios[node_num].emplace(std::move(nodesock));
  151. #ifdef VERBOSE_NET
  152. std::cerr << "Received connection from " <<
  153. config.nodes[node_num].name << "\n";
  154. #endif
  155. }
  156. }
  157. for(size_t i=me+1; i<num_nodes; ++i) {
  158. boost::system::error_code err;
  159. tcp::socket nodesock(io_context);
  160. while(1) {
  161. #ifdef VERBOSE_NET
  162. std::cerr << "Connecting to " << config.nodes[i].name << "...\n";
  163. #endif
  164. boost::asio::connect(nodesock,
  165. resolver.resolve(config.nodes[i].listenhost,
  166. config.nodes[i].listenport), err);
  167. if (!err) break;
  168. std::cerr << "Connection to " << config.nodes[i].name <<
  169. " refused, will retry.\n";
  170. sleep(1);
  171. }
  172. // Write 2 bytes to the socket to tell the peer node our node
  173. // number
  174. unsigned short node_num = (unsigned short)me;
  175. boost::asio::write(nodesock,
  176. boost::asio::buffer(&node_num, sizeof(node_num)));
  177. nodeios[i].emplace(std::move(nodesock));
  178. #ifdef VERBOSE_NET
  179. std::cerr << "Connected to " << config.nodes[i].name << "\n";
  180. #endif
  181. }
  182. }