2017-02-07 04:59:30 +00:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2016-11-30 06:15:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-11-30 07:54:15 +00:00
|
|
|
|
using System.Diagnostics;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
2016-09-27 10:22:02 +00:00
|
|
|
|
using osu.Framework;
|
2016-12-01 07:44:24 +00:00
|
|
|
|
using osu.Framework.Configuration;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
using osu.Framework.Logging;
|
2016-09-27 10:22:02 +00:00
|
|
|
|
using osu.Framework.Threading;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
using osu.Game.Online.API.Requests;
|
2017-03-27 15:04:07 +00:00
|
|
|
|
using osu.Game.Users;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
2016-09-27 10:22:02 +00:00
|
|
|
|
public class APIAccess : IUpdateable
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly OAuth authentication;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2017-05-16 12:37:55 +00:00
|
|
|
|
public string Endpoint = @"https://osu.ppy.sh";
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private const string client_id = @"5";
|
|
|
|
|
private const string client_secret = @"FGc9GAtyHzeQDshWP5Ah7dega8hJACAJpQtw6OXk";
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private ConcurrentQueue<APIRequest> queue = new ConcurrentQueue<APIRequest>();
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2016-09-27 10:22:02 +00:00
|
|
|
|
public Scheduler Scheduler = new Scheduler();
|
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
public string Username;
|
|
|
|
|
|
2016-11-30 10:22:36 +00:00
|
|
|
|
//private SecurePassword password;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2016-11-30 10:22:36 +00:00
|
|
|
|
public string Password;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2017-05-16 11:08:22 +00:00
|
|
|
|
public Bindable<User> LocalUser = new Bindable<User>(createGuestUser());
|
2016-12-01 07:44:24 +00:00
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
public string Token
|
|
|
|
|
{
|
|
|
|
|
get { return authentication.Token?.ToString(); }
|
2017-03-07 01:59:19 +00:00
|
|
|
|
set { authentication.Token = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value); }
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 07:07:34 +00:00
|
|
|
|
protected bool HasLogin => Token != null || !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password);
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2017-03-07 01:59:19 +00:00
|
|
|
|
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable (should dispose of this or at very least keep a reference).
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly Thread thread;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly Logger log;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public APIAccess()
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
2017-02-07 07:15:45 +00:00
|
|
|
|
authentication = new OAuth(client_id, client_secret, Endpoint);
|
2016-08-31 10:49:34 +00:00
|
|
|
|
log = Logger.GetLogger(LoggingTarget.Network);
|
|
|
|
|
|
|
|
|
|
thread = new Thread(run) { IsBackground = true };
|
|
|
|
|
thread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
2016-11-30 06:15:07 +00:00
|
|
|
|
|
|
|
|
|
public void Register(IOnlineComponent component)
|
|
|
|
|
{
|
2016-11-30 10:22:36 +00:00
|
|
|
|
Scheduler.Add(delegate
|
|
|
|
|
{
|
|
|
|
|
components.Add(component);
|
|
|
|
|
component.APIStateChanged(this, state);
|
|
|
|
|
});
|
2016-11-30 06:15:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unregister(IOnlineComponent component)
|
|
|
|
|
{
|
2016-11-30 10:22:36 +00:00
|
|
|
|
Scheduler.Add(delegate
|
|
|
|
|
{
|
|
|
|
|
components.Remove(component);
|
|
|
|
|
});
|
2016-11-30 06:15:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public string AccessToken => authentication.RequestAccessToken();
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of consecutive requests which failed due to network issues.
|
|
|
|
|
/// </summary>
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private int failureCount;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
|
|
|
|
private void run()
|
|
|
|
|
{
|
2017-03-07 01:59:19 +00:00
|
|
|
|
while (thread.IsAlive)
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
switch (State)
|
|
|
|
|
{
|
|
|
|
|
case APIState.Failing:
|
|
|
|
|
//todo: replace this with a ping request.
|
2017-03-07 01:59:19 +00:00
|
|
|
|
log.Add(@"In a failing state, waiting a bit before we try again...");
|
2016-08-31 10:49:34 +00:00
|
|
|
|
Thread.Sleep(5000);
|
|
|
|
|
if (queue.Count == 0)
|
|
|
|
|
{
|
2017-03-07 01:59:19 +00:00
|
|
|
|
log.Add(@"Queueing a ping request");
|
|
|
|
|
Queue(new ListChannelsRequest { Timeout = 5000 });
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case APIState.Offline:
|
|
|
|
|
//work to restore a connection...
|
|
|
|
|
if (!HasLogin)
|
|
|
|
|
{
|
|
|
|
|
//OsuGame.Scheduler.Add(() => { OsuGame.ShowLogin(); });
|
|
|
|
|
|
|
|
|
|
State = APIState.Offline;
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (State < APIState.Connecting)
|
|
|
|
|
State = APIState.Connecting;
|
|
|
|
|
|
2016-11-30 10:22:36 +00:00
|
|
|
|
if (!authentication.HasValidAccessToken && !authentication.AuthenticateWithLogin(Username, Password))
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
//todo: this fails even on network-related issues. we should probably handle those differently.
|
|
|
|
|
//NotificationManager.ShowMessage("Login failed!");
|
|
|
|
|
log.Add(@"Login failed!");
|
2017-03-27 15:03:09 +00:00
|
|
|
|
Password = null;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 07:44:24 +00:00
|
|
|
|
|
|
|
|
|
var userReq = new GetUserRequest();
|
2017-03-07 01:59:19 +00:00
|
|
|
|
userReq.Success += u => {
|
2016-12-01 07:44:24 +00:00
|
|
|
|
LocalUser.Value = u;
|
2017-02-07 11:28:50 +00:00
|
|
|
|
//we're connected!
|
|
|
|
|
State = APIState.Online;
|
|
|
|
|
failureCount = 0;
|
2016-12-01 07:44:24 +00:00
|
|
|
|
};
|
2017-02-07 11:28:50 +00:00
|
|
|
|
|
2016-12-01 07:44:24 +00:00
|
|
|
|
if (!handleRequest(userReq))
|
|
|
|
|
{
|
|
|
|
|
State = APIState.Failing;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-08-31 10:49:34 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//hard bail if we can't get a valid access token.
|
|
|
|
|
if (authentication.RequestAccessToken() == null)
|
|
|
|
|
{
|
|
|
|
|
State = APIState.Offline;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//process the request queue.
|
|
|
|
|
APIRequest req;
|
|
|
|
|
while (queue.TryPeek(out req))
|
|
|
|
|
{
|
|
|
|
|
if (handleRequest(req))
|
|
|
|
|
{
|
|
|
|
|
//we have succeeded, so let's unqueue.
|
|
|
|
|
queue.TryDequeue(out req);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 07:15:45 +00:00
|
|
|
|
private void clearCredentials()
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
Username = null;
|
2016-11-30 10:22:36 +00:00
|
|
|
|
Password = null;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 07:54:15 +00:00
|
|
|
|
public void Login(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(State == APIState.Offline);
|
|
|
|
|
|
|
|
|
|
Username = username;
|
|
|
|
|
Password = password;
|
|
|
|
|
|
|
|
|
|
State = APIState.Connecting;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handle a single API request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="req">The request.</param>
|
|
|
|
|
/// <returns>true if we should remove this request from the queue.</returns>
|
|
|
|
|
private bool handleRequest(APIRequest req)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-30 07:54:15 +00:00
|
|
|
|
Logger.Log($@"Performing request {req}", LoggingTarget.Network);
|
2016-08-31 10:49:34 +00:00
|
|
|
|
req.Perform(this);
|
|
|
|
|
|
2017-02-22 05:09:48 +00:00
|
|
|
|
//we could still be in initialisation, at which point we don't want to say we're Online yet.
|
2017-05-16 10:55:45 +00:00
|
|
|
|
if (IsLoggedIn)
|
2017-02-22 05:09:48 +00:00
|
|
|
|
State = APIState.Online;
|
|
|
|
|
|
2016-08-31 10:49:34 +00:00
|
|
|
|
failureCount = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (WebException we)
|
|
|
|
|
{
|
|
|
|
|
HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode ?? HttpStatusCode.RequestTimeout;
|
|
|
|
|
|
|
|
|
|
switch (statusCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.Unauthorized:
|
|
|
|
|
State = APIState.Offline;
|
|
|
|
|
return true;
|
|
|
|
|
case HttpStatusCode.RequestTimeout:
|
|
|
|
|
failureCount++;
|
|
|
|
|
log.Add($@"API failure count is now {failureCount}");
|
|
|
|
|
|
|
|
|
|
if (failureCount < 3)
|
|
|
|
|
//we might try again at an api level.
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
State = APIState.Failing;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Fail(we);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (e is TimeoutException)
|
|
|
|
|
log.Add(@"API level timeout exception was hit");
|
|
|
|
|
|
|
|
|
|
req.Fail(e);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private APIState state;
|
|
|
|
|
public APIState State
|
|
|
|
|
{
|
|
|
|
|
get { return state; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
APIState oldState = state;
|
|
|
|
|
APIState newState = value;
|
|
|
|
|
|
|
|
|
|
state = value;
|
|
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case APIState.Failing:
|
|
|
|
|
case APIState.Offline:
|
|
|
|
|
flushQueue();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (oldState != newState)
|
|
|
|
|
{
|
|
|
|
|
//OsuGame.Scheduler.Add(delegate
|
|
|
|
|
{
|
|
|
|
|
//NotificationManager.ShowMessage($@"We just went {newState}!", newState == APIState.Online ? Color4.YellowGreen : Color4.OrangeRed, 5000);
|
|
|
|
|
log.Add($@"We just went {newState}!");
|
2016-10-13 01:10:15 +00:00
|
|
|
|
Scheduler.Add(delegate
|
|
|
|
|
{
|
2016-11-30 06:15:07 +00:00
|
|
|
|
components.ForEach(c => c.APIStateChanged(this, newState));
|
2016-10-13 01:10:15 +00:00
|
|
|
|
OnStateChange?.Invoke(oldState, newState);
|
|
|
|
|
});
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 10:55:45 +00:00
|
|
|
|
public bool IsLoggedIn => LocalUser.Value.Id > 1;
|
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public void Queue(APIRequest request)
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
queue.Enqueue(request);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public event StateChangeDelegate OnStateChange;
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public delegate void StateChangeDelegate(APIState oldState, APIState newState);
|
2016-08-31 10:49:34 +00:00
|
|
|
|
|
|
|
|
|
private void flushQueue(bool failOldRequests = true)
|
|
|
|
|
{
|
|
|
|
|
var oldQueue = queue;
|
|
|
|
|
|
|
|
|
|
//flush the queue.
|
|
|
|
|
queue = new ConcurrentQueue<APIRequest>();
|
|
|
|
|
|
|
|
|
|
if (failOldRequests)
|
|
|
|
|
{
|
|
|
|
|
APIRequest req;
|
2017-03-07 01:59:19 +00:00
|
|
|
|
while (oldQueue.TryDequeue(out req))
|
2016-08-31 10:49:34 +00:00
|
|
|
|
req.Fail(new Exception(@"Disconnected from server"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-27 09:31:36 +00:00
|
|
|
|
public void Logout()
|
2016-08-31 10:49:34 +00:00
|
|
|
|
{
|
2017-02-07 07:15:45 +00:00
|
|
|
|
clearCredentials();
|
2016-08-31 10:49:34 +00:00
|
|
|
|
authentication.Clear();
|
|
|
|
|
State = APIState.Offline;
|
2017-05-16 11:08:22 +00:00
|
|
|
|
LocalUser.Value = createGuestUser();
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
2016-09-27 10:22:02 +00:00
|
|
|
|
|
2017-05-16 11:08:22 +00:00
|
|
|
|
private static User createGuestUser() => new User
|
2017-05-16 10:49:50 +00:00
|
|
|
|
{
|
|
|
|
|
Username = @"Guest",
|
|
|
|
|
Id = 1,
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-27 10:22:02 +00:00
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
Scheduler.Update();
|
|
|
|
|
}
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|
2016-11-30 06:15:07 +00:00
|
|
|
|
|
|
|
|
|
public enum APIState
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We cannot login (not enough credentials).
|
|
|
|
|
/// </summary>
|
|
|
|
|
Offline,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We are having connectivity issues.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Failing,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We are in the process of (re-)connecting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Connecting,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// We are online.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Online
|
|
|
|
|
}
|
2016-08-31 10:49:34 +00:00
|
|
|
|
}
|