Browse Source

Refactor server code into its own module.

wip
Titouan Rigoudy 9 years ago
parent
commit
e6c8338f7e
4 changed files with 150 additions and 130 deletions
  1. +8
    -0
      src/proto/server/constants.rs
  2. +6
    -0
      src/proto/server/mod.rs
  3. +132
    -0
      src/proto/server/request.rs
  4. +4
    -130
      src/proto/server/response.rs

+ 8
- 0
src/proto/server/constants.rs View File

@ -0,0 +1,8 @@
pub const CODE_LOGIN: u32 = 1;
pub const CODE_SET_LISTEN_PORT: u32 = 2;
pub const CODE_CONNECT_TO_PEER: u32 = 18;
pub const CODE_ROOM_LIST: u32 = 64;
pub const CODE_PRIVILEGED_USERS: u32 = 69;
pub const CODE_PARENT_MIN_SPEED: u32 = 83;
pub const CODE_PARENT_SPEED_RATIO: u32 = 84;
pub const CODE_WISHLIST_INTERVAL: u32 = 104;

+ 6
- 0
src/proto/server/mod.rs View File

@ -0,0 +1,6 @@
mod constants;
mod request;
mod response;
pub use self::request::*;
pub use self::response::*;

+ 132
- 0
src/proto/server/request.rs View File

@ -0,0 +1,132 @@
use std::io;
use crypto::md5::Md5;
use crypto::digest::Digest;
use super::constants::*;
use super::super::packet::Packet;
trait WriteToPacket {
fn write_to_packet(&self, &mut Packet) -> io::Result<()>;
}
/*================*
* SERVER REQUEST *
*================*/
#[derive(Debug)]
pub enum ServerRequest {
LoginRequest(LoginRequest),
RoomListRequest(RoomListRequest),
SetListenPortRequest(SetListenPortRequest),
}
impl ServerRequest {
pub fn to_packet(&self) -> io::Result<Packet> {
let (mut packet, request): (Packet, &WriteToPacket) = match *self {
ServerRequest::LoginRequest(ref request) =>
(Packet::new(CODE_LOGIN), request),
ServerRequest::RoomListRequest(ref request) =>
(Packet::new(CODE_ROOM_LIST), request),
ServerRequest::SetListenPortRequest(ref request) =>
(Packet::new(CODE_SET_LISTEN_PORT), request),
};
try!(request.write_to_packet(&mut packet));
Ok(packet)
}
}
fn md5_str(string: &str) -> String {
let mut hasher = Md5::new();
hasher.input_str(string);
hasher.result_str()
}
/*=======*
* LOGIN *
*=======*/
#[derive(Debug)]
pub struct LoginRequest {
username: String,
password: String,
major: u32,
minor: u32,
}
impl LoginRequest {
pub fn new(username: &str, password: &str, major: u32, minor: u32)
-> Result<Self, &'static str> {
if password.len() > 0 {
Ok(LoginRequest {
username: username.to_string(),
password: password.to_string(),
major: major,
minor: minor,
})
} else {
Err("Empty password")
}
}
}
impl WriteToPacket for LoginRequest {
fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> {
let userpass = String::new() + &self.username + &self.password;
let userpass_md5 = md5_str(&userpass);
try!(packet.write_str(&self.username));
try!(packet.write_str(&self.password));
try!(packet.write_uint(self.major));
try!(packet.write_str(&userpass_md5));
try!(packet.write_uint(self.minor));
Ok(())
}
}
/*===========*
* ROOM LIST *
*===========*/
#[derive(Debug)]
pub struct RoomListRequest;
impl RoomListRequest {
pub fn new() -> Self {
RoomListRequest
}
}
impl WriteToPacket for RoomListRequest {
fn write_to_packet(&self, _: &mut Packet) -> io::Result<()> {
Ok(())
}
}
/*=================*
* SET LISTEN PORT *
*=================*/
#[derive(Debug)]
pub struct SetListenPortRequest {
port: u16,
}
impl SetListenPortRequest {
fn new(port: u16) -> Self {
SetListenPortRequest {
port: port,
}
}
}
impl WriteToPacket for SetListenPortRequest {
fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> {
try!(packet.write_uint(self.port as u32));
Ok(())
}
}

src/proto/server.rs → src/proto/server/response.rs View File

@ -1,53 +1,10 @@
use std::io; use std::io;
use std::net; use std::net;
use crypto::md5::Md5;
use crypto::digest::Digest;
use super::constants::*;
use super::super::packet::Packet;
use super::Packet;
const MAX_PORT: u32 = 1 << 16;
const CODE_LOGIN: u32 = 1;
const CODE_SET_LISTEN_PORT: u32 = 2;
const CODE_CONNECT_TO_PEER: u32 = 18;
const CODE_ROOM_LIST: u32 = 64;
const CODE_PRIVILEGED_USERS: u32 = 69;
const CODE_PARENT_MIN_SPEED: u32 = 83;
const CODE_PARENT_SPEED_RATIO: u32 = 84;
const CODE_WISHLIST_INTERVAL: u32 = 104;
trait WriteToPacket {
fn write_to_packet(&self, &mut Packet) -> io::Result<()>;
}
/*================*
* SERVER REQUEST *
*================*/
#[derive(Debug)]
pub enum ServerRequest {
LoginRequest(LoginRequest),
RoomListRequest(RoomListRequest),
SetListenPortRequest(SetListenPortRequest),
}
impl ServerRequest {
pub fn to_packet(&self) -> io::Result<Packet> {
let (mut packet, request): (Packet, &WriteToPacket) = match *self {
ServerRequest::LoginRequest(ref request) =>
(Packet::new(CODE_LOGIN), request),
ServerRequest::RoomListRequest(ref request) =>
(Packet::new(CODE_ROOM_LIST), request),
ServerRequest::SetListenPortRequest(ref request) =>
(Packet::new(CODE_SET_LISTEN_PORT), request),
};
try!(request.write_to_packet(&mut packet));
Ok(packet)
}
}
const MAX_PORT: u32 = (1 << 16) - 1;
/*=================* /*=================*
* SERVER RESPONSE * * SERVER RESPONSE *
@ -118,12 +75,6 @@ impl ServerResponse {
} }
} }
fn md5_str(string: &str) -> String {
let mut hasher = Md5::new();
hasher.input_str(string);
hasher.result_str()
}
/*=================* /*=================*
* CONNECT TO PEER * * CONNECT TO PEER *
*=================*/ *=================*/
@ -169,45 +120,6 @@ impl ConnectToPeerResponse {
* LOGIN * * LOGIN *
*=======*/ *=======*/
#[derive(Debug)]
pub struct LoginRequest {
username: String,
password: String,
major: u32,
minor: u32,
}
impl LoginRequest {
pub fn new(username: &str, password: &str, major: u32, minor: u32)
-> Result<Self, &'static str> {
if password.len() > 0 {
Ok(LoginRequest {
username: username.to_string(),
password: password.to_string(),
major: major,
minor: minor,
})
} else {
Err("Empty password")
}
}
}
impl WriteToPacket for LoginRequest {
fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> {
let userpass = String::new() + &self.username + &self.password;
let userpass_md5 = md5_str(&userpass);
try!(packet.write_str(&self.username));
try!(packet.write_str(&self.password));
try!(packet.write_uint(self.major));
try!(packet.write_str(&userpass_md5));
try!(packet.write_uint(self.minor));
Ok(())
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum LoginResponse { pub enum LoginResponse {
LoginOk { LoginOk {
@ -303,21 +215,6 @@ impl PrivilegedUsersResponse {
* ROOM LIST * * ROOM LIST *
*===========*/ *===========*/
#[derive(Debug)]
pub struct RoomListRequest;
impl RoomListRequest {
pub fn new() -> Self {
RoomListRequest
}
}
impl WriteToPacket for RoomListRequest {
fn write_to_packet(&self, _: &mut Packet) -> io::Result<()> {
Ok(())
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct RoomListResponse { pub struct RoomListResponse {
pub rooms: Vec<(String, u32)>, pub rooms: Vec<(String, u32)>,
@ -387,30 +284,6 @@ impl RoomListResponse {
} }
} }
/*=================*
* SET LISTEN PORT *
*=================*/
#[derive(Debug)]
pub struct SetListenPortRequest {
port: u16,
}
impl SetListenPortRequest {
fn new(port: u16) -> Self {
SetListenPortRequest {
port: port,
}
}
}
impl WriteToPacket for SetListenPortRequest {
fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> {
try!(packet.write_uint(self.port as u32));
Ok(())
}
}
/*===================* /*===================*
* WISHLIST INTERVAL * * WISHLIST INTERVAL *
*===================*/ *===================*/
@ -428,3 +301,4 @@ impl WishlistIntervalResponse {
}) })
} }
} }

Loading…
Cancel
Save