-
Notifications
You must be signed in to change notification settings - Fork 69
Codebase improvements. #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TimonPost
merged 6 commits into
TimonPost:master
from
fraillt:better_interfaces_separation
Oct 10, 2019
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7598af4
SocketController, ConnectionController and Utilities for unit tests
fraillt 3e07d88
Suggestions from code review. ROUND1
fraillt 441732c
link conditioner behind feature=tester
fraillt c828ff0
bugfix in test utils, and more integration tests
fraillt 2110b46
Addressing some issues from code review: ROUND3
fraillt 024690b
new Connection trait and connection manager
fraillt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #[derive(Debug)] | ||
| pub(crate) enum Either<L, R> { | ||
| Left(L), | ||
| Right(R), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,18 @@ | ||
| //! This module provides the logic between the low-level abstract types and the types that the user will be interacting with. | ||
| //! You can think of the socket, connection management, congestion control. | ||
|
|
||
| mod connection; | ||
| mod connection_controller; | ||
| mod events; | ||
| mod link_conditioner; | ||
| mod quality; | ||
| mod socket; | ||
| mod socket_controller; | ||
| mod virtual_connection; | ||
|
|
||
| pub mod constants; | ||
|
|
||
| pub use self::connection_controller::ConnectionController; | ||
| pub use self::events::SocketEvent; | ||
| pub use self::link_conditioner::LinkConditioner; | ||
| pub use self::quality::{NetworkQuality, RttMeasurer}; | ||
| pub use self::socket::Socket; | ||
| pub use self::socket_controller::{SocketController, SocketReceiver, SocketSender}; | ||
| pub use self::virtual_connection::VirtualConnection; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| use crate::{ | ||
| config::Config, | ||
| error::Result, | ||
| net::{events::SocketEvent, SocketSender, VirtualConnection}, | ||
| packet::{DeliveryGuarantee, OutgoingPackets, Packet, PacketInfo}, | ||
| }; | ||
| use crossbeam_channel::Sender; | ||
| use log::error; | ||
| use std::{self, net::SocketAddr, time::Instant}; | ||
|
|
||
| /// Controls all aspects of the connection: | ||
| /// * Processes incoming data (from a socket) or events (from a user). | ||
| /// * Updates connection state: resends dropped packets, sends heartbeat packet, etc. | ||
| /// * Creates new connections. | ||
| /// * Checks if connection should be dropped. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| /// It doesn't own connections, but only owns necessary components to process them. | ||
| #[derive(Debug)] | ||
| pub struct ConnectionController<PacketSender> { | ||
| config: Config, | ||
| packet_sender: PacketSender, | ||
| event_sender: Sender<SocketEvent>, | ||
| } | ||
|
|
||
| /// Defines a connection type. | ||
| type Connection = VirtualConnection; | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| /// Defines a user event type. | ||
| type UserEvent = Packet; | ||
| /// Defines a connection event type. | ||
| type ConnectionEvent = SocketEvent; | ||
|
|
||
| impl<PacketSender: SocketSender> ConnectionController<PacketSender> { | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| /// Creates a new instance of `ConnectionHandler`. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn new( | ||
| config: Config, | ||
| packet_sender: PacketSender, | ||
| event_sender: Sender<ConnectionEvent>, | ||
| ) -> Self { | ||
| ConnectionController { | ||
| config, | ||
| packet_sender, | ||
| event_sender, | ||
| } | ||
| } | ||
|
|
||
| /// Creates new connection. Also will init it and send connection event to a user. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn create_connection( | ||
| &self, | ||
| address: SocketAddr, | ||
| time: Instant, | ||
| initial_data: Option<&[u8]>, | ||
| ) -> Connection { | ||
| // emit connect event if this is initiated by remote host. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| if initial_data.is_some() { | ||
| self.event_sender | ||
| .send(ConnectionEvent::Connect(address)) | ||
| .unwrap(); | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| } | ||
| Connection::new(address, &self.config, time) | ||
| } | ||
|
|
||
| /// Determine if this connection should be dropped due to its state. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn should_drop(&self, connection: &mut Connection, time: Instant) -> bool { | ||
| let should_drop = connection.packets_in_flight() > self.config.max_packets_in_flight | ||
| || connection.last_heard(time) >= self.config.idle_connection_timeout; | ||
| if should_drop { | ||
| self.event_sender | ||
| .send(ConnectionEvent::Timeout(connection.remote_address)) | ||
| .unwrap(); | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| } | ||
| should_drop | ||
| } | ||
|
|
||
| /// Handle a packet received from a socket. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn handle_packet(&mut self, connection: &mut Connection, payload: &[u8], time: Instant) { | ||
| match connection.process_incoming(payload, time) { | ||
| Ok(packets) => { | ||
| for incoming in packets { | ||
| self.event_sender | ||
| .send(ConnectionEvent::Packet(incoming.0)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| Err(err) => error!("Error occured processing incomming packet: {:?}", err), | ||
| } | ||
| } | ||
|
|
||
| /// Handle an event received from a user. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn handle_event(&mut self, connection: &mut Connection, event: UserEvent, time: Instant) { | ||
| self.send_packets( | ||
| &connection.remote_address.clone(), | ||
| connection.process_outgoing( | ||
| PacketInfo::user_packet( | ||
| event.payload(), | ||
| event.delivery_guarantee(), | ||
| event.order_guarantee(), | ||
| ), | ||
| None, | ||
| time, | ||
| ), | ||
| "user packet", | ||
| ); | ||
| } | ||
|
|
||
| /// Process various connection related tasks: resend dropped packets, send heartbeat packet, etc... | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| /// This function gets called very frequently. | ||
| pub fn update(&mut self, connection: &mut Connection, time: Instant) { | ||
| // resend dropped packets | ||
| let dropped_packets = connection.gather_dropped_packets(); | ||
| for dropped in dropped_packets { | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| let packets = connection.process_outgoing( | ||
| PacketInfo { | ||
| packet_type: dropped.packet_type, | ||
| payload: &dropped.payload, | ||
| // Because a delivery guarantee is only sent with reliable packets | ||
| delivery: DeliveryGuarantee::Reliable, | ||
| // This is stored with the dropped packet because they could be mixed | ||
| ordering: dropped.ordering_guarantee, | ||
| }, | ||
| dropped.item_identifier, | ||
| time, | ||
| ); | ||
| self.send_packets(&connection.remote_address, packets, "dropped packets"); | ||
| } | ||
|
|
||
| // send heartbeat packets if required. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| if let Some(heartbeat_interval) = self.config.heartbeat_interval { | ||
| if connection.last_sent(time) >= heartbeat_interval { | ||
| self.send_packets( | ||
| &connection.remote_address.clone(), | ||
| connection.process_outgoing(PacketInfo::heartbeat_packet(&[]), None, time), | ||
| "heatbeat packet", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Helper function that sends multiple outgoing packets | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| fn send_packets( | ||
| &mut self, | ||
| address: &SocketAddr, | ||
| packets: Result<OutgoingPackets>, | ||
| err_context: &str, | ||
| ) { | ||
| match packets { | ||
| Ok(packets) => { | ||
| for outgoing in packets { | ||
| if let Err(error) = self | ||
| .packet_sender | ||
| .send_packet(address, &outgoing.contents()) | ||
| { | ||
| error!("Error occured sending {}: {:?}", err_context, error); | ||
| } | ||
| } | ||
| } | ||
| Err(error) => error!("Error occured processing {}: {:?}", err_context, error), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.