protected virtual void m_AcceptArgs_Completed(object sender, SocketAsyncEventArgs e) { Socket listenSocket = (Socket)sender; do { try { if (e.SocketError != SocketError.Success) { LogUtility.LogUtility.LogFile("Error in Accept " + Convert.ToString(e.SocketError), LogUtility.LogLevels.LEVEL_LOG_HIGH); continue; } Proxy serverSideProxy = null; Socket newClientSocket = e.AcceptSocket; LogUtility.LogUtility.LogFile("Accepted " + Convert.ToString(e.BytesTransferred) + " " + Convert.ToString(newClientSocket != null), LogUtility.LogLevels.LEVEL_LOG_HIGH); switch (m_ProxyType) { case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_RAW: serverSideProxy = new RawServerSideProxy(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_HTTP: serverSideProxy = new HttpServerSideProxy(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_PACK_HTTP: serverSideProxy = new PackHttpServerSide(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_PACK_RAW: serverSideProxy = new PackRawServerSide(newClientSocket); break; } if (serverSideProxy != null) { Proxy.OnGotResults cbk = new Proxy.OnGotResults(OnGotResults); serverSideProxy.SetOnGotResults(cbk); Proxy.OnDisposed ondisp = new Proxy.OnDisposed(OnDisposed); serverSideProxy.SetOnDisposed(ondisp); /*Monitor.Enter(listLock); * clientsList.Add(serverSideProxy); * Monitor.Exit(listLock);*/ serverSideProxy.SetRemoteEndpoint(m_remoteEndpoint); serverSideProxy.Start(); } } catch (Exception exc) { LogUtility.LogUtility.LogFile("EXCEPTION: " + exc.Message + " " + exc.StackTrace, LogUtility.LogLevels.LEVEL_LOG_HIGH); } finally { e.AcceptSocket = null; // to enable reuse } } while (!listenSocket.AcceptAsync(e)); }
public virtual void OnAccept(IAsyncResult ar) { try { Socket newClientSocket = m_socket.EndAccept(ar); Proxy serverSideProxy = null; switch (m_ProxyType) { case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_RAW: serverSideProxy = new RawServerSideProxy(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_HTTP: serverSideProxy = new HttpServerSideProxy(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_PACK_HTTP: serverSideProxy = new PackHttpServerSide(newClientSocket); break; case (byte)ProxyLibTypes.ServerSideProxyTypes.SERVER_SIDE_PROXY_PACK_RAW: serverSideProxy = new PackRawServerSide(newClientSocket); break; } if (serverSideProxy != null) { Proxy.OnGotResults cbk = new Proxy.OnGotResults(OnGotResults); serverSideProxy.SetOnGotResults(cbk); Proxy.OnDisposed ondisp = new Proxy.OnDisposed(OnDisposed); serverSideProxy.SetOnDisposed(ondisp); /*Monitor.Enter(listLock); * clientsList.Add(serverSideProxy); * Monitor.Exit(listLock);*/ serverSideProxy.SetRemoteEndpoint(m_remoteEndpoint); serverSideProxy.Start(); } IAsyncResult ar2 = m_socket.BeginAccept(m_OnAccept, null); if (ar2.CompletedSynchronously) { LogUtility.LogUtility.LogFile(" completed synchronously ", LogUtility.LogLevels.LEVEL_LOG_HIGH); OnAccept(ar2); } } catch (Exception exc) { LogUtility.LogUtility.LogFile("EXCEPTION: " + exc.Message + " " + exc.StackTrace, LogUtility.LogLevels.LEVEL_LOG_HIGH); } }