Esempio n. 1
0
 private static void printResponseLog(string url, Response response)
 {
     if (GamedoniaBackend.INSTANCE.debug) {
         String debugMsg = "[Api Response][" + url + "] - " + response.status;
         if (!String.IsNullOrEmpty(response.message)) debugMsg += " " + response.message;
         if (!String.IsNullOrEmpty(response.Text)) debugMsg += " " + response.Text;
         Debug.Log(debugMsg);
     }
 }
Esempio n. 2
0
        private void GetResponse() {
            System.Diagnostics.Stopwatch curcall = new System.Diagnostics.Stopwatch();
            curcall.Start();
            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);

                    var client = new TcpClient ();
                    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 );
                        response = new Response ();
                        response.request = this;
                        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 (Exception e) {
                Console.WriteLine ("Unhandled Exception, aborting request.");
                Console.WriteLine (e);
                exception = e;
                response = null;
            }
            state = RequestState.Done;
            isDone = true;
            responseTime = curcall.ElapsedMilliseconds;

            if ( completedCallback != null )
            {
                if (synchronous) {
                    completedCallback(this);
                } else {
                    // we have to use this dispatcher to avoid executing the callback inside this worker thread
                    ResponseCallbackDispatcher.Singleton.requests.Enqueue( this );
                }
            }

            if ( LogAllRequests )
            {
#if !UNITY_EDITOR
                System.Console.WriteLine("NET: " + InfoString( VerboseLogging ));
#else
                if ( response != null && response.status >= 200 && response.status < 300 )
                {
                    Debug.Log( InfoString( VerboseLogging ) );
                }
                else if ( response != null && response.status >= 400 )
                {
                    Debug.LogError( InfoString( VerboseLogging ) );
                }
                else
                {
                    Debug.LogWarning( InfoString( VerboseLogging ) );
                }
#endif
            }            
        }
Esempio n. 3
0
        public static GDError buildError(Response response)
        {
            GDError error = null;
            if (!String.IsNullOrEmpty (response.Text)) {
                error = JsonMapper.ToObject<GDError>(response.Text);
                error.httpErrorCode = response.status;
                if (!String.IsNullOrEmpty(response.message)) error.httpErrorMessage = response.message;

            } else {
                error = new GDError();
                error.httpErrorCode = response.status;
            }

            return error;
        }
Esempio n. 4
0
        public void Send()
        {
            isDone = false;
            state = RequestState.Waiting;

            if (acceptGzip)
                SetHeader ("Accept-Encoding", "gzip");
            ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) {
                try {

                    var retry = 0;
                    while (++retry < maximumRedirects) {
                        if (useCache) {
                            string etag = "";
                            if (etags.TryGetValue (uri.AbsoluteUri, out etag)) {
                                SetHeader ("If-None-Match", etag);
                            }
                        }
                        var hostHeader = uri.Host;
                        if (uri.Port != 80 && uri.Port != 443)
                            hostHeader += ":" + uri.Port.ToString ();
                        SetHeader ("Host", hostHeader);

                        ActiveConnection connection;
                        try {
                            //pull a connection from the pool (a new one is created if needed)
                            connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https");
                        } catch (Exception e) {
                            exception = e;
                            response = null;
                            break;
                        }

                        try {
                            WriteToStream (connection.stream);
                        } catch (IOException e) {
                            exception = new IOException ("Server closed the connection:" + e.ToString ());
                            response = null;
                            break;
                        }
                        response = new Response (this);
                        state = RequestState.Reading;
                        try {
                            response.ReadFromStream (connection.stream);
                        } catch (IOException e) {
                            exception = new IOException ("Server closed the connection:" + e.ToString ());
                            response = null;
                            break;
                        }

                        switch (response.status) {
                        case 101:
                            upgradedConnection = connection;
                            retry = maximumRedirects;
                            break;
                        case 304:
                            retry = maximumRedirects;
                            break;
                        case 307:
                        case 302:
                        case 301:
                            uri = new Uri (response.GetHeader ("Location"));
                            if (OnRedirect != null) {
                                OnRedirect (uri);
                                retry = maximumRedirects;
                            }
                            break;
                        default:
                            retry = maximumRedirects;
                            break;
                        }
                        //place the connection back into the pool if not upgraded.
                        if(upgradedConnection == null) {
                            if (response.GetHeader ("Connection").ToLower () != "keep-alive") {
                                connection.stream.Close ();
                            }
                            activeConnections [connection.key].Add (connection);
                        }
                    }
                    if (useCache) {
                        string etag = response.GetHeader ("etag");
                        if (etag.Length > 0) {
                            etags [uri.AbsoluteUri] = etag;
                            SaveEtags ();
                        }
                    }
                } catch (Exception e) {
                    exception = e;
                    response = null;
                }
                state = RequestState.Done;
                isDone = true;
            }));
        }
Esempio n. 5
0
		public void Send ()
		{
			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);
						var client = new TcpClient ();
						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);
							response = new Response ();
							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 (Exception e) {
					Console.WriteLine ("Unhandled Exception, aborting request.");
					Console.WriteLine (e);
					exception = e;
					response = null;
				}
				state = RequestState.Done;
				isDone = true;
			}));
		}
Esempio n. 6
0
		private void SendFunction(System.Object t) {
			Debug.Log("SendFunction 0 ");
			try {
				var retry = 0;
				Debug.Log("Send 1 ");
				while (++retry < maximumRetryCount) {
					Debug.Log("Send 2 ");
					if (useCache) {
						string etag = "";
						if (etags.TryGetValue (uri.AbsoluteUri, out etag)) {
							SetHeader ("If-None-Match", etag);
						}
					}
					Debug.Log("Send 3 ");
					
					SetHeader ("Host", uri.Host);
					var client = new TcpClient ();
					client.Connect (uri.Host, uri.Port);
					Debug.Log("Send 4 ");
					using (var stream = client.GetStream ()) {
						Debug.Log("Send 5 ");
						
						var ostream = stream as Stream;
						if (uri.Scheme.ToLower() == "https") {
							Debug.Log("Send 6 ");
							
							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;
							}
						}
						Debug.Log("Send 7 ");
						
						WriteToStream (ostream);
						response = new Response ();
						state = RequestState.Reading;
						response.ReadFromStream(ostream);
					}
					Debug.Log("Send 8 ");
					
					client.Close ();
					switch (response.status) {
					case 307:
					case 302:
					case 301:
						uri = new Uri (response.GetHeader ("Location"));
						continue;
					default:
						retry = maximumRetryCount;
						break;
					}
					Debug.Log("Send 9 ");
					
				}
				Debug.Log("Send 10 ");
				
				if (useCache) {
					string etag = response.GetHeader ("etag");
					if (etag.Length > 0)
						etags[uri.AbsoluteUri] = etag;
				}
				Debug.Log("Send 11 ");
				
			} catch (Exception e) {
				Debug.Log("Send 12 ");
				
				Debug.LogException(e);
				Debug.LogError(e.Message);
				exception = e;
				response = null;
			}
			Debug.Log("Send 13 ");
			
			state = RequestState.Done;
			isDone = true;
		}