Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example 1: create a simple tcp echo server
- {
- auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- server.listen();
- while (auto client = server.accept())
- {
- io::buffer buf;
- client.recv(buf);
- client.send(buf);
- client.close();
- }
- }
- // Example 2: create a simpele tcp client for a single send/recv operation
- {
- auto client = net::make_client_sock(net::endpoint::tcpv4{});
- client.connect(net::endpoint::tcpv4{"127.0.0.1", 7321});
- io::buffer send_buf;
- auto writer = io::make_buffer_writer(send_buf);
- writer << "Hello server!";
- client.send(send_buf);
- io::buffer recv_buf;
- client.recv(recv_buf);
- }
- // Example 3: simple udp echo server (connectionless style)
- {
- auto server = net::make_server_sock(net::endpoint::udpv4{"127.0.0.1", 7321});
- server.listen();
- net::endpoint::udpv4 client_info;
- io::buffer buf;
- server.recv(buf, client_info);
- server.send(buf, client_info);
- }
- // Example 4: socket specific async handlers
- {
- io::service service;
- auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- server.listen();
- server.on_accept([service&](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- client.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- service.observe(client);
- client.send(buf);
- // or
- client.send_async(buf);
- });
- });
- service.observe(server);
- service.run();
- }
- // Example 5: generic async handlers by service & multithreaded server applications
- {
- io::service service1, service2;
- auto server1 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- auto server2 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 1237});
- service1.on_accept([service1&, service2&](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- service2.observe(client);
- });
- service2.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- client.send(buf);
- });
- // same thing but by using the factory
- auto service_thread2 = io::make_service_thread(service2);
- service1.observe(server1);
- service1.run();
- }
- // Example 6: use of a service group for real multithreading (each service is handling new clients and recvs)
- {
- io::service_group service_group{2};
- auto server1 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- auto server2 = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 1237});
- service_group.on_accept([service1&, service2&](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- service_group.observe(client);
- });
- service_group.on_recv([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- client.send(buf);
- });
- service_group.observe(server1);
- service_group.observe(server2);
- service_group.run();
- }
- // Example 7: per socket async io (one-shot handlers); simple tcp echo client again
- {
- io::service service;
- auto client = net::make_client_sock(net::endpoint::tcpv4{});
- service.observe(client);
- client.connect(net::endpoint::tcpv4{"127.0.0.1", 7321});
- io::buffer send_buf;
- auto writer = io::make_buffer_writer(send_buf);
- writer << "Hello server!";
- client.send_async(send_buf, [](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- client.recv_async_once([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- // buf received
- });
- });
- service.run();
- }
- // Example 8: per socket async io; server
- {
- io::service service{io::service::observe_accepted};
- auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- service.observe(server);
- server.listen();
- server.accept_async([](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- io::buffer buf;
- // Could also use sync io here (will indeed block the io::service!)
- client.recv_async([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- client.send_async(buf);
- });
- client.close();
- });
- service.run();
- }
- // Example 9: read a predefined structure from a newly connected client (e.g. packet parsing)
- {
- io::service service{io::service::observe_accepted};
- auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- service.observe(server);
- server.listen();
- server.accept_async([](net::connected_socket<net::endpoint::tcpv4> &client)
- {
- io::buffer buf;
- // Could also use sync io here (will indeed block the io::service!)
- client.recv_async_cond([](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- auto reader = io::make_binary_reader(buf);
- my_packet packet;
- reader >> packet;
- client.recv_async_cond_once([packet&](net::connected_socket<net::endpoint::tcpv4> &client, io::buffer buf)
- {
- // do sth. on packet and buf (data)
- // ...
- }, io::cond_exact{packet.data_length});
- }, io::cond_exact{sizeof(my_packet)});
- client.close();
- });
- service.run();
- }
- // Example 10: thread per connection concept
- {
- auto server = net::make_server_sock(net::endpoint::tcpv4{"127.0.0.1", 7321});
- server.listen();
- while (auto _client = server.accept())
- {
- // Spawn thread, sync io from here
- std::thread thread{[client{std::move(_client)}]
- {
- io::buffer buf;
- client.recv_cond(buf, io::cond_exact{sizeof(my_packet)});
- auto reader = io::make_binary_reader(buf);
- my_packet packet;
- reader >> packet;
- buf.clear();
- client.recv_cond(buf, io::cond_exact{packet.data_length});
- // do sth. on packet and buf (data)
- // ...
- }};
- thread.detach();
- }
- }
- /* TODO:
- * - error handling (error_code in async handlers, exceptions in sync io? both?)
- * - how to distinguish on_accept/on_recv in io_service
- * - 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?
- * - sth like recv_async_if and recv_async_cond_if giving a predicate whether to start receiving at all
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement