Ejemplo n.º 1
0
        private ITcpClient GetClient()
        {
            lock (connections)
            {
                List <ITcpClient> clients = null;
                if (connections.TryGetValue(connectionKey, out clients))
                {
                    // remove bad clients
                    //Debug.Log("removing clients " + connectionKey);
                    clients.RemoveAll(delegate(ITcpClient client)
                    {
                        if (!client.Connected || Time.Since(client.LastTime) >= kConnectionTimeout)
                        {
                            //Debug.Log("removing client");
                            client.Close();
                            return(true);
                        }
                        return(false);
                    });

                    if (clients.Count > 0)
                    {
                        var client = clients[clients.Count - 1];
                        clients.RemoveAt(clients.Count - 1);
                        //Debug.Log("reusing client " + Time.Since(client.LastTime));
                        return(client);
                    }
                }
            }

            //Debug.Log("creating new client " + connectionKey);
            ITcpClient tcpClient = TcpClientFactory.Create(_uri.Scheme == "https");

            if (Proxy != null && _uri.Scheme == "https")
            {
                (tcpClient as TcpClientEvent).ConnectedEvent += delegate(object sender)
                {
                    const string CRLF = "\r\n";

                    string request = "CONNECT " + _uri.Host + ":" + _uri.Port + " HTTP/1.1" + CRLF
                                     + "Host: " + _uri.HostAndPort + CRLF
                                     + CRLF;

                    var bytes = Encoding.GetBytes(request);
                    tcpClient.Write(bytes, 0, bytes.Length);

                    string header = ReadLine(tcpClient).ToLower();
                    if (!header.StartsWith("http/1.1 200 connection established") && !header.StartsWith("http/1.0 200 connection established"))
                    {
                        tcpClient.Close();
                        tcpClient = null;
                        throw new System.Net.WebException("failed to connect to proxy");
                    }

                    // read the headers
                    while (true)
                    {
                        var line = ReadLine(tcpClient);
                        //Debug.Log("line: " + line + " " + line.Length);
                        if (line.Length == 0)
                        {
                            break;
                        }
                    }
                };
            }

            return(tcpClient);
        }
Ejemplo n.º 2
0
            bool DoConnect()
            {
                if (Proxy == null)
                {
                    _client = TcpClientFactory.Create(_uri.Scheme == "wss");

                    EB.Debug.Log("connect " + _uri.Host + " on port " + _uri.Port);
                    if (!_client.Connect(_uri.Host, _uri.Port, 5 * 1000))
                    {
                        _client.Close();
                        _client = null;
                        _state  = WebSocketState.None;
                        Error("failed to connect");
                        return(false);
                    }
                }
                else
                { // use proxy
                    bool secure = _uri.Scheme == "wss";

                    _client = TcpClientFactory.Create(secure);

                    if (secure)
                    {
                        (_client as TcpClientEvent).ConnectedEvent += delegate(object sender)
                        {
                            const string CRLF = "\r\n";

                            string request = "CONNECT " + _uri.Host + ":" + _uri.Port + " HTTP/1.1" + CRLF
                                             + "Host: " + _uri.HostAndPort + CRLF
                                             + CRLF;

                            var bytes = Encoding.GetBytes(request);
                            _client.Write(bytes, 0, bytes.Length);

                            string header = ReadLine(_client).ToLower();
                            if (!header.StartsWith("http/1.1 200 connection established") && !header.StartsWith("http/1.0 200 connection established"))
                            {
                                _client.Close();
                                _state = WebSocketState.None;
                                throw new System.Net.WebException("failed to connect to proxy");
                            }

                            // read the headers
                            while (true)
                            {
                                var line = ReadLine(_client);
                                //Debug.Log("line: " + line + " " + line.Length);
                                if (line.Length == 0)
                                {
                                    break;
                                }
                            }
                        };
                    }

                    EB.Debug.Log("connect proxy " + Proxy.Host + " on port " + Proxy.Port);
                    if (!_client.Connect(Proxy.Host, Proxy.Port, 5 * 1000))
                    {
                        _client.Close();
                        _client = null;
                        _state  = WebSocketState.None;
                        Error("failed to connect proxy");
                        return(false);
                    }
                }

                _client.ReadTimeout  = 5 * 1000;
                _client.WriteTimeout = 5 * 1000;

                if (!_running)
                {
                    return(false);
                }

                return(true);
            }