Exemplo n.º 1
0
        /// <summary>
        /// Sets up a new <see cref="RoutedSession"/> and immediately begins listening
        /// for data to propagate to both sides of a session. This method should be
        /// invoked after a client has connected to a route listener and the connection
        /// socket has been accepted by the listener. A new socket will be created by this
        /// method to the routed target host.
        /// </summary>
        /// <param name="route"></param>
        /// <param name="clientSocket"></param>
        /// <returns></returns>
        public static RoutedSession BeginSession(Route route, Socket clientSocket)
        {
            var newSession = new RoutedSession {
                ClientSocket = clientSocket
            };

            newSession.Route = route;
            var       extnSocket = new TcpClient();
            IPAddress ipaddr     = null;

            if (!IPAddress.TryParse(route.TargetHost, out ipaddr))
            {
                ipaddr = Dns.GetHostAddresses(route.TargetHost).First();
            }
            extnSocket.Connect(new System.Net.IPEndPoint(ipaddr, route.TargetPort));
            newSession.ClientSocket    = clientSocket;
            newSession.ExtensionSocket = extnSocket.Client;
            newSession.ClientEndPoint  = (IPEndPoint)newSession.ClientSocket.RemoteEndPoint;
            newSession.Monitor();
            return(newSession);
        }
Exemplo n.º 2
0
        private void TcpListener_AcceptSocket(IAsyncResult ar)
        {
            // Get the listener that handles the client request.
            var listenerkvp = (KeyValuePair <string, TcpListener>)ar.AsyncState;
            var name        = listenerkvp.Key;
            var listener    = listenerkvp.Value;
            var route       = Routes[listenerkvp.Key];

            try
            {
                // Start the session
                var session = RoutedSession.BeginSession(route, listener.EndAcceptSocket(ar));
                session.Closed += new EventHandler(Session_Closed);
                ActiveSessions.Add(session);
            }
            catch { } // skip the session, it failed

            // continue listening
            var stateObj2 = new KeyValuePair <string, TcpListener>(route.Name, listener);

            listener.BeginAcceptSocket(new AsyncCallback(TcpListener_AcceptSocket), stateObj2);
        }