Advertisement
bueddl

netlib2 api examples

Aug 10th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1.   // Example 1: create a simple tcp echo server
  2.   {
  3.     auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  4.     server.listen();
  5.     while (auto client = server.accept())
  6.     {
  7.       io::buffer buf;
  8.       client.recv(buf);
  9.       client.send(buf);
  10.  
  11.       client.close();
  12.     }
  13.   }
  14.  
  15.   // Example 2: create a simpele tcp client for a single send/recv operation
  16.   {
  17.     auto client = net::make_client_sock(net::endpoint::tcpv4{});
  18.     client.connect(net::endpoint::tcpv4{"127.0.0.1", 7321});
  19.  
  20.     io::buffer send_buf;
  21.     auto writer = io::make_buffer_writer(send_buf);
  22.     writer << "Hello server!";
  23.  
  24.     client.send(send_buf);
  25.  
  26.     io::buffer recv_buf;
  27.     client.recv(recv_buf);
  28.   }
  29.  
  30.   // Example 3: simple udp echo server (connectionless style)
  31.   {
  32.     auto server = net::make_server_sock(net::endpoint::udpv4{"127.0.0.1", 7321});
  33.     server.listen();
  34.  
  35.     net::endpoint::udpv4 client_info;
  36.     io::buffer buf;
  37.     server.recv(buf, client_info);
  38.  
  39.     server.send(buf, client_info);
  40.   }
  41.  
  42.   // Example 4: socket specific async handlers
  43.   {
  44.     io::service service;
  45.     auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  46.     server.listen();
  47.  
  48.     server.on_accept([service&](net::connected_socket<net::endpoint::tcpv4> &client)
  49.     {
  50.       client.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  51.       {
  52.         service.observe(client);
  53.         client.send(buf);
  54.         // or
  55.         client.send_async(buf);
  56.       });
  57.     });
  58.  
  59.     service.observe(server);
  60.     service.run();
  61.   }
  62.  
  63.   // Example 5: generic async handlers by service & multithreaded server applications
  64.   {
  65.     io::service service1, service2;
  66.  
  67.     auto server1 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  68.     auto server2 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 1237});
  69.  
  70.     service1.on_accept([service1&, service2&](net::connected_socket<net::endpoint::tcpv4> &client)
  71.     {
  72.       service2.observe(client);
  73.     });
  74.  
  75.     service2.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  76.     {
  77.       client.send(buf);
  78.     });
  79.  
  80.     // same thing but by using the factory
  81.     auto service_thread2 = io::make_service_thread(service2);
  82.  
  83.     service1.observe(server1);
  84.     service1.run();
  85.   }
  86.  
  87.   // Example 6: use of a service group for real multithreading (each service is handling new clients and recvs)
  88.   {
  89.     io::service_group service_group{2};
  90.  
  91.     auto server1 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  92.     auto server2 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 1237});
  93.  
  94.     service_group.on_accept([service1&, service2&](net::connected_socket<net::endpoint::tcpv4> &client)
  95.     {
  96.       service_group.observe(client);
  97.     });
  98.  
  99.     service_group.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  100.     {
  101.       client.send(buf);
  102.     });
  103.  
  104.     service_group.observe(server1);
  105.     service_group.observe(server2);
  106.     service_group.run();
  107.   }
  108.  
  109.   // Example 7: per socket async io (one-shot handlers); simple tcp echo client again
  110.   {
  111.     io::service service;
  112.    
  113.     auto client = net::make_client_sock(net::endpoint::tcpv4{});
  114.     service.observe(client);
  115.  
  116.     client.connect(net::endpoint::tcpv4{"127.0.0.1", 7321});
  117.  
  118.     io::buffer send_buf;
  119.     auto writer = io::make_buffer_writer(send_buf);
  120.     writer << "Hello server!";
  121.  
  122.     client.send_async(send_buf, [](net::connected_socket<net::endpoint::tcpv4> &client)
  123.     {
  124.       client.recv_async_once([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  125.       {
  126.         // buf received    
  127.       });
  128.     });
  129.  
  130.     service.run();
  131.   }
  132.  
  133.   // Example 8: per socket async io; server
  134.   {
  135.     io::service service{io::service::observe_accepted};
  136.  
  137.     auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  138.     service.observe(server);
  139.  
  140.     server.listen();
  141.     server.accept_async([](net::connected_socket<net::endpoint::tcpv4> &client)
  142.     {
  143.       io::buffer buf;
  144.  
  145.       // Could also use sync io here (will indeed block the io::service!)
  146.       client.recv_async([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  147.       {
  148.         client.send_async(buf);
  149.       });
  150.  
  151.       client.close();
  152.     });
  153.  
  154.     service.run();
  155.   }
  156.  
  157.   // Example 9: read a predefined structure from a newly connected client (e.g. packet parsing)
  158.   {    
  159.     io::service service{io::service::observe_accepted};
  160.  
  161.     auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  162.     service.observe(server);
  163.  
  164.     server.listen();
  165.     server.accept_async([](net::connected_socket<net::endpoint::tcpv4> &client)
  166.     {
  167.       io::buffer buf;
  168.  
  169.       // Could also use sync io here (will indeed block the io::service!)
  170.       client.recv_async_cond([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  171.       {
  172.         auto reader = io::make_binary_reader(buf);
  173.  
  174.         my_packet packet;
  175.         reader >> packet;
  176.  
  177.         client.recv_async_cond_once([packet&](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
  178.         {
  179.           // do sth. on packet and buf (data)
  180.           // ...
  181.         }, io::cond_exact{packet.data_length});
  182.       }, io::cond_exact{sizeof(my_packet)});
  183.  
  184.       client.close();
  185.     });
  186.  
  187.     service.run();
  188.   }
  189.  
  190.   // Example 10: thread per connection concept
  191.   {
  192.     auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
  193.  
  194.     server.listen();
  195.     while (auto _client = server.accept())
  196.     {
  197.       // Spawn thread, sync io from here
  198.       std::thread thread{[client{std::move(_client)}]
  199.       {
  200.         io::buffer buf;
  201.         client.recv_cond(buf, io::cond_exact{sizeof(my_packet)});
  202.         auto reader = io::make_binary_reader(buf);
  203.  
  204.         my_packet packet;
  205.         reader >> packet;
  206.  
  207.         buf.clear();
  208.         client.recv_cond(buf, io::cond_exact{packet.data_length});
  209.      
  210.         // do sth. on packet and buf (data)
  211.         // ...
  212.       }};
  213.       thread.detach();
  214.     }
  215.   }
  216.  
  217.   /* TODO:
  218.    * - error handling (error_code in async handlers, exceptions in sync io? both?)
  219.    * - how to distinguish on_accept/on_recv in io_service
  220.    * - which per socket async concept to use? on_xy or xy_async for permanent handlers and xy_async or xy_async_once for one shot handlers?
  221.    * - sth like recv_async_if and recv_async_cond_if giving a predicate whether to start receiving at all
  222.    */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement