mmids_core/net/tcp/mod.rs
1//! A TCP socket manager actor that allows other systems to request TCP connections. The socket
2//! manager will manage listeners for different ports, accept connections, unwrap SSL sessions (if
3//! requested), and pass networked data to requesters.
4mod listener;
5mod socket_manager;
6
7use super::ConnectionId;
8use bytes::Bytes;
9use native_tls::Identity;
10use std::net::SocketAddr;
11use tokio::sync::mpsc;
12
13pub use listener::OutboundPacket;
14pub use socket_manager::start as start_socket_manager;
15
16/// Reasons why the request to listen for TCP connections can fail
17#[derive(Debug)]
18pub enum RequestFailureReason {
19 /// The port being requested has already been opened for another requester
20 PortInUse,
21
22 /// A TLS port was requested to be opened, but the certificate could not be opened
23 InvalidCertificate(String),
24
25 /// A TLS port was requested to be opened, but the password provided did not unlock the
26 /// provided certificate.
27 CertPasswordIncorrect,
28
29 /// A TLS port was requested to be opened, but the TCP socket manager was not given any
30 /// details required to accept TLS sessions.
31 NoTlsDetailsGiven,
32}
33
34/// Options required for TLS session handling
35pub struct TlsOptions {
36 pub certificate: Identity,
37}
38
39/// Requests by callers to the TCP socket manager
40#[derive(Debug)]
41pub enum TcpSocketRequest {
42 /// Request for the server to start listening on a specific TCP port
43 OpenPort {
44 /// TCP port to be opened
45 port: u16,
46
47 /// If the port should be accepting TLS connections or not
48 use_tls: bool,
49
50 /// The channel in which responses should be sent. If the port is successfully opened
51 /// then all state changes for the port (such as new connections) will use this channel
52 /// for notifications
53 response_channel: mpsc::UnboundedSender<TcpSocketResponse>,
54 },
55}
56
57#[derive(Debug)]
58/// Response messages that the TCP socket manager may send back
59pub enum TcpSocketResponse {
60 /// Notification that the specified request that was previously made was accepted
61 RequestAccepted {},
62
63 /// Notification that the specified request that was previously made was denied
64 RequestDenied {
65 /// Reason why the request was denied
66 reason: RequestFailureReason,
67 },
68
69 /// Notification to system that requested a port be opened that the port has been
70 /// forced closed. This is mostly due to an error listening onto the socket.
71 PortForciblyClosed { port: u16 },
72
73 /// Notification that a client has connected to a TCP port opened by the receiver of this
74 /// notification.
75 NewConnection {
76 /// The port the TCP connection came in on
77 port: u16,
78
79 /// Unique identifier for this new connection
80 connection_id: ConnectionId,
81
82 /// Channel the owner can use to receive bytes sent from the client
83 incoming_bytes: mpsc::UnboundedReceiver<Bytes>,
84
85 /// Channel the owner can use to send bytes to the client
86 outgoing_bytes: mpsc::UnboundedSender<OutboundPacket>,
87
88 /// The socket address the client connected from
89 socket_address: SocketAddr,
90 },
91
92 /// Notification that a client has disconnected from a TCP port
93 Disconnection {
94 /// Unique identifier of the connection that disconnected
95 connection_id: ConnectionId,
96 },
97}