Browse Source

split packet to sendpacket and recvpacket in serverstate

pbs
Jake Probst 5 years ago
parent
commit
606bd191b8
  1. 25
      src/common/client.rs
  2. 19
      src/common/serverstate.rs

25
src/common/client.rs

@ -1,7 +1,7 @@
use libpso::crypto::{PSOCipher, NullCipher}; use libpso::crypto::{PSOCipher, NullCipher};
use libpso::{PSOPacket, PacketParseError}; use libpso::{PSOPacket, PacketParseError};
use crate::common::serverstate::{ServerState, ServerPacket, OnConnect};
use crate::common::serverstate::{ServerState, SendServerPacket, RecvServerPacket, OnConnect};
use crate::common::network::{recv_packet, PacketNetworkError}; use crate::common::network::{recv_packet, PacketNetworkError};
use std::net; use std::net;
@ -9,17 +9,24 @@ use std::io::Write;
use mio::tcp::TcpStream; use mio::tcp::TcpStream;
use mio::{Poll, Events, Token, Ready, PollOpt}; use mio::{Poll, Events, Token, Ready, PollOpt};
pub struct Client<P, E> {
pub struct Client<S, R, E> {
running: bool, running: bool,
socket: mio::tcp::TcpStream, socket: mio::tcp::TcpStream,
cipher_in: Box<dyn PSOCipher>, cipher_in: Box<dyn PSOCipher>,
cipher_out: Box<dyn PSOCipher>, cipher_out: Box<dyn PSOCipher>,
state: Box<dyn ServerState<Packet = P, PacketError = E>>,
state: Box<dyn ServerState<SendPacket = S, RecvPacket = R, PacketError = E>>,
send_buffer: Vec<u8>, send_buffer: Vec<u8>,
} }
impl<P: ServerPacket + std::fmt::Debug, E: std::fmt::Debug> Client<P, E> {
pub fn new(socket: mio::tcp::TcpStream, state: Box<dyn ServerState<Packet = P, PacketError = E>>) -> Client<P, E> {
impl<S, R, E> Client<S, R, E> where
S: SendServerPacket + std::fmt::Debug,
R: RecvServerPacket + std::fmt::Debug,
E: std::fmt::Debug,
{
pub fn new(socket: mio::tcp::TcpStream,
state: Box<dyn ServerState<SendPacket = S, RecvPacket = R, PacketError = E>>)
-> Client<S, R, E>
{
let mut client = Client { let mut client = Client {
running: true, running: true,
socket: socket, socket: socket,
@ -31,7 +38,7 @@ impl<P: ServerPacket + std::fmt::Debug, E: std::fmt::Debug> Client<P, E> {
for task in client.state.on_connect() { for task in client.state.on_connect() {
match task { match task {
OnConnect::Packet(pkt) => client.send(&*pkt),
OnConnect::Packet(pkt) => client.send(pkt),
OnConnect::Cipher((cipher_in, cipher_out)) => { OnConnect::Cipher((cipher_in, cipher_out)) => {
client.cipher_in = cipher_in; client.cipher_in = cipher_in;
client.cipher_out = cipher_out; client.cipher_out = cipher_out;
@ -60,7 +67,7 @@ impl<P: ServerPacket + std::fmt::Debug, E: std::fmt::Debug> Client<P, E> {
} }
// TODO: this may need to pad to 8 bytes for bb cipher // TODO: this may need to pad to 8 bytes for bb cipher
fn send(&mut self, pkt: &dyn PSOPacket) {
fn send(&mut self, pkt: S) {
println!("[send] {:?}", pkt); println!("[send] {:?}", pkt);
let buf = pkt.as_bytes(); let buf = pkt.as_bytes();
let mut cbuf = self.cipher_out.encrypt(&buf).unwrap(); let mut cbuf = self.cipher_out.encrypt(&buf).unwrap();
@ -85,7 +92,7 @@ impl<P: ServerPacket + std::fmt::Debug, E: std::fmt::Debug> Client<P, E> {
loop { loop {
let pkt = recv_packet(&mut self.socket, &mut *self.cipher_in) let pkt = recv_packet(&mut self.socket, &mut *self.cipher_in)
.and_then(|pkt| { .and_then(|pkt| {
P::from_bytes(&pkt)
R::from_bytes(&pkt)
.map_err(|err| err.into()) .map_err(|err| err.into())
}); });
@ -94,7 +101,7 @@ impl<P: ServerPacket + std::fmt::Debug, E: std::fmt::Debug> Client<P, E> {
println!("[recv] {:?}", pkt); println!("[recv] {:?}", pkt);
let response = self.state.handle(&pkt); let response = self.state.handle(&pkt);
for r in response { for r in response {
self.send(&*r);
self.send(r);
} }
}, },
Err(err) => { Err(err) => {

19
src/common/serverstate.rs

@ -1,21 +1,26 @@
use libpso::{PSOPacket, PacketParseError};
use libpso::PacketParseError;
use libpso::crypto::PSOCipher; use libpso::crypto::PSOCipher;
pub enum OnConnect {
Packet(Box<dyn PSOPacket>),
pub enum OnConnect<S: SendServerPacket> {
Packet(S),
Cipher((Box<dyn PSOCipher>, Box<dyn PSOCipher>)), Cipher((Box<dyn PSOCipher>, Box<dyn PSOCipher>)),
} }
pub trait ServerPacket: Sized {
pub trait RecvServerPacket: Sized {
fn from_bytes(data: &Vec<u8>) -> Result<Self, PacketParseError>; fn from_bytes(data: &Vec<u8>) -> Result<Self, PacketParseError>;
} }
pub trait SendServerPacket: Sized {
fn as_bytes(&self) -> Vec<u8>;
}
pub trait ServerState { pub trait ServerState {
type Packet: ServerPacket;
type SendPacket: SendServerPacket;
type RecvPacket: RecvServerPacket;
type PacketError; type PacketError;
fn on_connect(&mut self) -> Vec<OnConnect>;
fn handle(&mut self, pkt: &Self::Packet) -> Box<dyn Iterator<Item = Box<dyn PSOPacket>>>;
fn on_connect(&mut self) -> Vec<OnConnect<Self::SendPacket>>;
fn handle(&mut self, pkt: &Self::RecvPacket) -> Box<dyn Iterator<Item = Self::SendPacket>>;
} }
Loading…
Cancel
Save