Exemplo n.º 1
0
        //客户端断开连接
        private void client_DisconnectCompleted(object sender, SocketEventArgs e)
        {
            //移除客户端
            lock (Clients)
                Clients.Remove((TcpListenerClient)e.Socket);

            e.Socket.DisconnectCompleted -= client_DisconnectCompleted;
            e.Socket.ReceiveCompleted    -= client_ReceiveCompleted;
            e.Socket.SendCompleted       -= client_SendCompleted;
            DisconnectCompleted?.Invoke(this, e);
        }
Exemplo n.º 2
0
 public void SendCompleted(object Sender, SocketEventArgs E)
 {
     if (Status == SenderStatus.Sending)
     {
         if (SendedLength < DataLength)
         {
             Buffer = new byte[DataLength - SendedLength < BufferLength ?
                               DataLength - SendedLength : BufferLength];
             Buffer = FullData.Skip(SendedLength).Take(Buffer.Length).ToArray();
             E.Socket.SendAsync(Buffer); //从内存中读取二进制流,并发送
             SendedLength += Buffer.Length;
         }
         else
         {
             Status = SenderStatus.Ready;
         }
     }
     else if (Status == SenderStatus.Preparing)
     {
         Status = SenderStatus.Sending;
         Buffer = new byte[BufferLength];
         MemoryStream.Position = 0;  //将流的当前位置重新归0,否则Read方法将读取不到任何数据
         if (SendedLength < DataLength)
         {
             Buffer = new byte[DataLength - SendedLength < BufferLength ?
                               DataLength - SendedLength : BufferLength];
             Buffer = FullData.Skip(SendedLength).Take(Buffer.Length).ToArray();
             E.Socket.SendAsync(Buffer); //从内存中读取二进制流,并发送
             SendedLength += Buffer.Length;
         }
     }
     SendOnce?.Invoke(this, new SendOnceEventArgs()
     {
         Socket           = Socket,
         NewData          = Buffer,
         CumulativeLength = SendedLength,
         ReceivedLength   = Buffer.Length,
         TotalLength      = DataLength,
         Status           = Status
     });
 }
Exemplo n.º 3
0
        public void ReceiveCompleted(object Sender, SocketEventArgs E)
        {
            Status = ReceiverStatus.Receiving;
            int ReceiveCount = E.DataLength;

            if (ReceiveCount == 0)
            {
                Status = ReceiverStatus.Extractable;
                ReceiveOnce?.Invoke(this, new ReceiveOnceEventArgs()
                {
                    Socket           = Socket,
                    NewData          = null,
                    CumulativeLength = ReceivedLength,
                    ReceivedLength   = 0,
                    TotalLength      = DataLength,
                    Status           = Status
                });
                return;
            }
            //var Temp = Buffer.Clone() as byte[];
            //Buffer = new byte[ReceivedLength + ReceiveCount];
            //Temp.CopyTo(Buffer, 0);
            //E.Data.CopyTo(Buffer, ReceivedLength);
            Buffer = E.Data;
            Buffer.CopyTo(FullData, ReceivedLength);
            ReceivedLength += ReceiveCount;
            ReceiveOnce?.Invoke(this, new ReceiveOnceEventArgs()
            {
                Socket           = Socket,
                NewData          = Buffer,
                CumulativeLength = ReceivedLength,
                ReceivedLength   = ReceiveCount,
                TotalLength      = DataLength,
                Status           = Status
            });
            if (ReceivedLength >= DataLength)
            {
                Status = ReceiverStatus.Extractable;
            }
        }
Exemplo n.º 4
0
 //向客户端发送数据完成
 private void client_SendCompleted(object sender, SocketEventArgs e)
 {
     SendCompleted?.Invoke(this, e);
 }