Ejemplo n.º 1
0
        public void Update(float deltaTime)
        {
            int handledCnt = 0;

            while (NetMessageQueue.Count > 0)
            {
                lock (NetMessageQueue)
                {
                    NetMessageData netMsgData = NetMessageQueue.Dequeue();
                    handledCnt++;
                    try
                    {
                        if (null != OnMessage)
                        {
                            OnMessage(netMsgData.protocol, netMsgData.eventData);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("try to handle message error!" + e.ToString());
                    }
                    if (handledCnt >= perHandleCnt)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 接收网络数据
        /// </summary>
        private void _onReceiveSocket()
        {
            while (true)
            {
                if (!clientSocket.Connected)
                {
                    isConnected = false;
                    _ReConnect();
                    break;
                }
                try
                {
                    int receiveLength = clientSocket.Receive(tmpReceiveBuff);
                    if (receiveLength > 0)
                    {
                        databuffer.AddBuffer(tmpReceiveBuff, receiveLength); //将收到的数据添加到缓存器中
                        while (databuffer.GetData(out socketData))           //取出一条完整数据
                        {
                            //只有消息协议才进入队列
                            if (Constants.PING_PROTO_CODE != socketData.protocal)
                            {
                                NetMessageData netMsgData = new NetMessageData();
                                netMsgData.protocol  = socketData.protocal;
                                netMsgData.eventData = socketData.data;

                                //锁死消息中心消息队列,并添加数据
                                lock (NetMessageCenter.Instance.NetMessageQueue)
                                {
                                    NetMessageCenter.Instance.NetMessageQueue.Enqueue(netMsgData);
                                }
                            }
                            else
                            {
                                //TODO:处理ping协议
                            }
                        }
                    }
                }
                catch (System.Exception e)
                {
                    if (null != OnErrorCode)
                    {
                        OnErrorCode((int)NetErrorEnum.ReceiveError);
                    }
                    clientSocket.Disconnect(true);
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                    break;
                }
            }
        }