Asynchronous network message processing framework

Recently, most of the code of the original kendynet network framework has been rewritten to make the provided interface clearer and more user-friendly.

The architecture of the entire framework is layered in 3 layers:

1) Single-threaded, network interface based on raw data flow, at this layer, no packet processing, timer events, etc. are provided. Users can further encapsulate it according to their own needs.

2) Single thread, providing connection, packet processing, receiving and sending timeout processing.

3) An asynchronous network framework with network logic separation, abstracting three main types: asynnet_t, sock_ident and msgdisp_t.

asynnet_t: The network processing engine, the user can pass in the pollercount parameter when creating an instance, and each poller will run in a separate thread.

sock_ident: Socket encapsulation of logical layer operations, which can be safely used in a multi-threaded environment.

msgdisp_t: message separator, each separator has a corresponding message queue for receiving messages from network threads.

msgdisp_t provides two usage modes:

The first: the typical thread pool mode. In this mode, a message separator can be created, and multiple logical threads call this message separator

msg_loop.

The second: shared network layer mode. Starts N threads in a process, each thread runs a different service, all of which share the network communication layer.

In this case, network messages need to be routed to the correct service. Each thread can create a message separator, and each thread is on its own message separator

Call msg_loop to process messages that only belong to you.

Here is an example of an asynchronous web server:

copy code
#include <stdio.h>
#include <stdlib.h>
#include "core/msgdisp.h"
#include "testcommon.h"

uint32_t recvsize = 0;
uint32_t recvcount = 0;

///int32_t asynnet_bind(msgdisp_t disp,sock_ident sock,void *ud,int8_t raw,uint32_t send_timeout,uint32_t recv_timeout)
void asynconnect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port)
{
    printf("asynconnect\n");
    disp->bind(disp,sock,1,0,30*1000);
}

void asynconnected(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port)
{
    printf("asynconnected\n");
   ++client_count;
}

void asyndisconnected(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port,uint32_t err)
{
    --client_count;
}


int32_t asynprocesspacket(msgdisp_t disp,sock_ident sock,rpacket_t rpk)
{
    recvsize + = rpk_len (rpk);
    recvcount++;
    asyn_send(sock,wpk_create_by_other((struct packet*)rpk));
    return 1;
}


void asynconnectfailed(msgdisp_t disp,const char *ip,int32_t port,uint32_t reason)
{

}


int main(int argc,char **argv)
{
    setup_signal_handler();
    InitNetSystem();
    asynnet_t asynet = asynnet_new(1);
    msgdisp_t  disp = new_msgdisp(asynet,
                                  asynconnect,
                                  asynconnected,
                                  asyndisconnected,
                                  asynprocesspacket,
                                  asynconnectfailed);
    int32_t err = 0;
    disp->listen(disp,argv[1],atoi(argv[2]),&err);
    uint32_t tick,now;
    tick = now = GetSystemMs();
    while(!stop){
        msg_loop(disp,50);
        now = GetSystemMs();
        if(now - tick > 1000)
        {
            uint32_t elapse = now-tick;
            recvsize = (recvsize/elapse)/1000;
            printf("client_count:%d,recvsize:%d,recvcount:%d\n",client_count,recvsize,recvcount);
            tick = now;
            packet_send_count = 0;
            recvcount = 0;
            recvsize = 0;
        }
    }
    CleanNetSystem();
    return 0;
}
copy code

The project code is at: https://github.com/sniperHW/luanet

At present, only the network support for linux and tcp has been implemented. In the future, this part of the code will be improved first, and RPC support based on user level thread will be provided on top of this.


Reprinted from: http://www.cnblogs.com/sniperHW/p/3517767.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652882&siteId=291194637