Пример #1
0
        /// <summary>
        ///     Resolve <see cref="ProcessingContext.ServerEndPoint" /> based on <see cref="ProcessingContext.RequestHeader" />,
        ///     establish connection to destination server and open <see cref="ProcessingContext.ServerStream" />.
        ///     <see cref="ProcessingContext.RequestHeader" /> should be defined.
        /// </summary>
        /// <param name="context">current request context</param>
        protected virtual void ConnectToServer(ProcessingContext context)
        {
            Contract.Requires <ArgumentNullException>(context != null, "context");
            Contract.Requires <InvalidContextException>(context.RequestHeader != null, "RequestHeader");

            context.ServerEndPoint = DnsUtils.ResolveRequestEndpoint(context.RequestHeader, _defaultPort);

            context.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveTimeout = (Int32)ServerReadTimeout.TotalMilliseconds,
                SendTimeout    = (Int32)ServerWriteTimeout.TotalMilliseconds
            };

            context.ServerSocket.Connect(context.ServerEndPoint.Host, context.ServerEndPoint.Port);

            context.ServerStream = new NetworkStream(context.ServerSocket, true);

            if (Logger.IsDebugEnabled)
            {
                Logger.DebugFormat("Connection Established: {0}:{1}",
                                   context.ServerEndPoint.Host,
                                   context.ServerEndPoint.Port
                                   );
            }
        }
Пример #2
0
        /// <summary>
        ///     Resolve <see cref="ProcessingContext.ServerEndPoint" /> based on <see cref="ProcessingContext.RequestHeader" />,
        ///     establish connection to destination server and open <see cref="ProcessingContext.ServerStream" />.
        ///     <see cref="ProcessingContext.RequestHeader" /> should be defined.
        /// </summary>
        /// <param name="context">current request context</param>
        protected virtual void ConnectToServer(ProcessingContext context)
        {
            ContractUtils.Requires <ArgumentNullException>(context != null, "context");
            ContractUtils.Requires <InvalidContextException>(context.RequestHeader != null, "RequestHeader");

            context.ServerEndPoint = DnsUtils.ResolveRequestEndpoint(context.RequestHeader, _defaultPort);

            bool requiresNewSocket = true;

            if (EnableKeepAlive && activeSockets.ContainsKey(context.ServerEndPoint))
            {
                context.ServerSocket = activeSockets[context.ServerEndPoint];
                if (context.ServerSocket.IsConnected())
                {
                    requiresNewSocket = false;
                }
                else
                {
                    context.ServerSocket.Close();
                    activeSockets.Remove(context.ServerEndPoint);
                    OnLog(LogLevel.Debug, "Server socket appears disconnected. Requiring new socket.");
                }
            }

            if (requiresNewSocket)
            {
                context.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                {
                    ReceiveTimeout = (int)ServerReadTimeout.TotalMilliseconds,
                    SendTimeout    = (int)ServerWriteTimeout.TotalMilliseconds
                };

                context.ServerSocket.Connect(context.ServerEndPoint.Host, context.ServerEndPoint.Port);
                if (EnableKeepAlive)
                {
                    activeSockets[context.ServerEndPoint] = context.ServerSocket;
                }
            }

            context.ServerStream = new NetworkStream(context.ServerSocket, !EnableKeepAlive);

            OnLog(LogLevel.Debug,
                  "Connection Established: {0}:{1}",
                  context.ServerEndPoint.Host,
                  context.ServerEndPoint.Port
                  );
        }