Advertisement
AyrA

EventSocket

Aug 17th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Net;
  4.  
  5. namespace Server
  6. {
  7.     public delegate void DataTransferredHandler(byte[] data);
  8.     public delegate void DisconnectedHandler(IPEndPoint Remote);
  9.  
  10.     public class EventSocket
  11.     {
  12.         /// <summary>
  13.         /// Called, when data arrives
  14.         /// </summary>
  15.         public event DataTransferredHandler DataReceived;
  16.         /// <summary>
  17.         /// Called, when data is sent
  18.         /// </summary>
  19.         public event DataTransferredHandler DataSent;
  20.         /// <summary>
  21.         /// Called when the socket is disconnected (and null reference has been set)
  22.         /// </summary>
  23.         public event DisconnectedHandler Disconnected;
  24.  
  25.         private const int BUFFER = 1024 * 1024;
  26.  
  27.         private TcpClient c;
  28.         private IPEndPoint ep;
  29.         private byte[] buffer;
  30.  
  31.         /// <summary>
  32.         /// Looks up IP addresses of a given DNS name
  33.         /// </summary>
  34.         /// <param name="FQDN">DNS name (ayra.ch)</param>
  35.         /// <param name="setNull">if true, null is returned if lookup fails, if false an empty array</param>
  36.         /// <returns>List of found IP addresses</returns>
  37.         public static IPAddress[] lookup(string FQDN,bool setNull)
  38.         {
  39.             try
  40.             {
  41.                 return Dns.GetHostAddresses(FQDN);
  42.             }
  43.             catch
  44.             {
  45.                 if (setNull)
  46.                 {
  47.                     return null;
  48.                 }
  49.                 return new IPAddress[] { };
  50.             }
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Checks if the socket is still connected
  55.         /// </summary>
  56.         public bool IsConnected
  57.         {
  58.             get
  59.             {
  60.                 if (c != null)
  61.                 {
  62.                     if (c.Client.Poll(0, SelectMode.SelectRead))
  63.                     {
  64.                         if (!c.Connected)
  65.                         {
  66.                             return false;
  67.                         }
  68.                         else
  69.                         {
  70.                             byte[] b = new byte[1];
  71.                             try
  72.                             {
  73.                                 if (c.Client.Receive(b, SocketFlags.Peek) == 0)
  74.                                 {
  75.                                     return false;
  76.                                 }
  77.                             }
  78.                             catch
  79.                             {
  80.                                 return false;
  81.                             }
  82.                         }
  83.                     }
  84.                     return true;
  85.                 }
  86.                 return false;
  87.             }
  88.         }
  89.  
  90.         public EventSocket(TcpClient cli)
  91.         {
  92.             c = cli;
  93.             ep=(IPEndPoint)c.Client.RemoteEndPoint;
  94.             linkEvents();
  95.             listen();
  96.         }
  97.  
  98.         public EventSocket(IPEndPoint iEP)
  99.         {
  100.             ep = iEP;
  101.             c = new TcpClient();
  102.             c.Connect(iEP);
  103.             linkEvents();
  104.             listen();
  105.         }
  106.  
  107.         ~EventSocket()
  108.         {
  109.             Disconnect(true);
  110.         }
  111.  
  112.         public void send(byte[] data)
  113.         {
  114.             send(data, 0, data.Length);
  115.         }
  116.  
  117.         public void send(byte[] data, int start, int length)
  118.         {
  119.             byte[] send = new byte[length];
  120.             if(length==data.Length)
  121.             {
  122.                 send = (byte[])data.Clone();
  123.             }
  124.             else
  125.             {
  126.                 Array.Copy(data, start, send, 0, length);
  127.             }
  128.  
  129.             try
  130.             {
  131.                 c.Client.Send(send);
  132.                 DataSent(send);
  133.             }
  134.             catch
  135.             {
  136.                 Disconnect(true);
  137.             }
  138.         }
  139.  
  140.         /// <summary>
  141.         /// Disconnects the Client
  142.         /// </summary>
  143.         /// <param name="gracefully">true, to call Disconnect, false to just close the connection and dispose it.</param>
  144.         public void Disconnect(bool gracefully)
  145.         {
  146.             if (gracefully)
  147.             {
  148.                 try
  149.                 {
  150.                     c.Client.Disconnect(false);
  151.                 }
  152.                 catch
  153.                 {
  154.                     //NA
  155.                 }
  156.             }
  157.             if (c != null)
  158.             {
  159.                 c.Close();
  160.             }
  161.             c = null;
  162.             eDisconnected(ep);
  163.         }
  164.  
  165.         private void listen()
  166.         {
  167.             buffer = new byte[BUFFER];
  168.             try
  169.             {
  170.                 c.Client.BeginReceive(buffer, 0, BUFFER, SocketFlags.None, new AsyncCallback(DataRec), null);
  171.             }
  172.             catch
  173.             {
  174.                 Disconnect(true);
  175.             }
  176.         }
  177.  
  178.         private void DataRec(IAsyncResult ar)
  179.         {
  180.             int readed = 0;
  181.             try
  182.             {
  183.                 readed = c.Client.EndReceive(ar);
  184.             }
  185.             catch
  186.             {
  187.                 Disconnect(true);
  188.                 readed = -1;
  189.             }
  190.             if (readed > 0)
  191.             {
  192.                 //Trim to the amount of data and fire event
  193.                 byte[] data = new byte[readed];
  194.                 Array.Copy(buffer, data, readed);
  195.                 DataReceived(data);
  196.                 listen();
  197.             }
  198.             else
  199.             {
  200.                 Disconnect(true);
  201.             }
  202.         }
  203.  
  204.         private void linkEvents()
  205.         {
  206.             DataReceived += new DataTransferredHandler(eDataTransfer);
  207.             DataSent += new DataTransferredHandler(eDataTransfer);
  208.             Disconnected += new DisconnectedHandler(eDisconnected);
  209.         }
  210.  
  211.         private void eDisconnected(IPEndPoint Remote)
  212.         {
  213.             Console.WriteLine("EVT: Disconnected");
  214.             //NOOP EVENT
  215.         }
  216.  
  217.         private void eDataTransfer(byte[] data)
  218.         {
  219.             Console.WriteLine("EVT: DATA");
  220.             //NOOP EVENT
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement