public NettyRequestListener(HttpProvider_HttpRequestListener listener, NettyFullAddress target, IChannel ch, HttpPoolManager channelPool)
 {
     this.wrapped     = listener;
     this.target      = target;
     this.ch          = ch;
     this.channelPool = channelPool;
 }
        public virtual RequestHandle createConnection(Protocol protocol, LightstreamerRequest request, com.lightstreamer.client.transport.providers.HttpProvider_HttpRequestListener httpListener, IDictionary <string, string> extraHeaders, Proxy proxy, long tcpConnectTimeout, long tcpReadTimeout)
        {
            string address = request.TargetServer + "lightstreamer/" + request.RequestName + ".txt" + "?LS_protocol=" + Constants.TLCP_VERSION;
            Uri    uri;

            try
            {
                uri = new Uri(address);
            }
            catch (Exception e)
            {
                log.Fatal("Unexpectedly invalid URI: " + address, e);
                throw new System.ArgumentException(e.Message);
            }

            bool secure = isSSL(address);
            int  port   = uri.Port == -1 ? (secure ? 443 : 80) : uri.Port;

            IFullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, uri.PathAndQuery);

            httpRequest.Headers.Set(HttpHeaderNames.Host, uri.Host);

            string cookies = CookieHelper.getCookieHeader(uri);

            log.Info("Requested cookies for uri " + uri + ": " + cookies);
            if (!string.ReferenceEquals(cookies, null) && cookies.Length > 0)
            {
                httpRequest.Headers.Set(HttpHeaderNames.Cookie, cookies);
            }

            httpRequest.Headers.Set(HttpHeaderNames.UserAgent, ua);
            httpRequest.Headers.Set(HttpHeaderNames.ContentType, "text/plain; charset=UTF-8");

            if (extraHeaders != null)
            {
                foreach (KeyValuePair <string, string> header in extraHeaders.SetOfKeyValuePairs())
                {
                    httpRequest.Headers.Set(new AsciiString(header.Key), header.Value);
                }
            }

            IByteBuffer bbuf = Unpooled.CopiedBuffer(request.getTransportAwareQueryString(null, true) + "\r\n", Encoding.UTF8);

            httpRequest.Headers.Set(HttpHeaderNames.ContentLength, bbuf.ReadableBytes);

            httpRequest.Content.Clear().WriteBytes(bbuf);

            string host4Netty = System.Net.Dns.GetHostAddresses(uri.Host)[0].ToString();

            log.Debug("cs ---- address: " + address + ", " + host4Netty);

            NettyFullAddress target = new NettyFullAddress(secure, host4Netty, port, uri.Host, proxy);

            NettyInterruptionHandler interruptionHandler = new NettyInterruptionHandler();

            bindAsync(uri, target, httpListener, httpRequest, interruptionHandler);

            return(interruptionHandler);
        }
Пример #3
0
        public virtual void release(NettyFullAddress address, IChannel ch)
        {
            ChannelPoolMapWrapper poolMap = poolMapRef.Value;

            if (poolMap != null)
            {
                SimpleChannelPool pool = poolMap.PoolMap.Get(address);
                pool.ReleaseAsync(ch);
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            NettyFullAddress check = (NettyFullAddress)obj;

            if ((this.proxy == null && check.proxy == null) || (this.proxy != null && this.proxy.Equals(check.proxy)))
            {
                return(addressObj.Equals(check.addressObj));
            }
            return(false);
        }
Пример #5
0
        public virtual async void connect(string address, SessionRequestListener networkListener, IDictionary <string, string> extraHeaders, string cookies, Proxy proxy, long timeout)
        {
            Uri    uri    = LsUtils.uri(address);
            string host   = uri.Host;
            int    port   = LsUtils.port(uri);
            bool   secure = LsUtils.isSSL(uri);

            try
            {
                string host4Netty = System.Net.Dns.GetHostAddresses(host)[0].ToString();

                NettyFullAddress         remoteAddress         = new NettyFullAddress(secure, host4Netty, port, host, proxy);
                ExtendedNettyFullAddress extendedRemoteAddress = new ExtendedNettyFullAddress(remoteAddress, extraHeaders, cookies);

                WebSocketChannelPool wsPool = (WebSocketChannelPool)wsPoolManager.get(extendedRemoteAddress);

                IChannel ch = await wsPool.AcquireNewOr(timeout);

                if (ch != null)
                {
                    if (ch.Active)
                    {
                        this.channel = new MyChannel(ch, wsPool, networkListener);
                        WebSocketChannelHandler chHandler = new WebSocketChannelHandler(networkListener, channel);
                        PipelineUtils.populateWSPipeline(ch, chHandler);
                        networkListener.onOpen();
                    }
                    else
                    {
                        log.Error("WebSocket handshake error, ");
                        networkListener.onBroken();
                    }
                }
                else
                {
                    log.Error("WebSocket handshake error, channel unexpectedly null");
                    networkListener.onBroken();
                }
            } catch (Exception e)
            {
                log.Error("WebSocket handshake error: " + e.Message);
                networkListener.onBroken();
            }
        }
        /// <summary>
        /// Populates the channel pipeline in order to read data from a HTTP connection.
        /// </summary>
        public static void populateHttpPipeline(IChannel ch, NettyFullAddress remoteAddress, IChannelHandler httpChHandler)
        {
            IChannelPipeline pipeline = ch.Pipeline;

            ProxyHandler proxy = remoteAddress.Proxy;

            if (proxy != null)
            {
                log.Info("Add Proxy: " + proxy.ProxyAddress);

                pipeline.AddLast("proxy", proxy);
            }

            if (remoteAddress.Secure)
            {
                pipeline.AddLast("tls", new TlsHandler(new ClientTlsSettings(remoteAddress.HostName)));
            }

            pipeline.AddLast("http", new HttpClientCodec());

            pipeline.AddLast(READER_KEY, httpChHandler);
        }
Пример #7
0
        public virtual ValueTask <IChannel> acquire(NettyFullAddress address)
        {
            SimpleChannelPool pool = PoolMapWrapper.PoolMap.Get(address);

            return(pool.AcquireAsync());
        }
Пример #8
0
 public HttpChannelPool(NettyFullAddress remoteAddress, Bootstrap bootstrap, IChannelPoolHandler handler) : base(bootstrap, handler)
 {
     this.remoteAddress = remoteAddress;
     this.bootstrap     = bootstrap;
 }
Пример #9
0
 internal HttpChannelPoolHandler(NettyFullAddress key)
 {
     this.key = key;
 }
Пример #10
0
 public virtual HttpChannelPool getChannelPool(NettyFullAddress address)
 {
     return(PoolMapWrapper.PoolMap.Get(address));
 }
 private string format(NettyFullAddress target)
 {
     return(target.Host + ":" + target.Port + " - " + target.Secure + "(" + target.ToString() + ").");
 }
        private async void bindAsync(Uri uri, NettyFullAddress target, com.lightstreamer.client.transport.providers.HttpProvider_HttpRequestListener httpListener, IFullHttpRequest httpRequest, NettyInterruptionHandler interruptionHandler)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug("HTTP transport connection establishing (oid=" + objectId + "): " + format(uri, httpRequest));
                log.Debug(" -  target: " + format(target));
            }
            try
            {
                IChannel channel_e = await httpPoolManager.acquire(target);

                log.Debug(" - acquired! " + channel_e.Id);

                // IChannel ch2 = channelFuture.Result;
                NettySocketHandler socketHandler2 = (NettySocketHandler)PipelineUtils.getChannelHandler(channel_e);

                if (socketHandler2 == null)
                {
                    log.Debug("Socket Handler null.");
                }
                else
                {
                    NettyRequestListener requestListener2 = new NettyRequestListener(httpListener, target, channel_e, httpPoolManager);

                    bool listenerBound = socketHandler2.switchListener(uri, requestListener2, interruptionHandler);
                    if (!listenerBound)
                    {
                        this.sessionThread.schedule(new Task(() =>
                        {
                            bindAsync(uri, target, httpListener, httpRequest, interruptionHandler);
                        }), 500);

                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Go Bind!");
                        }
                    }
                    else
                    {
                        // Thread.Sleep(400);
                        Task snd_req = channel_e.WriteAndFlushAsync(httpRequest).ContinueWith((snd_req_task, fu2) =>
                        {
                            log.Debug("Send Request Task status = " + snd_req_task.Status);

                            if (snd_req_task.IsFaulted)
                            {
                                log.Error("HTTP write failed [" + channel_e.Id + "]: " + httpRequest.Uri + ", " + snd_req_task.Exception);
                                channel_e.CloseAsync();
                                requestListener2.onBroken();
                            }
                        }, this);

                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Go with the request " + channel_e.Active);
                        }
                    }
                }

                return;
            }
            catch (ConnectException ce)
            {
                log.Info("Connection error: " + ce.Message);

                if (log.IsDebugEnabled)
                {
                    log.Debug("HTTP transport connection error (Couldn't get a socket, try again) (oid=" + objectId + "): " + format(uri, httpRequest));
                    log.Debug(" - " + ce.StackTrace);
                }
            }
            catch (ConnectTimeoutException cte)
            {
                log.Info("Timeout error: " + cte.Message);

                if (log.IsDebugEnabled)
                {
                    log.Debug("HTTP transport timeout error (Couldn't get a socket, try again) (oid=" + objectId + "): " + format(uri, httpRequest));
                    log.Debug(" - " + cte.StackTrace);
                }
            }
            catch (Exception e)
            {
                log.Info("Error: " + e.Message);

                if (log.IsDebugEnabled)
                {
                    log.Debug("HTTP transport error (Couldn't get a socket, try again) (oid=" + objectId + "): " + format(uri, httpRequest));
                    log.Debug(" - " + e.StackTrace);
                }
            }
        }