Open source web-based communication framework asio the asio2, supports TCP, UDP, HTTP, RPC, SSL, cross-platform, supports reliable UDP, TCP supports automatic unpacking, TCP datagram mode

Open source web-based communication framework asio the asio2, supports TCP, UDP, HTTP, RPC, SSL, cross-platform, supports reliable UDP, TCP supports automatic unpacking, TCP datagram mode 

C ++ with the development of network communication program asio is a good choice, but asio itself is a set of functions that he had to deal with such as "communication thread pool management, connection and lifecycle management, multi-thread synchronization protection such as sending and receiving data." Thus there was a layer of asio packaging greatly simplifies the use of asio. The code uses C ++ 17-related functions, it can only be used in C ++ 17 or more.

Http websocket part and which is used in boost :: beast, so if you need to use http or websocket function, you must use the boost library, if less than http is used directly to independent asio. ASIO_STANDALONE macro definition of this switch can be provided using a boost :: asio asio standalone or in use by config.hpp.

CRTP code that makes heavy use of template programming (without the use of virtual and static with CRTP achieve polymorphism), so the compiler is time-consuming, but the efficiency is relatively good point.


github Address : https://github.com/zhllxt/asio2

Cloud code Address : https://gitee.com/zhllxt/asio2

A open source cross-platform c++ library for network programming based on asio,support for tcp,udp,http,rpc,ssl and so on.

  • Supports TCP, UDP, HTTP, WEBSOCKET, RPC, ICMP, SERIAL_PORT and so on;
  • It supports reliable UDP (based KCP), support SSL, the SSL certificate is loaded from the memory support string;
  • TCP support data unpacking function (specified delimiter automatically unpacking data, the received user data is to ensure that a complete packet); implements TCP datagram mode (similar WebSocket);
  • Support windows, linux, 32, 64;
  • Dependent asio (boost :: asio asio independently or can, if desired HTTP function must boost :: asio), dependent on C ++ 17;
  • Hpp manner using the code header file, links to the source code level, without compiling, simply include the project added ASIO2 Include directory path, and then in the source #include <asio2 / asio2.hpp> can include headers;
  • demo directory contains a large number of example projects (based on VS2017 project created), using a variety of methods, please refer to the sample code;

TCP:

Server:

asio2::tcp_server server;
server.bind_recv([&server](std::shared_ptr<asio2::tcp_session> & session_ptr, std::string_view s)
{
    session_ptr->no_delay(true);

    printf("recv : %u %.*s\n", (unsigned)s.size(), (int)s.size(), s.data());
    session_ptr->send(s, [](std::size_t bytes_sent) {});
}).bind_connect([&server](auto & session_ptr)
{
    printf("client enter : %s %u %s %u\n",
        session_ptr->remote_address().c_str(), session_ptr->remote_port(),
        session_ptr->local_address().c_str(), session_ptr->local_port());
}).bind_disconnect([&server](auto & session_ptr)
{
    printf("client leave : %s %u %s\n",
        session_ptr-> remote_address () the c_str (),.
        session_ptr-> REMOTE_PORT (), ASIO2 :: last_error_msg () the c_str ());. 
}); 
server.start ( "0.0.0.0", 8080); 
//server.start("0.0.0.0 ", 8080, ' \ n '); // press \ n automatic unpacking (any character can be specified) 
//server.start("0.0.0.0 ", 8080," \ r \ n "); // press \ r \ n automatic demolition package (can be any string) 
//server.start("0.0.0.0 ", 8080, match_role ( '#')); // the specified rule by the automatic unpacking match_role (refer to match_role demo code) (for user-defined protocol unpacking) 
//server.start("0.0.0.0 ", 8080, ASIO :: transfer_exactly (100)); // every reception of 100 bytes of fixed 
//server.start("0.0 .0.0 ", 8080, asio2 :: use_dgram ); TCP // datagram mode, regardless of how long the data transmission must be received both the corresponding length of the entire packet data

  

Client:

asio2::tcp_client client;
client.bind_connect([&](asio::error_code ec)
{
    if (asio2::get_last_error())
        printf("connect failure : %d %s\n", asio2::last_error_val(), asio2::last_error_msg().c_str());
    else
        printf("connect success : %s %u\n", client.local_address().c_str(), client.local_port());

    client.send("<abcdefghijklmnopqrstovuxyz0123456789>");
}).bind_disconnect([](asio::error_code ec)
{
    printf("disconnect : %d %s\n", asio2::last_error_val(), asio2::last_error_msg().c_str());
}).bind_recv([&](std::string_view sv)
{
    printf("recv : %u %.*s\n", (unsigned)sv.size(), (int)sv.size(), sv.data());

    client.send(sv);
}) 
    //.Bind_recv(on_recv) // Bind global function 
    //.bind_recv(std::bind(&listener::on_recv, & lis, std :: placeholders :: _ 1)) // bind member function (For details, please View demo code) 
    //.bind_recv(&listener::on_recv, lis) // lis by reference to the object to bind member function (for details, please see the demo code) 
    //.bind_recv(&listener::on_recv, & lis) // press lis pointer to the object to bind member function (for details, please see the demo code) 
    ; 
client.async_start ( "0.0.0.0", 8080); // asynchronous connection server 
//client.start("0.0.0.0 ", 8080) ; // synchronization connection server 
//client.async_start("0.0.0.0 ", 8080, '\ n '); // press \ n automatic unpacking (any character can be specified) 
//client.async_start("0.0. 0.0 ", 8080," \ r \ n "); // press \ r \ n automatic unpacking (can be any string) 
//client.async_start("0.0.0.0", 8080, match_role); // press match_role automatically unpacking specified rules (refer to match_role demo code) (protocol for user-defined unpacking) 
//client.async_start("0.0.0.0 ", 8080, ASIO :: transfer_exactly (100)) ; // every 100 bytes received fixed 
//client.start("0.0.0.0 ", 8080,asio2 :: use_dgram); TCP // datagram mode, regardless of how long the data transmission must be received both the corresponding length of the entire packet data

  

UDP:

Server:

:: udp_server Server ASIO2; 
// ... bind listeners (See the demo code) 
server.start ( "0.0.0.0", 8080); // regular UDP 
//server.start("0.0.0.0 ", 8080, asio2 :: use_kcp); // reliable UDP

  

Client: 

:: udp_client Client ASIO2; 
// ... bind listeners (See the demo code) 
client.start ( "0.0.0.0", 8080); 
//client.async_start("0.0.0.0 ", 8080, ASIO2: : use_kcp); // reliable UDP

  

RPC:

Server:

:: rpc_server Server ASIO2; 
// ... bind listeners (See the demo code) 
A A; // A defined, please see the demo code 
server.bind ( "add", add) ; // Bind RPC Global function 
server.bind ( "mul", & A :: mul, a); // RPC binding member function 
server.bind ( "cat", [& ] (const std :: string & a, const std :: string & b) {return a + b;}) ; // bind lambda expression 
server.bind ( "get_user", & A :: get_user, a); // bind the member function (by reference) 
server.bind ( "del_user", & A :: del_user, & a) ; // bind the member function (by pointers) 
//server.start("0.0.0.0 ", 8080, ASIO2 :: use_dgram); // use the TCP data communication mode as the underlying support RPC when starting the server must use use_dgram parameter 
server.start ( "0.0.0.0", 8080) ; // use as RPC communication websocket underlying support (rcp_server.hpp end of the file need to choose code websocket)

  

Client: 

:: rpc_client Client ASIO2; 
// ... bind listeners (See the demo code) 
//client.start("0.0.0.0 ", 8080, ASIO2 :: use_dgram); // use TCP datagram mode as RPC communication underlying support, when starting the server must use use_dgram parameter 
client.start ( "0.0.0.0", 8080) ; // use as RPC communication websocket underlying support 
ASIO :: ERROR_CODE EC; 
// synchronous call RPC function 
int sum = the Client.call <int> (EC, Chrono :: :: seconds The STD (. 3), "the Add",. 11, 2); 
the printf ( "SUM: ERR% D: D%% S \ n-", SUM, EC. value (), ec.message () the c_str ());. 
// asynchronous RPC call function, the first parameter is the callback function when the call is completed or the timeout callback function is called automatically, if a timeout or other error, 
// ec stored in the error code, there async_call return type is not specified, the second parameter lambda expressions must specify the type 
client.async_call ([] (ASIO :: ERROR_CODE ec, V int) 
{ 
    the printf ( "SUM: D ERR%:% S% D \ n-", V, ec.value (), ec.message () the c_str ());. 
}," the Add ", 10,20);
// here async_call indicates the return type of the lambda expression parameter may be a second type of auto 
client.async_call <int> ([] (ASIO :: ERROR_CODE EC, auto V) 
{ 
    the printf ( "SUM:% D ERR:% S% D \ n-", V, ec.value (), ec.message () the c_str ());. 
}," the Add ", 12 is, 21 is); 
// the return value of the user-defined data types (user defined type See demo code) 
User U = the Client.call <User> (EC, "the get_user"); 
the printf ( "% S% D", u.name.c_str (), u.age); 
for (Auto & [K, V]: u.purview) 
{ 
    the printf ( "% S% D", K, v.c_str ()); 
} 
the printf ( "\ n-"); 

u.name = "Hanmeimei"; 
U = .age ((int) Time (nullptr a)) 100%; 
u.purview = {{10, "GET"}, {20 is, "SET"}}; 
// return value void if the RPC functions, the user The callback function only one parameter to 
client.async_call([](asio::error_code ec)
{
}, "del_user", std::move(u));

  

HTTP和WEBSOCKET:

See the demo sample code section http websocket

ICMP:

See the demo sample code ping test section

Serial:

See the demo sample code serial port section

Guess you like

Origin www.cnblogs.com/zhllxt/p/10977764.html