protected ConcurrentQueue <AllTcpRequestData> mAllTcpRequestDatas = new ConcurrentQueue <AllTcpRequestData>(); //所有的Tcp 请求 public void AddRequestMessage(Socket client, byte[] request) { if (client == null || request == null) { Debug.LogError($"接收消息失败"); return; } lock (new object()) { AllTcpRequestData tcpRequestData = new AllTcpRequestData() { mClientSocket = client, RequestMessage = request, }; mAllTcpRequestDatas.Enqueue(tcpRequestData); } }
private void Update() { AllTcpRequestData requestData = null; while (mAllTcpRequestDatas.TryDequeue(out requestData)) { int protocolID = SocketHead.GetPacketProtocolID(requestData.RequestMessage, 4); if (protocolID == ProtocolCommand.RequestHearBeat) { Debug.Log($"收到{requestData.mClientSocket.RemoteEndPoint} 心跳包"); HeartbeatResponse response = new HeartbeatResponse() { mSystemTime = System.DateTime.UtcNow.Ticks }; string responseStr = SerializeManager.SerializeObject(response); TcpServerUIComponent.Instance.mTcpServer.SendMessage(ProtocolCommand.ResponseHearBeat, responseStr, requestData.mClientSocket); } else if (protocolID == ProtocolCommand.RequestLogin) { string message = Encoding.UTF8.GetString(requestData.RequestMessage, SocketHead.S_HeadLength, requestData.RequestMessage.Length - SocketHead.S_HeadLength); Debug.Log($"接收到来自{requestData.mClientSocket.RemoteEndPoint} 的登录信息{message}"); LoginRequest request = SerializeManager.DeserializeObject <LoginRequest>(message); LoginResponse response = new LoginResponse(); response.mUserName = request.mUserName; response.mToken = "xyz"; response.mIsSuccess = true; string responseStr = SerializeManager.SerializeObject(response); TcpServerUIComponent.Instance.mTcpServer.SendMessage(ProtocolCommand.ResponseLogin, responseStr, requestData.mClientSocket); } } }