/// <summary>
 ///   When a connection goes into the close wait state, call this
 /// </summary>
 /// <param name="conn"></param>
 public void ReleaseConnection(RdpConnection conn)
 {
     try {
         log_status.InfoFormat("Released connection: {0}", conn);
         Monitor.Enter(closeWaitConnections);
         closeWaitConnections.Add(conn);
     } finally {
         Monitor.Exit(closeWaitConnections);
     }
 }
 /// <summary>
 ///   When a connection goes into the closed state, call this
 /// </summary>
 /// <param name="conn"></param>
 public void CloseConnection(RdpConnection conn)
 {
     try {
         connectionsLock.BeginWrite();
         log_status.InfoFormat("Closing connection to {0}", conn.RemoteEndPoint);
         connections.Remove(conn.RemoteEndPoint);
     } finally {
         connectionsLock.EndWrite();
     }
 }
 /// <summary>
 ///   Create a new RdpConnection
 /// </summary>
 /// <param name="udpConn">the underlying UdpClient object used for sending messages</param>
 /// <param name="remoteEP">remote ip end point for this connection</param>
 /// <param name="passive">true if the connection should just be listening, or false to send a syn</param>
 /// <returns></returns>
 protected RdpConnection GetConnection(UdpClient udpConn, IPEndPoint remoteEP, bool passive)
 {
     try {
         connectionsLock.BeginWrite();
         RdpConnection conn = new RdpConnection(this, passive, udpConn, remoteEP, rcvMax, rbufMax, sequenced);
         connections[remoteEP] = conn;
         return(conn);
     } finally {
         connectionsLock.EndWrite();
     }
 }
        public override RdpConnection HandleNewConnection(IPEndPoint remoteEP)
        {
            // Construct an RdpConnection for this new "connection"
            RdpConnection conn = GetConnection(udpConn, remoteEP, true);

            try {
                Monitor.Enter(newConnections);
                newConnections.Add(conn);
                Monitor.PulseAll(newConnections);
                return(conn);
            } finally {
                Monitor.Exit(newConnections);
            }
        }
        public void OnDataReceived(IAsyncResult asyn)
        {
            // Logger.Log(0, "Begin OnDataReceived");
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            byte[] buf = null;
            try {
                buf = udpConn.EndReceive(asyn, ref remoteIpEndPoint);
            } catch (SocketException e) {
                if (e.SocketErrorCode == SocketError.Interrupted)
                {
                    // ignore
                    return;
                }
                log_status.WarnFormat("Caught socket exception: {0}", e);
                WaitForData();
                return;
            } catch (ObjectDisposedException) {
                // just ignore this one
                return;
            }
            RdpConnection conn = GetConnection(remoteIpEndPoint);

            if (conn == null)
            {
                log_status.Info("Handling new connection");
                conn = HandleNewConnection(remoteIpEndPoint);
            }
            if (conn != null)
            {
                RdpPacket packet = new RdpPacket(buf);
                // Logger.Log(0, "Got data in OnDataReceived");
                conn.OnSegmentArrival(packet, remoteIpEndPoint);
                // Logger.Log(0, "Done with OnDataReceived");
            }
            try {
                WaitForData();
            } catch (SocketException e) {
                if (e.SocketErrorCode != SocketError.Interrupted)
                {
                    throw;
                }
            } catch (ObjectDisposedException) {
                // Just ignore this
            }
        }
        /// <summary>
        ///   Connect to the remote endpoint with the specified timeout on
        ///   receiving the response that will transition the connection to
        ///   the connected state.
        /// </summary>
        /// <param name="remoteEP"></param>
        /// <param name="millisecondsTimeout">number of milliseconds to wait, or -1 to wait indefinitely</param>
        /// <returns></returns>
        public RdpConnection Connect(IPEndPoint remoteEP, int millisecondsTimeout)
        {
            RdpConnection conn    = GetConnection(udpConn, remoteEP, false);
            bool          success = false;

            log_status.InfoFormat("Created connection to: {0}", remoteEP);
            try {
                success = conn.WaitForState(ConnectionState.Open, millisecondsTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }
            } catch (Exception) {
                ReleaseConnection(conn);
                throw;
            }
            return(conn);
        }
 public RdpConnection Accept()
 {
     try {
         Monitor.Enter(newConnections);
         while (true)
         {
             if (newConnections.Count != 0)
             {
                 RdpConnection conn = newConnections[0];
                 newConnections.RemoveAt(0);
                 return(conn);
             }
             Monitor.Wait(newConnections);
         }
     } finally {
         Monitor.Exit(newConnections);
     }
 }
 /// <summary>
 ///   Create a new RdpConnection
 /// </summary>
 /// <param name="udpConn">the underlying UdpClient object used for sending messages</param>
 /// <param name="remoteEP">remote ip end point for this connection</param>
 /// <param name="passive">true if the connection should just be listening, or false to send a syn</param>
 /// <returns></returns>
 protected RdpConnection GetConnection(UdpClient udpConn, IPEndPoint remoteEP, bool passive)
 {
     try {
         connectionsLock.BeginWrite();
         RdpConnection conn = new RdpConnection(this, passive, udpConn, remoteEP, rcvMax, rbufMax, sequenced);
         connections[remoteEP] = conn;
         return conn;
     } finally {
         connectionsLock.EndWrite();
     }
 }
 /// <summary>
 ///   When a connection goes into the close wait state, call this
 /// </summary>
 /// <param name="conn"></param>
 public void ReleaseConnection(RdpConnection conn)
 {
     try {
         log_status.InfoFormat("Released connection: {0}", conn);
         Monitor.Enter(closeWaitConnections);
         closeWaitConnections.Add(conn);
     } finally {
         Monitor.Exit(closeWaitConnections);
     }
 }
 /// <summary>
 ///   When a connection goes into the closed state, call this
 /// </summary>
 /// <param name="conn"></param>
 public void CloseConnection(RdpConnection conn)
 {
     try {
         connectionsLock.BeginWrite();
         log_status.InfoFormat("Closing connection to {0}", conn.RemoteEndPoint);
         connections.Remove(conn.RemoteEndPoint);
     } finally {
         connectionsLock.EndWrite();
     }
 }
 public IncomingMessage(RdpConnection rdpConn)
 {
     IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
     byte[] buf = rdpConn.Receive(ref remoteEP);
     Init(buf, 0, buf.Length, remoteEP);
 }
 public static void connectionReset(RdpConnection con)
 {
     // do nothing
 }
        private static void Main(string[] args)
        {
            SetupDebug();
            if (args.Length != 1 && args.Length != 2) {
                Logit("usage: TestRDPServer localPort <messageCount>");
                Trace.Flush();
            }
            else {

                serverPort = int.Parse(args[0]);

                rdpServer = new RdpServer(serverPort, 100, 1000, true);
                rdpConnection = rdpServer.Accept();
                Logit("Accepted client connection; server state = " + rdpConnection.ConnectionState);
                rdpConnection.WaitForState(ConnectionState.Open);
                for (int i=0; i<1; i++) {
                    Thread incomingThread = new Thread(RunIncoming);
                    incomingThread.Start();
                    if (args.Length == 2) {
                        Thread outgoingThread = new Thread(RunOutgoing);
                        outgoingThread.Start();
                    }
                    while (incomingThread.IsAlive)
                        Thread.Sleep(500);
                    Logit("incomingThread.IsAlive = " + incomingThread.IsAlive);
                }
            }
            Trace.Flush();
        }
 private bool IsExpired(RdpConnection conn)
 {
     return((conn.CloseWaitTime != null) && (lastTick >= conn.CloseWaitTime));
 }
 private bool IsExpired(RdpConnection conn)
 {
     return (conn.CloseWaitTime != null) && (lastTick >= conn.CloseWaitTime);
 }
        private static void Main(string[] args)
        {
            SetupDebug();
            if (args.Length != 3 && args.Length != 4) {
                Logit("usage: TestRDPClient hostname remotePort localPort <run-incoming-thread>");
                Trace.Flush();
            }

            remoteHostname = args[0];
            remotePort = int.Parse(args[1]);
            localPort = int.Parse(args[2]);

            for (int i=0; i<1; i++) {
                rdpClient = new RdpClient(localPort + i, 100, 1000, true);
                IPHostEntry IPHost = Dns.GetHostEntry(remoteHostname);
                IPAddress[] addr = IPHost.AddressList;
                IPEndPoint sendpt = new IPEndPoint(addr[0], remotePort);
                Logit("Connecting to host " + remoteHostname + ", remotePort " + remotePort + ", localPort " + localPort);
                rdpConnection = rdpClient.Connect(sendpt);
                rdpConnection.WaitForState(ConnectionState.Open);
                Logit("Connection successful");
                Thread outgoingThread = new Thread(RunOutgoing);
                outgoingThread.Start();
                if (args.Length == 4) {
                    Thread incomingThread = new Thread(RunIncoming);
                    incomingThread.Start();
                }
                while (outgoingThread.IsAlive)
                    Thread.Sleep(500);
            }
            Trace.Flush();
        }
 public void Send(RdpConnection rdpConn)
 {
     writer.Flush();
     try {
         rdpConn.Send(memStream.ToArray());
     } catch (RdpFragmentationException) {
         log.Error("Failed to send packet that was too large");
     }
 }