net.cpp 8.6 KB

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