Using Kendynet to write Gateway Services

Source: Internet
Author: User
Tags bind

Most of the online gaming servers provide gateway services for communication between users and internal server groups. The Gateway Service distributes user messages from the client to the correct internal server on the one hand.

On the other hand, packets from the internal server are forwarded to the client. Generally for gateway applications, the most stressful is broadcast services. A user's behavior message generated in the game

You might want to broadcast it to the hundreds of other players who can see him. Here, write a simple Gateway service with kendynet, of course it's just a sample program, it's just a simple one

The connected data is sent to another connection. The Gateway service in the real network game is much more complicated.

First, introduce the basic design,

Static msgdisp_t Disp_to_server;

Static msgdisp_t disp_to_client;

Sock_ident To_server;

First, two message processors are defined, one user processes messages from, and one is used to process messages from internal servers.

A sock_ident is then used to represent the connection to the internal server.

Then in the main function:

asynnet_t asynet = asynnet_new (3);//3 poller,1 for listening, 1 for processing client connections, 1 for processing server connections msgdisp_t Disp_to_server = NEW_MSGD ISP (Asynet, To_server_connect, To_server_connect   
                                  Ed, NULL, To_server_process,   
       
    NULL);   
                                  msgdisp_t disp_to_client = New_msgdisp (asynet, To_client_connect, NULL, NULL, to_client_process   
       
    , NULL);   
    thread_t Service1 = Create_thread (thread_joinable);   
       
    thread_t Service2 = Create_thread (thread_joinable);   
    TO_CLIENT_IP = argv[1];   
       
       
    To_client_port = Atoi (argv[2]);   
    TO_SERVER_IP = argv[3];   
       
   To_server_port = Atoi (argv[4]); Thread_start_run (Service1,service_toserver, (void*) disp_to_server);   
    SLEEPMS (1000); Thread_start_run (Service2,service_toclient, (void*) disp_to_client);

First create an asynchronous network engine, pass in Parameter 3, to create 3 Poller, where the 1th is for processing the listener socket interface, and the 2nd for the internal server

Connection, the 3rd user processing and the client connection.

The two message services are then created with a different message callback function.

Finally, create two separate threads to run two message services respectively.

And then look at the processing of the callback service:

void to_server_connected (msgdisp_t disp,sock_ident sock,const char *ip,int32_t port,uint32_t err) {To_server   
= sock; } int32_t to_client_process (msgdisp_t disp,sock_ident sock,rpacket_t rpk) {if (!eq_sockiden   
    T (sock,to_server)) {//from cliet,send to server push_msg (Disp_to_server, (msg_t) RPK);   
        }else {//from server,send to client sock_ident client = Read_from_rpacket (RPK);   
    Asyn_send (Client,wpk_create_by_other (struct packet*) rpk));   
return 1; } void To_client_connect (msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) {//3rd Poller   
Handles the connection to the client Disp->bind (disp,3,sock,1,3*1000,0); } int32_t to_server_process (msgdisp_t disp,sock_ident sock,rpacket_t rpk) {if (!eq_sockiden T (sock,to_server)) {//from cliet,send to server asyn_send (to_server,wpk_create_by_other (struct PAcket*)) (RPK));   
    }else{//from server,send to client push_msg (Disp_to_client, (msg_t) RPK);   
return 1; } void To_server_connect (msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) {//with second Poller   
Handle the connection to the server Disp->bind (disp,2,sock,1,3*1000,0); }

First notice two connect callbacks, for server bound to number 2nd Poller, for client bound to number 3rd Poller.

Then look at the two process functions. For the client's process function, if the package interface is found to be not to_server, the packet

Posted to Disp_to_server, disp_to_server this data packages to the internal service. If you find that the packet is from To_server,

Then read the sending target from the packet and packages the data to the target client.

The server's process function, on the contrary, delivers messages from To_server to disp_to_client. Messages from the client are sent from the To_server

Send it out. A simple message forwarding service is implemented.

The complete sample program can refer to:

Https://github.com/sniperHW/luanet/blob/master/kendynet/test/gateser vice.c

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.