Exemplo n.º 1
0
        private IEnumerator BeginSendPacket <T>(ENetworkMessage networkMessage, T packet, uint timeoutMessage, byte[] msgIDBytes) where T : global::ProtoBuf.IExtensible
        {
            string msgID = BitConverter.ToString(msgIDBytes);

            lock (_msgIDDict)
            {
                _msgIDDict.Add(BitConverter.ToString(msgIDBytes), packet);
            }

            Log4U.LogInfo("Send : " + networkMessage + " msgID : " + msgID);

            DoBeginSendPacket <T>(networkMessage, packet, msgIDBytes);
            yield return(new WaitForSeconds(REQ_TIME_OUT));

            lock (_msgIDDict)
            {
                if (_msgIDDict.ContainsKey(msgID))
                {
                    RemoveMsgID(msgID);

                    NetworkMessageParam param = new NetworkMessageParam
                    {
                        msgID = msgID,
                        req   = packet,
                    };
                    MessageDispatcher.GetInstance().DispatchMessage(timeoutMessage, param);
                    DialogManager.GetInstance().CreateSingleButtonDialog("Send Packet Type : " + networkMessage + " msgID : " + msgID + " timeout ");
                }
            }
        }
Exemplo n.º 2
0
        public void OnSendChatRsp(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;
            SendChatRsp         rsp   = param.rsp as SendChatRsp;

            Log4U.LogDebug(rsp.resultCode);
            if (rsp.resultCode == SendChatRsp.ResultCode.SUCCESS)
            {
                if (_waitSendChatDict.ContainsKey(param.msgID))
                {
                    SendChatReq req = param.req as SendChatReq;
                    _waitSendChatDict[param.msgID].isSend = true;
                    _waitSendChatDict.Remove(param.msgID);
                }
                MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_SEND_CHAT);
            }
            else
            {
                if (_waitSendChatDict.ContainsKey(param.msgID))
                {
                    SendChatReq req = param.req as SendChatReq;
                    _waitSendChatDict[param.msgID].isSend = false;
                    _waitSendChatDict.Remove(param.msgID);
                }
                MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_SEND_CHAT);
            }
        }
Exemplo n.º 3
0
        public void OnSendChatTimeOut(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;

            if (_waitSendChatDict.ContainsKey(param.msgID))
            {
                SendChatReq req = param.req as SendChatReq;
                _waitSendChatDict[param.msgID].isSend = false;
                _waitSendChatDict.Remove(param.msgID);
            }
            MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_SEND_CHAT);
        }
Exemplo n.º 4
0
        public void OnGetPersonalInfoRsp(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;
            GetPersonalInfoRsp  rsp   = param.rsp as GetPersonalInfoRsp;
            GetPersonalInfoReq  req   = param.req as GetPersonalInfoReq;

            if (rsp.resultCode == GetPersonalInfoRsp.ResultCode.SUCCESS &&
                req.userInfo)
            {
                _userName  = rsp.userInfo.userName;
                _headIndex = rsp.userInfo.headIndex;
                MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_PERSONAL_DETAIL, null);
            }
        }
Exemplo n.º 5
0
        public void OnChangeGroupRsp(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;
            ChangeGroupReq      req   = param.req as ChangeGroupReq;
            ChangeGroupRsp      rsp   = param.rsp as ChangeGroupRsp;

            if (rsp.resultCode == ChangeGroupRsp.ResultCode.SUCCESS &&
                req.changeType == ChangeGroupReq.ChangeType.DELETE)
            {
                _groupDict.Remove(req.groupId);
                GlobalChat.GetInstance().RemoveChatLog(req.groupId);
                StateManager.GetInstance().ClearStatesExceptBottom(true);
            }
        }
Exemplo n.º 6
0
        public void OnGetPersonalInfoRsp(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;
            GetPersonalInfoRsp  rsp   = param.rsp as GetPersonalInfoRsp;
            GetPersonalInfoReq  req   = param.req as GetPersonalInfoReq;

            if (rsp.resultCode == GetPersonalInfoRsp.ResultCode.SUCCESS &&
                req.friendInfo)
            {
                _groupDict.Clear();
                foreach (GroupItem group in rsp.groups)
                {
                    _groupDict.Add(group.groupId, group);
                }
            }
        }
Exemplo n.º 7
0
        public void OnGetPersonalInfoRsp(uint iMessageType, object kParam)
        {
            NetworkMessageParam param = kParam as NetworkMessageParam;
            GetPersonalInfoRsp  rsp   = param.rsp as GetPersonalInfoRsp;
            GetPersonalInfoReq  req   = param.req as GetPersonalInfoReq;

            if (rsp.resultCode == GetPersonalInfoRsp.ResultCode.SUCCESS &&
                req.friendInfo)
            {
                _friendDict.Clear();
                foreach (UserItem friend in rsp.friends)
                {
                    _friendDict[friend.userId] = friend;
                }
                MessageDispatcher.GetInstance().DispatchMessageAsync((uint)EUIMessage.UPDATE_CHAT_LIST, null);
            }
        }
Exemplo n.º 8
0
        private void EndReceivePacket(IAsyncResult ar)
        {
            int bytesRead = -1;

            try
            {
                if (IsConncted)
                {
                    lock (_socket)
                    {
                        bytesRead = _socket.EndReceive(ar);
                    }
                }

                if (bytesRead == -1)
                {
                    CloseConnection();
                    return;
                }
            }
            catch (ObjectDisposedException)
            {
                Log4U.LogInfo("Receive Closed");
            }
            catch (Exception ex)
            {
                Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source);
            }


            // Begin Read //
            int position = 0;

            while (position < bytesRead)
            {
                int             bufferSize     = MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 0);
                ENetworkMessage networkMessage = (ENetworkMessage)MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 1);

                byte[] msgIDBytes = new byte[HEAD_SIZE];
                for (int i = 0; i < HEAD_SIZE; i++)
                {
                    msgIDBytes[i] = _receiveBuffer[position + HEAD_SIZE * 2 + i];
                }
                string msgID = BitConverter.ToString(msgIDBytes);

                if (networkMessage != ENetworkMessage.KEEP_ALIVE_SYNC)
                {
                    Log4U.LogInfo("networkMessage : " + networkMessage, "msgID : " + msgID, "bufferSize : " + bufferSize);
                }

                if (position + bufferSize > bytesRead)
                {
                    Log4U.LogError("Error receive packet, packet is too long : " + bufferSize);
                    break;
                }

                IExtensible rspPacket = UnPackTool.UnPack(networkMessage, position + HEAD_SIZE * HEAD_NUM, bufferSize - HEAD_NUM * HEAD_SIZE, _receiveBuffer);
                if (rspPacket == null)
                {
                    continue;
                }

                MessageArgs args = new MessageArgs
                {
                    iMessageType = (uint)networkMessage,
                    kParam       = rspPacket,
                };

                NetworkMessageParam networkParam = new NetworkMessageParam
                {
                    rsp   = rspPacket,
                    msgID = msgID,
                };

                lock (_msgIDDict)
                {
                    if (_msgIDDict.ContainsKey(msgID))
                    {
                        networkParam.req = _msgIDDict[msgID];
                    }

                    if (_needReqMessageType.Contains(networkMessage))
                    {
                        args.kParam = networkParam;
                    }

                    if (_forcePushMessageType.Contains(networkMessage) || _msgIDDict.ContainsKey(msgID))
                    {
                        MessageDispatcher.GetInstance().DispatchMessageAsync(args.iMessageType, args.kParam);
                    }

                    if (_msgIDDict.ContainsKey(msgID))
                    {
                        RemoveMsgID(msgID);
                    }
                }

                //if (_forcePushMessageType.Contains(networkMessage))
                //{
                //    DoBeginSendPacket(networkMessage, msgIDBytes);
                //}

                position += bufferSize;
            }

            Array.Clear(_receiveBuffer, 0, _socket.ReceiveBufferSize);

            BeginReceivePacket();
        }