Example #1
0
 private void Update()
 {
     while (NetMessageDataQueue.Count > 0)
     {
         Event_NetCommand _event = NetMessageDataQueue.Dequeue();
         if (NetCommandList.ContainsKey(_event._commandType))
         {
             facade.SendNotification(NetCommandList[_event._commandType], _event._commandData);
         }
     }
 }
Example #2
0
    /// <summary>
    /// 接受网络数据
    /// </summary>
    private void _onReceiveSocket()
    {
        while (true)
        {
            if (!clientSocket.Connected)
            {
                _isConnected = false;
                _ReConnect();
                break;
            }
            try
            {
                int receiveLength = clientSocket.Receive(_tempReceiveBuff);
                if (receiveLength > 0)
                {
                    _databuffer.AddBuffer(_tempReceiveBuff, receiveLength); //将收到的数据添加到缓存器中
                    while (_databuffer.GetData(out _socketData))            //取出一条完整数据
                    {
                        Debug.Log("取出一条数据");
                        Event_NetCommand tempNetMessageData = new Event_NetCommand();
                        tempNetMessageData._commandType = _socketData._commandType;
                        tempNetMessageData._commandData = new ByteStreamBuffer(_socketData._data);

                        //锁死消息中心消息队列,并添加数据
                        lock (MessageCenter.Instance.NetMessageDataQueue)
                        {
                            MessageCenter.Instance.NetMessageDataQueue.Enqueue(tempNetMessageData);
                        }
                    }
                }
            }
            catch (System.Exception)
            {
                clientSocket.Disconnect(true);
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                break;
            }
        }
    }