示例#1
0
        void HandleReceivedBuffer(int ReceivedDataSize, SReceivedData ReceivedData)
        {
            if (ReceivedDataSize != ReceivedData.Buffer.Length)
            {
                ClientSock.BeginReceive(ReceivedData.Buffer, ReceivedDataSize, ReceivedData.Buffer.Length - ReceivedDataSize, SocketFlags.None, BeginReceive_Callback, ReceivedData);
            }
            else
            {
                if (ReceivedData.NextBufferSize == -1)
                {
                    ReceivedData.NextBufferSize = BitConverter.ToInt32(ReceivedData.Buffer, 0);
                    ReceivedData.Buffer         = new byte[ReceivedData.NextBufferSize];
                    ClientSock.BeginReceive(ReceivedData.Buffer, 0, ReceivedData.NextBufferSize, SocketFlags.None, BeginReceive_Callback, ReceivedData);
                }
                else
                {
                    if (OnClientReceivedData != null)
                    {
                        OnClientReceivedData.Invoke(ReceivedData.Buffer);
                    }

                    ReceivedData.NextBufferSize = -1;
                    ReceivedData.Buffer         = new byte[4];
                    ClientSock.BeginReceive(ReceivedData.Buffer, 0, ReceivedData.Buffer.Length, SocketFlags.None, BeginReceive_Callback, ReceivedData);
                }
            }
        }
示例#2
0
        protected virtual void DataReceived(IAsyncResult ar)
        {
            ClientSocket = (Socket)ar.AsyncState;

            int len = ClientSocket.EndReceive(ar, out SocketError error);

            if (IsRunning)
            {
                switch (error)
                {
                case SocketError.Success:
                    if (len > 0)
                    {
                        _rcvBuffer.Write(ReceivedBuffer, 0, len);

                        OnClientReceivedData?.Invoke(_rcvBuffer.ToArray());
                        ReceivedBuffer = new byte[BUFFER_LENGTH];

                        _rcvBuffer.Dispose();
                        _rcvBuffer = new MemoryStream();

                        Receive();
                    }
                    return;
                }
            }
            else
            {
                if (!_leaveRequest)
                {
                    OnClientDisconnected?.Invoke();
                }
            }
        }
示例#3
0
        private void _dataReceived(IAsyncResult ar)
        {
            socket = (Socket)ar.AsyncState;

            int len = socket.EndReceive(ar, out SocketError errorCode);

            if (isRunning && errorCode == SocketError.Success && len > 0)
            {
                dataRcvBuffer = new MemoryStream();
                dataRcvBuffer.Write(rcvDataBuffer, 0, len);

                OnClientReceivedData?.Invoke(dataRcvBuffer);

                BeginReceive();
                return;
            }
            Stop();
        }
示例#4
0
文件: Client.cs 项目: Karrazyne/Past
 private void ClientReceiveData(byte[] data)
 {
     OnClientReceivedData?.Invoke(data);
 }