Skip to content

Commit

Permalink
Move networking to a separate thread
Browse files Browse the repository at this point in the history
This rework splits the networking system to a separate crate and moves the processing to a networking thread. The main thread will no longer be blocked by networking operations, which was the main reason for the client freezing.

I took this opportunity to also refactor the way that networking routines are handled. Instead of sending a packet and waiting for a response, the request and response are completely decoupled, offering more resilience to packets arriving out of order.
  • Loading branch information
vE5li committed May 6, 2024
1 parent c99dea1 commit b34d6e9
Show file tree
Hide file tree
Showing 75 changed files with 3,768 additions and 2,313 deletions.
974 changes: 858 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ bitflags = "2.4.2"
cgmath = { version = "0.18", features = ["serde"] }
chrono = "0.4"
derive-new = "0.6.0"
korangar_interface = { path = "korangar_interface" }
korangar_debug = { path = "korangar_debug" }
korangar_interface = { path = "korangar_interface" }
korangar_networking = { path = "korangar_networking" }
num = "0.4.1"
ragnarok_bytes = { path = "ragnarok_bytes" }
ragnarok_networking = { path = "ragnarok_networking" }
ragnarok_packets = { path = "ragnarok_packets" }
ragnarok_procedural = { path = "ragnarok_procedural" }
serde = "1.0.137"
6 changes: 4 additions & 2 deletions korangar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ collision = { git = "https://github.com/rustgd/collision-rs.git" }
derive-new = { workspace = true }
image = "0.24.2"
korangar_interface = { workspace = true, features = ["serde", "cgmath"] }
korangar_networking = { workspace = true, features = ["debug"] }
lunify = "1.1.0"
mlua = { version = "0.8", features = ["lua51", "vendored"] }
num = { workspace = true }
Expand All @@ -20,14 +21,15 @@ option-ext = "0.2.0"
pathfinding = "2.2.2"
korangar_debug = { workspace = true, optional = true }
ragnarok_bytes = { workspace = true, features = ["derive", "cgmath"] }
ragnarok_networking = { workspace = true, features = ["derive", "interface"] }
ragnarok_packets = { workspace = true, features = ["derive", "interface", "packet-to-prototype-element"] }
rand = "0.8.5"
random_color = { version = "0.6.1", optional = true }
rayon = "1.5.3"
ron = "0.8.0"
rusttype = { version = "0.9.2", features = ["gpu_cache"] }
serde = { workspace = true }
serde-xml-rs = "0.6.0"
tokio = { version = "1.37.0", features = ["full"] }
vulkano = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
vulkano-shaders = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
vulkano-win = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
Expand All @@ -38,6 +40,6 @@ yazi = "0.1.4"

[features]
patched_as_folder = []
debug = ["korangar_debug", "ragnarok_networking/debug", "random_color"]
debug = ["korangar_debug", "ragnarok_packets/debug", "random_color"]
unicode = ["korangar_debug/unicode"]
plain = ["debug"]
2 changes: 1 addition & 1 deletion korangar/src/graphics/particles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use cgmath::{Vector2, Vector3};
use derive_new::new;
use ragnarok_networking::{EntityId, QuestColor, QuestEffectPacket};
use ragnarok_packets::{EntityId, QuestColor, QuestEffectPacket};
use rand::{thread_rng, Rng};

use crate::graphics::*;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::sync::Arc;
use cgmath::SquareMatrix;
use cgmath::{Matrix4, Vector2, Vector3};
use korangar_interface::application::FontSizeTrait;
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;
use vulkano::device::{DeviceOwned, Queue};
use vulkano::format::Format;
use vulkano::image::Image;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use korangar_debug::profile_block;
#[cfg(feature = "debug")]
use korangar_debug::profiling::Profiler;
use option_ext::OptionExt;
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;
use vulkano::buffer::{Buffer, BufferUsage, Subbuffer};
use vulkano::command_buffer::{
AutoCommandBufferBuilder, ClearAttachment, ClearRect, CommandBufferUsage, CopyImageToBufferInfo, PrimaryAutoCommandBuffer,
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/picker/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fragment_shader!("src/graphics/renderers/picker/entity/fragment_shader.glsl");
use std::sync::Arc;

use cgmath::{Vector2, Vector3};
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;
use vulkano::descriptor_set::WriteDescriptorSet;
use vulkano::device::{Device, DeviceOwned};
use vulkano::image::sampler::Sampler;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod tile;
use std::sync::Arc;

use cgmath::{Matrix4, Vector2, Vector3};
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;
use vulkano::device::{DeviceOwned, Queue};
use vulkano::format::Format;
use vulkano::pipeline::graphics::viewport::Viewport;
Expand Down
4 changes: 2 additions & 2 deletions korangar/src/graphics/renderers/picker/target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;

#[cfg(feature = "debug")]
use crate::world::MarkerIdentifier;
Expand Down Expand Up @@ -100,7 +100,7 @@ impl From<PickerTarget> for u32 {
#[cfg(test)]
#[allow(clippy::unusual_byte_groupings)]
mod encoding {
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;

use crate::graphics::PickerTarget;
#[cfg(feature = "debug")]
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/renderers/shadow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod indicator;
use std::sync::Arc;

use cgmath::{Matrix4, Vector2, Vector3};
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;
use serde::{Deserialize, Serialize};
use vulkano::device::{DeviceOwned, Queue};
use vulkano::format::{ClearValue, Format};
Expand Down
4 changes: 2 additions & 2 deletions korangar/src/input/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cgmath::Vector2;
use korangar_interface::event::ClickAction;
use korangar_interface::ElementEvent;
use ragnarok_networking::{AccountId, CharacterId, CharacterServerInformation, EntityId};
use ragnarok_packets::{AccountId, CharacterId, CharacterServerInformation, EntityId, TilePosition};

use super::HotbarSlot;
use crate::interface::application::{InterfaceSettings, InternalThemeKind};
Expand Down Expand Up @@ -50,7 +50,7 @@ pub enum UserEvent {
SwitchCharacterSlot(usize),
RequestPlayerMove(Vector2<usize>),
RequestPlayerInteract(EntityId),
RequestWarpToMap(String, Vector2<usize>),
RequestWarpToMap(String, TilePosition),
SendMessage(String),
NextDialog(EntityId),
CloseDialog(EntityId),
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use korangar_interface::elements::{ElementCell, Focus};
use korangar_interface::event::ClickAction;
use korangar_interface::state::{PlainTrackedState, TrackedState};
use korangar_interface::Interface;
use ragnarok_networking::ClientTick;
use ragnarok_packets::ClientTick;
use winit::dpi::PhysicalPosition;
use winit::event::{ElementState, MouseButton, MouseScrollDelta, VirtualKeyCode};

Expand Down
2 changes: 1 addition & 1 deletion korangar/src/interface/cursor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use ragnarok_networking::ClientTick;
use ragnarok_packets::ClientTick;

use super::application::InterfaceSettings;
use super::layout::{ScreenClip, ScreenPosition, ScreenSize};
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/interface/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use derive_new::new;
use korangar_interface::state::{PlainTrackedState, TrackedStateExt, TrackedStateVec};
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;

use super::elements::DialogElement;
use super::windows::DialogWindow;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/interface/elements/containers/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use korangar_interface::event::{ChangeEvent, ClickAction, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::state::{PlainRemote, Remote};
use korangar_interface::{dimension_bound, size_bound};
use ragnarok_networking::CharacterInformation;
use ragnarok_packets::CharacterInformation;

use crate::graphics::{Color, InterfaceRenderer, Renderer};
use crate::input::{MouseInputMode, UserEvent};
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/interface/elements/containers/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use korangar_interface::event::{ChangeEvent, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::size_bound;
use korangar_interface::state::{PlainRemote, Remote};
use ragnarok_networking::EntityId;
use ragnarok_packets::EntityId;

use crate::graphics::{Color, InterfaceRenderer, Renderer};
use crate::input::{MouseInputMode, UserEvent};
Expand Down
31 changes: 29 additions & 2 deletions korangar/src/interface/elements/containers/equipment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use korangar_interface::event::{ChangeEvent, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::state::{PlainRemote, Remote};
use korangar_interface::{dimension_bound, size_bound};
use ragnarok_networking::EquipPosition;
use ragnarok_packets::EquipPosition;

use crate::graphics::{Color, InterfaceRenderer, Renderer};
use crate::input::MouseInputMode;
Expand Down Expand Up @@ -41,9 +41,36 @@ impl EquipmentContainer {
(0..SLOT_POSITIONS.len())
.map(|index| {
let slot = SLOT_POSITIONS[index];
let display_name = match slot {
EquipPosition::None => panic!(),
EquipPosition::HeadLower => "Head lower",
EquipPosition::HeadMiddle => "Head middle",
EquipPosition::HeadTop => "Head top",
EquipPosition::RightHand => "Right hand",
EquipPosition::LeftHand => "Left hand",
EquipPosition::Armor => "Armor",
EquipPosition::Shoes => "Shoes",
EquipPosition::Garment => "Garment",
EquipPosition::LeftAccessory => "Left accessory",
EquipPosition::RigthAccessory => "Right accessory",
EquipPosition::CostumeHeadTop => "Costume head top",
EquipPosition::CostumeHeadMiddle => "Costume head middle",
EquipPosition::CostumeHeadLower => "Costume head lower",
EquipPosition::CostumeGarment => "Costume garment",
EquipPosition::Ammo => "Ammo",
EquipPosition::ShadowArmor => "Shadow ammo",
EquipPosition::ShadowWeapon => "Shadow weapon",
EquipPosition::ShadowShield => "Shadow shield",
EquipPosition::ShadowShoes => "Shadow shoes",
EquipPosition::ShadowRightAccessory => "Shadow right accessory",
EquipPosition::ShadowLeftAccessory => "Shadow left accessory",
EquipPosition::LeftRightAccessory => "Accessory",
EquipPosition::LeftRightHand => "Two hand weapon",
EquipPosition::ShadowLeftRightAccessory => "Shadow accessory",
};

let text = Text::default()
.with_text(slot.display_name().to_string())
.with_text(display_name.to_string())
.with_foreground_color(|_| Color::monochrome_u8(200))
.with_width(dimension_bound!(!))
.wrap();
Expand Down
22 changes: 11 additions & 11 deletions korangar/src/interface/elements/containers/friends.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
use std::cell::{RefCell, UnsafeCell};
use std::cell::RefCell;
use std::rc::{Rc, Weak};

use korangar_interface::elements::{
ButtonBuilder, ContainerState, Element, ElementCell, ElementState, ElementWrap, Expandable, Focus, WeakElementCell,
};
use korangar_interface::elements::{ButtonBuilder, ContainerState, Element, ElementCell, ElementState, ElementWrap, Expandable, Focus};
use korangar_interface::event::{ChangeEvent, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::size_bound;
use korangar_interface::state::{PlainRemote, Remote};
use ragnarok_networking::Friend;
use ragnarok_packets::Friend;

use crate::graphics::{InterfaceRenderer, Renderer};
use crate::input::{MouseInputMode, UserEvent};
use crate::interface::application::InterfaceSettings;
use crate::interface::layout::{ScreenClip, ScreenPosition, ScreenSize};
use crate::interface::linked::LinkedElement;
use crate::interface::theme::InterfaceTheme;

pub struct FriendView {
friends: PlainRemote<Vec<(Friend, UnsafeCell<Option<WeakElementCell<InterfaceSettings>>>)>>,
friends: PlainRemote<Vec<(Friend, LinkedElement)>>,
state: ContainerState<InterfaceSettings>,
}

impl FriendView {
pub fn new(friends: PlainRemote<Vec<(Friend, UnsafeCell<Option<WeakElementCell<InterfaceSettings>>>)>>) -> Self {
pub fn new(friends: PlainRemote<Vec<(Friend, LinkedElement)>>) -> Self {
let elements = {
let friends = friends.get();

friends
.iter()
.map(|(friend, linked_element)| {
let element = Self::friend_to_element(friend);
unsafe { *linked_element.get() = Some(Rc::downgrade(&element)) };
linked_element.link(&element);
element
})
.collect()
Expand Down Expand Up @@ -114,15 +113,16 @@ impl Element<InterfaceSettings> for FriendView {
// Remove elements of old friends from the start of the list and add new friends
// to the list.
self.friends.get().iter().enumerate().for_each(|(index, (friend, linked_element))| {
if let Some(linked_element) = unsafe { &(*linked_element.get()) } {
while !std::ptr::addr_eq(linked_element.as_ptr(), Rc::downgrade(&self.state.elements[index]).as_ptr()) {
if linked_element.is_linked() {
while !linked_element.is_linked_to(&self.state.elements[index]) {
self.state.elements.remove(index);
}
} else {
let element = Self::friend_to_element(friend);
unsafe { *linked_element.get() = Some(Rc::downgrade(&element)) };
let weak_self = self.state.state.self_element.clone();

linked_element.link(&element);

element.borrow_mut().link_back(Rc::downgrade(&element), weak_self);

self.state.elements.insert(index, element);
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/interface/elements/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub use self::friends::FriendView;
pub use self::hotbar::HotbarContainer;
pub use self::inventory::InventoryContainer;
#[cfg(feature = "debug")]
pub use self::packet::{PacketEntry, PacketView};
pub use self::packet::{PacketHistoryCallback, PacketHistoryRemote, PacketView};
pub use self::skill_tree::SkillTreeContainer;
Loading

0 comments on commit b34d6e9

Please sign in to comment.