net.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. commands_deque_lock.lock();
  43. commands_inflight.push_back({header, data, len});
  44. if (commands_inflight.size() == 1) {
  45. async_send_commands();
  46. }
  47. commands_deque_lock.unlock();
  48. }
  49. void NodeIO::async_send_commands()
  50. {
  51. std::vector<boost::asio::const_buffer> tosend;
  52. CommandTuple *commandp = &(commands_inflight.front());
  53. tosend.push_back(boost::asio::buffer(&(std::get<0>(*commandp)), 5));
  54. if (std::get<1>(*commandp) != NULL && std::get<2>(*commandp) > 0) {
  55. tosend.push_back(boost::asio::buffer(std::get<1>(*commandp),
  56. std::get<2>(*commandp)));
  57. }
  58. boost::asio::async_write(sock, tosend,
  59. [this, commandp](boost::system::error_code, std::size_t){
  60. // When the write completes, pop the command from the deque
  61. // (which should now be in the front)
  62. commands_deque_lock.lock();
  63. assert(!commands_inflight.empty() &&
  64. &(commands_inflight.front()) == commandp);
  65. uint8_t *data = std::get<1>(*commandp);
  66. commands_inflight.pop_front();
  67. if (commands_inflight.size() > 0) {
  68. async_send_commands();
  69. }
  70. // And return the frame
  71. return_frame(data);
  72. commands_deque_lock.unlock();
  73. });
  74. }
  75. void NodeIO::send_epoch(uint32_t epoch_num)
  76. {
  77. uint64_t header = (uint64_t(epoch_num) << 8) + 0x00;
  78. send_header_data(header, NULL, 0);
  79. }
  80. void NodeIO::send_message_header(uint32_t tot_message_len)
  81. {
  82. uint64_t header = (uint64_t(tot_message_len) << 8) + 0x01;
  83. send_header_data(header, NULL, 0);
  84. // If we're sending a new message header, we have to have finished
  85. // sending the previous message.
  86. assert(chunksize_inflight == msgsize_inflight);
  87. msgsize_inflight = tot_message_len;
  88. chunksize_inflight = 0;
  89. }
  90. void NodeIO::send_chunk(uint8_t *data, uint32_t chunk_len)
  91. {
  92. assert(chunk_len <= MAXCHUNKSIZE);
  93. uint64_t header = (uint64_t(chunk_len) << 8) + 0x02;
  94. send_header_data(header, data, chunk_len);
  95. chunksize_inflight += chunk_len;
  96. assert(chunksize_inflight <= msgsize_inflight);
  97. }
  98. void NodeIO::recv_commands(
  99. std::function<void(boost::system::error_code)> error_cb,
  100. std::function<void(uint32_t)> epoch_cb,
  101. std::function<void(uint32_t)> message_cb,
  102. std::function<void(uint8_t*,uint32_t)> chunk_cb)
  103. {
  104. // Asynchronously read the header
  105. receive_header = 0;
  106. boost::asio::async_read(sock, boost::asio::buffer(&receive_header, 5),
  107. [this, error_cb, epoch_cb, message_cb, chunk_cb]
  108. (boost::system::error_code ec, std::size_t) {
  109. if (ec) {
  110. error_cb(ec);
  111. return;
  112. }
  113. if ((receive_header & 0xff) == 0x00) {
  114. epoch_cb(uint32_t(receive_header >> 8));
  115. recv_commands(error_cb, epoch_cb, message_cb, chunk_cb);
  116. } else if ((receive_header & 0xff) == 0x01) {
  117. assert(recv_msgsize_inflight == recv_chunksize_inflight);
  118. recv_msgsize_inflight = uint32_t(receive_header >> 8);
  119. recv_chunksize_inflight = 0;
  120. message_cb(recv_msgsize_inflight);
  121. recv_commands(error_cb, epoch_cb, message_cb, chunk_cb);
  122. } else if ((receive_header & 0xff) == 0x02) {
  123. uint32_t this_chunk_size = uint32_t(receive_header >> 8);
  124. assert(recv_chunksize_inflight + this_chunk_size <=
  125. recv_msgsize_inflight);
  126. recv_chunksize_inflight += this_chunk_size;
  127. boost::asio::async_read(sock, boost::asio::buffer(
  128. receive_frame, this_chunk_size),
  129. [this, error_cb, epoch_cb, message_cb, chunk_cb,
  130. this_chunk_size]
  131. (boost::system::error_code ecc, std::size_t) {
  132. if (ecc) {
  133. error_cb(ecc);
  134. return;
  135. }
  136. chunk_cb(receive_frame, this_chunk_size);
  137. recv_commands(error_cb, epoch_cb,
  138. message_cb, chunk_cb);
  139. });
  140. } else {
  141. error_cb(boost::system::errc::make_error_code(
  142. boost::system::errc::errc_t::invalid_argument));
  143. }
  144. });
  145. }
  146. NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
  147. : conf(config), myconf(config.nodes[config.my_node_num])
  148. {
  149. num_nodes = conf.nodes.size();
  150. nodeios.resize(num_nodes);
  151. me = conf.my_node_num;
  152. // Node number n will accept connections from nodes 0, ..., n-1 and
  153. // make connections to nodes n+1, ..., num_nodes-1. This is all
  154. // single threaded, but it doesn't deadlock because node 0 isn't
  155. // waiting for any incoming connections, so it immediately makes
  156. // outgoing connections. When it connects to node 1, that node
  157. // accepts its (only) incoming connection, and then starts making
  158. // its outgoing connections, etc.
  159. tcp::resolver resolver(io_context);
  160. tcp::acceptor acceptor(io_context,
  161. resolver.resolve(myconf.listenhost, myconf.listenport)->endpoint());
  162. for(size_t i=0; i<me; ++i) {
  163. #ifdef VERBOSE_NET
  164. std::cerr << "Accepting number " << i << "\n";
  165. #endif
  166. tcp::socket nodesock = acceptor.accept();
  167. #ifdef VERBOSE_NET
  168. std::cerr << "Accepted number " << i << "\n";
  169. #endif
  170. // Read 2 bytes from the socket, which will be the
  171. // connecting node's node number
  172. unsigned short node_num;
  173. boost::asio::read(nodesock,
  174. boost::asio::buffer(&node_num, sizeof(node_num)));
  175. if (node_num >= num_nodes) {
  176. std::cerr << "Received bad node number\n";
  177. } else {
  178. nodeios[node_num].emplace(std::move(nodesock));
  179. #ifdef VERBOSE_NET
  180. std::cerr << "Received connection from " <<
  181. config.nodes[node_num].name << "\n";
  182. #endif
  183. }
  184. }
  185. for(size_t i=me+1; i<num_nodes; ++i) {
  186. boost::system::error_code err;
  187. tcp::socket nodesock(io_context);
  188. while(1) {
  189. #ifdef VERBOSE_NET
  190. std::cerr << "Connecting to " << config.nodes[i].name << "...\n";
  191. #endif
  192. boost::asio::connect(nodesock,
  193. resolver.resolve(config.nodes[i].listenhost,
  194. config.nodes[i].listenport), err);
  195. if (!err) break;
  196. std::cerr << "Connection to " << config.nodes[i].name <<
  197. " refused, will retry.\n";
  198. sleep(1);
  199. }
  200. // Write 2 bytes to the socket to tell the peer node our node
  201. // number
  202. unsigned short node_num = (unsigned short)me;
  203. boost::asio::write(nodesock,
  204. boost::asio::buffer(&node_num, sizeof(node_num)));
  205. nodeios[i].emplace(std::move(nodesock));
  206. #ifdef VERBOSE_NET
  207. std::cerr << "Connected to " << config.nodes[i].name << "\n";
  208. #endif
  209. }
  210. }