Communication between GS and MS

Source: Internet
Author: User

GS and MS Communication Note that GS and MS is two threads, and now is a thread for each map, they are the internal protocol for communication, that is, since it is two threads how to communicate, see the net process communication this is relatively simple to cite an example M_pmap->gs2msdata (Gs2ms_add_player, M_nchannelid, (void*) &ractorex,sizeof(Ractorex));//Send player Online packagevoidMap::gs2msdata (intCmdintCHANNEL_ID,void* Data,intLen)    {MAPPKT pkt; Pkt.channelid=channel_id; Pkt.data=m_mempool.poppkt (len); Note that this m_mempool is a thread-safe queue, which is the memcpy of the boost lock-free queue implementation (Pkt.data, data, Len);//different threads require a memory copy and cannot use the same addressPkt.len =Len; Pkt.cmd=cmd; PUSHPKT (PKT);}voidthrtransdata::P ushpkt (mappkt Pkt) { for (;;) {        if(M_gs2mspkts2.push (PKT))//put the package into the Boost unlocked queue             Break; Cxx11::this_thread::interruptible_wait (1); }} then look at how the MS is taken. When the share is initialized, a thread is opened, and the thread can say that the entire map data is handled in this way .BOOLThrtransdata::getdatafromqueue (mappkt*ppkt) {    if(M_gs2mspkts2.empty ())return false;        {MAPPKT tmppkt; if(M_gs2mspkts2.pop (TMPPKT))//packet Pickup from GS to MS in a lock-free queue        {            *PPKT =tmppkt; return true; }        return false; }    return true;}if(Getdatafromqueue (&Pkt)) {processpkt (Pkt);//parsing the package calls the corresponding functionM_MEMPOOL.PUSHPKT (Pkt.data, Pkt.len);//returning a memory poolncount++; if(Ncount > -) {ncount=0; M_sptimerfactory-Drivetimer (); }    Continue;}//This is how the package from GS to MS is handled this way.that from the MS to the GS package from the map need to send to the GS of the package also not much, there is a way to go through the GS to map Sendcmd2gs (Ms2gs_prop_mgr, Nchannelid, PData, nlen);voidSendcmd2gs (intNcmd,intNchannelid,void* PData,intNlen)    {MAPPKT Pkt; Pkt.cmd=Ncmd; Pkt.channelid=Nchannelid; Char* Psznewbuffer =New Char[Nlen];//This is the new one, which is sent to Ms from the memory pool.memcpy (Psznewbuffer, PData, Nlen); Pkt.data=Psznewbuffer; Pkt.len=Nlen; Pushdata2gs (Pkt);};voidPushdata2gs (Constmappkt&Pkt) {     while(1)    {        if(M_quepkts2gs.push (PKT))//queue sent from MS to GS}} How did the GS go?voidShare::P rocmapsenddata ()//This is GS-driven.{    if(!itmap.second->recvdata (PKT))//fetching packets from the lock-free queue         Break; if(Pkt.cmd = = Ms2gs_ turn client_cmd)//processing for a package{onturntoclient (Pkt.channelid, Pkt.data, Pkt.len); }}//The package is bidirectional and requires two queues, which is also in net.//the second is the need for additional memory to be placed in the queue, because it is not possible to operate the same piece of memory, either own new, or allocate from the memory pool//between MS and GS through MAPPKT communication, for the client and the server through protocol communication, usually you define a piece of buf, and then packaged into BUF, net that block is also allocated a chunk in the memory pool and then store this buf, because this buf is also common //At first, I didn't understand the procedure, and these memory problems weren't clear.

Communication between GS and MS

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.