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::{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 std::net;
@ -9,17 +9,24 @@ use std::io::Write;
use mio::tcp::TcpStream;
use mio::{Poll, Events, Token, Ready, PollOpt};
pub struct Client<P, E> {
pub struct Client<S, R, E> {
running: bool,
socket: mio::tcp::TcpStream,
cipher_in: 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>,
}
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 {
running: true,
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() {
match task {
OnConnect::Packet(pkt) => client.send(&*pkt),
OnConnect::Packet(pkt) => client.send(pkt),
OnConnect::Cipher((cipher_in, cipher_out)) => {
client.cipher_in = cipher_in;
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
fn send(&mut self, pkt: &dyn PSOPacket) {
fn send(&mut self, pkt: S) {
println!("[send] {:?}", pkt);
let buf = pkt.as_bytes();
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 {
let pkt = recv_packet(&mut self.socket, &mut *self.cipher_in)
.and_then(|pkt| {
P::from_bytes(&pkt)
R::from_bytes(&pkt)
.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);
let response = self.state.handle(&pkt);
for r in response {
self.send(&*r);
self.send(r);
}
},
Err(err) => {

19
src/common/serverstate.rs

@ -1,21 +1,26 @@
use libpso::{PSOPacket, PacketParseError};
use libpso::PacketParseError;
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>)),
}
pub trait ServerPacket: Sized {
pub trait RecvServerPacket: Sized {
fn from_bytes(data: &Vec<u8>) -> Result<Self, PacketParseError>;
}
pub trait SendServerPacket: Sized {
fn as_bytes(&self) -> Vec<u8>;
}
pub trait ServerState {
type Packet: ServerPacket;
type SendPacket: SendServerPacket;
type RecvPacket: RecvServerPacket;
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