Ejemplo n.º 1
0
        private IEnumerator HttpPost(string connectionId, string url, object data, Action <string, object> succeeded, Action <string, int, string> failed)
        {
            var header = this.httpRequestHeaderDelegate("POST", url, new Dictionary <string, string>(), data);

            Action <string, object> onSucceeded = (conId, result) =>
            {
                succeeded(conId, result);
            };

            // send string or byte[] to server via http.
            if (data is string)
            {
                return(http.Post(
                           connectionId,
                           header,
                           url,
                           (string)data,
                           (string conId, int code, Dictionary <string, string> respHeaders, string result) =>
                {
                    httpResponseHandlingDelegate(conId, respHeaders, code, result, string.Empty, onSucceeded, failed);
                },
                           (conId, code, reason, respHeaders) =>
                {
                    httpResponseHandlingDelegate(conId, respHeaders, code, string.Empty, reason, onSucceeded, failed);
                },
                           PurchaseSettings.TIMEOUT_SEC
                           ));
            }

            if (data is byte[])
            {
                return(http.Post(
                           connectionId,
                           header,
                           url,
                           (byte[])data,
                           (string conId, int code, Dictionary <string, string> respHeaders, byte[] result) =>
                {
                    httpResponseHandlingDelegate(conId, respHeaders, code, result, string.Empty, onSucceeded, failed);
                },
                           (conId, code, reason, respHeaders) =>
                {
                    httpResponseHandlingDelegate(conId, respHeaders, code, string.Empty, reason, onSucceeded, failed);
                },
                           PurchaseSettings.TIMEOUT_SEC
                           ));
            }

            throw new Exception("should set string or byte[] to data.");
        }
Ejemplo n.º 2
0
        private IEnumerator HttpPost(string connectionId, string url, string data, Action <string, string> succeeded, Action <string, int, string> failed)
        {
            var header = this.httpRequestHeaderDelegate("POST", url, new Dictionary <string, string>(), data);

            Action <string, object> onSucceeded = (conId, result) =>
            {
                succeeded(conId, result as string);
            };

            return(http.Post(
                       connectionId,
                       header,
                       url,
                       data,
                       (string conId, int code, Dictionary <string, string> respHeaders, string result) =>
            {
                httpResponseHandlingDelegate(conId, respHeaders, code, result, string.Empty, onSucceeded, failed);
            },
                       (conId, code, reason, respHeaders) =>
            {
                httpResponseHandlingDelegate(conId, respHeaders, code, string.Empty, reason, onSucceeded, failed);
            },
                       PurchaseSettings.TIMEOUT_SEC
                       ));
        }
            private IEnumerator TokenRefreshRetryCoroutine()
            {
                // wait 2, 4, 8... sec.
                var refreshTokenRetryWait = Math.Pow(2, tokenRefreshRetryCount);

                if (forceFailTokenRefresh)
                {
                    // set 0sec for shortcut.
                    refreshTokenRetryWait = 0;
                }

                var limitTick = DateTime.Now.Ticks + TimeSpan.FromSeconds(refreshTokenRetryWait).Ticks;

                while (DateTime.Now.Ticks < limitTick)
                {
                    yield return(null);
                }

                // start refreshing token again.
                var refresingTokenHttp = new HTTPConnection();
                var refreshTokenUrl    = AuthSettings.AUTH_URL_REFRESH_TOKEN;

                var refreshTokenConnectionId = AuthSettings.AUTH_CONNECTIONID_REFRESH_TOKEN_PREFIX + Guid.NewGuid().ToString();

                Dictionary <string, string> refreshRequestHeader = null;
                var refreshTokenData = string.Empty;
                Action <Dictionary <string, string>, string> refreshRequestHeaderLoaded = (requestHeader, data) =>
                {
                    refreshRequestHeader = requestHeader;
                    refreshTokenData     = data;
                };

                var authKeyLoadCor = autoya.OnTokenRefreshRequest(refreshRequestHeaderLoaded);

                while (authKeyLoadCor.MoveNext())
                {
                    yield return(null);
                }

                if (string.IsNullOrEmpty(refreshTokenData))
                {
                    var cor = refresingTokenHttp.Get(
                        refreshTokenConnectionId,
                        refreshRequestHeader,
                        refreshTokenUrl,
                        (conId, code, responseHeader, data) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
                else
                {
                    var cor = refresingTokenHttp.Post(
                        refreshTokenConnectionId,
                        refreshRequestHeader,
                        refreshTokenUrl,
                        refreshTokenData,
                        (conId, code, responseHeader, data) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
            }
            private IEnumerator RefreshToken()
            {
                // start refreshing token.
                var refresingTokenHttp = new HTTPConnection();
                var refreshTokenUrl    = AuthSettings.AUTH_URL_REFRESH_TOKEN;

                var refreshTokenConnectionId = AuthSettings.AUTH_CONNECTIONID_REFRESH_TOKEN_PREFIX + Guid.NewGuid().ToString();

                Dictionary <string, string> refreshRequestHeader = null;
                var refreshTokenData = string.Empty;
                Action <Dictionary <string, string>, string> refreshRequestHeaderLoaded = (requestHeader, data) =>
                {
                    refreshRequestHeader = requestHeader;
                    refreshTokenData     = data;
                };

                var authKeyLoadCor = autoya.OnTokenRefreshRequest(refreshRequestHeaderLoaded);

                while (authKeyLoadCor.MoveNext())
                {
                    yield return(null);
                }

                if (string.IsNullOrEmpty(refreshTokenData))
                {
                    var refreshingTokenCor = refresingTokenHttp.Get(
                        refreshTokenConnectionId,
                        refreshRequestHeader,
                        refreshTokenUrl,
                        (conId, code, responseHeader, data) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (refreshingTokenCor.MoveNext())
                    {
                        yield return(null);
                    }
                }
                else
                {
                    var refreshingTokenCor = refresingTokenHttp.Post(
                        refreshTokenConnectionId,
                        refreshRequestHeader,
                        refreshTokenUrl,
                        refreshTokenData,
                        (conId, code, responseHeader, data) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnRefreshResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (refreshingTokenCor.MoveNext())
                    {
                        yield return(null);
                    }
                }
            }
            private IEnumerator BootRetryCoroutine()
            {
                // wait 2, 4, 8... sec.
                var bootRetryWait = Math.Pow(2, bootRetryCount);

                if (forceFailFirstBoot)
                {
                    // set 0sec for shortcut.
                    bootRetryWait = 0;
                }

                var limitTick = DateTime.Now.Ticks + TimeSpan.FromSeconds(bootRetryWait).Ticks;

                while (DateTime.Now.Ticks < limitTick)
                {
                    yield return(null);
                }

                // start boot authentication again.
                var tokenHttp         = new HTTPConnection();
                var tokenConnectionId = AuthSettings.AUTH_CONNECTIONID_BOOT_PREFIX + Guid.NewGuid().ToString();

                var tokenUrl = AuthSettings.AUTH_URL_BOOT;

                Dictionary <string, string> tokenRequestHeader = null;
                var bootData = string.Empty;
                Action <Dictionary <string, string>, string> authRequestHeaderLoaded = (requestHeader, data) =>
                {
                    tokenRequestHeader = requestHeader;
                    bootData           = data;
                };
                var bootKeyLoadCor = autoya.OnBootAuthRequest(authRequestHeaderLoaded);

                while (bootKeyLoadCor.MoveNext())
                {
                    yield return(null);
                }
                if (string.IsNullOrEmpty(bootData))
                {
                    var cor = tokenHttp.Get(
                        tokenConnectionId,
                        tokenRequestHeader,
                        tokenUrl,
                        (conId, code, responseHeader, data) =>
                    {
                        OnBootResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnBootResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
                else
                {
                    var cor = tokenHttp.Post(
                        tokenConnectionId,
                        tokenRequestHeader,
                        tokenUrl,
                        bootData,
                        (conId, code, responseHeader, data) =>
                    {
                        OnBootResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnBootResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
            }
            private IEnumerator FirstBoot()
            {
                var tokenHttp         = new HTTPConnection();
                var tokenConnectionId = AuthSettings.AUTH_CONNECTIONID_BOOT_PREFIX + Guid.NewGuid().ToString();

                var tokenUrl = AuthSettings.AUTH_URL_BOOT;

                Dictionary <string, string> tokenRequestHeader = null;
                var bootData = string.Empty;
                Action <Dictionary <string, string>, string> authRequestHeaderLoaded = (requestHeader, data) =>
                {
                    tokenRequestHeader = requestHeader;
                    bootData           = data;
                };
                var bootKeyLoadCor = autoya.OnBootAuthRequest(authRequestHeaderLoaded);

                while (bootKeyLoadCor.MoveNext())
                {
                    yield return(null);
                }

                if (string.IsNullOrEmpty(bootData))
                {
                    var cor = tokenHttp.Get(
                        tokenConnectionId,
                        tokenRequestHeader,
                        tokenUrl,
                        (conId, code, responseHeader, data) =>
                    {
                        OnBootResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnBootResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
                else
                {
                    var cor = tokenHttp.Post(
                        tokenConnectionId,
                        tokenRequestHeader,
                        tokenUrl,
                        bootData,
                        (conId, code, responseHeader, data) =>
                    {
                        OnBootResult(conId, responseHeader, code, data, string.Empty);
                    },
                        (conId, code, failedReason, responseHeader) =>
                    {
                        OnBootResult(conId, responseHeader, code, string.Empty, failedReason);
                    },
                        BackyardSettings.HTTP_TIMEOUT_SEC
                        );

                    while (cor.MoveNext())
                    {
                        yield return(null);
                    }
                }
            }
    // Use this for initialization
    void Start()
    {
        if (_checkFinished == true)
        {
            //起動チェック済みだから気にしなくていいと思う
            return;
        }

        //端末固有番号を取得する。とは言えmacアドレス収集とかはセンシティブなので
        //本当はここを暗号化するとグッド!
        if (PlayerPrefs.HasKey("guid"))
        {
            myGuid = PlayerPrefs.GetString("guid");
        }
        else
        {
            //izmのMacBookAir__q(長い文字列)we9werfgw___a(長い文字列)erwereqwrq3 みたいなのが取得出来て、こいつをGUIDとする
            myGuid = SystemInfo.deviceName + "__" + SystemInfo.deviceUniqueIdentifier + "__" +
                     Guid.NewGuid().ToString();
            PlayerPrefs.SetString("guid", myGuid);
        }

        _connectionID = Guid.NewGuid().ToString();

        _connection = new HTTPConnection();
        //Getのサンプル、最初に導通を確認したい的な
        StartCoroutine(
            _connection.Get(_connectionID, null, url, (s, i, arg3, responseBody) =>
        {
            if (responseBody.Contains("available") == false)
            {
                Debug.LogAssertion("ウェブ側で起動制限を掛けてるよ~!!!");
                //Maybe Quit Application??
                //ここで、Getの文字列が規定じゃないってことはたぶん、開発者としてはアプリが流出してロックを掛けてるってことだから
                //アプリを終了させちゃっても良い気がする
                Application.Quit();
            }

            Debug.Log(responseBody);
        },
                            (s, i, arg3, arg4) =>
        {
            Debug.LogAssertion("http not connection");
            //Maybe Quit Application??
            //ここで、Getが通じないってことはたぶんオフラインか、リバースプロクシで妨害してるっぽいってことだから、
            //アプリを終了させちゃっても良い気がする
            Application.Quit();
        }, 10));


        //ポストする
        DeviceInfo info = new DeviceInfo()
        {
            deviceName   = SystemInfo.deviceName,
            guid         = myGuid,
            appName      = Application.productName,
            appVersion   = Application.version,
            optionString = "default value"
        };

        //ここでoptionに「初投稿です…ども…」とか入れる

        //Postして自分の端末情報をGoogle Spread Sheetに送る
        StartCoroutine(_connection.Post(_connectionID, null, url, JsonUtility.ToJson(info),
                                        (s, i, arg3, body) =>
        {
            _checkFinished = true;
            Debug.Log("post success: " + body);
        },
                                        (s, i, arg3, arg4) => { Debug.LogError("failed"); }, 5d));
    }