Exemplo n.º 1
0
 private async Task ReceiveFromAsync(NetConnection client)
 {
     try
     {
         NetworkStream stream = client.client.GetStream();
         byte[] buffer = new byte[BufferSize];
         MemoryStream ms = new MemoryStream();
         while (client.client.Connected)
         {
             int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
             ms.Write(buffer, 0, bytesRead);
             if (!stream.DataAvailable)
             {
                 CallOnDataReceived(client, ms.ToArray());
                 ms.Seek(0, SeekOrigin.Begin);
                 ms.SetLength(0);
             }
         }
         CallOnDisconnect(client);
     }
     catch
     {
         CallOnDisconnect(client);
         throw;
     }
 }
Exemplo n.º 2
0
 private void StartReceiveFrom(NetConnection client)
 {
     tasks.Add(ReceiveFromAsync(client));
 }
Exemplo n.º 3
0
 private void CallOnDisconnect(NetConnection client)
 {
     if (OnDisconnect != null)
         OnDisconnect(this, client);
 }
Exemplo n.º 4
0
 private async Task ListenAsync()
 {
     while (true)
     {
         TcpClient client = await listener.AcceptTcpClientAsync();
         NetConnection connection = new NetConnection(client);
         StartReceiveFrom(connection);
         OnConnect(this, connection);
     }
 }
Exemplo n.º 5
0
 private void CallOnDataReceived(NetConnection connection, byte[] data)
 {
     if (OnDataReceived != null)
         OnDataReceived(this, connection, data);
 }