diff --git a/OpenPlayerIO/QuickConnect.cs b/OpenPlayerIO/QuickConnect.cs index beaa459..285d827 100644 --- a/OpenPlayerIO/QuickConnect.cs +++ b/OpenPlayerIO/QuickConnect.cs @@ -313,90 +313,70 @@ public bool SimpleChangePasswordByEmail(string gameId, string connectionId, stri #region Authenticate internal bool SimpleChangePassword(string gameId, string connectionId, string connectionType, string currentUsernameOrEmail, string currentPassword, string newPassword, out string requestResponse) { - var requestFinished = false; - var changeSuccessful = false; - var playerIOError = default(PlayerIOError); - - _channel.Request(13, new AuthenticateArgs + using (var mre = new ManualResetEvent(false)) { - GameId = gameId, - ClientAPI = PlayerIO.GetClientAPI(), - ConnectionId = connectionId, - AuthenticationArguments = new KeyValuePair[] { + var changeSuccessful = false; + var playerIOError = default(PlayerIOError); + + _channel.Request(13, new AuthenticateArgs { + GameId = gameId, + ClientAPI = PlayerIO.GetClientAPI(), + ConnectionId = connectionId, + AuthenticationArguments = new KeyValuePair[] { new KeyValuePair() { Key = connectionType, Value = currentUsernameOrEmail }, new KeyValuePair() { Key = "password", Value = currentPassword }, new KeyValuePair() { Key = "changepassword", Value = "true" }, new KeyValuePair() { Key = "newpassword", Value = newPassword } } - }, new Callback((error) => { - if (error.ErrorCode == ErrorCode.GeneralError && error.Message.ToLower().Contains("email address changed")) - changeSuccessful = true; - - playerIOError = error; - requestFinished = true; - })); - - // this is really sloppy and could be improved, but it's quick and easy... - // and the same description would generally apply to Player.IO as a whole :wink: - // - atillabyte - - requestResponse = playerIOError?.Message ?? ""; + }, new Callback((error) => { + if (error.ErrorCode == ErrorCode.GeneralError && error.Message.ToLower().Contains("email address changed")) + changeSuccessful = true; - var requestTimeout = 0; - while (!requestFinished && ++requestTimeout <= 10000 / 100) - Thread.Sleep(100); + playerIOError = error; + mre.Set(); + })); - if (!requestFinished) - throw new Exception("The email change request timed out without returning a response."); + requestResponse = playerIOError?.Message ?? ""; - if (requestFinished && changeSuccessful) - return true; + if (!mre.WaitOne(TimeSpan.FromMilliseconds(10_000))) + throw new Exception("The email change request timed out without returning a response."); - return false; + return changeSuccessful; + } } internal bool SimpleChangeEmail(string gameId, string connectionId, string connectionType, string currentUsernameOrEmail, string currentPassword, string newEmail, out string requestResponse) { - var requestFinished = false; - var changeSuccessful = false; - var playerIOError = default(PlayerIOError); - - _channel.Request(13, new AuthenticateArgs + using (var mre = new ManualResetEvent(false)) { - GameId = gameId, - ClientAPI = PlayerIO.GetClientAPI(), - ConnectionId = connectionId, - AuthenticationArguments = new KeyValuePair[] { + var changeSuccessful = false; + var playerIOError = default(PlayerIOError); + + _channel.Request(13, new AuthenticateArgs { + GameId = gameId, + ClientAPI = PlayerIO.GetClientAPI(), + ConnectionId = connectionId, + AuthenticationArguments = new KeyValuePair[] { new KeyValuePair() { Key = connectionType, Value = currentUsernameOrEmail }, new KeyValuePair() { Key = "password", Value = currentPassword }, new KeyValuePair() { Key = "changeemail", Value = "true" }, new KeyValuePair() { Key = "newemail", Value = newEmail } } - }, new Callback((error) => { - if (error.ErrorCode == ErrorCode.GeneralError && error.Message.ToLower().Contains("email address changed")) - changeSuccessful = true; - - playerIOError = error; - requestFinished = true; - })); - - // this is really sloppy and could be improved, but it's quick and easy... - // and the same description would generally apply to Player.IO as a whole :wink: - // - atillabyte - - requestResponse = playerIOError?.Message ?? ""; + }, new Callback((error) => { + if (error.ErrorCode == ErrorCode.GeneralError && error.Message.ToLower().Contains("email address changed")) + changeSuccessful = true; - var requestTimeout = 0; - while (!requestFinished && ++requestTimeout <= 10000/100) - Thread.Sleep(100); + playerIOError = error; + mre.Set(); + })); - if (!requestFinished) - throw new Exception("The email change request timed out without returning a response."); + requestResponse = playerIOError?.Message ?? ""; - if (requestFinished && changeSuccessful) - return true; + if (!mre.WaitOne(TimeSpan.FromMilliseconds(10_000))) + throw new Exception("The email change request timed out without returning a response."); - return false; + return changeSuccessful; + } } #endregion