Skip to content

Commit

Permalink
update message-io and cargo update
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed May 18, 2023
1 parent 1c7aa8f commit b37e30a
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 328 deletions.
476 changes: 215 additions & 261 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = "1.0"
serde_json = "1.0.59"
log = "0.4.11"
geom = { path = "../geom" }
miniz_oxide = "0.6.2"
miniz_oxide = "0.7"
rustc-hash = "1.1.0"
inline_tweak = {version = "1.0.8"}
log-panics = { version = "2.0.0", features=["with-backtrace"] }
4 changes: 2 additions & 2 deletions native_app/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ mod inner {
pub(crate) fn start_client(info: &mut NetworkConnectionInfo) -> Option<Client> {
let mut s = info.ip.to_string();
if !s.contains(':') {
s += ":80"
s += ":23019"
}
let parsed_addr = match s.to_socket_addrs() {
Ok(x) => match x.into_iter().next() {
Expand All @@ -263,7 +263,7 @@ mod inner {
let client = match networking::Client::connect(ConnectConf {
name: info.name.clone(),
addr: parsed_addr.ip(),
port: if port != 80 { Some(port) } else { None },
port: if port != 23019 { Some(port) } else { None },
frame_buffer_advance: 8,
version: VERSION.to_string(),
}) {
Expand Down
2 changes: 1 addition & 1 deletion networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"

[dependencies]
common = { path = "../common" }
message-io = { version = "0.11", default-features = false, features = ["udp", "tcp"] }
message-io = { version = "0.16", default-features = false, features = ["udp", "tcp"] }
serde = "1.0.124"
log = "0.4.14"

Expand Down
6 changes: 3 additions & 3 deletions networking/src/authent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::packets::{AuthentResponse, ServerReliablePacket, ServerUnreliablePacket};
use crate::{encode, hash_str, Frame, UserID};
use common::{FastMap, FastSet};
use message_io::network::{Endpoint, Network};
use message_io::network::{Endpoint, NetworkController};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::time::Duration;
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Authent {
None
}

pub fn udp_connect(&mut self, e: Endpoint, id: AuthentID, net: &mut Network) {
pub fn udp_connect(&mut self, e: Endpoint, id: AuthentID, net: &mut NetworkController) {
self.addr_to_client.insert(e.addr(), id);
if let Some(ClientConnectState::Connecting { unreliable, .. }) =
self.get_client_state_mut(e)
Expand All @@ -131,7 +131,7 @@ impl Authent {
}
}

pub fn tcp_connected(&mut self, e: Endpoint, net: &mut Network) {
pub fn tcp_connected(&mut self, e: Endpoint, net: &mut NetworkController) {
log::info!("connected:{}", e);

let id = self.next_auth_id();
Expand Down
4 changes: 2 additions & 2 deletions networking/src/catchup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::authent::{Client, ClientGameState};
use crate::packets::ServerReliablePacket;
use crate::{encode, AuthentID, Frame, MergedInputs};
use common::FastMap;
use message_io::network::Network;
use message_io::network::NetworkController;

struct CatchUpState {
inputs: Vec<MergedInputs>,
Expand Down Expand Up @@ -48,7 +48,7 @@ impl CatchUp {
}
}

pub fn update(&mut self, c: &mut Client, net: &mut Network) {
pub fn update(&mut self, c: &mut Client, net: &mut NetworkController) {
let state = match self.frame_history.get_mut(&c.id) {
Some(x) => x,
None => return,
Expand Down
68 changes: 38 additions & 30 deletions networking/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;

use message_io::events::EventQueue;
use message_io::network::{Endpoint, NetEvent, Network, Transport};
use message_io::network::{Endpoint, NetEvent, NetworkController, NetworkProcessor, Transport};
use serde::de::DeserializeOwned;
use serde::Serialize;

Expand Down Expand Up @@ -62,8 +62,8 @@ enum ClientState<W, I> {
}

pub struct Client<WORLD: DeserializeOwned, INPUT: Serialize + DeserializeOwned + Default> {
network: Network,
events: EventQueue<NetEvent>,
network: NetworkController,
events: Option<NetworkProcessor>,
tcp: Endpoint,
udp: Endpoint,

Expand All @@ -88,15 +88,15 @@ pub struct ConnectConf {

impl<W: DeserializeOwned, I: Serialize + DeserializeOwned + Default> Client<W, I> {
pub fn connect(conf: ConnectConf) -> io::Result<Self> {
let (mut network, events) = Network::split();
let (network, events) = message_io::network::split();
let addr = conf.addr;
let port = conf.port.unwrap_or(DEFAULT_PORT);
let (tcp, _) = network.connect(Transport::FramedTcp, SocketAddr::new(addr, port))?;
let (udp, _) = network.connect(Transport::Udp, SocketAddr::new(addr, port))?;

Ok(Self {
network,
events,
events: Some(events),
tcp,
udp,
state: ClientState::Connecting,
Expand All @@ -110,38 +110,46 @@ impl<W: DeserializeOwned, I: Serialize + DeserializeOwned + Default> Client<W, I

#[allow(clippy::collapsible_if)]
pub fn poll(&mut self, input: I) -> PollResult<W, I> {
while let Some(x) = self.events.try_receive() {
match x {
NetEvent::Message(e, m) => {
match e.resource_id().adapter_id() == Transport::FramedTcp.id() {
true => {
if let Some(packet) = decode(&m) {
self.message_reliable(packet);
} else {
log::error!("could not decode reliable packet from server");
let mut called = true;
let mut events = self.events.take().unwrap();
while called {
called = false;
events.process_poll_event(Some(Duration::ZERO), |x| {
called = true;
match x {
NetEvent::Message(e, m) => {
match e.resource_id().adapter_id() == Transport::FramedTcp.id() {
true => {
if let Some(packet) = decode(&m) {
self.message_reliable(packet);
} else {
log::error!("could not decode reliable packet from server");
}
}
}
false => {
if let Some(packet) = decode(&m) {
self.message_unreliable(packet)
} else {
log::error!("could not decode unreliable packet from server");
false => {
if let Some(packet) = decode(&m) {
self.message_unreliable(packet)
} else {
log::error!("could not decode unreliable packet from server");
}
}
}
}
}
NetEvent::Connected(e, _) => {
log::info!("connected {}", e)
}
NetEvent::Disconnected(_) => {
if !matches!(self.state, ClientState::Disconnected { .. }) {
self.state = ClientState::Disconnected {
reason: "connection lost".to_string(),
NetEvent::Connected(e, _) => {
log::info!("connected {}", e)
}
NetEvent::Disconnected(_) => {
if !matches!(self.state, ClientState::Disconnected { .. }) {
self.state = ClientState::Disconnected {
reason: "connection lost".to_string(),
}
}
}
NetEvent::Accepted(_, _) => {}
}
}
});
}
self.events = Some(events);

match self.state {
ClientState::Disconnected { ref reason } => {
Expand Down
57 changes: 32 additions & 25 deletions networking/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::io;
use std::time::Duration;

use message_io::events::EventQueue;
use message_io::network::{Endpoint, NetEvent, Network, Transport};
use message_io::network::{Endpoint, NetEvent, NetworkController, NetworkProcessor, Transport};
use serde::Serialize;

use crate::authent::{Authent, AuthentID, ClientGameState};
Expand Down Expand Up @@ -46,8 +45,8 @@ struct VirtualClient {
}

pub struct Server<WORLD: Serialize, INPUT> {
network: Network,
events: EventQueue<NetEvent>,
network: NetworkController,
events: Option<NetworkProcessor>,

authent: Authent,
v_client: Option<VirtualClient>,
Expand All @@ -67,7 +66,7 @@ pub struct Server<WORLD: Serialize, INPUT> {

impl<WORLD: 'static + Serialize, INPUT: Serialize + DeserializeOwned> Server<WORLD, INPUT> {
pub fn start(conf: ServerConfiguration) -> io::Result<Self> {
let (mut network, events) = Network::split();
let (network, events) = message_io::network::split();

let port = conf.port.unwrap_or(DEFAULT_PORT);
let (_, tcp_addr) = network.listen(Transport::FramedTcp, format!("0.0.0.0:{}", port))?;
Expand All @@ -81,7 +80,7 @@ impl<WORLD: 'static + Serialize, INPUT: Serialize + DeserializeOwned> Server<WOR

Ok(Self {
network,
events,
events: Some(events),
step: Timestep::new(conf.period),
buffer: ServerPlayoutBuffer::new(conf.start_frame),
v_client,
Expand All @@ -102,28 +101,36 @@ impl<WORLD: 'static + Serialize, INPUT: Serialize + DeserializeOwned> Server<WOR
frame: Frame,
local_inputs: Option<INPUT>,
) -> ServerPollResult<INPUT> {
while let Some(ev) = self.events.try_receive() {
match ev {
NetEvent::Message(e, data) => match is_reliable(&e) {
true => {
if let Some(packet) = decode::<ClientReliablePacket>(&data) {
let _ = self.message_reliable(e, packet, world, frame);
} else {
log::error!("client sent invalid reliable packet");
let mut called = true;
let mut events = self.events.take().unwrap();
while called {
called = false;
events.process_poll_event(Some(Duration::ZERO), |ev| {
called = true;
match ev {
NetEvent::Message(e, data) => match is_reliable(&e) {
true => {
if let Some(packet) = decode::<ClientReliablePacket>(&data) {
let _ = self.message_reliable(e, packet, world, frame);
} else {
log::error!("client sent invalid reliable packet");
}
}
}
false => {
if let Some(packet) = decode::<ClientUnreliablePacket>(&data) {
let _ = self.message_unreliable(e, packet);
} else {
log::error!("client sent invalid unreliable packet");
false => {
if let Some(packet) = decode::<ClientUnreliablePacket>(&data) {
let _ = self.message_unreliable(e, packet);
} else {
log::error!("client sent invalid unreliable packet");
}
}
}
},
NetEvent::Connected(e, _) => self.tcp_connected(e),
NetEvent::Disconnected(e) => self.tcp_disconnected(e),
}
},
NetEvent::Accepted(e, _) => self.tcp_connected(e),
NetEvent::Disconnected(e) => self.tcp_disconnected(e),
NetEvent::Connected(_, _) => {}
}
});
}
self.events = Some(events);

self.send_merged_inputs();
self.send_long_running();
Expand Down
11 changes: 8 additions & 3 deletions networking/src/worldsend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::authent::{Client, ClientGameState};
use crate::packets::{ClientReliablePacket, ServerReliablePacket, WorldDataFragment};
use crate::{decode, encode, AuthentID, Frame, MAX_WORLDSEND_PACKET_SIZE};
use common::FastMap;
use message_io::network::{Endpoint, Network};
use message_io::network::{Endpoint, NetworkController};
use serde::de::DeserializeOwned;

#[derive(Eq, PartialEq)]
Expand Down Expand Up @@ -47,7 +47,7 @@ impl WorldSend {
}
}

pub fn update(&mut self, c: &mut Client, net: &mut Network) {
pub fn update(&mut self, c: &mut Client, net: &mut NetworkController) {
if let Some(state) = self.send_state.get_mut(&c.id) {
if state.status == WorldSendStatus::Over {
self.send_state.remove(&c.id);
Expand Down Expand Up @@ -123,7 +123,12 @@ impl<W> Default for WorldReceive<W> {
}

impl<W: DeserializeOwned> WorldReceive<W> {
pub fn handle(&mut self, fragment: WorldDataFragment, net: &mut Network, tcp: Endpoint) {
pub fn handle(
&mut self,
fragment: WorldDataFragment,
net: &mut NetworkController,
tcp: Endpoint,
) {
if let WorldReceive::Downloading {
ref mut datasize,
ref mut data_so_far,
Expand Down

0 comments on commit b37e30a

Please sign in to comment.