Ejemplo n.º 1
0
        /// <summary>
        /// 协议格式:
        /// SIZE : 4 | TYPE : 4 | MsgID : 4 | PACKET : dynamic
        /// </summary>
        /// <typeparam name="T">向服务器发送的packet的类型</typeparam>
        /// <param name="networkMessage">向服务器发送的请求的类型</param>
        /// <param name="packet">向服务器发送的packet</param>
        private void DoBeginSendPacket <T>(ENetworkMessage networkMessage, T packet, byte[] msgID) where T : global::ProtoBuf.IExtensible
        {
            try
            {
                byte[] sendBuffer = new byte[_socket.SendBufferSize];

                MemoryStream streamForProto = new MemoryStream();
                Serializer.Serialize <T>(streamForProto, packet);
                int bufferSize = HEAD_SIZE * HEAD_NUM + (int)streamForProto.Length;

                byte[] bufferSizeBytes     = MiniConverter.IntToBytes(bufferSize);
                byte[] networkMessageBytes = MiniConverter.IntToBytes((int)networkMessage);

                Array.Copy(bufferSizeBytes, 0, sendBuffer, HEAD_SIZE * 0, HEAD_SIZE);
                Array.Copy(networkMessageBytes, 0, sendBuffer, HEAD_SIZE * 1, HEAD_SIZE);
                Array.Copy(msgID, 0, sendBuffer, HEAD_SIZE * 2, HEAD_SIZE);
                Array.Copy(streamForProto.ToArray(), 0, sendBuffer, HEAD_SIZE * HEAD_NUM, streamForProto.Length);
                lock (_socket)
                {
                    if (_socket != null && _socket.Connected)
                    {
                        _socket.BeginSend(sendBuffer, 0, bufferSize, SocketFlags.None, new AsyncCallback(EndSendPacket), null);
                    }
                }
                streamForProto.Dispose();
            }
            catch (ObjectDisposedException)
            {
                Log4U.LogInfo("Send Closed");
            }
            catch (Exception ex)
            {
                Log4U.LogError(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public void OnReceiveChatSync(uint iMessageType, object kParam)
        {
            ReceiveChatSync rsp = kParam as ReceiveChatSync;

            foreach (var chatItem in rsp.chatData)
            {
                AddChatDataItem(MiniConverter.ChatItemToDataItem(chatItem));
            }
            MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_CHAT_LIST, null);
            MessageDispatcher.GetInstance().DispatchMessage((uint)EUIMessage.UPDATE_RECEIVE_CHAT, null);
        }
Ejemplo n.º 3
0
        public void SendChatReq(ChatDataItem chatDataItem)
        {
            SendChatReq req = new SendChatReq
            {
                chatData = MiniConverter.ChatDataItemToItem(chatDataItem),
            };

            AddChatDataItem(chatDataItem);

            string msgID = NetworkManager.GetInstance().SendPacket <SendChatReq>(ENetworkMessage.SEND_CHAT_REQ, req, (uint)EModelMessage.SEND_CHAT_TIMEOUT);

            _waitSendChatDict.Add(msgID, chatDataItem);
        }
Ejemplo n.º 4
0
        private void DoBeginSendPacket(ENetworkMessage networkMessage, byte[] msgID)
        {
            try
            {
                byte[] sendBuffer = new byte[HEAD_SIZE * HEAD_NUM];

                byte[] bufferSizeBytes     = MiniConverter.IntToBytes(HEAD_SIZE * HEAD_NUM);
                byte[] networkMessageBytes = MiniConverter.IntToBytes((int)networkMessage);

                Array.Copy(bufferSizeBytes, 0, sendBuffer, HEAD_SIZE * 0, HEAD_SIZE);
                Array.Copy(networkMessageBytes, 0, sendBuffer, HEAD_SIZE * 1, HEAD_SIZE);
                Array.Copy(msgID, 0, sendBuffer, HEAD_SIZE * 2, HEAD_SIZE);

                lock (_socket)
                {
                    _socket.BeginSend(sendBuffer, 0, HEAD_SIZE * HEAD_NUM, SocketFlags.None, new AsyncCallback(EndSendPacket), null);
                }
            }
            catch (Exception ex)
            {
                Log4U.LogError(ex.Message);
            }
        }
Ejemplo n.º 5
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();
        }