示例#1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="connection">The network stream connection instance.</param>
 /// <param name="key">The crypto key.</param>
 /// <param name="iv">The crypto initial vector.</param>
 public RCSConnectionService(NetworkStreamConnection connection, byte[] key, byte[] iv)
 {
     this.key        = key;
     this.iv         = iv;
     this.connection = connection;
     networkStream   = connection.NetworkStream;
 }
        /// <summary>
        /// Inform the application that we are connected.  This event fires in a worker thread.
        /// </summary>
        /// <param name="state"></param>
        protected void ConnectionProcess(object state)
        {
            NetworkStreamConnection conn = (NetworkStreamConnection)state;

            try
            {
                OnConnected(new TcpServiceEventArgs(conn));
            }
            catch (Exception e)
            {
                // Report exception not caught by the application.
                try
                {
                    OnHandleApplicationException(new TcpLibApplicationExceptionEventArgs(e));
                }
                catch (Exception ex2)
                {
                    // Oh great the app's handler threw an exception!
                    System.Diagnostics.Trace.WriteLine(ex2.Message);
                }
                finally
                {
                    // In any case, close the connection.
                    conn.Close();
                }
            }
        }
        /// <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);
        }
示例#4
0
 public TcpServiceEventArgs(NetworkStreamConnection c)
 {
     connection = c;
 }