コード例 #1
0
 /// <summary>
 /// Initializes the KiiCloudStorageSDK and KiiAnalyticsSDK.
 /// </summary>
 /// <remarks>
 /// This method is called by UnityGameEngine. Do not use it from your application.
 /// </remarks>
 public virtual void Awake()
 {
     if (!KiiInitializeBehaviour.isInitialized)
     {
         KiiInitializeBehaviour.isInitialized = true;
         DontDestroyOnLoad(this);
         INSTANCE = this;
         if (!Utils.IsEmpty(this.ServerUrl))
         {
             Kii.Initialize(this.AppID, this.AppKey, this.ServerUrl,
                            new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl());
             KiiAnalytics.Initialize(this.AppID, this.AppKey, this.ServerUrl, this.GetDeviceID(),
                                     new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl());
         }
         else
         {
             Kii.Initialize(this.AppID, this.AppKey, this.Site,
                            new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl());
             KiiAnalytics.Initialize(this.AppID, this.AppKey, ToAnalyticsSite(this.Site), this.GetDeviceID(),
                                     new KiiUnityClientFactoryImpl(), new KiiAsyncUnityClientFactoryImpl());
         }
         WWWRequestLooper.ApiTimeout = this.ApiTimeout;
         StartCoroutine(WWWRequestLooper.RunLoop());
     }
 }
コード例 #2
0
        private void ExecSendRequest(ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            float timeout = Time.realtimeSinceStartup + Timeout;

            this.SetSDKClientInfo();
            WWWRequestLooper.RegisterNetworkRequest(new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary()), progressCallback, (WWW www, ProgressCallbackHelper progress) => {
                if (!www.isDone)
                {
                    if (timeout < Time.realtimeSinceStartup)
                    {
                        DisposeWWW(www);
                        callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                        return(true);
                    }
                    if (progress != null)
                    {
                        progress.NotifyUploadProgress(www, this.body.Length);
                    }
                    return(false);
                }
                else
                {
                    try
                    {
                        Exception e = this.CheckHttpError(www);
                        if (e != null)
                        {
                            callback(null, e);
                            return(true);
                        }
                        ApiResponse response = new ApiResponse();
                        Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
                        this.CopyHttpHeaders(responseHeaders, response);
                        response.Status      = WWWUtils.GetStatusCode(responseHeaders, www.bytes == null ? 204 : 200);
                        response.ContentType = WWWUtils.GetHeader(responseHeaders, "Content-Type");
                        response.ETag        = WWWUtils.GetHeader(responseHeaders, "ETag");
                        if (www.bytes != null)
                        {
                            response.Body = this.GetString(www.bytes);
                        }
                        callback(response, null);
                        return(true);
                    }
                    catch (Exception e)
                    {
                        if (Kii.Logger != null)
                        {
                            Kii.Logger.Debug("[ERROR] Unexpected exception occurred when handling http response. msg=" + e.Message);
                        }
                        callback(null, e);
                        return(true);
                    }
                    finally
                    {
                        DisposeWWW(www);
                    }
                }
            });
        }
コード例 #3
0
 /// <summary>
 /// Registers the network request.
 /// </summary>
 /// <param name="www">Www.</param>
 /// <param name="progressCallback">Callback for progrees.</param>
 /// <param name="action">Action.</param>
 internal static void RegisterNetworkRequest(WWW www, ProgressCallbackHelper progressCallback, Func <WWW, ProgressCallbackHelper, bool> action)
 {
     WWWRequestLooper.RunOnMainThread(() => {
         bool isDone = action(www, progressCallback);
         if (!isDone)
         {
             WWWRequestLooper.RegisterNetworkRequest(www, progressCallback, action);
         }
     });
 }
コード例 #4
0
 private void _SendRequest(KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.SetHttpMethodOverride();
     WWWRequestLooper.RunOnMainThread(() => {
         this.ExecSendRequest(null, callback);
     });
 }
コード例 #5
0
 public override void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     WWWRequestLooper.RunOnMainThread(() => {
         // TODO:If huge file will be uploaded, this way is bad performance.
         // Try to Upload an Object Body in Multiple Pieces
         // See:http://documentation.kii.com/en/guides/rest/managing-data/object-storages/uploading/
         this.ExecSendRequest(new ProgressCallbackHelper(progressCallback), callback);
     });
 }
コード例 #6
0
        private void ExecSendRequestForDownload(Stream outStream, ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            if (callback == null)
            {
                callback(null, new ArgumentNullException("callback is null"));
                return;
            }
            if (outStream == null)
            {
                callback(null, new ArgumentNullException("outStream is null"));
                return;
            }
            float timeout = Time.realtimeSinceStartup + Timeout;

            this.SetHttpMethodOverride();
            this.SetSDKClientInfo();
            WWWRequestLooper.RunOnMainThread(() => {
                WWWRequestLooper.RegisterNetworkRequest(new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary()), progressCallback, (WWW www, ProgressCallbackHelper progress) => {
                    if (!www.isDone)
                    {
                        if (timeout < Time.realtimeSinceStartup)
                        {
                            DisposeWWW(www);
                            callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                            return(true);
                        }
                        if (progress != null)
                        {
                            progress.NotifyDownloadProgress(www);
                        }
                        return(false);
                    }
                    else
                    {
                        try
                        {
                            Exception e = this.CheckHttpError(www);
                            if (e != null)
                            {
                                callback(null, e);
                                return(true);
                            }
                            ApiResponse response = new ApiResponse();
                            Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
                            this.CopyHttpHeaders(responseHeaders, response);
                            response.Status      = WWWUtils.GetStatusCode(responseHeaders, www.bytes == null ? 204 : 200);
                            response.ContentType = WWWUtils.GetHeader(responseHeaders, "Content-Type");
                            response.ETag        = WWWUtils.GetHeader(responseHeaders, "ETag");

                            response.Body = "";
                            if (www.bytes != null)
                            {
                                BinaryWriter writer = new BinaryWriter(outStream);
                                writer.Write(www.bytes);
                            }
                            callback(response, null);
                            return(true);
                        }
                        catch (Exception e)
                        {
                            callback(null, e);
                            return(true);
                        }
                        finally
                        {
                            DisposeWWW(www);
                        }
                    }
                });
            });
        }