Introduction: This article is compiled by the editor of Programming Notes# for you. It mainly introduces two network communication architectures: socket and RPC-related knowledge. I hope it will be of certain reference value to you.
The communication between client and server is the foundation of online games. For an online game, most of the business requirements involve network communication, but in fact, only a small number of developers in the development team have actually dealt with network communication logic. Because the network communication logic is always encapsulated in the bottom layer of the framework, the programmers who handle the business only need to call the interface provided by the network engineering lion to achieve all the requirements. And the gap between programmers and engineering lions has also been opened.
This article will start from the realization of the network communication function of the Unity client, and introduce two architectures of network communication, Socket and RPC. The opinions here are all from my collection of network resources, and some of them are my personal perceptions, which are not guaranteed to be completely correct. Welcome to discuss.
Please indicate the source for reprinting.
The structure of the article is shown in the figure below, and you can choose to read according to your interests.
-
The use of Socket
-
About thread Thread
-
Socket Encyclopedia
-
Design concept and implementation of socket
-
NetworkView.rpc
-
UNet
-
RPC Encyclopedia
> -
The design concept and implementation of RPC
-
A comparison of socket and RPC architecture
SocketUse
As mentioned above, This article will start from the implementation of the Unity3D client Network communication begins, here will use C # language.
After understanding the following information, you can realize basic network communication functions:
-
C# language provides a Socket class in the System.Net.Sockets namespace.
-
Use the Socket.Connect(IPEndPoint endPoint) method to establish a connection with the remote host of the specified IP.
-
Use the Socket.Send(byte[] message) method to send byte messages.
-
Use the Socket.Receive(byte[] message) method to accept byte messages.
So far, the simple network communication function of a client has been completed.
For server-side processing, please refer to:
C#socket communication-Code Farmer is coming-Blog Garden
https:// www.cnblogs.com/sdyinfang/p/5519708.html
About thread Thread
Thread is not directly related to network communication, the main purpose of this article It is because network communication is usually placed in a separate thread, and it is also for the symmetry of chapter layout.
After understanding the following information, you can implement basic multithreading functions:
-
C# The language provides a Thread class in the System.Threading namespace.
-
Use the Thread.Start() method to start a new thread.
So, why are network communication functions usually placed in separate threads?
It turns out that this is because the mechanism of the socket architecture provided by C# is synchronous. That is to say, the thread executes to the sending protocol, it will hang up, and wait for the server to complete the execution before starting to execute the next line.
In this way, the problem becomes obvious, and the fluency of the product will be limited by the network speed.
In order to prevent the game from being affected by the slow network speed and freeze, most game project clients choose to put the network communication function in a separate thread, that is, in addition to the main thread of the game In addition, there will be a thread that sends the agreement, and a thread that accepts the agreement.
But such an approach is not a mandatory option. I guess that some mainstream products on the market are single-threaded. The basis for my guess is that when using the product delivery protocol, the screen is locked, and the network loading icon appears, and the next step can only be started after the network loading is complete. This is probably because these products themselves do not conduct network communication as frequently as games, and the content of communication is very important for the next step.
socket encyclopedia
Read At this point, you already know how to use sockets.
But, what exactly is a socket?
Two programs on the network exchange data through a two-way communication connection, and one end of this connection is called a socket.
At least a pair of port numbers (sockets) are required to establish a network communication connection. The essence of a socket is a programming interface (API). It encapsulates TCP/IP, and TCP/IP also provides an interface for programmers to do network development. This is the Socket programming interface ;HTTP is a car, which provides a specific form of encapsulation or display data; Socket is an engine, which provides the ability of network communication.
For more information, please refer to:
socket (computer term)_Baidu Encyclopedia
https://baike.baidu.com/item/socket/281150?fr=aladdin
socket design concept and implementation
socket is not a protocol, it is a protocol for TCP at the programmer level /IP protocol encapsulation and application. In fact, it is a call interface, which is convenient for programmers to use the TCP/IP protocol stack. Programmers use the tcp/ip protocol through sockets. However, the socket does not necessarily use the tcp/ip protocol. When the Socket programming interface is designed, it is hoped that it can also adapt to other network protocols.
For more information, please refer to:
C# Socket Network Programming Essence – Slightly Cold Rain – Blog Garden
http:/ /www.cnblogs.com/weilengdeyu/archive/2013/03/08/2949101.html
Socket Implementation- kakawater – Blog Park
https://www.cnblogs.com/kakawater/p/7085122.html
NetworkView.rpc
It is still starting from the realization of the network communication of the Unity3D client, using the C# language.
After understanding the following information, you can realize basic network communication functions:
-
Unity The game engine provides a NetworkView component.
-
Use Network.Connect to establish a connection with a remote host with a specified IP.
-
Call the NetworkView.RPC method to call the function on the remote host.
-
Only functions with the [RPC] tag can be called remotely.
For more information, please refer to:
Unity – Scripting API: NetworkView.RPC
https://docs.unity3d.com/ScriptReference/NetworkView.RPC.html
UNet
Perhaps you have noticed that the “Method group” on NetworkView.rpc is marked on the official API provided by Unity is Obsolete”. Unity’s version update iteration is relatively fast, and the version difference is relatively large, and important BUGs are often fixed, or framework adjustments are made. I wanted to study UNet carefully, but I also saw a prompt “this API is experimental and might be changed or removed in the future.”.
For more information, please refer to:
Unity – Scripting API: UNetUpdate
https://docs.unity3d. com/ScriptReference/Experimental.PlayerLoop.PreLateUpdate.UNetUpdate.html
RPC Encyclopedia
No matter how the version of Unity iterates, it can be seen that it will always provide an RPC architecture, although the method used is ever-changing.
So, what exactly is RPC?
RPC (Remote Procedure Call)—Remote Procedure Call, which is a protocol that requests services from remote computer programs over the network without understanding the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprogramming.
RPC adopts the client/server model. A requester is a client, and a service provider is a server. First, the client calling process sends a call message with process parameters to the service process, and then waits for a reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server obtains the process parameters, calculates the result, sends a reply message, and then waits for the next call message. Finally, the client call process receives the reply message, obtains the process result, and then the call execution continues.
For more information, please refer to:
Remote Procedure Call Protocol_Baidu Encyclopedia
https://baike.baidu.com/ item/%E8%BF%9C%E7%A8%8B%E8%BF%87%E7%A8%8B%E8%B0%83%E7%94%A8%E5%8D%8F%E8%AE%AE /6893245?fromtitle=RPC&fromid=609861&fr=aladdin
RPC Design Concept and Implementation
The conceptual term RPC was proposed by Bruce Jay Nelson in the 1980s. Here we go back to what was the original motivation for developing RPC? In Nelson’s paper “Implementing Remote Procedure Calls” he makes a few points:
-
Simple: the semantics of the RPC concept are very Clarity and simplicity make it easier to set up distributed computing.
-
Efficient: Procedure calls seem very simple and efficient.
-
General: In single-computer computing, procedures are often the most important communication mechanism between different algorithm parts.
Plainly speaking, general programmers are familiar with local procedure calls, so we make RPC completely similar to local calls, then It is easier to accept and use it without any obstacles. Nelson’s paper was published 30 years ago, and his views seem far-sighted today. The RPC framework we use today basically achieves this goal.
For more information, please refer to:
In-depth explanation of RPC – Simple articles-CSDN blog
https://blog.csdn. net/mindfloating/article/details/39473807
In-depth explanation of RPC – in-depth articles-CSDN blog
https://blog.csdn.net/mindfloating/article/details/39474123
Simple understanding of RPC architecture – ChrisMurphy – Blog Garden
https://www.cnblogs. com/ChrisMurphy/p/6550184.html
socket and RPC Architecture comparison
socket is used as a “socket” and it transmits remote data; RPC is implemented on the basis of socket, and it transmits remote data Procedures (i.e. function methods).
Please indicate the source
Welcome to discuss! Welcome to correct me!
r>
Plainly speaking, general programmers are familiar with local procedure calls, so we make RPC completely similar to local calls, so it is easier to accept and use without obstacles. Nelson’s paper was published 30 years ago, and his views seem far-sighted today. The RPC framework we use today basically achieves this goal.
For more information, please refer to:
In-depth explanation of RPC – Simple articles-CSDN blog
https://blog.csdn. net/mindfloating/article/details/39473807
In-depth explanation of RPC – in-depth articles-CSDN blog
https://blog.csdn.net/mindfloating/article/details/39474123
Simple understanding of RPC architecture – ChrisMurphy – Blog Garden
https://www.cnblogs. com/ChrisMurphy/p/6550184.html
socket and RPC Architecture comparison
socket is used as a “socket” and it transmits remote data; RPC is implemented on the basis of socket, and it transmits remote data Procedures (i.e. function methods).
Please indicate the source
Welcome to discuss! Welcome to correct me!