From 13d397a0dc19930795ea15ccfdc7f6a6fba93634 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 30 Nov 2015 03:46:25 -0400 Subject: [PATCH] Added opcode enums --- .../Net/WebSockets/DataWebSocket.cs | 28 ++++++++++++++----- .../Net/WebSockets/VoiceWebSocket.cs | 23 +++++++++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Discord.Net/Net/WebSockets/DataWebSocket.cs b/src/Discord.Net/Net/WebSockets/DataWebSocket.cs index 659cb26a0..32fff5251 100644 --- a/src/Discord.Net/Net/WebSockets/DataWebSocket.cs +++ b/src/Discord.Net/Net/WebSockets/DataWebSocket.cs @@ -7,8 +7,21 @@ using System.Threading.Tasks; namespace Discord.Net.WebSockets { internal partial class DataWebSocket : WebSocket - { - private int _lastSeq; + { + public enum OpCodes : byte + { + Dispatch = 0, + Heartbeat = 1, + Identify = 2, + StatusUpdate = 3, + VoiceStateUpdate = 4, + VoiceServerPing = 5, + Resume = 6, + Redirect = 7, + RequestGuildMembers = 8 + } + + private int _lastSeq; public string SessionId => _sessionId; private string _sessionId; @@ -74,10 +87,11 @@ namespace Discord.Net.WebSockets var msg = JsonConvert.DeserializeObject(json); if (msg.Sequence.HasValue) _lastSeq = msg.Sequence.Value; - - switch (msg.Operation) + + var opCode = (OpCodes)msg.Operation; + switch (opCode) { - case 0: + case OpCodes.Dispatch: { JToken token = msg.Payload as JToken; if (msg.Type == "READY") @@ -96,7 +110,7 @@ namespace Discord.Net.WebSockets EndConnect(); } break; - case 7: //Redirect + case OpCodes.Redirect: { var payload = (msg.Payload as JToken).ToObject(_client.DataSocketSerializer); if (payload.Url != null) @@ -110,7 +124,7 @@ namespace Discord.Net.WebSockets break; default: if (_logLevel >= LogMessageSeverity.Warning) - RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {msg.Operation}"); + RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); break; } } diff --git a/src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs b/src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs index 37da540b8..23dd5d2a5 100644 --- a/src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs +++ b/src/Discord.Net/Net/WebSockets/VoiceWebSocket.cs @@ -17,6 +17,16 @@ namespace Discord.Net.WebSockets { internal partial class VoiceWebSocket : WebSocket { + public enum OpCodes : byte + { + Identify = 0, + SelectProtocol = 1, + Ready = 2, + Heartbeat = 3, + SessionDescription = 4, + Speaking = 5 + } + private const int MaxOpusSize = 4000; private const string EncryptedMode = "xsalsa20_poly1305"; private const string UnencryptedMode = "plain"; @@ -437,9 +447,10 @@ namespace Discord.Net.WebSockets { await base.ProcessMessage(json).ConfigureAwait(false); var msg = JsonConvert.DeserializeObject(json); - switch (msg.Operation) + var opCode = (OpCodes)msg.Operation; + switch (opCode) { - case 2: //READY + case OpCodes.Ready: { if (_state != (int)WebSocketState.Connected) { @@ -476,7 +487,7 @@ namespace Discord.Net.WebSockets } } break; - case 3: //PONG + case OpCodes.Heartbeat: { long time = EpochTime.GetMilliseconds(); var payload = (long)msg.Payload; @@ -484,7 +495,7 @@ namespace Discord.Net.WebSockets //TODO: Use this to estimate latency } break; - case 4: //SESSION_DESCRIPTION + case OpCodes.SessionDescription: { var payload = (msg.Payload as JToken).ToObject(_client.VoiceSocketSerializer); _secretKey = payload.SecretKey; @@ -492,7 +503,7 @@ namespace Discord.Net.WebSockets EndConnect(); } break; - case 5: + case OpCodes.Speaking: { var payload = (msg.Payload as JToken).ToObject(_client.VoiceSocketSerializer); RaiseIsSpeaking(payload.UserId, payload.IsSpeaking); @@ -500,7 +511,7 @@ namespace Discord.Net.WebSockets break; default: if (_logLevel >= LogMessageSeverity.Warning) - RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {msg.Operation}"); + RaiseOnLog(LogMessageSeverity.Warning, $"Unknown Opcode: {opCode}"); break; } }