Пример #1
0
        public void Connect()
        {
#if UNITY_WP8
            client = new SocketEx.TcpClient();
#else
            client = new TcpClient();
#endif

            client.Connect(host, port);
        }
Пример #2
0
 private void Close()
 {
     if (Client != null)
     {
         try
         {
             Client.Close();
         }
         catch
         {
         }
         finally
         {
             Stream = null;
             Client = null;
         }
     }
 }
Пример #3
0
 private void Close()
 {
     LastProcessedUri = null;
     if (Client != null)
     {
         try
         {
             Client.Close();
         }
         catch
         {
         }
         finally
         {
             Stream = null;
             Client = null;
         }
     }
 }
Пример #4
0
        private void Connect()
        {
            Uri uri = CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address : CurrentRequest.CurrentUri;

            if (Client == null)
            {
                Client = new TcpClient();
            }

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

#if NETFX_CORE
                Client.UseHTTPSProtocol = !CurrentRequest.UseAlternateSSL && HTTPProtocolFactory.IsSecureProtocol(uri);

                // On WinRT and >WP8 we use the more secure Tls12 protocol, but on WP8 only Ssl is available...
                #if UNITY_WP_8_1 || UNITY_METRO_8_1
                Client.HTTPSProtocol = (int)SocketProtectionLevel.Tls12;
                #else
                Client.HTTPSProtocol = (int)SocketProtectionLevel.Ssl;
                #endif
#endif
                Client.Connect(uri.Host, uri.Port);

                HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host);
            }
            else
            {
                HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host);
            }

            lock (HTTPManager.Locker)
                StartTime = DateTime.UtcNow;

            if (Stream == null)
            {
                if (HasProxy && !Proxy.IsTransparent)
                {
                    Stream = Client.GetStream();

                    var outStream = new BinaryWriter(Stream);
                    outStream.Write(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);

                    outStream.Write(string.Format("Proxy-Connection: Keep-Alive"));
                    outStream.Write(HTTPRequest.EOL);

                    outStream.Write(string.Format("Connection: Keep-Alive"));
                    outStream.Write(HTTPRequest.EOL);

                    outStream.Write(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);

                    outStream.Write(HTTPRequest.EOL);
                    outStream.Flush();

                    CurrentRequest.ProxyResponse = new HTTPProxyResponse(CurrentRequest, Stream, false, false);
                    if (!CurrentRequest.ProxyResponse.Receive())
                    {
                        throw new Exception("Connection to the Proxy Server failed!");
                    }

                    // TODO: check & handle proxy status code?
                }

                // We have to use CurrentRequest.CurrentUri here, becouse uri can be a proxy uri with a different protocol
                if (HTTPProtocolFactory.IsSecureProtocol(CurrentRequest.CurrentUri))
                {
                    // On WP8 there are no Mono, so we must use the 'alternate' TlsHandlers
                    if (CurrentRequest.UseAlternateSSL)
                    {
                        var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());
                        if (CurrentRequest.CustomCertificateVerifyer == null)
                        {
                            handler.Connect(new LegacyTlsClient(new AlwaysValidVerifyer()));
                        }
                        else
                        {
                            handler.Connect(new LegacyTlsClient(CurrentRequest.CustomCertificateVerifyer));
                        }
                        Stream = handler.Stream;
                    }
                    else
                    {
#if !UNITY_WP8 && !NETFX_CORE
                        SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                        {
                            return(CurrentRequest.CallCustomCertificationValidator(cert, chain));
                        });

                        if (!sslStream.IsAuthenticated)
                        {
                            sslStream.AuthenticateAsClient(uri.Host);
                        }
                        Stream = sslStream;
#else
                        Stream = Client.GetStream();
#endif
                    }
                }
                else
                {
                    Stream = Client.GetStream();
                }
            }
        }
        private void Close()
        {
            if (Client != null)
            {
                try
                {
                    Client.Close();
                }
                catch
                {

                }
                finally
                {
                    Stream = null;
                    Client = null;
                }
            }
        }
            Connect()
        {
            Uri uri = CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address : CurrentRequest.CurrentUri;

            if (Client == null)
                Client = new TcpClient();

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

//#if NETFX_CORE
                //await
//#endif
                string address = HTTPManager.GetHostCache (uri.Host);
                if (!string.IsNullOrEmpty (address)) {
                    Client.Connect (address, uri.Port);
                } else {
                    Client.Connect (uri.Host, uri.Port);
                    if (Client.IsConnected()) {
                        System.Net.IPEndPoint end_point = Client.Client.RemoteEndPoint as System.Net.IPEndPoint;
                        address = end_point.Address.ToString ();
                        HTTPManager.AddHostCache (uri.Host, address);
                    }
                }
                    
            } else {
//				NGUIDebug.print("Client.Connected ========>" + Client.Connected);
			}

            if (Stream == null)
            {
                if (HasProxy && !Proxy.IsTransparent)
                {
                    Stream = Client.GetStream();

                    var outStream = new BinaryWriter(Stream);
                    outStream.Write(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Proxy-Connection: Keep-Alive"));
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Flush();
                    
                    ReadTo(Stream, HTTPResponse.LF);
                    ReadTo(Stream, HTTPResponse.LF);
                }
                
                if (HTTPProtocolFactory.IsSecureProtocol(uri))
                {
                    // On WP8 there are no Mono, so we must use the 'alternate' TlsHandlers
#if !UNITY_WP8 && !NETFX_CORE
                    if (CurrentRequest.UseAlternateSSL)
                    {
#endif
                        var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());
                        handler.Connect(new LegacyTlsClient(new AlwaysValidVerifyer()));
                        Stream = handler.Stream;
#if !UNITY_WP8 && !NETFX_CORE
                    }
                    else
                    {
                        SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                        {
                            return CurrentRequest.CallCustomCertificationValidator(cert, chain);
                        });

                        if (!sslStream.IsAuthenticated)
                            sslStream.AuthenticateAsClient(uri.Host);
                        Stream = sslStream;
                    }
#endif
                }
                else
                    Stream = Client.GetStream();
            }
        }
        private void Close()
        {
            LastProcessedUri = null;
            if (Client != null)
            {
                try
                {
                    Client.Close();
                }
                catch
                {

                }
                finally
                {
                    Stream = null;
                    Client = null;
                }
            }
        }
        private void Connect()
        {
            Uri uri = CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address : CurrentRequest.CurrentUri;

            #region TCP Connection

            if (Client == null)
                Client = new TcpClient();

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

#if NETFX_CORE
                Client.UseHTTPSProtocol = !CurrentRequest.UseAlternateSSL && HTTPProtocolFactory.IsSecureProtocol(uri);

                // On WinRT and >WP8 we use the more secure Tls12 protocol, but on WP8 only Ssl is available...
                #if UNITY_WP_8_1 || UNITY_METRO_8_1 || UNITY_METRO
                    Client.HTTPSProtocol = (int)SocketProtectionLevel.Tls12;
                #else
                    Client.HTTPSProtocol = (int)SocketProtectionLevel.Ssl;
                #endif
#endif

                Client.Connect(uri.Host, uri.Port);

                if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
                    HTTPManager.Logger.Information("HTTPConnection", "Connected to " + uri.Host + ":" + uri.Port.ToString());
            }
            else if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
                    HTTPManager.Logger.Information("HTTPConnection", "Already connected to " + uri.Host + ":" + uri.Port.ToString());

            #endregion

            lock (HTTPManager.Locker)
                StartTime = DateTime.UtcNow;

            if (Stream == null)
            {
                bool isSecure = HTTPProtocolFactory.IsSecureProtocol(CurrentRequest.CurrentUri);

                #region Proxy Handling

                if (HasProxy && (!Proxy.IsTransparent || (isSecure && Proxy.NonTransparentForHTTPS)))
                {
                    Stream = Client.GetStream();
                    var outStream = new BinaryWriter(Stream);

                    bool retry;
                    do
                    {
                        // If we have to becouse of a authentication request, we will switch it to true
                        retry = false;

                        outStream.SendAsASCII(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port));
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII("Proxy-Connection: Keep-Alive");
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII("Connection: Keep-Alive");
                        outStream.Write(HTTPRequest.EOL);

                        outStream.SendAsASCII(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port));
                        outStream.Write(HTTPRequest.EOL);

                        // Proxy Authentication
                        if (HasProxy && Proxy.Credentials != null)
                        {
                            switch (Proxy.Credentials.Type)
                            {
                                case AuthenticationTypes.Basic:
                                    // With Basic authentication we don't want to wait for a challange, we will send the hash with the first request
                                    outStream.Write(string.Format("Proxy-Authorization: {0}", string.Concat("Basic ", Convert.ToBase64String(Encoding.UTF8.GetBytes(Proxy.Credentials.UserName + ":" + Proxy.Credentials.Password)))).GetASCIIBytes());
                                    outStream.Write(HTTPRequest.EOL);
                                    break;

                                case AuthenticationTypes.Unknown:
                                case AuthenticationTypes.Digest:
                                    var digest = DigestStore.Get(Proxy.Address);
                                    if (digest != null)
                                    {
                                        string authentication = digest.GenerateResponseHeader(CurrentRequest, Proxy.Credentials);
                                        if (!string.IsNullOrEmpty(authentication))
                                        {
                                            outStream.Write(string.Format("Proxy-Authorization: {0}", authentication).GetASCIIBytes());
                                            outStream.Write(HTTPRequest.EOL);
                                        }
                                    }

                                    break;
                            }
                        }

                        outStream.Write(HTTPRequest.EOL);

                        // Make sure to send all the wrote data to the wire
                        outStream.Flush();
                        
                        CurrentRequest.ProxyResponse = new HTTPResponse(CurrentRequest, Stream, false, false);

                        // Read back the response of the proxy
                        if (!CurrentRequest.ProxyResponse.Receive(-1, true))
                            throw new Exception("Connection to the Proxy Server failed!");

                        if (HTTPManager.Logger.Level <= Logger.Loglevels.Information)
                            HTTPManager.Logger.Information("HTTPConnection", "Proxy returned - status code: " + CurrentRequest.ProxyResponse.StatusCode + " message: " + CurrentRequest.ProxyResponse.Message);

                        switch(CurrentRequest.ProxyResponse.StatusCode)
                        {
                            // Proxy authentication required
                            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8
                            case 407:
                            {
                                string authHeader = DigestStore.FindBest(CurrentRequest.ProxyResponse.GetHeaderValues("proxy-authenticate"));
                                if (!string.IsNullOrEmpty(authHeader))
                                {
                                    var digest = DigestStore.GetOrCreate(Proxy.Address);
                                    digest.ParseChallange(authHeader);

                                    if (Proxy.Credentials != null && digest.IsUriProtected(Proxy.Address) && (!CurrentRequest.HasHeader("Proxy-Authorization") || digest.Stale))
                                        retry = true;
                                }
                                break;
                            }

                            default:
                                if (!CurrentRequest.ProxyResponse.IsSuccess)
                                    throw new Exception(string.Format("Proxy returned Status Code: \"{0}\", Message: \"{1}\" and Response: {2}", CurrentRequest.ProxyResponse.StatusCode, CurrentRequest.ProxyResponse.Message, CurrentRequest.ProxyResponse.DataAsText));
                                break;
                        }

                    } while (retry);
                }

                #endregion

                // We have to use CurrentRequest.CurrentUri here, becouse uri can be a proxy uri with a different protocol
                if (isSecure)
                {
                    #region SSL Upgrade

                    // On WP8 there are no Mono, so we must use the 'alternate' TlsHandlers
                    if (CurrentRequest.UseAlternateSSL)
                    {
                        var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());

                        // http://tools.ietf.org/html/rfc3546#section-3.1
                        // It is RECOMMENDED that clients include an extension of type "server_name" in the client hello whenever they locate a server by a supported name type.
                        List<string> hostNames = new List<string>(1);
                        hostNames.Add(CurrentRequest.CurrentUri.Host);

                        handler.Connect(new LegacyTlsClient(CurrentRequest.CurrentUri,
                                                            CurrentRequest.CustomCertificateVerifyer == null ? new AlwaysValidVerifyer() : CurrentRequest.CustomCertificateVerifyer, 
                                                            null, 
                                                            hostNames));

                        Stream = handler.Stream;
                    }
                    else
                    {
#if !UNITY_WP8 && !NETFX_CORE
                        SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                        {
                            return CurrentRequest.CallCustomCertificationValidator(cert, chain);
                        });

                        if (!sslStream.IsAuthenticated)
                            sslStream.AuthenticateAsClient(CurrentRequest.CurrentUri.Host);
                        Stream = sslStream;
#else
                        Stream = Client.GetStream();
#endif
                    }

                    #endregion
                }
                else
                    Stream = Client.GetStream();
            }
        }
Пример #9
0
        /// <summary>
        /// 这里面机会同时接受数据并解析
        /// 这段代码
        /// </summary>
        private void _Connect()
        {
            this._retry_times = 0;
            while (this._keepConnecting)
            {
                //如果已经连接就等待50 毫秒,再次检测
                if (this.isConnected)
                {
                    Thread.Sleep(10);
                }
                else
                {
                    if (this.startConnectEventHandler != null)
                    {
                        this.startConnectEventHandler();
                    }
                    //清掉已有的线程
                    if (_sendDataThread != null)
                    {
                        try{
                            _sendDataThread.Abort();
                        }catch (Exception e) {
                        }
                    }
                    if (_sendCheckTokenThread != null)
                    {
                        try{
                            _sendCheckTokenThread.Abort();
                        }catch (Exception e) {
                        }
                    }
                    if (_heartBeatThread != null)
                    {
                        try{
                            _heartBeatThread.Abort();
                        }catch (Exception e) {
                        }
                    }
                    this._tcpClient = new TcpClient();
//                    this._tcpClient.Client.io //.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive
                    this._state = SocketClientState.Closed;
                    //如果没有就连接端口,并且获取数据流
                    try{
                        this._tcpClient.Connect(this.ip, this.port);
                    }catch (Exception e) {
                        Debug.LogError(string.Format("IP {0} port {1} message {2} stackTrace {3}", this.ip, this.port, e.Message, e.StackTrace));
                        this._retry_times += 1;
                        ///抛出异常
                        if (this._retry_times >= Max_Retry_Times)
                        {
                            this._keepConnecting = false;
                            if (this.networkErrorHandler != null)
                            {
                                this.networkErrorHandler();
                            }
                        }
                    }

                    if (this.isConnected)
                    {
                        this._retry_times = 0;
                        this._state       = SocketClientState.Connected;
                        this._buffer      = new byte[this._bufferSize];
                        this.ReadHeader();

                        _sendDataThread = new Thread(this.doSendData);
                        _sendDataThread.Start();
                        _sendCheckTokenThread = new Thread(this._SocketCheckToken);
                        _sendCheckTokenThread.Start();
                        _heartBeatThread = new Thread(this._HeartBeat);
                        _heartBeatThread.Start();
                    }
                }
            }
            this._tcpClient.Close();
        }
Пример #10
0
        Connect()
        {
            Uri uri = CurrentRequest.HasProxy ? CurrentRequest.Proxy.Address : CurrentRequest.CurrentUri;

            if (Client == null)
            {
                Client = new TcpClient();
            }

            if (!Client.Connected)
            {
                Client.ConnectTimeout = CurrentRequest.ConnectTimeout;

//#if NETFX_CORE
                //await
//#endif
                Client.Connect(uri.Host, uri.Port);
            }

            if (Stream == null)
            {
                if (HasProxy && !Proxy.IsTransparent)
                {
                    Stream = Client.GetStream();

                    var outStream = new BinaryWriter(Stream);
                    outStream.Write(string.Format("CONNECT {0}:{1} HTTP/1.1", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Host: {0}:{1}", CurrentRequest.CurrentUri.Host, CurrentRequest.CurrentUri.Port).GetASCIIBytes());
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(string.Format("Proxy-Connection: Keep-Alive"));
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Write(HTTPRequest.EOL);
                    outStream.Flush();

                    ReadTo(Stream, HTTPResponse.LF);
                    ReadTo(Stream, HTTPResponse.LF);
                }

                if (HTTPProtocolFactory.IsSecureProtocol(uri))
                {
                    // On WP8 there are no Mono, so we must use the 'alternate' TlsHandlers
#if !UNITY_WP8 && !NETFX_CORE
                    if (CurrentRequest.UseAlternateSSL)
                    {
#endif
                    var handler = new TlsClientProtocol(Client.GetStream(), new Org.BouncyCastle.Security.SecureRandom());
                    handler.Connect(new LegacyTlsClient(new AlwaysValidVerifyer()));
                    Stream = handler.Stream;
#if !UNITY_WP8 && !NETFX_CORE
                }
                else
                {
                    SslStream sslStream = new SslStream(Client.GetStream(), false, (sender, cert, chain, errors) =>
                    {
                        return(CurrentRequest.CallCustomCertificationValidator(cert, chain));
                    });

                    if (!sslStream.IsAuthenticated)
                    {
                        sslStream.AuthenticateAsClient(uri.Host);
                    }
                    Stream = sslStream;
                }
#endif
                }
                else
                {
                    Stream = Client.GetStream();
                }
            }
        }