示例#1
0
        /// <summary>
        /// Performs a server request with the given request parameters. This is
        /// performed asynchronously.
        /// </summary>
        ///
        /// <param name="request">The request that should be performed, which must adhere
        /// to the immediate request protocol.</param>
        /// <param name="callback">The callback containing the response. The callback
        /// will be on a background thread.</param>
        public void SendImmediateRequest(IImmediateServerRequest request, Action <IImmediateServerRequest, ServerResponse> callback)
        {
            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
//                if (request.HttpRequestMethod == HttpRequestMethod.Post)
//                {
                var bodyString = MiniJSON.Json.Serialize(request.SerialiseBody());
                ReleaseAssert.IsTrue(bodyString != null, "Invalid body.");
                var bodyData = Encoding.UTF8.GetBytes(bodyString);

                var httpRequestDesc         = new HttpPostRequestDesc(request.Url, bodyData);
                httpRequestDesc.Headers     = request.SerialiseHeaders();
                httpRequestDesc.ContentType = "application/json";

                var httpRequest = new HttpPostRequest(httpRequestDesc);
                m_httpSystem.SendRequest(httpRequest, (HttpPostRequest receivedHttpRequest, HttpResponse httpResponse) =>
                {
                    ReleaseAssert.IsTrue(httpRequest == receivedHttpRequest, "Received response for wrong request.");

                    var serverResponse = new ServerResponse(httpResponse);
                    callback(request, serverResponse);
                });
//                }
//                else
//                {
//                    var httpRequestDesc = new HttpGetRequestDesc(request.Url);
//                    httpRequestDesc.Headers = request.SerialiseHeaders();
//
//                    var httpRequest = new HttpGetRequest(httpRequestDesc);
//                    m_httpSystem.SendRequest(httpRequest, (HttpGetRequest receivedHttpRequest, HttpResponse httpResponse) =>
//                    {
//                        ReleaseAssert.IsTrue(httpRequest == receivedHttpRequest, "Received response for wrong request.");
//
//                        var serverResponse = new ServerResponse(httpResponse);
//                        callback(request, serverResponse);
//                    });
//                }
            });
        }
        /// <summary>
        /// Deserialised a map from a Json compatible IDictionary. A callback is provided
        /// for deserialising each element in the IDictionary.
        /// </summary>
        ///
        /// <param name="map">The map to be deserialised.</param>
        /// <param name="elementCallback">The callback for deserialising the elements in the map.</param>
        ///
        /// <returns>The deserialised map.</returns>
        public static ReadOnlyDictionary <string, TType> DeserialiseMap <TType>(IDictionary <string, object> map, Func <object, TType> elementCallback)
        {
            var output = new Dictionary <string, TType>();

            foreach (var entry in map)
            {
                if (entry.Value == null)
                {
                    output.Add(entry.Key, default(TType));
                }
                else
                {
                    ReleaseAssert.IsTrue(entry.Value is bool || entry.Value is int || entry.Value is long || entry.Value is float || entry.Value is string || entry.Value == null ||
                                         entry.Value is IList <object> || entry.Value is IDictionary <string, object>, "Serialised element must be a valid Json type.");

                    var deserialisedObj = elementCallback(entry.Value);
                    ReleaseAssert.IsNotNull(deserialisedObj, "Deserialised list elements should not be null.");
                    output.Add(entry.Key, deserialisedObj);
                }
            }

            return(new ReadOnlyDictionary <string, TType>(output));
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the HTTP system with the given task
        /// scheduler.
        /// </summary>
        ///
        /// <param name="taskScheduler">The task scheduler.</param>
        public HttpSystem(TaskScheduler taskScheduler)
        {
            ReleaseAssert.IsTrue(taskScheduler != null, "The task scheduler in a HTTP request system must not be null.");

            m_taskScheduler = taskScheduler;
        }
示例#4
0
        /// <summary>
        /// <para>The coroutine for processing the HTTP request. This will yield until the
        /// request has completed then parse the information required by a HTTP response
        /// from the WWW object.</para>
        /// </summary>
        ///
        /// <returns>The coroutine enumerator.</returns>
        ///
        /// <param name="www">The WWW object.</param>
        /// <param name="callback">The callback providing the response from the server.</param>
        private IEnumerator ProcessRequest(WWW www, Action <HttpResponse> callback)
        {
            ReleaseAssert.IsTrue(www != null, "The WWW must not be null when sending a request.");
            ReleaseAssert.IsTrue(callback != null, "The callback must not be null when sending a request.");

            yield return(www);

            HttpResponseDesc desc = null;

            if (string.IsNullOrEmpty(www.error))
            {
                ReleaseAssert.IsTrue(www.responseHeaders != null, "A successful HTTP response must have a headers object.");

                desc         = new HttpResponseDesc(HttpResult.Success);
                desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                if (www.bytes != null)
                {
                    desc.Body = www.bytes;
                }

                var httpStatus = www.responseHeaders ["STATUS"];
                ReleaseAssert.IsTrue(!string.IsNullOrEmpty(httpStatus), "A successful HTTP response must have a HTTP status value in the header.");

                desc.HttpResponseCode = ParseHttpStatus(httpStatus);
            }
            else
            {
                var bytes            = www.bytes;
                int httpResponseCode = 0;

                                #if UNITY_IPHONE && !UNITY_EDITOR
                var text = www.text;
                if (!string.IsNullOrEmpty(text))
                {
                    var bodyDictionary = Json.Deserialize(text) as Dictionary <string, object>;
                    if (bodyDictionary != null && bodyDictionary.ContainsKey("HttpCode"))
                    {
                        httpResponseCode = Convert.ToInt32(bodyDictionary ["HttpCode"]);
                        bytes            = Encoding.UTF8.GetBytes(text);
                    }
                }
                                #else
                httpResponseCode = ParseHttpError(www.error);
                                #endif

                if (httpResponseCode != 0)
                {
                    desc         = new HttpResponseDesc(HttpResult.Success);
                    desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                    if (www.bytes != null)
                    {
                        desc.Body = www.bytes;
                    }

                    desc.HttpResponseCode = httpResponseCode;
                }
                else
                {
                    desc = new HttpResponseDesc(HttpResult.CouldNotConnect);
                }
            }

            HttpResponse response = new HttpResponse(desc);
            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
                callback(response);
            });
        }
示例#5
0
 /// <summary>
 /// Constructs a new instances of the Task Scheduler. This must be on the main
 /// thread as a reference to the thread is captured.
 /// </summary>
 void Start()
 {
     m_mainThread = System.Threading.Thread.CurrentThread;
     ReleaseAssert.IsTrue(m_mainThread != null, "Failed to initialise Task Scheduler.");
 }