Myicq1.0a1 server code analysis (3) server cluster

zhaozj2021-02-08  218

Myicq1.0a1 server code analysis (3) server cluster

Solarsoft

The server cluster is actually a connection between the servers, and maintains such links, such as: When you play a single, you will see a lot of servers, how do they work together! The analysis is as follows:

The server and the server are generally connected with TCP, and this table is created in each server. The table includes corresponding information, such as server IP, port, including the number of online people, etc., and so on. When entering other game hosts, it is actually an exiting the current server, the process of logging in other servers, the corresponding online data, and the server will be transmitted.

Take it, let's take a look, how myicq does the cluster.

Class Serverhash {

PUBLIC:

Void Put (Server * S) {

INT i = Hashfn (S-> IP);

Hash [i] .addhead (& S-> Hashitem);

}

Server * get (uint32 IP);

Private:

INT hashfn (uint32 ip) {

RETURN (IP & (Server_Hash_Size - 1));

}

Listhead hash [server_hash_size];

}

Server * Serverhash :: get (uint32 ip)

{

INT i = Hashfn (IP);

Listhead * head = & hash [i];

Listhead * POS;

List_for_each (pos, head) {

Server * s = list_entry (POS, Server, HashItem);

IF (S-> IP == IP)

Return S;

}

Return NULL;

}

Listhead server :: serverlist;

Static Serverhash Serverhash;

Here is actually the Hash table management of the server online list, this hash management is unclear, you can take a look at my article (1).

Let's take a look at the Socket class for the server to connect, where RefObject is the role of the counter.

Class Server: Public RefObject {

PUBLIC:

Server ();

Virtual ~ Server ();

Void OnDNSResolved (uint32 ip);

Void onConnect ();

Void onreceive ();

Void onclose ();

BOOL Connect (uint32 ip, uint16 port);

RemoteSession * getsession (uint32 uin);

Void Senduseronline (session * s);

Void Senduseroffline (uint32 uin);

Void SenduserStatus (uint32 uin, uint32 status);

Void SendMessage (uint8 type, uint32 dst, uint32 src, icq_str & text);

Void AddFriend (uint16 SEQ, UINT32 DST, UINT32 SRC);

Void Delfriend (Uint32 DST, UINT32 SRC);

Void UpdateContact (UINT16 SEQ, UINT32 DST, UINT32 SRC);

Void Searchrandom (UINT16 SEQ, UINT32 SRC);

Void searchuin (uint16 SEQ, UINT32 DST, UINT32 SRC);

Void AddFriendreply (uint16 SEQ, UINT32 DST, UINT32 SRC, UINT8 AUTH);

Void UpdateContactReply (uint16 SEQ, UINT32 SRC, UINT8 * DATA, INT N); Void SearchuinReply (uint16 SEQ, UINT32 SRC, UINT8 * DATA, INT N);

Static bool init ();

Static void destroy ();

Static Server * Get Server (const char * name);

Static Server * CreateServer (const char * name);

Static RemoteSession * GetSession (QID & QID);

Static Int GenerateFDS (fd_set & readfds, fd_set & writefds);

Static void Examinefds (fd_set & readfds, fd_set& writefds);

Static Listhead ServerList;

Char DomainName [MAX_DOMAIN_NAME 1];

Char descname [MAX_DESC_NAME 1];

ICQ_STR DOMAIN;

ICQ_STR DESC;

UINT32 IP;

Int Sock;

Int status;

TIME_T Expire;

UINT32 sessioncount;

Bool isaccepted;

Listhead ListItem;

Listhead hashitem;

Private:

Bool SetDomainandDesc (ICQ_STR & Name, ICQ_STR & DES);

RemoteSession * CreateSession (uint32 uin);

Void Notify (Uint32 UIN, DB_CALLBACK CB);

Void CreatePacket (TCPOUTPACKET & OUT, UINT16 CMD);

TCPOUTPACKET * CREATEPACKET (UINT16 CMD);

Void SendPacket (TCPOUTPACKET * OUT);

Void SendpendingPacket (INT S);

Void OnPacketReceived (TCPINPACKET & IN);

Void onhello (TCPINPACKET & IN);

Void OnsendMessage (TCPINPACKET & IN);

Void OnaddFriend (TCPINPACKET & IN);

Void OndelfRiend (TCPINPACKET & IN);

Void onuseronline (TCPINPACKET & IN);

Void Onuseroffline (TCPINPACKET & IN);

Void OnusersTatus (TCPINPACKET & IN);

Void onupdateContact (TCPINPACKET & IN);

Void OnSearchrandom (TCPINPACKET & IN);

Void OnSearchuin (TCPINPACKET & IN);

Void onhelloreply (TCPINPACKET & IN);

Void OnaddFriendreply (TCPINPACKET & IN);

Void OnUpdateContactReply (TCPINPACKET & IN);

Void OnSearchrandomreply (TCPINPACKET & IN);

Void OnSearchuinReply (TCPINPACKET & IN);

CHAR BUF [TCP_PACKET_SIZE]; int buffsize;

Listhead uinhash [uin_hash_size];

Listhead sessionList;

Listhead sendqueue;

DECLARE_SLAB (Server)

}

The above program, I don't do too much explanation, I think everyone should also understand. If you have any questions, you can publish your opinion.

转载请注明原文地址:https://www.9cbs.com/read-2545.html

New Post(0)