Exemplo n.º 1
0
 public WebSocketChannelPool(HttpChannelPool parentPool, ExtendedNettyFullAddress address, IChannelPoolHandler handler) : base(parentPool.Bootstrap, parentPool, handler)
 {
     this.address = address;
     if (log.IsDebugEnabled)
     {
         log.Debug("New WS channel pool created. Remote address: " + address.Address.Address);
     }
 }
        /// <summary>
        /// Upgrades the given channel.
        /// </summary>
        /// <param name="channelFuture"> the channel to upgrade </param>
        /// <param name="address"> the address to which the channel connects  </param>
        public WebSocketChannelUpgradeFuture(IChannel channel, ExtendedNettyFullAddress address)
        {
            if (!InstanceFieldsInitialized)
            {
                InitializeInstanceFields();
                InstanceFieldsInitialized = true;
            }

            this.chnl    = channel;
            this.address = address;
        }
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }
            ExtendedNettyFullAddress other = (ExtendedNettyFullAddress)obj;

            if (address == null)
            {
                if (other.address != null)
                {
                    return(false);
                }
            }
            else if (!address.Equals(other.address))
            {
                return(false);
            }
            if (string.ReferenceEquals(cookies, null))
            {
                if (!string.ReferenceEquals(other.cookies, null))
                {
                    return(false);
                }
            }
            else if (!cookies.Equals(other.cookies))
            {
                return(false);
            }
            if (extraHeaders == null)
            {
                if (other.extraHeaders != null)
                {
                    return(false);
                }
            }
            else if (!extraHeaders.Equals(other.extraHeaders))
            {
                return(false);
            }
            return(true);
        }
 /// <summary>
 /// Gets a channel from the pool.
 /// </summary>
 public virtual IChannelPool get(ExtendedNettyFullAddress addr)
 {
     return(poolMap.Get(addr));
 }
        /// <summary>
        /// Upgrade the channel to WebSocket.
        /// </summary>
        private void upgrade(ExtendedNettyFullAddress address)
        {
            /*
             * ========================================= Note =================================================
             * Operations on the channel must happen in the thread associated with the channel.
             * Otherwise subtle bugs can appear.
             * For example the method WebSocketClientHandshaker.finishHandshake can return before
             * the method WebSocketClientHandshaker.handshake returns leaving the channel pipeline in a mess.
             * ================================================================================================
             */

            chnl.EventLoop.Execute(() =>
            {
                /*
                 * If the eventLoop is overloaded, when this task is executed the channel can be broken
                 * (for example because the server has closed it).
                 * So the first thing to do is to check if the channel is healthy.
                 */
                if (chnl.Active)
                {
                    /* set cookies and extra headers */
                    string cookies = address.Cookies;
                    IDictionary <string, string> extraHeaders = address.ExtraHeaders;
                    DefaultHttpHeaders customHeaders          = new DefaultHttpHeaders();
                    if (extraHeaders != null)
                    {
                        foreach (KeyValuePair <string, string> entry in extraHeaders)
                        {
                            customHeaders.Add(new AsciiString(entry.Key), entry.Value);
                        }
                    }
                    if (!string.ReferenceEquals(cookies, null) && cookies.Length > 0)
                    {
                        customHeaders.Set(HttpHeaderNames.Cookie, cookies);
                    }
                    /* build url */
                    NettyFullAddress remoteAddress = address.Address;
                    string scheme = remoteAddress.Secure ? "wss" : "ws";
                    string host   = remoteAddress.Host;
                    string url;

                    int port = remoteAddress.Port;
                    if (host.Equals("::1"))
                    {
                        url = scheme + "://localhost:" + port + "/lightstreamer";
                    }
                    else
                    {
                        url = scheme + "://" + host + ":" + port + "/lightstreamer";
                    }

                    Uri uri            = LsUtils.uri(url);
                    string subprotocol = Constants.TLCP_VERSION + ".lightstreamer.com";
                    /* build pipeline */

                    WebSocketHandshakeHandler wsHandshakeHandler = new WebSocketHandshakeHandler(uri, subprotocol, customHeaders, this);

                    PipelineUtils.populateWSPipelineForHandshake(chnl, wsHandshakeHandler);

                    /* WS handshake */
                    futureTask = wsHandshakeHandler.handshake(chnl);
                }
                else
                {
                    futureTask = Task.Factory.StartNew(() => Thread.Sleep(2));
                }
            });

            return;
        }