Copyright Statement: This article is an original article, please declare when reprinting
The previous article mainly talks about the serialization and parsing of protobuf byte stream, serializing protobuf objects into words Although it can be transmitted directly after throttling, it is actually impossible to transmit only the protobuf byte stream in the project, because there are several very common problems in the TCP communication of the socket, namely sticky packets and missing packets. The so-called sticky packets simply mean that the socket will merge multiple smaller packets and send them together. Because TCP is connection-oriented, in order to send multiple packets to the receiving end more efficiently, the sending end uses an optimization method (Nagle algorithm) to merge multiple data with small intervals and small data volume into A large data block is then packetized. Missing packets means that after the buffer area is full, soket will send incomplete packets to the receiving end (according to my understanding, sticky packets and missing packets are actually a problem). In this way, the data received by the receiving end at one time may be multiple packets. In order to solve this problem, the length of the packet needs to be sent before sending the data. Therefore, the structure of the packet should be message length + message content.
This article, let’s talk about data splicing, here comes the useful information
First, splice the data packet
1 |
|
ByteBuffer used in the code The tool is provided in Java, but not in C#. The source code is excerpted. However, the author did not add a method to obtain all bytecodes in the tool, so he added a GetBytes() method
1 |
|
Of course, although there is no ByteBuffer in c#, there are ways to splice byte arrays, such as
1 2 |
|
After the byte array is spliced, you can use the send method of the socket to send it, but this article will continue to finish the processing of receiving data.
The order of receiving data is to receive the message length first. , and then receive the message of the specified length according to the message length
1 |
|
The GetBytesRecive method is used to receive the message and solve the problem of sticky packets and missing packets. The code is as follows
1 |
|
At this point, the simple sending and receiving of messages is basically done. However, in actual projects, we will definitely not have only one message. If it is a long-link project, we need to receive and send messages all the time. ,what to do?
As we all know, the display on Unity's UI can only be executed in the main thread. However, if we keep receiving and sending messages in the main thread, the experience will be extremely poor, so we must open another thread to be responsible for the messages. Receiving and sending, the next article is to use multi-threading to complete socket communication
The above is the detailed content of Detailed explanation of examples of socket transmission protobuf byte stream. For more information, please follow other related articles on the PHP Chinese website!