Skip to main content

tfserver/server/
handler.rs

1use std::net::SocketAddr;
2use std::sync::{Arc};
3use async_trait::async_trait;
4use tokio::sync::{RwLock};
5use tokio::sync::oneshot::Sender;
6use tokio_util::bytes::{BytesMut};
7use tokio_util::codec::{Framed};
8use crate::codec::codec_trait::TfCodec;
9use crate::structures::s_type::StructureType;
10use crate::structures::traffic_proc::TrafficProcessorHolder;
11use crate::structures::transport::Transport;
12
13#[async_trait]
14///The server handler trait. Used for handling data from client/
15pub trait Handler: Send + Sync {
16    type Codec:  TfCodec;
17    ///The serve_route called by router, when the new data arrived and designated to registered to this handler structure_types.
18    ///
19    /// 'client_meta' is client info, and signal for requesting to move stream.
20    ///If request needed, call take() on this option
21    ///'''if let Some(tx) = meta.1.take(){
22    ///'''            tx.send(...).unwrap();
23    ///'''}
24    /// 's_type' is identified request structure type
25    /// 'data' is binary representation of the structure. Call the deserialize from s_type to turn it into base structure.
26    async fn serve_route(
27        &mut self,
28        /*If request needed, call take() on this option
29        *if let Some(tx) = meta.1.take(){
30        *            tx.send(...).unwrap();
31        *        }
32        */
33        client_meta: (SocketAddr,  &mut Option<Sender<Arc<RwLock<dyn Handler<Codec = Self::Codec>>>>>),
34        s_type: Box<dyn StructureType>,
35        data: BytesMut,
36    ) -> Result<Vec<u8>, Vec<u8>>;
37
38    ///This function called, when server received the request of handler to move stream. It returns all needed data for this stream.
39    async fn accept_stream(&mut self, add: SocketAddr, stream: (Framed<Transport, Self::Codec>, TrafficProcessorHolder<Self::Codec>));
40
41}