Example #1
0
        HttpConnection CreateConnection(string host, int port, bool useSsl)
        {
            HttpConnection connection;

#if USE_KEEPALIVE
            if (connectionPool.ContainsKey(host))
            {
                connection = connectionPool[host];
                connectionPool.Remove(host);
                return(connection);
            }
#endif
            connection = new HttpConnection()
            {
                host = host, port = port
            };
            Debug.Log(host);
            connection.Connect();

            if (useSsl)
            {
#if USE_SSL
#if UNITY_WP8
                tlsClient = new LegacyTlsClient(new AlwaysValidVerifyer());
                var handler = new TlsProtocolHandler(connection.client.GetStream());
                handler.Connect(tlsClient);
                connection.stream = handler.Stream;
#else
                connection.stream = new SslStream(connection.client.GetStream(), false, ValidateServerCertificate);
                var ssl = connection.stream as SslStream;
                ssl.AuthenticateAsClient(uri.Host);
#endif
#endif
            }
            else
            {
                connection.stream = connection.client.GetStream();
            }
            return(connection);
        }
Example #2
0
        /// <summary>
        /// 创建http连接
        /// </summary>
        /// <param name="host">ip地址</param>
        /// <param name="port">端口</param>
        /// <param name="useSsl">是否使用ssl安全证书</param>
        /// <returns></returns>
        HttpConnection CreateConnection(string host, int port, bool useSsl)
        {
            // UnityVS will cause async method fail.
            // See: http://answers.unity3d.com/questions/892371/tcp-socket-async-beginsend-never-happens.html
            HttpConnection connection;

            var key = string.Format("{0}:{1}", host, port);

            if (connectionPool.ContainsKey(key))
            {
                connection = connectionPool[key];
                connectionPool.Remove(key);
                if (connection.IsConnected())
                {
                    //Debug.LogFormat("CreateConnection reuse connection for {0}:{1}", host, port);
                    return(connection);
                }
                connection.Dispose();
            }

            connection = new HttpConnection()
            {
                host = host, port = port
            };
            //Debug.Log(host);
            connection.Connect();
            //Debug.LogFormat("CreateConnection connected to {0}:{1}", host, port);

            if (useSsl)
            {
#if BNICKSON_UPDATED
                var net = connection.client.GetStream();
                net.ReadTimeout   = HttpConnection.ReadTimeout * 1000;
                net.WriteTimeout  = HttpConnection.WriteTimeout * 1000;
                connection.stream = new SslStream(net, false, ValidateServerCertificate);
#else
                connection.stream = new SslStream(connection.client.GetStream(), false, ValidateServerCertificate);
#endif
                var ssl = connection.stream as SslStream;
#if BNICKSON_UPDATED
                var async  = ssl.BeginAuthenticateAsClient(uri.Host, null, null);
                var result = async.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(HttpConnection.ReadTimeout));
                if (!result || !async.IsCompleted)
                {
                    throw new HTTP.HTTPException("CreateConnection AuthenticateAsClient timeout");
                }
#else
                ssl.AuthenticateAsClient(uri.Host);
#endif
                EB.Debug.Log("CreateConnection ssl authenticated {0}:{1}", host, port);
            }
            else
            {
                connection.stream = connection.client.GetStream();

#if BNICKSON_UPDATED
                connection.stream.ReadTimeout  = HttpConnection.ReadTimeout * 1000;
                connection.stream.WriteTimeout = HttpConnection.WriteTimeout * 1000;
#endif
            }

            return(connection);
        }