Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Separate senders for packets and channels #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl NetworkResource {
task_pool.clone(),
packet_rx,
server_socket.get_sender(),
server_socket.get_sender(),
address,
),
));
Expand Down Expand Up @@ -340,15 +341,17 @@ impl NetworkResource {
socket
}
};
let sender = client_socket.get_sender();
let packet_sender = client_socket.get_sender();
let channel_sender = client_socket.get_sender();

self.pending_connections
.lock()
.unwrap()
.push(Box::new(transport::ClientConnection::new(
self.task_pool.clone(),
client_socket,
sender,
packet_sender,
channel_sender,
)));
}

Expand Down
32 changes: 16 additions & 16 deletions src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub struct ServerConnection {
task_pool: TaskPool,

packet_rx: crossbeam_channel::Receiver<Result<Packet, NetworkError>>,
sender: Option<ServerSender>,
packet_sender: ServerSender,
channel_sender: Option<ServerSender>,
client_address: SocketAddr,
stats: Arc<RwLock<PacketStats>>,

Expand All @@ -128,13 +129,15 @@ impl ServerConnection {
pub fn new(
task_pool: TaskPool,
packet_rx: crossbeam_channel::Receiver<Result<Packet, NetworkError>>,
sender: ServerSender,
packet_sender: ServerSender,
channel_sender: ServerSender,
client_address: SocketAddr,
) -> Self {
ServerConnection {
task_pool,
packet_rx,
sender: Some(sender),
packet_sender,
channel_sender: Some(channel_sender),
client_address,
stats: Arc::new(RwLock::new(PacketStats::default())),
channels: None,
Expand All @@ -157,10 +160,7 @@ impl Connection for ServerConnection {
fn send(&mut self, payload: Packet) -> Result<(), Box<dyn Error + Sync + Send>> {
self.stats.write().expect("stats lock poisoned").add_tx(payload.len());
block_on(
self.sender
.as_mut()
.unwrap()
.send(ServerPacket::new(self.client_address, payload.to_vec())),
self.packet_sender.send(ServerPacket::new(self.client_address, payload.to_vec())),
)
}

Expand Down Expand Up @@ -201,7 +201,7 @@ impl Connection for ServerConnection {
let (channels_rx, mut channels_tx) = multiplexer.start();
self.channels_rx = Some(channels_rx);

let mut sender = self.sender.take().unwrap();
let mut sender = self.channel_sender.take().unwrap();
let client_address = self.client_address;
let stats = self.stats.clone();
self.channels_task = Some(self.task_pool.spawn(async move {
Expand Down Expand Up @@ -229,7 +229,8 @@ pub struct ClientConnection {
task_pool: TaskPool,

socket: Box<dyn ClientSocketTrait>,
sender: Option<ClientSender>,
packet_sender: ClientSender,
channel_sender: Option<ClientSender>,
stats: Arc<RwLock<PacketStats>>,

channels: Option<MessageChannels>,
Expand All @@ -246,12 +247,14 @@ impl ClientConnection {
pub fn new(
task_pool: TaskPool,
socket: Box<dyn ClientSocketTrait>,
sender: ClientSender,
packet_sender: ClientSender,
channel_sender: ClientSender,
) -> Self {
ClientConnection {
task_pool,
socket,
sender: Some(sender),
packet_sender,
channel_sender: Some(channel_sender),
stats: Arc::new(RwLock::new(PacketStats::default())),
channels: None,
channels_rx: None,
Expand All @@ -277,10 +280,7 @@ impl Connection for ClientConnection {

fn send(&mut self, payload: Packet) -> Result<(), Box<dyn Error + Sync + Send>> {
self.stats.write().expect("stats lock poisoned").add_tx(payload.len());
self.sender
.as_mut()
.unwrap()
.send(ClientPacket::new(payload.to_vec()))
self.packet_sender.send(ClientPacket::new(payload.to_vec()))
}

fn receive(&mut self) -> Option<Result<Packet, NetworkError>> {
Expand All @@ -307,7 +307,7 @@ impl Connection for ClientConnection {
let (channels_rx, mut channels_tx) = multiplexer.start();
self.channels_rx = Some(channels_rx);

let mut sender = self.sender.take().unwrap();
let mut sender = self.channel_sender.take().unwrap();
let stats = self.stats.clone();
#[allow(unused_variables)]
let channels_task = self.task_pool.spawn(async move {
Expand Down