Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed May 12, 2024
1 parent 8037914 commit b3b6a35
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 310 deletions.
18 changes: 13 additions & 5 deletions korangar/src/interface/elements/containers/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use korangar_interface::size_bound;
use korangar_interface::state::{PlainRemote, Remote, RemoteClone};
use ragnarok_bytes::{ByteStream, ConversionError, ConversionResult, FromBytes};
use ragnarok_packets::handler::PacketCallback;
use ragnarok_packets::{IncomingPacket, OutgoingPacket, PacketHeader};
use ragnarok_packets::{Packet, PacketHeader};

use crate::graphics::{InterfaceRenderer, Renderer};
use crate::input::MouseInputMode;
Expand All @@ -26,7 +26,7 @@ struct UnknownPacket {
pub bytes: Vec<u8>,
}

impl IncomingPacket for UnknownPacket {
impl Packet for UnknownPacket {
const HEADER: PacketHeader = PacketHeader(0);
const IS_PING: bool = false;

Expand All @@ -35,6 +35,10 @@ impl IncomingPacket for UnknownPacket {
unimplemented!()
}

fn payload_to_bytes(&self) -> ConversionResult<Vec<u8>> {
unimplemented!()
}

fn to_prototype_element<App: Application>(&self) -> Box<dyn PrototypeElement<App> + Send> {
Box::new(self.clone())
}
Expand Down Expand Up @@ -67,7 +71,7 @@ struct ErrorPacket {
pub error: Box<ConversionError>,
}

impl IncomingPacket for ErrorPacket {
impl Packet for ErrorPacket {
const HEADER: PacketHeader = PacketHeader(0);
const IS_PING: bool = false;

Expand All @@ -76,6 +80,10 @@ impl IncomingPacket for ErrorPacket {
unimplemented!()
}

fn payload_to_bytes(&self) -> ConversionResult<Vec<u8>> {
unimplemented!()
}

fn to_prototype_element<App: Application>(&self) -> Box<dyn PrototypeElement<App> + Send> {
Box::new(self.clone())
}
Expand Down Expand Up @@ -188,7 +196,7 @@ impl PacketHistoryCallback {
impl PacketCallback for PacketHistoryCallback {
fn incoming_packet<Packet>(&self, packet: &Packet)
where
Packet: IncomingPacket,
Packet: ragnarok_packets::Packet,
{
let mut lock = self.buffer_pointer.lock().unwrap();

Expand All @@ -201,7 +209,7 @@ impl PacketCallback for PacketHistoryCallback {

fn outgoing_packet<Packet>(&self, packet: &Packet)
where
Packet: OutgoingPacket,
Packet: ragnarok_packets::Packet,
{
let mut lock = self.buffer_pointer.lock().unwrap();

Expand Down
8 changes: 4 additions & 4 deletions korangar/src/loaders/archive/native/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl Writable for NativeArchiveBuilder {

let mut bytes = Vec::new();

bytes.extend_from_slice(&file_header.to_bytes().unwrap());
bytes.extend(file_header.to_bytes().unwrap());
bytes.extend_from_slice(&self.data);

let mut file_table_data = Vec::new();

for file_information in self.file_table.values() {
file_table_data.extend_from_slice(&file_information.to_bytes().unwrap());
file_table_data.extend(file_information.to_bytes().unwrap());
}

let compressed_file_information_data = compress(&file_table_data, Format::Zlib, CompressionLevel::Default).unwrap();
Expand All @@ -74,8 +74,8 @@ impl Writable for NativeArchiveBuilder {
uncompressed_size: file_table_data.len() as u32,
};

bytes.extend_from_slice(&file_table.to_bytes().unwrap());
bytes.extend_from_slice(&compressed_file_information_data);
bytes.extend(file_table.to_bytes().unwrap());
bytes.extend(compressed_file_information_data);

std::fs::write(&self.os_file_path, bytes).expect("unable to write file");
}
Expand Down
10 changes: 5 additions & 5 deletions korangar_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ where
mut read_account_id: bool,
) -> Result<(), NetworkTaskError>
where
Ping: OutgoingPacket,
Ping: ragnarok_packets::Packet + ClientPacket,
Callback: PacketCallback,
{
let mut stream = TcpStream::connect(address).await.map_err(|_| NetworkTaskError::FailedToConnect)?;
Expand Down Expand Up @@ -417,7 +417,7 @@ where

pub fn send_login_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: OutgoingPacket + LoginServerPacket,
Packet: ragnarok_packets::Packet + LoginServerPacket,
{
match &mut self.login_server_connection {
ServerConnection::Connected { action_sender, .. } => {
Expand All @@ -432,7 +432,7 @@ where

pub fn send_character_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: OutgoingPacket + CharacterServerPacket,
Packet: ragnarok_packets::Packet + CharacterServerPacket,
{
match &mut self.character_server_connection {
ServerConnection::Connected { action_sender, .. } => {
Expand All @@ -447,7 +447,7 @@ where

pub fn send_map_server_packet<Packet>(&mut self, packet: &Packet) -> Result<(), NotConnectedError>
where
Packet: OutgoingPacket + MapServerPacket,
Packet: ragnarok_packets::Packet + MapServerPacket,
{
match &mut self.map_server_connection {
ServerConnection::Connected { action_sender, .. } => {
Expand Down Expand Up @@ -930,7 +930,7 @@ where
let complete_message = format!("{} : {}", player_name, message);

self.send_map_server_packet(&GlobalMessagePacket::new(
complete_message.bytes().len() as u16 + 5,
PacketLength(complete_message.bytes().len() as u16 + 5),
complete_message,
))
}
Expand Down
13 changes: 13 additions & 0 deletions ragnarok_bytes/src/to_bytes/implement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ impl ToBytes for String {
}
}

impl<T: ToBytes> ToBytes for Vec<T> {
fn to_bytes(&self) -> ConversionResult<Vec<u8>> {
let mut bytes = Vec::new();

for item in self.iter() {
let item = item.to_bytes().trace::<Self>()?;
bytes.extend(item);
}

Ok(bytes)
}
}

#[cfg(feature = "cgmath")]
impl<T: ToBytes> ToBytes for Vector2<T> {
fn to_bytes(&self) -> ConversionResult<Vec<u8>> {
Expand Down
42 changes: 18 additions & 24 deletions ragnarok_packets/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use ragnarok_bytes::{ByteStream, ConversionError, ConversionResult, FromBytes};

use crate::{IncomingPacket, OutgoingPacket, PacketHeader};
use crate::PacketHeader;

/// Possible results of [`PacketHandler::process_one`].
pub enum HandlerResult<Output> {
Expand All @@ -28,42 +28,36 @@ pub trait PacketCallback: Clone + Send + 'static {
/// Called by the [`PacketHandler`] when a packet is received.
fn incoming_packet<Packet>(&self, packet: &Packet)
where
Packet: IncomingPacket;
Packet: ragnarok_packets::Packet,
{
let _ = packet;
}

/// Called by the [`NetworkingSystem`](super::NetworkingSystem) when a
/// packet is sent.
fn outgoing_packet<Packet>(&self, packet: &Packet)
where
Packet: OutgoingPacket;
Packet: ragnarok_packets::Packet,
{
let _ = packet;
}

/// Called by the [`PacketHandler`] when a packet arrives that doesn't have
/// a handler registered.
fn unknown_packet(&self, bytes: Vec<u8>);
fn unknown_packet(&self, bytes: Vec<u8>) {
let _ = bytes;
}

/// Called by the [`PacketHandler`] when a packet handler returned an error.
fn failed_packet(&self, bytes: Vec<u8>, error: Box<ConversionError>);
fn failed_packet(&self, bytes: Vec<u8>, error: Box<ConversionError>) {
let _ = (bytes, error);
}
}

#[derive(Debug, Default, Clone)]
pub struct NoPacketCallback;

impl PacketCallback for NoPacketCallback {
fn incoming_packet<Packet>(&self, _packet: &Packet)
where
Packet: IncomingPacket,
{
}

fn outgoing_packet<Packet>(&self, _packet: &Packet)
where
Packet: OutgoingPacket,
{
}

fn unknown_packet(&self, _bytes: Vec<u8>) {}

fn failed_packet(&self, _bytes: Vec<u8>, _error: Box<ConversionError>) {}
}
impl PacketCallback for NoPacketCallback {}

pub type HandlerFunction<Output, Meta> = Box<dyn Fn(&mut ByteStream<Meta>) -> ConversionResult<Output>>;

Expand Down Expand Up @@ -109,7 +103,7 @@ where
/// Register a new packet handler.
pub fn register<Packet, Return>(&mut self, handler: impl Fn(Packet) -> Return + 'static) -> Result<(), DuplicateHandlerError>
where
Packet: IncomingPacket,
Packet: ragnarok_packets::Packet,
Return: Into<Output>,
{
let packet_callback = self.packet_callback.clone();
Expand All @@ -135,7 +129,7 @@ where
/// Register a noop packet handler.
pub fn register_noop<Packet>(&mut self) -> Result<(), DuplicateHandlerError>
where
Packet: IncomingPacket,
Packet: ragnarok_packets::Packet,
{
let packet_callback = self.packet_callback.clone();
let old_handler = self.handlers.insert(
Expand Down
Loading

0 comments on commit b3b6a35

Please sign in to comment.