Interactive and management are interactive, and for players, interaction with other players, but for computers, implementation interaction is the mutual transfer of data messages. As we have learned about the basic concepts of network communication, it constitutes the most basic condition of interaction, and we need to communicate data on this network level. Unfortunately, the computer does not know how to express communication between players, so we need to provide a set of instruction organizations and parsing mechanisms that can make computer understanding, which is the network packet (data package) of us above. Processing mechanism.
In order to be more simple to explain the organizational form of network packets, we discuss with a chat processing module to see the following code structure:
Struct tagMessage {long LTYPE; long lplayerid;}; // message instruction // instruction-related player identifies char strtalk [256]; // message content
The above is an extremely simple message package structure, let's talk about the use of its various data domains: First, LTYPE is the type of message instruction, which is the most basic message identifier, this logo is used to tell the server or customer The specific use of this instruction is handled for the server or client. Lplayerid is identified as a player. Everyone knows that a player is actually a bunch of data inside the machine, especially in the game server, there may be thousands of players, at this time, we need a tag to distinguish the player, so you can quickly find a specific player. And apply communication data to it. Strtalk is the chat data we have to pass. This part is a real data entity. The front parameters are only limited by the data entity application. After the data is organized, it is followed by transmitting this structural data through the Socket connection. Here we have to understand, the network is in the data transmission process, it does not care about the data structure adopted by the data, which requires us to convert the data structure into a binary data code to send, in the recipient, we will convert these binary data codes The corresponding data structure used in the process. Let's take a look at how to achieve:
TagMessagemsg; msg.ltypemsg_chat; msg.lplayerid 1000; strcpy (& msg.strtalk, "chat information");
First, we assume that a packet has been organized, here MSG_Chat is the identifier of our own self-definition, of course, this identifier is unified in the server and client. The player's ID is set according to the game needs. Here 1000 is used as a hypothesis, now continue:
Char * p = (char *) & msg; long llength = sizeof (tagmessage); send (SSocket, P, LLEngth); // Get the length of the data structure
We convert the structure into a CHAR type data pointer by an forcibly converting, so that the flow data processing can be performed by SizeOf (), and then use the Winsock's send () function to send out the data. . Next, look at how to receive data:
Long LLENGTH = SIZEOF (Tagmessage); Char * Buffer = New Char [LLENGTH]; RECV (SSOCKET, BUFFER, LLENGTH); tagMessage * p = (tagMessage *) Buffer; // Get data
After obtaining network data through the RECV () function through Winsock, we also convert the obtained buffer data into the corresponding structure by forced conversion, so that the data can be easily accessed. (Note: Forced conversion only as a means of data conversion, there are more optional methods in practical applications, here only for simple description logic), have to mention how servers / clients can filter each A message and how to manage the communication packet. Whether it is a server or a client, when the network message is received, after the above data is parsed, the message type must be screened and failed. Simply, it is a message loop similar to Windows, and the different messages are different. This can be understood by a Switch statement (a friend who is familiar with the Windows Message loop), based on the LTYPE information in the message package, the message is drawn, consider the following code segment: Switch (P-> LTYPE) // Here P-> LTYPE is a message type identifier for us {CASE MSG_CHAT: // Chat, Case MSG_Move: // Player Mobile Message Break; Case Msg_exit: // Player leaves the message Break; default: Break;}
MSG_MOVE and MSG_EXIT in the above segment are our virtual message identifiers (identifiers in a real game may have hundreds, which requires consideration of optimization and priority message processing issues). In addition, a network gaming server faces hundreds of thousands of connection users, we also need some reasonable data organization management methods to perform relevant processing. Ordinary monomer gaming servers may cause the entire game network to be paralyzed because it is too much, and this is introduced into the packet server mechanism, we separate the server to distributed processing. We extract each module to make a dedicated server system, then build a data center to connect all servers to perform data interaction, where each module creates a connection with the data center, ensuring the correlation of each module, At the same time, the player transforms to connect to the server that is currently providing the service, so that the burden on a single server can be alleviated, disperse the pressure into multiple servers while ensuring the unity of the data, and even if a server is abnormal. And when it does not affect the game players of other modules, thereby increasing overall stability. The grouping server mitigates the server's pressure, but also brings server scheduling issues. The group-based server needs to process server jumps. Take a player's game scene jump as a foundation foundation: Assume some players in the game scene A, He wants to jump from the scene A to the scene b. In the game, we call the scenes to change. At this time, the player will trigger the jump demand, such as the switching point in the scene, so that the server will use the player data from "game scene A Server "delete, and build players in the" Game Scene B Server ". The simple model of scene switching is described here, and there are many steps in the process, but it is believed that you can derive a lot of application skills. However, it should be noted that when the scene switch or inter-module switches, it is necessary to consider the transmission safety of the data and logic rationality, otherwise the switch is likely to be a bridge for future players to replicate items.