Move INFO operations into it's own module
This commit is contained in:
parent
c7b9eb4668
commit
fb05f0e141
3 changed files with 134 additions and 126 deletions
129
src/infoops.rs
Normal file
129
src/infoops.rs
Normal file
|
@ -0,0 +1,129 @@
|
|||
use std::any::Any;
|
||||
use num_traits::real::Real;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde_json::Value;
|
||||
use serde_repr::{Serialize_repr, Deserialize_repr};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
/// Info message types
|
||||
#[derive(Serialize_repr, Deserialize_repr)]
|
||||
#[repr(u8)]
|
||||
pub enum InfoType {
|
||||
/// Request a channel to be created inside the voice server.
|
||||
CHANNEL_REQ = 0,
|
||||
|
||||
/// Sent by the Server to signal the successful creation of a voice channel.
|
||||
CHANNEL_ASSIGN = 1,
|
||||
|
||||
/// Sent by the client to signal the destruction of a voice channel. Be it
|
||||
/// a channel being deleted, or all members in it leaving.
|
||||
CHANNEL_DESTROY = 2,
|
||||
|
||||
/// Sent by the client to create a voice state.
|
||||
VST_CREATE = 3,
|
||||
|
||||
/// Sent by the server to indicate the success of a VST_CREATE.
|
||||
VST_DONE = 4,
|
||||
|
||||
/// Sent by the client when a user is leaving a channel OR moving between channels
|
||||
/// in a guild. More on state transitions later on.
|
||||
VST_UPDATE = 5,
|
||||
|
||||
/// Voice state leave.
|
||||
VST_LEAVE = 6
|
||||
}
|
||||
|
||||
/// Info message data
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum InfoData {
|
||||
/// Request a channel to be created inside the voice server.
|
||||
///
|
||||
/// The Server MUST reply back with a CHANNEL_ASSIGN when resources are
|
||||
/// allocated for the channel.
|
||||
CHANNEL_REQ {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the Server to signal the successful creation of a voice channel.
|
||||
CHANNEL_ASSIGN {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>,
|
||||
|
||||
/// Authentication token
|
||||
token: String
|
||||
},
|
||||
|
||||
/// Sent by the client to signal the destruction of a voice channel. Be it
|
||||
/// a channel being deleted, or all members in it leaving.
|
||||
CHANNEL_DESTROY {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the client to create a voice state.
|
||||
VST_CREATE {
|
||||
/// User ID
|
||||
user_id: u64,
|
||||
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the server to indicate the success of a VST_CREATE.
|
||||
VST_DONE {
|
||||
/// User ID
|
||||
user_id: u64,
|
||||
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>,
|
||||
|
||||
/// Session ID for the voice state
|
||||
session_id: String
|
||||
},
|
||||
|
||||
/// Sent by the client when a user is leaving a channel OR moving between channels
|
||||
/// in a guild. More on state transitions later on.
|
||||
VST_DESTROY {
|
||||
/// Session ID for the voice state
|
||||
session_id: String
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct InfoMessage {
|
||||
/// Info type
|
||||
#[serde(rename = "type")]
|
||||
_type: InfoType,
|
||||
|
||||
/// Info data, varies depending on InfoType
|
||||
data: InfoData
|
||||
}
|
||||
pub fn get_infotype(msg: Message) -> Result<(InfoType, InfoData), ()> {
|
||||
let message_json: Result<Value, serde_json::Error> = serde_json::from_str(msg.to_text().expect("Failed to convert message to str!"));
|
||||
|
||||
if message_json.is_ok() {
|
||||
// TODO: Maybe find a better way?
|
||||
let info_data: InfoMessage = serde_json::from_value(message_json.unwrap().get("d").unwrap().clone()).unwrap();
|
||||
|
||||
Ok((info_data._type, info_data.data))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
|
@ -12,7 +12,8 @@ use tokio::net::{TcpListener, TcpStream};
|
|||
use futures_util::{future, SinkExt, StreamExt, TryStreamExt};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use crate::OpCode::{HEARTBEAT_ACK, HELLO, READY};
|
||||
use crate::opcodes::{get_opcode, get_infotype, MessageData, OpCode, SocketMessage};
|
||||
use crate::opcodes::{get_opcode, MessageData, OpCode, SocketMessage};
|
||||
use crate::infoops::get_infotype;
|
||||
|
||||
use rand::prelude::*;
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
@ -20,6 +21,8 @@ use rand::distributions::Alphanumeric;
|
|||
use serde_json::Value::Array;
|
||||
|
||||
mod opcodes;
|
||||
mod infoops;
|
||||
mod socket_decode;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
|
@ -131,8 +134,6 @@ async fn handle_conn(peer: SocketAddr, stream: TcpStream) -> tokio_tungstenite::
|
|||
println!("INFO from {}", &peer);
|
||||
|
||||
let info = get_infotype(msg).unwrap();
|
||||
|
||||
println!("{}", info.0 as u8);
|
||||
},
|
||||
|
||||
_ => {
|
||||
|
|
124
src/opcodes.rs
124
src/opcodes.rs
|
@ -16,6 +16,7 @@ use serde::{Serialize, Deserialize};
|
|||
use serde_json::Value;
|
||||
use serde_repr::{Serialize_repr, Deserialize_repr};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use crate::infoops::{InfoData, InfoType};
|
||||
|
||||
/// Op codes sent/received by Litecord
|
||||
#[derive(FromPrimitive, Serialize_repr, Deserialize_repr, PartialEq)]
|
||||
|
@ -114,107 +115,6 @@ pub enum MessageData {
|
|||
}
|
||||
}
|
||||
|
||||
/// Info message types
|
||||
#[derive(Serialize_repr, Deserialize_repr)]
|
||||
#[repr(u8)]
|
||||
pub enum InfoType {
|
||||
/// Request a channel to be created inside the voice server.
|
||||
CHANNEL_REQ = 0,
|
||||
|
||||
/// Sent by the Server to signal the successful creation of a voice channel.
|
||||
CHANNEL_ASSIGN = 1,
|
||||
|
||||
/// Sent by the client to signal the destruction of a voice channel. Be it
|
||||
/// a channel being deleted, or all members in it leaving.
|
||||
CHANNEL_DESTROY = 2,
|
||||
|
||||
/// Sent by the client to create a voice state.
|
||||
VST_CREATE = 3,
|
||||
|
||||
/// Sent by the server to indicate the success of a VST_CREATE.
|
||||
VST_DONE = 4,
|
||||
|
||||
/// Sent by the client when a user is leaving a channel OR moving between channels
|
||||
/// in a guild. More on state transitions later on.
|
||||
VST_UPDATE = 5,
|
||||
|
||||
/// Voice state leave.
|
||||
VST_LEAVE = 6
|
||||
}
|
||||
|
||||
/// Info message data
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum InfoData {
|
||||
/// Request a channel to be created inside the voice server.
|
||||
///
|
||||
/// The Server MUST reply back with a CHANNEL_ASSIGN when resources are
|
||||
/// allocated for the channel.
|
||||
CHANNEL_REQ {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the Server to signal the successful creation of a voice channel.
|
||||
CHANNEL_ASSIGN {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>,
|
||||
|
||||
/// Authentication token
|
||||
token: String
|
||||
},
|
||||
|
||||
/// Sent by the client to signal the destruction of a voice channel. Be it
|
||||
/// a channel being deleted, or all members in it leaving.
|
||||
CHANNEL_DESTROY {
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the client to create a voice state.
|
||||
VST_CREATE {
|
||||
/// User ID
|
||||
user_id: u64,
|
||||
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>
|
||||
},
|
||||
|
||||
/// Sent by the server to indicate the success of a VST_CREATE.
|
||||
VST_DONE {
|
||||
/// User ID
|
||||
user_id: u64,
|
||||
|
||||
/// Channel ID
|
||||
channel_id: u64,
|
||||
|
||||
/// Guild ID, not provided if dm / group dm
|
||||
guild_id: Option<u64>,
|
||||
|
||||
/// Session ID for the voice state
|
||||
session_id: String
|
||||
},
|
||||
|
||||
/// Sent by the client when a user is leaving a channel OR moving between channels
|
||||
/// in a guild. More on state transitions later on.
|
||||
VST_DESTROY {
|
||||
/// Session ID for the voice state
|
||||
session_id: String
|
||||
}
|
||||
}
|
||||
|
||||
/// Message data is defined by each opcode.
|
||||
///
|
||||
/// **Note:** the snowflake type follows the same rules as the Discord Gateway's
|
||||
|
@ -228,15 +128,6 @@ pub struct SocketMessage {
|
|||
pub d: MessageData
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct InfoMessage {
|
||||
/// Info type
|
||||
#[serde(rename = "type")]
|
||||
_type: InfoType,
|
||||
|
||||
/// Info data, varies depending on InfoType
|
||||
data: InfoData
|
||||
}
|
||||
|
||||
pub fn get_opcode(msg: Message) -> Result<(OpCode, MessageData), ()> {
|
||||
let message_json: Result<SocketMessage, serde_json::Error> = serde_json::from_str(msg.to_text().expect("Failed to convert message to str!"));
|
||||
|
@ -248,17 +139,4 @@ pub fn get_opcode(msg: Message) -> Result<(OpCode, MessageData), ()> {
|
|||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_infotype(msg: Message) -> Result<(InfoType, InfoData), ()> {
|
||||
let message_json: Result<Value, serde_json::Error> = serde_json::from_str(msg.to_text().expect("Failed to convert message to str!"));
|
||||
|
||||
if message_json.is_ok() {
|
||||
// TODO: Maybe find a better way?
|
||||
let info_data: InfoMessage = serde_json::from_value(message_json.unwrap().get("d").unwrap().clone()).unwrap();
|
||||
|
||||
Ok((info_data._type, info_data.data))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue