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);
            }
        }
Ejemplo n.º 2
0
 private void OnReceiveCompleted()
 {
     if (ReceiveCompleted != null)
     {
         ReceiveCompleted.Invoke(this, new EventArgs());
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理接收的数据
        /// </summary>
        private void ProcessReceive()
        {
            isProcess = true;
            //解析数据包
            byte[] data = EncodeHelper.DecodePacket(ref dataCache);
            if (data == null)
            {
                isProcess = false;
                return;
            }
            SocketMsg msg = EncodeHelper.DecodeMsg(data);

            //回调给上层
            receiveCompleted?.Invoke(this, msg);
            //伪递归
            ProcessReceive();
        }
Ejemplo n.º 4
0
        private void processRecive()
        {
            IsReciveProcess = true;
            //解析数据包
            byte[] data = EncodeTool.DecodePacket(ref dataCache);

            if (data == null)
            {
                IsReciveProcess = false;
                return;
            }

            SocketMessage msg = EncodeTool.DecodeMsg(data);

            //回调给上层
            reciveCompleted?.Invoke(this, msg);

            processRecive();
        }
Ejemplo n.º 5
0
        private void ReceiveProcess()
        {
            isProcess = true;
            byte[] data = EncodTool.DecodeMessage(ref dataCache);

            if (null == data)
            {
                isProcess = false;
                return;
            }

            //TODO需要再次转成一个具体的类型供我们使用
            SocketMsg value = EncodTool.DecodeMsg(data);

            //回调给上层

            receivecompleted?.Invoke(this, value);
            //尾递归
            ReceiveProcess();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 处理接收到的数据包
        /// </summary>
        private void ProcessReceive()
        {
            isReceiveProcess = true;

            //处理数据
            byte[] msgBytes = EncodeTool.DecodeMessage(ref data);

            if (msgBytes == null)  //数据包没有解析成功
            {
                isReceiveProcess = false;
                return;
            }


            MessageData msg = EncodeTool.DecodeMsg(msgBytes);

            //回调给上层
            receiveCompleted?.Invoke(this, msg);
            //递归
            ProcessReceive();
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
 //收到客户端发送的数据
 private void client_ReceiveCompleted(object sender, SocketEventArgs e)
 {
     ReceiveCompleted?.Invoke(this, e);
 }
Ejemplo n.º 9
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;
            }
        }
Ejemplo n.º 10
0
 //收到客户端发送的数据
 private void client_ReceiveCompleted(object sender, byte[] e)
 {
     ReceiveCompleted?.Invoke(this, e);
 }