【Extreme】P3-1 Network Message Processing_Communication and Protocol (Client Communication)


foreword

This time I learned about the project communication components. I have probably gone through the functions of each communication component and how to communicate with each other.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Communication components

From top to bottom
网络客户端&服务端监听: one sends information on the client side, one listens to the information on the server and responds to the information
封包处理器: processing the conversion of packets and protocols, serializing the data received on the network into the data accepted by the Protro protocol, and deserializing the Protro The data of the protocol is converted into the data format of network communication
消息分发器&消息分配处理器: the server receives the client's information and distributes it to each communication module of the server, and uses multi-thread processing and the distributor to work together for speed.
The distributor only distributes to whomever, the distributor is used to do one-to-one distribution, and which module to maintain which message to process

Example: pandas is a NumPy-based tool created to solve data analysis tasks.

2. Client NetClient script

In order for the client to communicate with the server, the first thing is to link to the server.
The client only provides the function of sending link messages.

1. Connect connection method

int time 为重连时间,服务于断线重连
ifthis.connecting) 判断有没有链接,如果有就结束
this.DoConnect() 开始链接

insert image description here

2. DoConnect start link

使用try cath //防止链接中出错,抓取错误
new Soket 设置Soket //网络链接采用的是TCP协议
this.clientSocket.Blocking = true //设置阻塞的状态 客户端链接服务器时我们希望链接是阻塞的,因此我们能实时等待到链接的成果
this.clientSocket.BeginConnect() //链接 得到一个Result(请求)
result.AsyncWaitHandle.WailtOne() //这里使用了异步的方式,异步的时候并不会等待,因此我们需要使用AsyncWaitHandle来使它等待,WaitOne 会等待我们异步的事件,直到它完成才继续往下执行,反之如果不阻塞,将会直接往下执行,但会违背我们阻塞的初衷
---
catch(SocketException ex) //捕获 链接被拒绝或者链接异常这些属于网络专有的错误

catch(Exception e) //捕获常规异常

if(this.clientSocket.Connected) //前面执行完后,这里再会判断一次,继续判断本次的链接是否成功

this.clientSocket.Blocking=false; //链接成功之后就把阻塞清除,也为了后面玩起来和服务器进行通讯的时候是畅通无阻的

this.connecting = false //链接已完成,标记为false,表示现已不在链接,链接过程已完成

insert image description here

3. After the link is completed, send the message SendMessage()

NetMessageProtocol auto-generated classes
insert image description here

if!this.Conected) 判断是否链接状态
this.Connect(); 	没有就再链接一次
sendQueue.Equeue(message) 发送队列,为了不影响玩家操作(例如不小心在一帧发了一百个消息,但因为队列,不会在一瞬间发出)

insert image description here

4. The execution of Update() ends -> the client connection process ends

insert image description here

if(this.KeepConnect()) 保证断线重连,判断状态
   if(this.ProcessRecv()) 先接收(什么处理都不做),每一帧先看没有请求数据,有就收到之心执行链接
   	ifthis.Connected) 收到后判断有没有异常(有没有和服务器连着)
   			this.ProcessSend(); 发送消息
   			this.ProcessMessage(); 处理消息。
   			

Message distributor, directly distributes information
insert image description here
The reason why it can be directly distributed here is that when receiving information, the data is processed (ret=this.clientSocket.Poll(0,SelectMode.SelectRead));

int n = clientSocket.Receive(Write data into the cache area)
this.packageHandle.ReceiveDate()Call the server's packet processor to receive datainsert image description here


Summarize

I have a general understanding of the link process of the client, and the bottom layer is not too deep. At present, I only know what these codes are used for.

Guess you like

Origin blog.csdn.net/weixin_46172181/article/details/126432509