Browse Source

Adds sharding support

pull/288/head
Pat Murphy 8 years ago
parent
commit
8c1c16fe7b
4 changed files with 21 additions and 4 deletions
  1. +2
    -0
      src/Discord.Net/API/Client/GatewaySocket/Commands/Identify.cs
  2. +10
    -0
      src/Discord.Net/DiscordConfig.cs
  3. +3
    -0
      src/Discord.Net/Net/Rest/RestClient.cs
  4. +6
    -4
      src/Discord.Net/Net/WebSockets/GatewaySocket.cs

+ 2
- 0
src/Discord.Net/API/Client/GatewaySocket/Commands/Identify.cs View File

@@ -18,5 +18,7 @@ namespace Discord.API.Client.GatewaySocket
public int LargeThreshold { get; set; }
[JsonProperty("compress")]
public bool UseCompression { get; set; }
[JsonProperty("shard")]
public int[] ShardingParams { get; set; }
}
}

+ 10
- 0
src/Discord.Net/DiscordConfig.cs View File

@@ -47,6 +47,11 @@ namespace Discord
/// </summary>
public int LargeThreshold { get; set; } = 250;

/// <summary> Gets or sets the id for this shard. Must be less than TotalShards. </summary>
public int ShardId { get; set; } = 0;
/// <summary> Gets or sets the total number of shards for this application. </summary>
public int TotalShards { get; set; } = 1;

//Events

/// <summary> Gets or sets a handler for all log messages. </summary>
@@ -89,6 +94,9 @@ namespace Discord
public bool UsePermissionsCache { get; }
public bool EnablePreUpdateEvents { get; }

public int ShardId { get; }
public int TotalShards { get; }

internal DiscordConfig(DiscordConfigBuilder builder)
{
LogLevel = builder.LogLevel;
@@ -106,6 +114,8 @@ namespace Discord
MessageCacheSize = builder.MessageCacheSize;
UsePermissionsCache = builder.UsePermissionsCache;
EnablePreUpdateEvents = builder.EnablePreUpdateEvents;
ShardId = builder.ShardId;
TotalShards = builder.TotalShards;
}

private static string GetUserAgent(DiscordConfigBuilder builder)


+ 3
- 0
src/Discord.Net/Net/Rest/RestClient.cs View File

@@ -51,6 +51,9 @@ namespace Discord.Net.Rest
}
}

public int ShardId => _config.ShardId;
public int TotalShards => _config.TotalShards;

protected RestClient(DiscordConfig config, string baseUrl, ILogger logger = null)
{
_config = config;


+ 6
- 4
src/Discord.Net/Net/WebSockets/GatewaySocket.cs View File

@@ -50,7 +50,7 @@ namespace Discord.Net.WebSockets
Host = url;
await BeginConnect(parentCancelToken).ConfigureAwait(false);
if (SessionId == null)
SendIdentify(_rest.Token);
SendIdentify(_rest.Token, _rest.ShardId, _rest.TotalShards);
else
SendResume();
}
@@ -148,7 +148,7 @@ namespace Discord.Net.WebSockets
}
}

public void SendIdentify(string token)
public void SendIdentify(string token, int shardId = 0, int totalShards = 1)
{
var props = new Dictionary<string, string>
{
@@ -159,9 +159,11 @@ namespace Discord.Net.WebSockets
Token = token,
Properties = props,
LargeThreshold = _config.LargeThreshold,
UseCompression = true
UseCompression = true,
ShardingParams = new int[] { shardId, totalShards },
};
QueueMessage(msg);

QueueMessage(msg);
}

public void SendResume()


Loading…
Cancel
Save