Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Sockets;
- using System.Net;
- namespace Server
- {
- public delegate void DataTransferredHandler(byte[] data);
- public delegate void DisconnectedHandler(IPEndPoint Remote);
- public class EventSocket
- {
- /// <summary>
- /// Called, when data arrives
- /// </summary>
- public event DataTransferredHandler DataReceived;
- /// <summary>
- /// Called, when data is sent
- /// </summary>
- public event DataTransferredHandler DataSent;
- /// <summary>
- /// Called when the socket is disconnected (and null reference has been set)
- /// </summary>
- public event DisconnectedHandler Disconnected;
- private const int BUFFER = 1024 * 1024;
- private TcpClient c;
- private IPEndPoint ep;
- private byte[] buffer;
- /// <summary>
- /// Looks up IP addresses of a given DNS name
- /// </summary>
- /// <param name="FQDN">DNS name (ayra.ch)</param>
- /// <param name="setNull">if true, null is returned if lookup fails, if false an empty array</param>
- /// <returns>List of found IP addresses</returns>
- public static IPAddress[] lookup(string FQDN,bool setNull)
- {
- try
- {
- return Dns.GetHostAddresses(FQDN);
- }
- catch
- {
- if (setNull)
- {
- return null;
- }
- return new IPAddress[] { };
- }
- }
- /// <summary>
- /// Checks if the socket is still connected
- /// </summary>
- public bool IsConnected
- {
- get
- {
- if (c != null)
- {
- if (c.Client.Poll(0, SelectMode.SelectRead))
- {
- if (!c.Connected)
- {
- return false;
- }
- else
- {
- byte[] b = new byte[1];
- try
- {
- if (c.Client.Receive(b, SocketFlags.Peek) == 0)
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- }
- public EventSocket(TcpClient cli)
- {
- c = cli;
- ep=(IPEndPoint)c.Client.RemoteEndPoint;
- linkEvents();
- listen();
- }
- public EventSocket(IPEndPoint iEP)
- {
- ep = iEP;
- c = new TcpClient();
- c.Connect(iEP);
- linkEvents();
- listen();
- }
- ~EventSocket()
- {
- Disconnect(true);
- }
- public void send(byte[] data)
- {
- send(data, 0, data.Length);
- }
- public void send(byte[] data, int start, int length)
- {
- byte[] send = new byte[length];
- if(length==data.Length)
- {
- send = (byte[])data.Clone();
- }
- else
- {
- Array.Copy(data, start, send, 0, length);
- }
- try
- {
- c.Client.Send(send);
- DataSent(send);
- }
- catch
- {
- Disconnect(true);
- }
- }
- /// <summary>
- /// Disconnects the Client
- /// </summary>
- /// <param name="gracefully">true, to call Disconnect, false to just close the connection and dispose it.</param>
- public void Disconnect(bool gracefully)
- {
- if (gracefully)
- {
- try
- {
- c.Client.Disconnect(false);
- }
- catch
- {
- //NA
- }
- }
- if (c != null)
- {
- c.Close();
- }
- c = null;
- eDisconnected(ep);
- }
- private void listen()
- {
- buffer = new byte[BUFFER];
- try
- {
- c.Client.BeginReceive(buffer, 0, BUFFER, SocketFlags.None, new AsyncCallback(DataRec), null);
- }
- catch
- {
- Disconnect(true);
- }
- }
- private void DataRec(IAsyncResult ar)
- {
- int readed = 0;
- try
- {
- readed = c.Client.EndReceive(ar);
- }
- catch
- {
- Disconnect(true);
- readed = -1;
- }
- if (readed > 0)
- {
- //Trim to the amount of data and fire event
- byte[] data = new byte[readed];
- Array.Copy(buffer, data, readed);
- DataReceived(data);
- listen();
- }
- else
- {
- Disconnect(true);
- }
- }
- private void linkEvents()
- {
- DataReceived += new DataTransferredHandler(eDataTransfer);
- DataSent += new DataTransferredHandler(eDataTransfer);
- Disconnected += new DisconnectedHandler(eDisconnected);
- }
- private void eDisconnected(IPEndPoint Remote)
- {
- Console.WriteLine("EVT: Disconnected");
- //NOOP EVENT
- }
- private void eDataTransfer(byte[] data)
- {
- Console.WriteLine("EVT: DATA");
- //NOOP EVENT
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement