How Unity designs the network framework

The network framework that interfaces with the game server is also a very important module in Unity framework design. This article will share with you how to design a network framework based on Unity. It mainly explains the following points:

  1. TCP half-packet sticky, long connection and short connection, IO blocking;
  2. Technical solutions for Tcp Socket and UDP Socket;
  3. Unity’s serialization and deserialization technical solutions;
  4. TCP packetization and unpacking;
  5. HTTP-based short connection technology solution;
  6. Unity network framework design and implementation principles;

T CP half sticky package, long connection and short connection,  IO blocking 

TCP is a reliable network transmission protocol. Every time the network transmission bottom layer sends a TCP data packet , it must wait for confirmation from the other party. Only after receiving the confirmation message can the next TCP data packet be sent . When we send an application layer data packet at the application layer, the bottom layer of the TCP network may divide the application layer data packet into several " T CP packets " and send them out through the bottom layer of the network. Then there will be a problem here. The data packets sent by the application layer may be split into several "TCP data packets" and sent out. When we receive them at the other end, we may have to combine these split packets. This is the " half package ". The bottom layer may divide the data packets of the two application layers into one "TCP data packet" and send it. After receiving it, part of it will be the data packet belonging to the previous application layer, and part of it will belong to the data packet of the next application layer. This is called " Sticky bag ".

  TCP is connection-oriented. The server and client transmit data through the TCP connection. If the connection is always open and the connection is not closed after sending the data, the next time the data is sent, the data can be sent directly. This is called a long connection . If you send data to establish a connection first, then close the connection immediately after the data is sent, and re-establish the connection by sending data next time. This is called a " short connection ". Long connections always exist. The advantage is that the client and server can communicate with each other at any time, but the connection resources are always occupied. A short connection does not occupy connection resources, but only the client can send data to the server, and the server cannot actively notify the client after disconnecting.

  IO blocking refers to the speed at which our CPU processes data, which is much greater than the transmission speed of the network. When we want to send data, the CPU calls the IO interface to copy the data from the memory to the bottom layer of the network. After waiting for the bottom layer to transmit the data, the IO function called by the CPU returns. During this process, the CPU is waiting for the network to send the data. , the thread hangs, we call this IO blocking .

Technical solutions for Tcp Socket and U DP Socket

After understanding some basic concepts of the network, let's look at the technical solutions for using TCP/UDP Socket. Unity actually acts as a client of the network, and the client only needs to connect to the server and communicate with the server. It does not need to handle the data transmission of N clients at the same time like the server client. So the client Socket is very simple. Which plug-in do we use for the Unity client's Socket? In fact, there is no need to use any plug-ins at all, just use the Socket related API provided by the OS.

  TCP Socket is connection-oriented, and the usage process is as follows:

1: Client Connect server, establish Socket connection;

2: Call the Send function of Socket to send data;

3: Call the Sokcet Recv function to read data from the Socket;

4: Close the Socket connection;

  The use of UDP Socket, UDP Socket is not connection-oriented, it just calls the underlying network protocol and directly sends the data packet to a specific address + port. So directly SendTo (network address + port), RecvFrom (network address + port)

  TCP/UDP Socket is simple enough that Unity developers can use it directly when selecting technology.

Unity ’s serialization and deserialization technical solutions

  What is sent over the network is a data byte stream. Unity communicates with the server to convert the data object to be sent into a binary byte stream, and then transmits it through the network. After receiving the byte stream, it needs to reconstruct the data object back to complete. Data is sent. The process of turning a data object into a binary byte stream is called serialization, and converting the binary byte stream back into a data object is called deserialization.

  Serialization/deserialization currently has two main directions: one is binary serialization and the other is text serialization;

Binary serialization/deserialization: Based on binary bits, the data object is turned into a binary bit data stream through a protocol negotiated by the user. During deserialization, the bit data stream is turned into a data object according to the user agreement.

Text serialization: Convert data into human-readable text data. When the serialized text data is received, the data inside is parsed according to the rules of the text data to reconstruct the data object.

  Binary serialization/deserialization: We encode some basic data types with bits, such as int, float, string bool, etc. Write the encoding and decoding codes for these basic data types, then write the protocol for the data object, and then develop a compiler. He can use the encoding/decoding functions based on the basic data types based on this protocol file to combine the basic data according to the protocol. Type encoding and decoding is divided into several basic data types according to the structure of the object, and encoding/decoding function codes are automatically generated.

  Text serialization solutions: json, xml, etc.;

  Binary serialization/deserialization solution: protobuf;

TCP packetization and unpacking

As explained above, application layer data packets may be guessed into several " TCP data packets ", or two application layer data packets may be connected together in one " TCP data packet ". In order to receive the application layer data packets completely and correctly when receiving data, we must make separation marks between the two application layer datagrams. When we receive the data, we can decide which data according to the separation marks. Which package is it? There are currently two approaches:

  1. Size + data content + check code mode; [size] [data body] [check], this method is often used in game development to make packets. When receiving data, the data packets are completely combined according to the size. . WebSocket also uses this method.
  2. Specific delimiter pattern: 123456\r\n67890, Html uses specific delimiter symbols to split data content.

Short connection technical solution based on http

Short connection, as mentioned before, disconnect it after use and reconnect it next time you want to use it. Short connections use the TCP Socket strategy, and the most commonly used strategy is http (based on TCP Socket+http Hypertext Transfer Protocol).

HTTP is also used in the Unity network framework, mainly for docking with the server based on HTTP communication and downloading of resource servers. As a client, Unity needs to use http, which has already encapsulated UnityWebRequest. At the same time, UnityWebRequest is a multi-threaded mode and can concurrently issue N requests at the same time.

Unity network framework design and implementation principles

With so much laid out, let’s go straight to the architectural design structure diagram of the Unity network framework for your reference.

When receiving data here, multi-threading is used. After receiving the data, unpacking and deserialization are performed. After obtaining the data object, it is placed in the event queue. The main game thread obtains network events from the event queue and then processes them. When sending data, we call the asynchronous network Send function on the main game thread to avoid IO blocking. Paying attention to these points, you can design a good network framework.

Supongo que te gusta

Origin blog.csdn.net/Unity_RAIN/article/details/134144581
Recomendado
Clasificación