|
@@ -170,47 +170,44 @@ void NodeIO::recv_commands(
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-void accept_handler(const boost::system::error_code& error) {
|
|
|
+/*
|
|
|
+ Handler for received client messages.
|
|
|
+
|
|
|
+*/
|
|
|
+void NetIO::handle_async_clients(std::shared_ptr<tcp::socket> csocket,
|
|
|
+ const boost::system::error_code& error)
|
|
|
+{
|
|
|
if(!error) {
|
|
|
printf("Accept handler success\n");
|
|
|
// Read 2 bytes from the socket, which will be the
|
|
|
// connecting node's node number
|
|
|
- //unsigned short node_num;
|
|
|
- //boost::asio::read(acc_sock,
|
|
|
- // boost::asio::buffer(&node_num, sizeof(node_num)));
|
|
|
- //printf("node_num received = %d\n", node_num);
|
|
|
+ unsigned short node_num;
|
|
|
+ boost::asio::read(*csocket,
|
|
|
+ boost::asio::buffer(&node_num, sizeof(node_num)));
|
|
|
+ printf("node_num received = %d\n", node_num);
|
|
|
+
|
|
|
+ start_accept();
|
|
|
} else {
|
|
|
+ printf("Accept handler failed\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
-void start_accept(boost::asio::io_context& io_context, const NodeConfig& myconf);
|
|
|
-
|
|
|
-void handle_accept(boost::asio::io_context& io_context, const NodeConfig &myconf, tcp_connection::pointer new_connection,
|
|
|
- const boost::system::error_code& error)
|
|
|
- {
|
|
|
- if (!error)
|
|
|
- {
|
|
|
- new_connection->start();
|
|
|
- }
|
|
|
-
|
|
|
- start_accept(io_context, myconf);
|
|
|
- }
|
|
|
-
|
|
|
-void start_accept(boost::asio::io_context& io_context, const NodeConfig& myconf) {
|
|
|
- tcp::resolver resolver(io_context);
|
|
|
- tcp::acceptor async_acceptor(io_context,
|
|
|
- resolver.resolve(myconf.clistenhost, myconf.clistenport)->endpoint());
|
|
|
- tcp_connection::pointer new_connection =
|
|
|
- tcp_connection::create(io_context);
|
|
|
- async_acceptor.async_accept(new_connection->socket(),
|
|
|
- boost::bind(&handle_accept, io_context, myconf, new_connection,
|
|
|
+ Asynchronously accept client connections
|
|
|
+*/
|
|
|
+void NetIO::start_accept()
|
|
|
+{
|
|
|
+ std::shared_ptr<tcp::socket> csocket(new tcp::socket(io_context_));
|
|
|
+ std::cout << "Accepting on " << myconf.clistenhost << ":" << myconf.clistenport << "\n";
|
|
|
+ client_acceptor->async_accept(*csocket,
|
|
|
+ boost::bind(&NetIO::handle_async_clients, this, csocket,
|
|
|
boost::asio::placeholders::error));
|
|
|
}
|
|
|
-*/
|
|
|
+
|
|
|
|
|
|
NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
|
|
|
- : conf(config), myconf(config.nodes[config.my_node_num])
|
|
|
+ : io_context_(io_context), conf(config),
|
|
|
+ myconf(config.nodes[config.my_node_num])
|
|
|
{
|
|
|
num_nodes = nodenum_t(conf.nodes.size());
|
|
|
nodeios.resize(num_nodes);
|
|
@@ -278,24 +275,16 @@ NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ // Currently only server 0 handles incoming client connections.
|
|
|
if(me==0) {
|
|
|
client_acceptor = std::shared_ptr<tcp::acceptor>(
|
|
|
new tcp::acceptor(io_context,
|
|
|
- resolver.resolve(myconf.clistenhost,
|
|
|
- myconf.clistenport)->endpoint()));
|
|
|
- std::shared_ptr<tcp::socket> new_socket(new tcp::socket(io_context));
|
|
|
- std::cout << "Accepting on " << myconf.clistenhost << ":" << myconf.clistenport << "\n";
|
|
|
- client_acceptor->async_accept(*new_socket, accept_handler);
|
|
|
+ resolver.resolve(this->myconf.clistenhost,
|
|
|
+ this->myconf.clistenport)->endpoint()));
|
|
|
+ start_accept();
|
|
|
}
|
|
|
|
|
|
|
|
|
- /*
|
|
|
- if(me==0) {
|
|
|
- start_accept(io_context, myconf);
|
|
|
- }
|
|
|
- */
|
|
|
}
|
|
|
|
|
|
|