/// <summary> /// Fire the Connected event if it exists. /// </summary> /// <param name="e"></param> protected virtual void OnConnected(TcpServerEventArgs e) { if (Connected != null) { try { Connected(this, e); } catch (Exception ex) { // Close the connection if the application threw an exception that // is caught here by the server. e.ConnectionState.Close(); TcpLibApplicationExceptionEventArgs appErr = new TcpLibApplicationExceptionEventArgs(ex); try { OnHandleApplicationException(appErr); } catch (Exception ex2) { // Oh great, the exception handler threw an exception! System.Diagnostics.Trace.WriteLine(ex2.Message); } } } }
/// <summary> /// When a connection is made, get a network stream and start the worker thread. /// The worker thread uses the managed thread pool, which is safe for handling /// work that takes a lot of time. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnConnected(object sender, TcpServerEventArgs e) { ConnectionState cs = e.ConnectionState; NetworkStream ns = new NetworkStream(cs.Connection, true); ns.ReadTimeout = readTimeout; ns.WriteTimeout = writeTimeout; NetworkStreamConnection conn = new NetworkStreamConnection(ns, cs); ManagedThreadPool.QueueUserWorkItem(ConnectionProcess, conn); }