pub struct TcpServerHandle(_);

Implementations§

Examples found in repository?
src/io/tcp/server.rs (line 93)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    pub fn start(
        sender: mpsc::Sender<TcpClientAction>,
        address: String,
        port: u16,
    ) -> (TcpServerHandle, JoinHandle<Result<(), TcpServerError>>) {
        let (tcp_server_sender, mut receiver) = mpsc::channel(BUFFER_SIZE);
        let join_handle = tokio::spawn(async move {
            let (listener, mut client_tasks) = Self::bind_address(address, port).await?;
            let mut result: Result<(), TcpServerError> = Ok(());
            loop {
                select! {
                     accept_result = listener.accept() => { // Listen to new clients.
                        let (stream, _socket) = match accept_result {
                            Ok((stream, _socket)) => (stream, _socket),
                            Err(msg) => { // Stop server immediately if accept returns an error
                                result = Err(TcpServerError::AcceptClientError(msg));
                                break
                            }
                        };
                        client_tasks.push(TcpClientTask::handle_client(stream, sender.clone()));
                        // Create a TcpClientTask (which spawns a new task, to handle the client connection)
                    }
                    Some(_finished_client_task) = client_tasks.next() => { // Listen to exiting tcp client tasks
                        //TODO handle client task exiting
                    }
                    Some(TcpServerMessage::Stop) = receiver.recv() => { // Listen to tcpserver handle messages (e.g stop server)
                        info!("Server just stopped listening to messages");
                        break
                    }
                }
            }
            //TODO do we need to signal the client tasks to stop?
            futures::future::join_all(client_tasks).await; // Wait for all child tasks to terminate
            result
        });
        (TcpServerHandle::new(tcp_server_sender), join_handle)
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more