Browse Source

Client single async connect to Server (App)

Sajin Sasy 1 year ago
parent
commit
9422c28caf
2 changed files with 72 additions and 41 deletions
  1. 29 40
      App/net.cpp
  2. 43 1
      App/net.hpp

+ 29 - 40
App/net.cpp

@@ -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);
-    }
-    */
 }
 
 

+ 43 - 1
App/net.hpp

@@ -42,7 +42,7 @@
 // + that many bytes of data
 //
 // This command transmits the enclave-to-enclave data.  The data in the
-// chunk will be (after the enclace-to-enclave handshake, anyway)
+// chunk will be (after the enclave-to-enclave handshake, anyway)
 // AES-GCM encrypted to a key known to the receiving enclave (but not
 // the receiving untrusted node).  The chunk number (starting from 0 and
 // not reset between messages) will be the IV, which is not transmitted.
@@ -125,11 +125,16 @@ public:
 };
 
 class NetIO {
+    boost::asio::io_context& io_context_;
     const Config &conf;
     const NodeConfig &myconf;
     std::deque<std::optional<NodeIO>> nodeios;
     std::shared_ptr<tcp::acceptor> client_acceptor;
 
+    void handle_async_clients(std::shared_ptr<tcp::socket> socket,
+        const boost::system::error_code& error);
+    void start_accept();
+
 public:
     NetIO(boost::asio::io_context &io_context, const Config &config);
 
@@ -150,6 +155,43 @@ public:
     void close();
 };
 
+/*
+class async_server{
+public:
+    async_server(boost::asio::io_context& io_context)
+      : io_context_(io_context),
+        acceptor_(io_context, tcp::endpoint(tcp::v4(), 13000))
+    {
+      start_accept();
+    }
+
+private:
+    void start_accept()
+    {
+      std::shared_ptr<tcp::socket> csocket(new tcp::socket(io_context));
+
+      acceptor_.async_accept(csocket,
+          boost::bind(&tcp_server::handle_accept, this, new_connection,
+            boost::asio::placeholders::error));
+    }
+
+    void handle_accept(std::shared_ptr<tcp::socket> csocket,
+        const boost::system::error_code& error)
+    {
+      if (!error)
+      {
+        //new_connection->start();
+        printf("handle_accept: SUCCES\n");
+      }
+
+      start_accept();
+    }
+
+    boost::asio::io_context& io_context_;
+    tcp::acceptor acceptor_;
+};
+*/
+
 extern NetIO *g_netio;
 
 #endif