1 /**
2   Implementations of packets sent over the Voice websocket.
3 */
4 module dscord.voice.packets;
5 
6 import std.stdio;
7 
8 import dscord.types,
9        dscord.gateway;
10 
11 enum VoiceOPCode : ushort {
12   VOICE_IDENTIFY = 0,
13   VOICE_SELECT_PROTOCOL = 1,
14   VOICE_READY = 2,
15   VOICE_HEARTBEAT = 3,
16   VOICE_SESSION_DESCRIPTION = 4,
17   VOICE_SPEAKING = 5,
18   VOICE_HEARTBEAT_ACK = 6,
19   VOICE_RESUME = 7,
20   VOICE_HELLO = 8,
21   VOICE_RESUMED = 9,
22 }
23 
24 class VoiceIdentifyPacket : BasePacket, Serializable {
25   Snowflake  serverID;
26   Snowflake  userID;
27   string     sessionID;
28   string     token;
29 
30   this(Snowflake server, Snowflake user, string session, string token) {
31     this.serverID = server;
32     this.userID = user;
33     this.sessionID = session;
34     this.token = token;
35   }
36 
37   override VibeJSON serialize() {
38     return super.serialize(VoiceOPCode.VOICE_IDENTIFY, VibeJSON([
39       "server_id": VibeJSON(this.serverID),
40       "user_id": VibeJSON(this.userID),
41       "session_id": VibeJSON(this.sessionID),
42       "token": VibeJSON(this.token),
43     ]));
44   }
45 }
46 
47 class VoiceReadyPacket : BasePacket, Deserializable {
48   ushort    ssrc;
49   ushort    port;
50   string[]  modes;
51   ushort    heartbeatInterval;
52 
53   /*
54   void deserialize(JSONDecoder obj) {
55     obj.keySwitch!("ssrc", "port", "modes", "heartbeat_interval")(
56       { this.ssrc = obj.read!ushort; },
57       { this.port = obj.read!ushort; },
58       { this.modes = obj.readArray!string; },
59       { this.heartbeatInterval = obj.read!ushort; },
60     );
61   }
62   */
63 }
64 
65 class VoiceSelectProtocolPacket : BasePacket, Serializable {
66   string  protocol;
67   string  mode;
68   string  ip;
69   ushort  port;
70 
71   this(string protocol, string mode, string ip, ushort port) {
72     this.protocol = protocol;
73     this.mode = mode;
74     this.ip = ip;
75     this.port = port;
76   }
77 
78   override VibeJSON serialize() {
79     auto data = VibeJSON([
80       "port": VibeJSON(this.port),
81       "address": VibeJSON(this.ip),
82       "mode": VibeJSON(this.mode),
83     ]);
84 
85     return super.serialize(VoiceOPCode.VOICE_SELECT_PROTOCOL, VibeJSON([
86       "protocol": VibeJSON(this.protocol),
87       "data": data,
88     ]));
89   }
90 }
91 
92 class VoiceHeartbeatPacket : BasePacket, Serializable {
93   uint  ts;
94 
95   this(uint ts) {
96     this.ts = ts;
97   }
98 
99   override VibeJSON serialize() {
100     return super.serialize(VoiceOPCode.VOICE_HEARTBEAT, VibeJSON(this.ts));
101   }
102 }
103 
104 class VoiceSpeakingPacket : BasePacket, Serializable {
105   bool  speaking;
106   uint  delay;
107 
108   this(bool speaking, uint delay) {
109     this.speaking = speaking;
110     this.delay = delay;
111   }
112 
113   override VibeJSON serialize() {
114     return super.serialize(VoiceOPCode.VOICE_SPEAKING, VibeJSON([
115       "speaking": VibeJSON(this.speaking),
116       "delay": VibeJSON(this.delay),
117     ]));
118   }
119 }
120 
121 class VoiceSessionDescriptionPacket : BasePacket, Deserializable {
122   byte[]  secretKey;
123 }