//AsyncHandlid code //private : /// <summary> /// Fire Socket Events /// called by the Class when events happend /// </summary> /// <param name="ar">the Asyc result </param> void OnSendEvents(IAsyncResult ar) { string s = ar.AsyncState.ToString(); switch (s) { case "connect": connect(this, null); if (ar.IsCompleted) { m_Socket.EndConnect(ar); } break; case "send": if (ar.IsCompleted) { m_Socket.EndSend(ar); } send(this, null); break; default: RecieveEventArgs e = (RecieveEventArgs)ar.AsyncState; e.m_ar = ar; recieve(this, e); break; } }
/// <summary> /// connect to a spacific Host and port /// </summary> /// <param name="Host">the host to connect can be either a doted IP or a host name </param> /// <param name="port">port to connect to</param> /// <param name="Mode">one of the ConnectionMode enum, use SyncConnetionAsycRecv as recommanded option</param> public void Connect(string Host, int port, ConnectionMode Mode) { create(this, null); IPHostEntry he = Dns.Resolve(Host); IPEndPoint ep = new IPEndPoint(he.AddressList[0], port); RecieveEventArgs rv = new RecieveEventArgs(); switch (Mode) { case ConnectionMode.AsyncConnectAsyncRecv: m_Socket.BeginConnect(ep, new AsyncCallback(OnSendEvents), "connect"); rv.m_DataLength = 255; m_Socket.BeginReceive(rv.m_bytesArray, 0, 255, SocketFlags.None, new AsyncCallback(OnSendEvents), rv); return; case ConnectionMode.AsyncConnectSyncRecv: m_Socket.BeginConnect(ep, new AsyncCallback(OnSendEvents), "connect"); return; case ConnectionMode.SyncConnectAsyncRecv: m_Socket.Connect(ep); rv.m_DataLength = 255; m_Socket.BeginReceive(rv.m_bytesArray, 0, 255, SocketFlags.None, new AsyncCallback(OnSendEvents), rv); return; case ConnectionMode.SyncConnectSyncRecv: m_Socket.Connect(ep); return; default: return; } }
private void m_Socket_recieve(object Sender, RecieveEventArgs e) { byte[] b = e.Data; int j = e.Data.Length - 1; for (int i = 0; i != e.Length; i++) { b[j] = 0; j--; } Parse(b); }
protected void OnRecieve(object Sender, RecieveEventArgs e) { try { e.m_DataLength = m_Socket.EndReceive(e.AsyncResult); RecieveEventArgs ev = new RecieveEventArgs(); m_Socket.BeginReceive(ev.m_bytesArray, 0, 255, SocketFlags.None, new AsyncCallback(OnSendEvents), ev); } catch (SocketException er) { CloseEventArgs c = new CloseEventArgs(); c.create(e.AsyncResult, er, "Connection closed from the Server"); close(this, c); } }
//operations // public : /// <summary> /// connect to a spacific Host and port /// </summary> /// <param name="Host">the host to connect can be either a doted IP or a host name </param> /// <param name="port">port to connect to</param> public void Connect(string Host, int port) { create(this, null); IPHostEntry he = Dns.Resolve(Host); IPEndPoint ep = new IPEndPoint(he.AddressList[0], port); if (m_BlockingMode) { m_Socket.Connect(ep); return; } m_Socket.BeginConnect(ep, new AsyncCallback(OnSendEvents), "connect"); RecieveEventArgs rv = new RecieveEventArgs(); rv.m_DataLength = 255; m_Socket.BeginReceive(rv.m_bytesArray, 0, 255, SocketFlags.None, new AsyncCallback(OnSendEvents), rv); }
protected void OnRecieve(object Sender, RecieveEventArgs e) { try { e.m_DataLength = m_Socket.EndReceive(e.AsyncResult); RecieveEventArgs ev = new RecieveEventArgs(); m_Socket.BeginReceive(ev.m_bytesArray, 0, Sockets.BufferSize.Size, SocketFlags.None, new AsyncCallback(OnSendEvents), ev); } catch (SocketException er) { CloseEventArgs c = new CloseEventArgs(); c.create(e.AsyncResult, er, "Connection closed from the Server"); close(this, c); } }
/// <summary> /// connect to a spacific Host and port /// </summary> /// <param name="Host">the host to connect can be either a doted IP or a host name </param> /// <param name="port">port to connect to</param> /// <param name="Mode">one of the ConnectionMode enum, use SyncConnetionAsycRecv as recommanded option</param> public void Connect(string Host, int port, ConnectionMode Mode) { create(this, null); IPHostEntry he = Dns.GetHostEntry(Host); IPEndPoint ep = new IPEndPoint(he.AddressList[0], port); RecieveEventArgs rv = new RecieveEventArgs(); switch (Mode) { case ConnectionMode.AsyncConnectAsyncRecv: m_Socket.BeginConnect(ep, new AsyncCallback(OnSendEvents), "connect"); rv.m_DataLength = Sockets.BufferSize.Size; m_Socket.ReceiveBufferSize = Sockets.BufferSize.Size; m_Socket.BeginReceive(rv.m_bytesArray, 0, Sockets.BufferSize.Size, SocketFlags.None, new AsyncCallback(OnSendEvents), rv); return; case ConnectionMode.AsyncConnectSyncRecv: m_Socket.BeginConnect(ep, new AsyncCallback(OnSendEvents), "connect"); return; case ConnectionMode.SyncConnectAsyncRecv: m_Socket.Connect(ep); rv.m_DataLength = Sockets.BufferSize.Size; m_Socket.ReceiveBufferSize = Sockets.BufferSize.Size; m_Socket.BeginReceive(rv.m_bytesArray, 0, Sockets.BufferSize.Size, SocketFlags.None, new AsyncCallback(OnSendEvents), rv); return; case ConnectionMode.SyncConnectSyncRecv: m_Socket.Connect(ep); return; default: return; } }