void Service::start() { // Assign a driver (VirtioNet) to a network interface (eth0) hw::Nic<VirtioNet>& eth0 = hw::Dev::eth<0,VirtioNet>(); // Bring up a network stack, attached to the nic inet = std::make_unique<net::Inet4<VirtioNet> >(eth0); // Static IP configuration, until we (possibly) get DHCP inet->network_config( { 10,0,0,42 }, // IP { 255,255,255,0 }, // Netmask { 10,0,0,1 }, // Gateway { 8,8,8,8 } ); // DNS // Set up a TCP server on port 80 auto& server = inet->tcp().bind(80); inet->dhclient()->on_config([&server](bool timeout) { if(!timeout) printf("Server IP updated: %s\n", server.local().to_string().c_str()); }); printf("Server listening: %s \n", server.local().to_string().c_str()); // When someone connects to our server server.onConnect([](Connection_ptr client) { printf("Connected [Client]: %s\n", client->to_string().c_str()); // Make an outgoing connection to our python server auto outgoing = inet->tcp().connect(python_server); // When outgoing connection to python sever is established outgoing->onConnect([client](Connection_ptr python) { printf("Connected [Python]: %s\n", python->to_string().c_str()); // Setup handlers for when data is received on client and python connection // When client reads data client->read(1024, [python](auto buf, size_t n) { std::string data{ (char*)buf.get(), n }; handle_client_on_read(python, data); }); // When python server reads data python->read(1024, [client](auto buf, size_t n) { std::string data{ (char*)buf.get(), n }; handle_python_on_read(client, data); }); // When client is disconnecting client->onDisconnect([python](Connection_ptr, Disconnect reason) { printf("Disconnected [Client]: %s\n", reason.to_string().c_str()); python->close(); }); // When python is disconnecting python->onDisconnect([client](Connection_ptr, Disconnect reason) { printf("Disconnected [Python]: %s\n", reason.to_string().c_str()); client->close(); }); }); // << onConnect (outgoing (python)) }); // << onConnect (client) }
void Service::start() { auto& inet = net::Super_stack::get<net::IP4>(0); // Set up a TCP server on port 80 auto& server = inet.tcp().listen(80); printf("Server listening: %s \n", server.local().to_string().c_str()); // When someone connects to our server server.on_connect( [&inet] (Connection_ptr client) { printf("Connected [Client]: %s\n", client->to_string().c_str()); // Make an outgoing connection to our python server auto outgoing = inet.tcp().connect(python_server); // When outgoing connection to python sever is established outgoing->on_connect( [client] (Connection_ptr python) { printf("Connected [Python]: %s\n", python->to_string().c_str()); // Setup handlers for when data is received on client and python connection // When client reads data client->on_read(1024, [python](auto buf, size_t n) { std::string data{ (char*)buf.get(), n }; handle_client_on_read(python, data); }); // When python server reads data python->on_read(1024, [client](auto buf, size_t n) { std::string data{ (char*)buf.get(), n }; handle_python_on_read(client, data); }); // When client is disconnecting client->on_disconnect([python](Connection_ptr, Disconnect reason) { printf("Disconnected [Client]: %s\n", reason.to_string().c_str()); python->close(); }); // When python is disconnecting python->on_disconnect([client](Connection_ptr, Disconnect reason) { printf("Disconnected [Python]: %s\n", reason.to_string().c_str()); client->close(); }); }); // << onConnect (outgoing (python)) }); // << onConnect (client) }