private void ProcessConnect(SocketAsyncEventArgs e) { AsyncToken token = (AsyncToken)e.UserToken; if (!token.Socket.ConnectAsync(e)) { NetworkOnConnect(null, e); } }
private void ProcessAccept(SocketAsyncEventArgs e) { AsyncToken token = (AsyncToken)e.UserToken; e.AcceptSocket = null; if (!token.Socket.AcceptAsync(e)) { ThreadPool.QueueUserWorkItem(DispatchAccept, e); } }
private void NetworkOnAccept(object sender, SocketAsyncEventArgs e) { AsyncToken token = (AsyncToken)e.UserToken; Socket socket = e.AcceptSocket; ProcessAccept(e); // Start the next accept asap. if (socket == null) { return; // Ignore errors because there is nothing to do } AsyncState state = new AsyncState(this, socket, AsyncOperation.Accept, token.Interface, token.User); // Now handle the current connection. bool result = false; try { result = state.Context.Interface.OnConnect(state.Context); } catch (Exception) { } if (!result) { try { state.Context.Interface.OnError(state.Context, token.User); // Ensure the user can cleanup anything before the object dies } catch (Exception) { } state.Cleanup(); // Cleanup the socket return; } try { state.Read(); // Begin receiving data on the socket } catch (Exception) { state.Cleanup(); // Cleanup the object return; } AddState(state); // Store the state to keep it alive }
private void NetworkOnConnect(object sender, SocketAsyncEventArgs e) { AsyncToken token = (AsyncToken)e.UserToken; Socket socket = e.ConnectSocket; if (socket == null) // The connection failed { token.Interface.OnError(null, token.User); // There is no state object yet, so pass the user object return; } AsyncState state = new AsyncState(this, socket, AsyncOperation.Connect, token.Interface, token.User); // Now handle the current connection. bool result = false; try { result = state.Context.Interface.OnConnect(state.Context); } catch (Exception) { } if (!result) { try { state.Context.Interface.OnError(state.Context, token.User); // Ensure the user can cleanup anything before the object dies } catch (Exception) { } state.Cleanup(); // Cleanup the object return; } try { state.Read(); // Begin receiving data on the socket } catch (Exception) { state.Cleanup(); // Cleanup the object return; } AddState(state); // Store the state to keep it alive }