protected void EndReceive(IAsyncResult result)
        {
            SocketAsyncState state = (SocketAsyncState)result.AsyncState;

            //接收到的数据
            byte[] rawData = Handler.EndReceive(result);

            ValidateData(rawData, out byte[] data);
            //如果数据长度为0,则断开Socket连接
            if (data.Length == 0)
            {
                Disconnected(true);
                return;
            }
            //引发接收完成事件
            ReceiveCompleted?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Receive)
            {
                Data = data
            });
            //再次开始接收数据
            if (IsConnected)
            {
                Handler.BeginReceive(Stream, EndReceive, state);
            }
        }
Example #2
0
 private void OnReceiveCompleted()
 {
     if (ReceiveCompleted != null)
     {
         ReceiveCompleted.Invoke(this, new EventArgs());
     }
 }
Example #3
0
        /// <summary>
        /// 从客户端接收数据
        /// </summary>
        /// <param name="clientAddress"></param>
        /// <param name="data"></param>
        public override void Receive(string clientAddress, byte[] data)
        {
            try
            {
                //解析头部信息,转换成统一的流格式:head[MsgType(1)+uid(4)+gameId(4)+serverId(4)+gzip(1)] + (len(4)+data)
                BufferReader reader      = new BufferReader(data);
                string       paramString = reader.ReadPacketString();
                paramString = HttpUtility.UrlDecode(paramString, Encoding.UTF8);
                int index = paramString.IndexOf("?d=");
                if (index != -1)
                {
                    index      += 3;
                    paramString = paramString.Substring(index, paramString.Length - index);
                }
                PacketMessage packet = ParsePacketMessage(clientAddress, paramString, ConnectType.Tcp);
                var           token  = new UserToken();
                token.GameId   = packet.Head.GameId;
                token.ServerId = packet.Head.ServerId;
                token.Uid      = packet.Head.Uid;
                ClientConnectManager.Push(clientAddress, token);

                if (ReceiveCompleted != null)
                {
                    //分发送到游戏服
                    byte[] packData   = packet.ToByte();
                    string successMsg = string.Format("{0}>>{1}接收到{2}字节!",
                                                      DateTime.Now.ToString("HH:mm:ss:ms"), clientAddress, data.Length);
                    ReceiveCompleted.BeginInvoke(clientAddress, packData, OnReceiveCompleted, successMsg);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Receive form client {0} error:{1}", clientAddress, ex);
            }
        }
Example #4
0
        public void Request(HttpConnection connection, string clientAddress, byte[] data)
        {
            string paramString = string.Empty;

            try
            {
                paramString = Encoding.ASCII.GetString(data);
                PacketMessage packet = ParsePacketMessage(clientAddress, paramString, ConnectType.Http);
                packet.Head.SSID = connection.SSID.ToString("N");
                HttpConnectionManager.Push(connection);

                if (ReceiveCompleted != null)
                {
                    //分发送到游戏服
                    byte[] packData   = packet.ToByte();
                    string successMsg = string.Format("{0}>>{1}接收到{2}字节!",
                                                      DateTime.Now.ToString("HH:mm:ss:ms"), clientAddress, data.Length);
                    ReceiveCompleted.BeginInvoke(clientAddress, packData, OnReceiveCompleted, successMsg);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Receive form http request:{0} error:{1}", paramString, ex);
            }
        }
Example #5
0
 private void OnSendToClient(string address, byte[] data)
 {
     if (ReceiveCompleted != null)
     {
         //推送到客户端
         ReceiveCompleted.BeginInvoke(address, data, null, null);
     }
 }
Example #6
0
        private void EndReceive(IAsyncResult result)
        {
            SocketAsyncState state = (SocketAsyncState)result.AsyncState;

            //接收到的数据
            byte[] data = Handler.EndReceive(result);
            //如果数据长度为0,则断开Socket连接
            if (data.Length == 0)
            {
                Disconnected(true);
                return;
            }

            if (DataReceiver.Status != SocketDataReceiver.ReceiverStatus.Preparing &&
                DataReceiver.Status != SocketDataReceiver.ReceiverStatus.Receiving)
            {
                //引发接收完成事件
                ReceiveCompleted?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Receive)
                {
                    Data = data
                });
            }
            else
            {
                DataReceiver.ReceiveCompleted(this, new SocketEventArgs(this, SocketAsyncOperation.Receive)
                {
                    Data = data
                });
                if (DataReceiver.Status == SocketDataReceiver.ReceiverStatus.Extractable)
                {
                    ReceiveCompleted?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Receive)
                    {
                        Data = DataReceiver.FullData
                    });
                }
            }
            //再次开始接收数据
            Handler.BeginReceive(Stream, EndReceive, state);
        }
Example #7
0
        void Handle(ReceiveCompleted message)
        {
            Interlocked.Decrement(ref _receiverCount);

            QueueReceiver();
        }
		void Handle(ReceiveCompleted message)
		{
			Interlocked.Decrement(ref _receiverCount);

			QueueReceiver();
		}
Example #9
0
 //收到客户端发送的数据
 private void client_ReceiveCompleted(object sender, SocketEventArgs e)
 {
     ReceiveCompleted?.Invoke(this, e);
 }
Example #10
0
        private void socket_ReceiveCompleted(object sender, SocketEventArgs e)
        {
            byte[] data = null;
            Guid   id   = new Guid();
            CommunicationReceiveEventArgs receiveEventArgs = null;

            switch (e.Data[0])
            {
            case 3:
                if (e.DataLength < 21)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                int    dataLength = BitConverter.ToInt32(e.Data.Skip(17).Take(4).ToArray(), 0);
                byte[] head       = null;
                if (e.DataLength > 21)
                {
                    head = e.Data.Skip(21).ToArray();
                }
                receiveEventArgs = new CommunicationReceiveEventArgs(this, id, dataLength, head);
                PreviewReceive?.Invoke(this, receiveEventArgs);
                data    = new byte[18];
                data[0] = 4;
                int i = 1;
                do
                {
                    data[i] = e.Data[i];
                    i++;
                }while (i <= 16);
                if (!receiveEventArgs.Handled)
                {
                    receiveEventArgs.Data = new byte[dataLength];
                    ReceiveCache.Add(id, receiveEventArgs);
                    data[17] = 1;
                }
                else
                {
                    data[17] = 0;
                }
                Socket.SendAsync(data);
                break;

            case 4:
                if (e.DataLength != 18)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                if (!SendWait.ContainsKey(id))
                {
                    return;
                }
                CommunicationSendEventArgs sendEventArgs = SendWait[id];
                SendWait.Remove(id);
                sendEventArgs.Success = e.Data[17] == 1;
                if (!sendEventArgs.Success)
                {
                    if (SendCompleted != null)
                    {
                        SendCompleted(this, sendEventArgs);
                    }
                    return;
                }
                data = GetSendBlock(sendEventArgs, 0);
                SendCache.Add(data, sendEventArgs);
                SendMark.Add(sendEventArgs, data.Length - 21);
                Socket.SendAsync(data);
                break;

            case 5:
                if (e.DataLength < 22)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                if (!ReceiveCache.ContainsKey(id))
                {
                    return;
                }
                receiveEventArgs = ReceiveCache[id];
                int offset = BitConverter.ToInt32(e.Data.Skip(17).Take(4).ToArray(), 0);
                for (int j = 0; j < e.DataLength - 21; j++)
                {
                    receiveEventArgs.Data[offset + j] = e.Data[21 + j];
                }
                if (offset + e.DataLength - 21 == receiveEventArgs.DataLength)
                {
                    ReceiveCache.Remove(id);
                    ReceiveCompleted?.Invoke(this, receiveEventArgs);
                }
                break;
            }
        }
Example #11
0
 //收到客户端发送的数据
 private void client_ReceiveCompleted(object sender, byte[] e)
 {
     ReceiveCompleted?.Invoke(this, e);
 }