|
| 1 | +use crate::endpoint::{Endpoint}; |
| 2 | +use crate::resource_id::{ResourceId}; |
| 3 | +use crate::poll::{PollRegister}; |
| 4 | +use crate::util::{SendingStatus}; |
| 5 | + |
| 6 | +use std::net::{SocketAddr}; |
| 7 | +use std::io::{self}; |
| 8 | + |
| 9 | +/// Struct used to pack the events generated by the adapter. |
| 10 | +/// The upper layer will traduce this event into a [crate::network::NetEvent] |
| 11 | +/// that the user can manage. |
| 12 | +pub enum AdapterEvent<'a> { |
| 13 | + Added, |
| 14 | + Data(&'a [u8]), |
| 15 | + Removed, |
| 16 | +} |
| 17 | + |
| 18 | +/// High level trait to represent an adapter for a transport protocol. |
| 19 | +/// The adapter is used to pack a [`Controller`] and [`Adapter`]. |
| 20 | +/// Two traits to describes how an adapter behaves. |
| 21 | +pub trait Adapter<C> |
| 22 | +where C: FnMut(Endpoint, AdapterEvent<'_>) |
| 23 | +{ |
| 24 | + type Controller: Controller + Send; |
| 25 | + type Processor: Processor<C> + Send; |
| 26 | + |
| 27 | + /// Creates a [`Controller`] and [`Processor`] that represents the adapter. |
| 28 | + /// The **implementator** must create their [`Controller`] and [`Processor`] here. |
| 29 | + fn split(self, poll_register: PollRegister) -> (Self::Controller, Self::Processor); |
| 30 | +} |
| 31 | + |
| 32 | +/// It is in change to perform direct actions from the user. |
| 33 | +pub trait Controller { |
| 34 | + /// The user performs a connection request to an specific address. |
| 35 | + /// The **implementator** is in change of creating the corresponding instance in order |
| 36 | + /// to manage it later. |
| 37 | + fn connect(&mut self, addr: SocketAddr) -> io::Result<Endpoint>; |
| 38 | + |
| 39 | + /// The user performs a listening request from an specific address. |
| 40 | + /// The **implementator** is in change of creating the corresponding instance in order |
| 41 | + /// to manage it later. |
| 42 | + fn listen(&mut self, addr: SocketAddr) -> io::Result<(ResourceId, SocketAddr)>; |
| 43 | + |
| 44 | + /// The user performs a remove action in order to remove a resource generated by |
| 45 | + /// [connect()] and [listen()] functions. |
| 46 | + /// The **implementator** must remove the resource here. |
| 47 | + fn remove(&mut self, id: ResourceId) -> Option<()>; |
| 48 | + |
| 49 | + /// The user wants to get the local address of some resource (listener or remote). |
| 50 | + /// The **implementator** must return that address or None if the resource is not found. |
| 51 | + /// Note: The *peer* address can be retreived from the Endpoint that the user already has. |
| 52 | + fn local_addr(&self, id: ResourceId) -> Option<SocketAddr>; |
| 53 | + |
| 54 | + /// Sends a raw message by the specific endpoint. |
| 55 | + /// The **implementator** is in charge to send the `data` using the instance represented by |
| 56 | + /// `endpoint.resource_id()`. |
| 57 | + fn send(&mut self, endpoint: Endpoint, data: &[u8]) -> SendingStatus; |
| 58 | +} |
| 59 | + |
| 60 | +/// It is in change to perform eventual actions comming from the internal network engine. |
| 61 | +/// The `event_callback` is the action that will be performed when an [AdapterEvent] is |
| 62 | +/// generated for some `Endpoint`. |
| 63 | +pub trait Processor<C> |
| 64 | +where C: FnMut(Endpoint, AdapterEvent<'_>) |
| 65 | +{ |
| 66 | + /// Called when a listener received an event. |
| 67 | + /// It means that an endpoint has try to connect and the connection should accept. |
| 68 | + /// The `id` represents the listener that have generated the event. |
| 69 | + /// The **implementator** is in charge of retrive the instance represented by this `id` |
| 70 | + /// to accept that connection. |
| 71 | + fn process_listener(&mut self, id: ResourceId, event_callback: &mut C); |
| 72 | + |
| 73 | + /// Called when a remote endpoint received an event. |
| 74 | + /// It means that the endpoint has available data to read, |
| 75 | + /// or there is some connection related issue, as a disconnection. |
| 76 | + /// The `id` represents the remote entity that has generated the event. |
| 77 | + /// The **implementator** is in charge of retrive the instance represented by this `id` |
| 78 | + /// and process the event. |
| 79 | + fn process_remote(&mut self, id: ResourceId, event_callback: &mut C); |
| 80 | +} |
0 commit comments