| @@ -92,7 +92,7 @@ namespace Discord | |||||
| _connectedEvent = new ManualResetEventSlim(false); | _connectedEvent = new ManualResetEventSlim(false); | ||||
| _rand = new Random(); | _rand = new Random(); | ||||
| _api = new DiscordAPIClient(_config.LogLevel); | |||||
| _api = new DiscordAPIClient(_config.LogLevel, _config.APITimeout); | |||||
| _dataSocket = new DataWebSocket(this); | _dataSocket = new DataWebSocket(this); | ||||
| _dataSocket.Connected += (s, e) => { if (_state == (int)DiscordClientState.Connecting) CompleteConnect(); }; | _dataSocket.Connected += (s, e) => { if (_state == (int)DiscordClientState.Connecting) CompleteConnect(); }; | ||||
| _dataSocket.Disconnected += async (s, e) => { RaiseDisconnected(e); if (e.WasUnexpected) await _dataSocket.Login(_token); }; | _dataSocket.Disconnected += async (s, e) => { RaiseDisconnected(e); if (e.WasUnexpected) await _dataSocket.Login(_token); }; | ||||
| @@ -17,6 +17,9 @@ namespace Discord | |||||
| /// <summary> Gets or sets the time (in milliseconds) to wait after an reconnect fails before retrying. </summary> | /// <summary> Gets or sets the time (in milliseconds) to wait after an reconnect fails before retrying. </summary> | ||||
| public int FailedReconnectDelay { get { return _failedReconnectDelay; } set { SetValue(ref _failedReconnectDelay, value); } } | public int FailedReconnectDelay { get { return _failedReconnectDelay; } set { SetValue(ref _failedReconnectDelay, value); } } | ||||
| private int _failedReconnectDelay = 10000; | private int _failedReconnectDelay = 10000; | ||||
| /// <summary> Max time (in milliseconds) to wait for an API request to complete. </summary> | |||||
| public int APITimeout { get { return _apiTimeout; } set { SetValue(ref _apiTimeout, value); } } | |||||
| private int _apiTimeout = 5000; | |||||
| /// <summary> Gets or sets the time (in milliseconds) to wait when the websocket's message queue is empty before checking again. </summary> | /// <summary> Gets or sets the time (in milliseconds) to wait when the websocket's message queue is empty before checking again. </summary> | ||||
| public int WebSocketInterval { get { return _webSocketInterval; } set { SetValue(ref _webSocketInterval, value); } } | public int WebSocketInterval { get { return _webSocketInterval; } set { SetValue(ref _webSocketInterval, value); } } | ||||
| @@ -11,9 +11,9 @@ namespace Discord.Net.API | |||||
| public RestClient RestClient => _rest; | public RestClient RestClient => _rest; | ||||
| private readonly RestClient _rest; | private readonly RestClient _rest; | ||||
| public DiscordAPIClient(LogMessageSeverity logLevel) | |||||
| public DiscordAPIClient(LogMessageSeverity logLevel, int timeout) | |||||
| { | { | ||||
| _rest = new RestClient(logLevel); | |||||
| _rest = new RestClient(logLevel, timeout); | |||||
| } | } | ||||
| private string _token; | private string _token; | ||||
| @@ -14,7 +14,7 @@ namespace Discord.Net.API | |||||
| { | { | ||||
| private readonly HttpClient _client; | private readonly HttpClient _client; | ||||
| public BuiltInRestEngine(string userAgent) | |||||
| public BuiltInRestEngine(string userAgent, int timeout) | |||||
| { | { | ||||
| _client = new HttpClient(new HttpClientHandler | _client = new HttpClient(new HttpClientHandler | ||||
| { | { | ||||
| @@ -25,6 +25,7 @@ namespace Discord.Net.API | |||||
| _client.DefaultRequestHeaders.Add("accept", "*/*"); | _client.DefaultRequestHeaders.Add("accept", "*/*"); | ||||
| _client.DefaultRequestHeaders.Add("accept-encoding", "gzip,deflate"); | _client.DefaultRequestHeaders.Add("accept-encoding", "gzip,deflate"); | ||||
| _client.DefaultRequestHeaders.Add("user-agent", userAgent); | _client.DefaultRequestHeaders.Add("user-agent", userAgent); | ||||
| _client.Timeout = TimeSpan.FromMilliseconds(timeout); | |||||
| } | } | ||||
| public void SetToken(string token) | public void SetToken(string token) | ||||
| @@ -12,7 +12,7 @@ namespace Discord.Net.API | |||||
| { | { | ||||
| private readonly RestSharp.RestClient _client; | private readonly RestSharp.RestClient _client; | ||||
| public RestSharpRestEngine(string userAgent) | |||||
| public RestSharpRestEngine(string userAgent, int timeout) | |||||
| { | { | ||||
| _client = new RestSharp.RestClient(Endpoints.BaseApi) | _client = new RestSharp.RestClient(Endpoints.BaseApi) | ||||
| { | { | ||||
| @@ -20,7 +20,8 @@ namespace Discord.Net.API | |||||
| }; | }; | ||||
| _client.AddDefaultHeader("accept", "*/*"); | _client.AddDefaultHeader("accept", "*/*"); | ||||
| _client.AddDefaultHeader("accept-encoding", "gzip,deflate"); | _client.AddDefaultHeader("accept-encoding", "gzip,deflate"); | ||||
| _client.UserAgent = userAgent; | |||||
| _client.UserAgent = userAgent; | |||||
| _client.ReadWriteTimeout = timeout; | |||||
| } | } | ||||
| public void SetToken(string token) | public void SetToken(string token) | ||||
| @@ -21,16 +21,16 @@ namespace Discord.Net.API | |||||
| private readonly LogMessageSeverity _logLevel; | private readonly LogMessageSeverity _logLevel; | ||||
| private CancellationToken _cancelToken; | private CancellationToken _cancelToken; | ||||
| public RestClient(LogMessageSeverity logLevel) | |||||
| public RestClient(LogMessageSeverity logLevel, int timeout) | |||||
| { | { | ||||
| _logLevel = logLevel; | _logLevel = logLevel; | ||||
| string version = typeof(RestClient).GetTypeInfo().Assembly.GetName().Version.ToString(2); | string version = typeof(RestClient).GetTypeInfo().Assembly.GetName().Version.ToString(2); | ||||
| string userAgent = $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)"; | string userAgent = $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)"; | ||||
| #if DNXCORE50 | #if DNXCORE50 | ||||
| _engine = new BuiltInRestEngine(userAgent); | |||||
| _engine = new BuiltInRestEngine(userAgent, timeout); | |||||
| #else | #else | ||||
| _engine = new RestSharpRestEngine(userAgent); | |||||
| _engine = new RestSharpRestEngine(userAgent, timeout); | |||||
| #endif | #endif | ||||
| } | } | ||||