介绍
在阅读了罗培羽著作的Unity3D网络游戏实战一书后,博主综合自己的开发经验与考虑进行部分修改和调整,将通用的客户端网络模块和通用的服务端框架进行提取,形成专栏,介绍Socket网络编程,希望对其他人有所帮助。目录如下:
一、通用服务端框架
二、通用客户端网络模块
(二)、Receive 接收并处理数据
本篇内容:
Receive 接收数据:
连接服务端成功后调用socket.BeginReceive开始接收数据:
//Connect回调
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket socket = (Socket)ar.AsyncState;
socket.EndConnect(ar);
isConnecting = false;
Debug.Log($"成功连接服务端.");
//发布消息
Messenger.Publish("连接服务端", true);
//开始接收数据
socket.BeginReceive(readBuff.bytes, readBuff.writeIdx, readBuff.remain, 0, ReceiveCallback, socket);
}
catch (SocketException error)
{
Debug.Log($"连接服务端失败:{error}");
isConnecting = false;
//发布消息
Messenger.Publish("连接服务端", false);
}
}
BeginReceive的回调函数ReceiveCallback会判断是否成功收到数据,如果收到数据长度为0,断开连接;如果收到正常的数据,更新缓冲区的writeIdx,再调用处理消息的函数OnReceiveData:
//Receive回调
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
Socket socket = (Socket)(ar.AsyncState);
//获取接收数据长度
int count = socket.EndReceive(ar);
if (count == 0)
{
Close();
Debug.Log("关闭连接.");
return;
}
readBuff.writeIdx += count;
//处理二进制消息
OnReceiveData();
//继续接收数据
if (readBuff.remain < 8)
{
readBuff.MoveBytes();
readBuff.ReSize(readBuff.length * 2);
}
socket.BeginReceive(readBuff.bytes, readBuff.writeIdx, readBuff.remain, 0, ReceiveCallback, socket);
}
catch (SocketException error)
{
Debug.Log($"接收数据失败: {error}");
}
}
处理数据:
OnReceiveData中进行协议的解码,并把协议对象添加到消息列表msgList,通信协议与服务端同样使用ProtoBuf,因此需要将protobuf-net.dll导入Unity工程中,并在代码中引入命名空间ProtoBuf,注意将它们在Init函数中进行初始化:
//消息列表
private static List<IExtensible> msgList;
//消息列表长度
private static int msgCount;
//数据处理
private static void OnReceiveData()
{
//消息长度
if (readBuff.length <= 2) return;
//获取消息体长度
int readIdx = readBuff.readIdx;
byte[] bytes = readBuff.bytes;
Int16 bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]);
if (readBuff.length < bodyLength + 2) return;
readBuff.readIdx += 2;
//解析协议名
string protoName = ProtoUtility.DecodeName(readBuff.bytes, readBuff.readIdx, out int nameCount);
if (string.IsNullOrEmpty(protoName))
{
Debug.Log("协议名解码失败.");
return;
}
readBuff.readIdx += nameCount;
//解析协议体
int bodyCount = bodyLength - nameCount;
IExtensible proto = ProtoUtility.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);
readBuff.readIdx += bodyCount;
readBuff.CheckAndMoveBytes();
//添加到消息队列
lock (msgList)
{
msgList.Add(proto);
}
msgCount++;
//继续读取消息
if (readBuff.length > 2)
{
OnReceiveData();
}
}
定义每帧处理协议的最大数量,并处理协议:
//每一次Update处理的消息量
private static readonly int MAX_MESSAGE_FIRE = 10;
//协议处理
private static void ProtoUpdate()
{
//初步判断 提升效率
if (msgCount == 0) return;
//重复处理消息
for (int i = 0; i < MAX_MESSAGE_FIRE; i++)
{
//获取第一条消息
IExtensible msg = null;
lock (msgList)
{
if (msgList.Count > 0)
{
msg = msgList[0];
msgList.RemoveAt(0);
msgCount--;
}
}
//分发消息
if (msg != null)
{
Messenger.Publish(msg.GetType().Name, msg);
}
else
{
break;
}
}
}
public static void Update()
{
ProtoUpdate();
}
参考资料:《Unity3D网络游戏实战》(第2版)罗培羽 著