private void OnReceivedDatas(ClientSocket clientSocket)
        {
            ByteArray readBuffer = clientSocket.ReadBuffer;

            if (readBuffer.Length <= 4 || readBuffer.ReadIndex < 0)
            {
                return;
            }

            int readIdx = readBuffer.ReadIndex;

            byte[] bytes      = readBuffer.Bytes;
            int    bodyLength = BitConverter.ToInt32(bytes, readIdx);

            //判断接收到的信息长度是否小于包体长度+包体头长度,如果小于,代表我们的信息不全,大于代表信息全了(有可能有粘包存在)
            if (readBuffer.Length < bodyLength + 4)
            {
                return;
            }

            readBuffer.ReadIndex += 4;

            //解析协议名
            int          nameCount = 0;
            ProtocolEnum proto     = ProtocolEnum.None;

            try
            {
                proto = _messageParser.DecodeName(readBuffer.Bytes, readBuffer.ReadIndex, out nameCount);
            }
            catch (Exception ex)
            {
                Debug.LogError($"解析协议名出错:{ex}");
                CloseClient(clientSocket);
                return;
            }

            if (proto == ProtocolEnum.None)
            {
                Debug.LogError($"OnReceiveData MsgBase.DecodeName  fail.Proto type is Null!");
                CloseClient(clientSocket);
                return;
            }

            readBuffer.ReadIndex += nameCount;

            //解析协议体
            int         bodyCount   = bodyLength - nameCount;
            MessageBase messageBase = null;

            try
            {
                messageBase = _messageParser.DecodeContent(proto, readBuffer.Bytes, readBuffer.ReadIndex, bodyCount);
                if (messageBase == null)
                {
                    Debug.LogError($"{proto}协议内容解析出错!!!!!");
                    CloseClient(clientSocket);
                    return;
                }
            }
            catch (Exception ex)
            {
                Debug.LogError($"接收数据协议内容解析错误:{ex}");
                CloseClient(clientSocket);
                return;
            }

            readBuffer.ReadIndex += bodyCount;
            readBuffer.CheckMoveBytes();

            if (!_protocolHandles.ContainsKey(proto))
            {
                Debug.LogError($"没有协议的处理方法:{proto}");
                CloseClient(clientSocket);
                return;
            }

            _protocolHandles[proto]?.Invoke(clientSocket, messageBase);

            if (readBuffer.Length > 4)
            {
                OnReceivedDatas(clientSocket);
            }
        }
示例#2
0
 void OnReceiveData(ClientSocket clientSocket)
 {
     //如果信息长度不够,需要再次读取信息
     //OnReceiveData(clientSocket);
 }
 private void CloseClient(ClientSocket clientSocket)
 {
     clientSocket.Socket.Close();
     _clientDict.Remove(clientSocket.Socket);
     Debug.Log($"一个客户端断开了连接,当前总连接数:{_clientDict.Count}");
 }
示例#4
0
 //关闭客户端
 public void CloseClient(ClientSocket client)
 {
     client.Socket.Close();
     m_ClientDic.Remove(client.Socket);
     Debug.Log("一个客户端断开连接 ,当前总连接数:{0}", m_ClientDic.Count);
 }