Esempio n. 1
0
        /// <summary>
        /// Create a multitransport, RDP_UDP_reliable or RDP_UDP_Lossy
        /// </summary>
        /// <param name="transportType">Type of the transport, reliable or lossy</param>
        public void CreateMultipleTransport(DynamicVC_TransportType transportType)
        {
            if (transportDic.ContainsKey(transportType))
            {
                throw new InvalidOperationException("The multiple transport have already been created:" + transportType);
            }

            Rdpemt_DVCClientTransport transport = new Rdpemt_DVCClientTransport(clientSessionContext, transportType);

            transport.UnhandledExceptionReceived += (ex) =>
            {
                UnhandledExceptionReceived?.Invoke(ex);
            };

            if (transportType == DynamicVC_TransportType.RDP_UDP_Reliable)
            {
                transport.Received += ProcessPacketFromUDPR;
            }
            else
            {
                transport.Received += ProcessPacketFromUDPL;
            }

            transport.EstablishTransportConnection();

            transportDic.Add(transportType, transport);
        }
        /// <summary>
        /// Receive Loop
        /// </summary>
        private void ReceiveLoop()
        {
            try
            {
                TimeSpan    timeout;
                object      remoteEndpoint;
                StackPacket receivedPacket;
                timeout = new TimeSpan(0, 0, 0, 0, 100); // 100 milliseconds.

                // Check whether cancellation is requested before entering each receive loop.
                while (!receiveThreadCancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        receivedPacket = udpTransport.ExpectPacket(timeout, out remoteEndpoint);
                        socket.ReceivePacket(receivedPacket);
                    }
                    catch (TimeoutException)
                    { }
                }
            }
            catch (Exception ex)
            {
                UnhandledExceptionReceived?.Invoke(ex);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Receive Loop
        /// </summary>
        private void ReceiveLoop()
        {
            try
            {
                TimeSpan    timeout;
                object      remoteEndpoint;
                StackPacket receivedPacket;
                timeout = new TimeSpan(0, 0, 0, 0, 100); // 100 milliseconds.

                // Check whether cancellation is requested before entering each receive loop.
                while (!receiveThreadCancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        receivedPacket = udpTransport.ExpectPacket(timeout, localEndPoint, out remoteEndpoint);

                        if (serverSocketDic.ContainsKey(remoteEndpoint as IPEndPoint))
                        {
                            serverSocketDic[remoteEndpoint as IPEndPoint].ReceivePacket(receivedPacket);
                        }
                        else // If the packet belong to no RDPEUDP socket, try to Accept as a new RDPEUDP socket.
                        {
                            StackPacketInfo packetinfo = new StackPacketInfo(localEndPoint, remoteEndpoint, receivedPacket);
                            lock (this.unprocessedPacketBuffer)
                            {
                                unprocessedPacketBuffer.Add(packetinfo);
                            }

                            // ETW Provider Dump Message
                            // byte[] packetBytes = receivedPacket.ToBytes();
                            // string messageName = "RDPEUDP:ReceivedPDU";
                            // ExtendedLogger.DumpMessage(messageName, RdpeudpSocket.DumpLevel_LayerTLS, typeof(RdpeudpPacket).Name, packetBytes);
                        }
                    }
                    catch (TimeoutException)
                    { }
                }
            }
            catch (Exception ex)
            {
                UnhandledExceptionReceived?.Invoke(ex);
            }
        }
        /// <summary>
        /// Receive Loop
        /// </summary>
        private void ReceiveLoop()
        {
            try
            {
                TimeSpan    timeout;
                object      remoteEndpoint;
                StackPacket receivedPacket;
                timeout = new TimeSpan(0, 0, 10); // 100 milliseconds.
                while (running)
                {                                 // An infinite loop to receive packet from transport stack.
                    try
                    {
                        receivedPacket = udpTransport.ExpectPacket(timeout, localEndPoint, out remoteEndpoint);
                        if (serverSocketDic.ContainsKey(remoteEndpoint as IPEndPoint))
                        {
                            serverSocketDic[remoteEndpoint as IPEndPoint].ReceivePacket(receivedPacket);
                        }
                        else                // If the packet belong to no RDPEUDP socket, try to Accept as a new RDPEUDP socket.
                        {
                            StackPacketInfo packetinfo = new StackPacketInfo(localEndPoint, remoteEndpoint, receivedPacket);
                            lock (this.unprocessedPacketBuffer)
                            {
                                unprocessedPacketBuffer.Add(packetinfo);
                            }

                            // ETW Provider Dump Message
                            byte[] packetBytes = receivedPacket.ToBytes();
                            string messageName = "RDPEUDP:ReceivedPDU";
                            // ExtendedLogger.DumpMessage(messageName, RdpeudpSocket.DumpLevel_LayerTLS, typeof(RdpeudpPacket).Name, packetBytes);
                        }
                    }
                    catch (TimeoutException)
                    { }
                    Thread.Sleep(RdpeudpSocketConfig.ReceivingInterval);
                }
            }
            catch (Exception ex)
            {
                UnhandledExceptionReceived?.Invoke(ex);
            }
        }
        /// <summary>
        /// Establish a MultiTransport Connection
        /// </summary>
        public void EstablishTransportConnection()
        {
            TransportMode udpTransportMode = TransportMode.Reliable;
            uint          requestId        = clientSessionContext.RequestIdReliable;

            byte[] cookie = clientSessionContext.CookieReliable;
            int    port   = portR;

            if (transportProtocol == Multitransport_Protocol_value.INITITATE_REQUEST_PROTOCOL_UDPFECL)
            {
                udpTransportMode = TransportMode.Lossy;
                requestId        = clientSessionContext.RequestIdLossy;
                cookie           = clientSessionContext.CookieLossy;
                port             = portL;
            }

            if (cookie == null)
            {
                // Not receive a Server Initiate Multitransport Request PDU
                throw new InvalidOperationException("Cannot establish the connection, since the corresponding Server Initiate Multitransport Request PDU wasn't received!");
            }

            IPEndPoint localEndpoint = new IPEndPoint(((IPEndPoint)clientSessionContext.LocalIdentity).Address, port);

            rdpeudpClient = new RdpeudpClient(localEndpoint, (IPEndPoint)clientSessionContext.RemoteIdentity, udpTransportMode);

            rdpeudpClient.UnhandledExceptionReceived += (ex) =>
            {
                UnhandledExceptionReceived?.Invoke(ex);
            };

            rdpeudpClient.Start();

            rdpeudpClient.Connect(timeout);

            rdpemtClient           = new RdpemtClient(rdpeudpClient.Socket, ((IPEndPoint)clientSessionContext.RemoteIdentity).Address.ToString(), false);
            rdpemtClient.Received += ReceivedBytes;

            rdpemtClient.Connect(requestId, cookie, timeout);
        }