Initial INFO decode
This commit is contained in:
parent
766ddc9eff
commit
c7b9eb4668
2 changed files with 35 additions and 6 deletions
12
src/main.rs
12
src/main.rs
|
@ -12,7 +12,7 @@ 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::{check_if_opcode, MessageData, OpCode, SocketMessage};
|
||||
use crate::opcodes::{get_opcode, get_infotype, MessageData, OpCode, SocketMessage};
|
||||
|
||||
use rand::prelude::*;
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
@ -88,9 +88,10 @@ async fn handle_conn(peer: SocketAddr, stream: TcpStream) -> tokio_tungstenite::
|
|||
let msg = msg?;
|
||||
|
||||
if msg.is_text() {
|
||||
let op = check_if_opcode(msg.clone());
|
||||
let op = get_opcode(msg.clone());
|
||||
if op.is_ok() {
|
||||
match op.unwrap().0 {
|
||||
let op = op.unwrap();
|
||||
match op.0 {
|
||||
OpCode::IDENTIFY => {
|
||||
println!("IDENTIFY from {}", &peer);
|
||||
println!("READY to {}", &peer);
|
||||
|
@ -128,7 +129,10 @@ async fn handle_conn(peer: SocketAddr, stream: TcpStream) -> tokio_tungstenite::
|
|||
|
||||
OpCode::INFO => {
|
||||
println!("INFO from {}", &peer);
|
||||
unimplemented!()
|
||||
|
||||
let info = get_infotype(msg).unwrap();
|
||||
|
||||
println!("{}", info.0 as u8);
|
||||
},
|
||||
|
||||
_ => {
|
||||
|
|
|
@ -11,12 +11,14 @@
|
|||
//!
|
||||
//! [Source](https://gitlab.com/litecord/litecord/-/blob/master/docs/lvsp.md)
|
||||
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;
|
||||
|
||||
/// Op codes sent/received by Litecord
|
||||
#[derive(FromPrimitive, Serialize_repr, Deserialize_repr)]
|
||||
#[derive(FromPrimitive, Serialize_repr, Deserialize_repr, PartialEq)]
|
||||
#[repr(u8)]
|
||||
pub enum OpCode {
|
||||
/// Sent by the server when a connection is established.
|
||||
|
@ -226,7 +228,17 @@ pub struct SocketMessage {
|
|||
pub d: MessageData
|
||||
}
|
||||
|
||||
pub fn check_if_opcode(msg: Message) -> Result<(OpCode, 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!"));
|
||||
|
||||
if message_json.is_ok() {
|
||||
|
@ -236,4 +248,17 @@ pub fn check_if_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