Advertisement
Guest User

AsyncTcpClient.cs

a guest
Feb 21st, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8.  
  9. namespace Phurious.Android.Blazr
  10. {
  11.     /// <summary>
  12.     /// Internal class to join the TCP client and buffer together
  13.     /// for easy management in the server
  14.     /// </summary>
  15.     public class AsyncTcpClient
  16.     {
  17.         public delegate void DataReceivedHandler(AsyncTcpClient client);
  18.         public event DataReceivedHandler DataReceived;
  19.  
  20.         /// <summary>
  21.         /// The encoding to use when sending / receiving strings.
  22.         /// </summary>
  23.         public Encoding Encoding { get; set; }
  24.  
  25.         private TcpClient _tcpClient = null;
  26.         private Byte[] _buffer = null;
  27.  
  28.         /// <summary>
  29.         /// Constructor for a new Client
  30.         /// </summary>
  31.         /// <param name="tcpClient">The TCP client</param>
  32.         /// <param name="buffer">The byte array buffer</param>
  33.         public AsyncTcpClient(TcpClient tcpClient, Byte[] buffer)
  34.             : this()
  35.         {
  36.             if (tcpClient == null) throw new ArgumentNullException("tcpClient is null");
  37.  
  38.             if (buffer == null) throw new ArgumentNullException("Buffer is null");
  39.  
  40.             this.TcpClient = tcpClient;
  41.  
  42.             this.Buffer = buffer;
  43.         }
  44.  
  45.         private AsyncTcpClient()
  46.         {
  47.             this.Encoding = Encoding.ASCII;
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Gets the TCP Client
  52.         /// </summary>
  53.         public TcpClient TcpClient
  54.         {
  55.             get
  56.             {
  57.                 return _tcpClient;
  58.             }
  59.             set
  60.             {
  61.                 _tcpClient = value;
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Gets the Buffer.
  67.         /// </summary>
  68.         public Byte[] Buffer
  69.         {
  70.             get
  71.             {
  72.                 return _buffer;
  73.             }
  74.             set
  75.             {
  76.                 _buffer = value;
  77.                 if (this.DataReceived != null) this.DataReceived(this);
  78.             }
  79.         }
  80.  
  81.  
  82.         /// <summary>
  83.         /// Gets the network stream
  84.         /// </summary>
  85.         public NetworkStream NetworkStream
  86.         {
  87.             get
  88.             {
  89.                 return TcpClient.GetStream();
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement