-
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 4 commits
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,20 @@ | ||
| //! 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,162 @@ | ||
| 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 the connection should be dropped. | ||
| /// 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 `ConnectionController`. | ||
| pub fn new( | ||
| config: Config, | ||
| packet_sender: PacketSender, | ||
| event_sender: Sender<ConnectionEvent>, | ||
| ) -> Self { | ||
| ConnectionController { | ||
| config, | ||
| packet_sender, | ||
| event_sender, | ||
| } | ||
| } | ||
|
|
||
| /// Creates new connection and initialize it by sending an connection event to the user. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| /// * address - defines a address that connection is associated with. | ||
| /// * time - creation time, used by connection, so that it doesn't get dropped immediately or send heartbeat packet. | ||
| /// * initial_data - if initiated by remote host, this will hold that a packet data. | ||
| pub fn create_connection( | ||
| &self, | ||
| address: SocketAddr, | ||
| time: Instant, | ||
| initial_data: Option<&[u8]>, | ||
| ) -> Connection { | ||
| // Emit connect event if this is initiated by the remote host. | ||
| if initial_data.is_some() { | ||
| self.event_sender | ||
| .send(SocketEvent::Connect(address)) | ||
| .expect("Event receiver must exists."); | ||
| } | ||
| Connection::new(address, &self.config, time) | ||
| } | ||
|
|
||
| /// Determine if the given `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(SocketEvent::Timeout(connection.remote_address)) | ||
| .expect("Event receiver must exists."); | ||
| } | ||
| should_drop | ||
| } | ||
|
|
||
| /// Process a received packet: parse it and emit an event. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn process_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(SocketEvent::Packet(incoming.0)) | ||
| .expect("Event receiver must exists."); | ||
| } | ||
| } | ||
| Err(err) => error!("Error occured processing incomming packet: {:?}", err), | ||
| } | ||
| } | ||
|
|
||
| /// Process a received event and send a packet. | ||
|
fraillt marked this conversation as resolved.
Outdated
|
||
| pub fn process_event(&mut self, connection: &mut Connection, event: UserEvent, time: Instant) { | ||
| let addr = connection.remote_address; | ||
| self.send_packets( | ||
| &addr, | ||
| 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 | ||
| for dropped in connection.gather_dropped_packets() { | ||
| 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 | ||
| if let Some(heartbeat_interval) = self.config.heartbeat_interval { | ||
| let addr = connection.remote_address; | ||
| if connection.last_sent(time) >= heartbeat_interval { | ||
| self.send_packets( | ||
| &addr, | ||
| connection.process_outgoing(PacketInfo::heartbeat_packet(&[]), None, time), | ||
| "heatbeat packet", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Sends multiple outgoing packets. | ||
| 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.