Ejemplo n.º 1
0
        public void Send(string filePath = null, bool onlyContentLength = false)
        {
            isDone = false;
            state = RequestState.Waiting;
            if (acceptGzip)
                SetHeader ("Accept-Encoding", "gzip");
            ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) {
                try {
                    var retry = 0;
                    while (++retry < maximumRetryCount) {
                        if (useCache) {
                            string etag = "";
                            if (etags.TryGetValue (uri.AbsoluteUri, out etag)) {
                                SetHeader ("If-None-Match", etag);
                            }
                        }
                        SetHeader ("Host", uri.Host);

                        #if UNITY_WEBPLAYER
                        /*
                        string ip = Dns.GetHostAddresses(uri.Host)[0].ToString();
                        if (!Security.PrefetchSocketPolicy (ip, 1843)) {
                            Debug.LogError("Security Exception. Policy file load failed!");
                        }
                        */
                        #endif

                        //Init the response
                        if (filePath != null) {
                            response = new Response(filePath, onlyContentLength);
                        }else {
                            response = new Response ();
                        }

                        var client = new TcpClient ();

                        client.SendTimeout = 5000;
                        client.ReceiveTimeout = 5000;

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

                        using (var stream = client.GetStream ()) {
                            var ostream = stream as Stream;
                            if (uri.Scheme.ToLower() == "https") {
                                ostream = new SslStream (stream, false, new RemoteCertificateValidationCallback (ValidateServerCertificate));
                                try {
                                    var ssl = ostream as SslStream;
                                    ssl.AuthenticateAsClient (uri.Host);
                                } catch (Exception e) {
                                    Debug.LogError ("Exception: " + e.Message);
                                    return;
                                }
                            }

                            WriteToStream (ostream);

                            if (downloadDelegate != null) response.downloadDelegate = downloadDelegate;

                            state = RequestState.Reading;
                            response.ReadFromStream(ostream);
                        }
                        client.Close ();
                        switch (response.status) {
                        case 307:
                        case 302:
                        case 301:
                            uri = new Uri (response.GetHeader ("Location"));
                            continue;
                        default:
                            retry = maximumRetryCount;
                            break;
                        }
                    }
                    if (useCache) {
                        string etag = response.GetHeader ("etag");
                        if (etag.Length > 0)
                            etags[uri.AbsoluteUri] = etag;
                    }

                } catch (SocketException e) {
                    response.status = -100;
                    response.message = "Failed";
                    if (downloadDelegate != null) {
                        IOErrorEvent ioErrorEvent = new IOErrorEvent();
                        ioErrorEvent.ErrorCode = e.ErrorCode;
                        ioErrorEvent.Message = e.Message;
                        downloadDelegate.OnIOError(ioErrorEvent);
                    }
                    Debug.LogException(e);
                } catch (IOException ioex) {
                    response.status = -100;
                    response.message = "Failed";
                    if (downloadDelegate != null) {
                        IOErrorEvent ioErrorEvent = new IOErrorEvent();
                        ioErrorEvent.Message = ioex.Message;
                        downloadDelegate.OnIOError(ioErrorEvent);
                    }

                    Debug.LogException(ioex);
                } catch (Exception e) {
                    Console.WriteLine ("Unhandled Exception, aborting request.");
                    Console.WriteLine (e);
                    Debug.Log(e.Message + " Type: " + e.GetType().FullName);
                    exception = e;
                    response = null;
                }

                //Debug.Log ("Request finished");
                state = RequestState.Done;
                isDone = true;
            }));
        }
Ejemplo n.º 2
0
        public void Send(string filePath = null, bool onlyContentLength = false)
        {
            isDone = false;
            state  = RequestState.Waiting;
            if (acceptGzip)
            {
                SetHeader("Accept-Encoding", "gzip");
            }
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) {
                try {
                    var retry = 0;
                    while (++retry < maximumRetryCount)
                    {
                        if (useCache)
                        {
                            string etag = "";
                            if (etags.TryGetValue(uri.AbsoluteUri, out etag))
                            {
                                SetHeader("If-None-Match", etag);
                            }
                        }
                        SetHeader("Host", uri.Host);

                                                #if UNITY_WEBPLAYER
                        /*
                         * string ip = Dns.GetHostAddresses(uri.Host)[0].ToString();
                         * if (!Security.PrefetchSocketPolicy (ip, 1843)) {
                         *      Debug.LogError("Security Exception. Policy file load failed!");
                         * }
                         */
                                                #endif

                        var client = new TcpClient();


                        client.SendTimeout    = 5000;
                        client.ReceiveTimeout = 5000;

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

                        using (var stream = client.GetStream()) {
                            var ostream = stream as Stream;
                            if (uri.Scheme.ToLower() == "https")
                            {
                                ostream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                                try {
                                    var ssl = ostream as SslStream;
                                    ssl.AuthenticateAsClient(uri.Host);
                                } catch (Exception e) {
                                    Debug.LogError("Exception: " + e.Message);
                                    return;
                                }
                            }

                            WriteToStream(ostream);
                            if (filePath != null)
                            {
                                response = new Response(filePath, onlyContentLength);
                            }
                            else
                            {
                                response = new Response();
                            }

                            if (downloadDelegate != null)
                            {
                                response.downloadDelegate = downloadDelegate;
                            }

                            state = RequestState.Reading;
                            response.ReadFromStream(ostream);
                        }
                        client.Close();
                        switch (response.status)
                        {
                        case 307:
                        case 302:
                        case 301:
                            uri = new Uri(response.GetHeader("Location"));
                            continue;

                        default:
                            retry = maximumRetryCount;
                            break;
                        }
                    }
                    if (useCache)
                    {
                        string etag = response.GetHeader("etag");
                        if (etag.Length > 0)
                        {
                            etags[uri.AbsoluteUri] = etag;
                        }
                    }
                } catch (SocketException e) {
                    response.status = -100;
                    if (downloadDelegate != null)
                    {
                        IOErrorEvent ioErrorEvent = new IOErrorEvent();
                        ioErrorEvent.ErrorCode    = e.ErrorCode;
                        ioErrorEvent.Message      = e.Message;
                        downloadDelegate.OnIOError(ioErrorEvent);
                    }
                    Debug.LogException(e);
                } catch (IOException ioex) {
                    response.status = -100;
                    if (downloadDelegate != null)
                    {
                        IOErrorEvent ioErrorEvent = new IOErrorEvent();
                        ioErrorEvent.Message      = ioex.Message;
                        downloadDelegate.OnIOError(ioErrorEvent);
                    }

                    Debug.LogException(ioex);
                } catch (Exception e) {
                    Console.WriteLine("Unhandled Exception, aborting request.");
                    Console.WriteLine(e);
                    Debug.Log(e.Message + " Type: " + e.GetType().FullName);
                    exception = e;
                    response  = null;
                }

                //Debug.Log ("Request finished");
                state  = RequestState.Done;
                isDone = true;
            }));
        }
Ejemplo n.º 3
0
    public void OnIOError(IOErrorEvent ioError)
    {
        Debug.LogError ("IO Error event: " + ioError.Message);
        ConnectionChecker checker = new ConnectionChecker ();

        checker.check (
            delegate (bool success) {
                if (success) {
                    cleanupConnectionSuccessful(false,0);
                }else {
                    cleanupConnectionSuccessful(false,1);
                }
        });
    }