Skip to main content

tfserver/server/
handler.rs

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