net.cpp 8.6 KB

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