private static void DisconnectCallback(IAsyncResult IAR) { StateObject state = (StateObject)IAR.AsyncState; Socket handler = state.workSocket; string key = state.key; handler.EndDisconnect(IAR); handler.Shutdown(SocketShutdown.Both); handler.Disconnect(true); handler.Close(); handler.Dispose(); D2Sockets.Remove(key); D2TReceive.Remove(key); D2TSend.Remove(key); D3BReceive.Remove(key); D3BSend.Remove(key); }
private static void AcceptCallback(IAsyncResult IAR) { Socket Server, handler; try { Server = (Socket)IAR.AsyncState; handler = Server.EndAccept(IAR); } catch (ObjectDisposedException ode) { ExceptionBuffer.Add(ode); return; } allDone.Set(); string sKey = TcpAsyncCommon.DefaultUID + ++iAcceptCounter; StateObject state = new StateObject(); state.workSocket = handler; state.key = sKey; D2Sockets.Set(sKey, state); D2TReceive.Set(sKey, null); D2TSend.Set(sKey, null); D3BReceive.Set(sKey, new DataBuffer()); D3BSend.Set(sKey, new DataBuffer()); }
private void SendThread(string sKey) { DataBuffer DBuffer = D3BSend.Get(sKey); Thread ThrdSend = D2TSend.Get(sKey); StateObject state = D2Sockets.Get(sKey); Socket handler = state.workSocket; if (!handler.Connected) { return; } if (DBuffer == null) { return; } if (!DBuffer.IsDataAvalaible) { return; } string sData = DBuffer.Get(false); //Don't increment data, becouse it is not certain at this point, if it will be send if (sData == null) { return; } else if (sData.Length > 1024) { throw new Exception("Exception in Send() method, packet size is to large."); } DBuffer.IndexerIcrement(); sData.Replace(@"\", @"\\"); sData = TcpAsyncCommon.SOM + sData + TcpAsyncCommon.EOM; byte[] baData = Encoding.ASCII.GetBytes(sData); handler.BeginSend(baData, 0, baData.Length, 0, new AsyncCallback(SendCallback), state); }
public void Stop(string key) { StateObject state = D2Sockets.Get(key); try { if (state != null && state.workSocket != null) { Socket handler = state.workSocket; handler.Shutdown(SocketShutdown.Both); handler.Disconnect(true); handler.Close(); handler.Dispose(); } } catch { } finally { if (D2Sockets != null) { D2Sockets.Remove(key); } if (D2TReceive != null) { D2TReceive.Remove(key); } if (D2TSend != null) { D2TSend.Remove(key); } if (D3BReceive != null) { D3BReceive.Remove(key); } if (D3BSend != null) { D3BSend.Remove(key); } } }