Online game communication model, primary exploration (3)

xiaoxiao2021-03-06  41

Everyone knows that the game needs to continue to loop the logic in the game and draw the game world. The Winsock handles described above is carried out in a blocking method, which in violation of the implementation of the game, can be imagined, connected to the client During the server, your game cannot be controlled, then if the player wants to cancel or do other processing, even display a most basic dynamic connection prompt can't. So we need to use other ways to handle network communication, so that it will not conflict with the game main line, you may think that you can create a network thread to handle it? That's right, we can create a sub-thread specifically for network communication to solve this problem. Of course, we have a thread in our game. We need to do more considerations, let us see how to create a network communication thread. In the Windows system, we can make threads created through the CreateThread () function, see the following code snippet:

DWORD DWTHREADID; Handle Hthread = Createthread (NULL, 0, Netthread / * Network Cluster Function * /, SSocket, 0, & DWTHREADID); if (hthread == null) {Failed ("Winsocket Thread Create Error!");}

Here we have created a thread while incorporating our Socket in thread:

DWORD WINAPINETTHREAD (LPVOID LPARAM) {socket ssocket (socket) LPARAM; ... RETURN 0;}

Netthread is our network thread we used to handle network communications. So, how do we introduce Socket's processing into threads? Take a look at the code snippet below:

HANDLE hEvent; hEvent = CreateEvent (NULL, 0,0,0); // Set asynchronous communication if (WSAEventSelect (sSocket, hEvent, FD_ACCEPT | FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR) {Failed ( "WinSocket EventSelect Error ! ");

After the above settings, the Winsock API function will run in a non-blocking method, that is, return immediately after the function is executed, and the network communication will be stored in HEVENT in the event without parking the entire branch. After completing the above steps, we need to respond and process on the event, let us see how to get the event message generated by network communications in the network thread:

WSAEnumNetworkEvents (sSocket, hEvent, & SocketEvents); if (SocketEvents.lNetworkEvents = 0!) {Switch (SocketEvents.lNetworkEvents) {case FD_ACCEPT: WSANETWORKEVENTS SocketEvents; break; case FD_CONNECT: {if (SocketEvents.iErrorCode [FD_CONNECT_BIT] == 0) // Connect the success {// Connection successfully notified the main thread (game thread) for processing}} Break; Case FD_READ: // Get Network Data {IF (RECV (SSOCKET, PBUFFER, LLENGTH, 0) == Socket_ERROR) {Failed ("Winsocket Recv Error!");}} Break; Case FD_WRITE: BREAK; CASE FD_CLOSE: // Notify the main thread (game thread), the network has disconnected Break; default: Break;}} Only web connections (FD_Connect) ) And read data (fd_read) perform simple simulation operations, but after receiving the event message, the network thread will organize the data to organize the data, then return the data to our game main thread, the game main thread Send the handled data, such a round-trip forms data communication in our online game, is the most basic element that makes online games. Finally, let's talk about the organization of the network packet (data package), the online game data package is the most basic unit of the game data communication. The online game generally does not use the word throttle to perform data transmission, one data package package It can also be seen as a message instruction, in the game, the server and the client will continue to send and receive these message packets, and then convert the message package parsing to the command meaning and execution.

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

New Post(0)