fidl_next/protocol/
server.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
95
96
97
98
99
100
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! FIDL protocol servers.

use crate::protocol::{encode_buffer, MessageBuffer, ProtocolError, Transport};
use crate::{Decoder, Encode, EncodeError, Encoder};

/// Makes a server endpoint from a transport endpoint.
pub fn make_server<T: Transport>(transport: T) -> (Server<T>, Requests<T>) {
    let (sender, receiver) = transport.split();
    (Server { sender }, Requests { receiver })
}

/// A sender for a server endpoint.
#[derive(Clone)]
pub struct Server<T: Transport> {
    sender: T::Sender,
}

impl<T: Transport> Server<T> {
    /// Send an event.
    pub fn send_event<'s, M>(
        &'s self,
        ordinal: u64,
        event: &mut M,
    ) -> Result<T::SendFuture<'s>, EncodeError>
    where
        for<'a> T::Encoder<'a>: Encoder,
        M: for<'a> Encode<T::Encoder<'a>>,
    {
        let mut buffer = T::acquire(&self.sender);
        encode_buffer(&mut buffer, 0, ordinal, event)?;
        Ok(T::send(&self.sender, buffer))
    }

    /// Send a response to a transactional request.
    pub fn send_response<'s, M>(
        &'s self,
        responder: Responder,
        ordinal: u64,
        response: &mut M,
    ) -> Result<T::SendFuture<'s>, EncodeError>
    where
        for<'a> T::Encoder<'a>: Encoder,
        M: for<'a> Encode<T::Encoder<'a>>,
    {
        let mut buffer = T::acquire(&self.sender);
        encode_buffer(&mut buffer, responder.txid, ordinal, response)?;
        Ok(T::send(&self.sender, buffer))
    }
}

/// A receiver for a server endpoint.
pub struct Requests<T: Transport> {
    receiver: T::Receiver,
}

impl<T: Transport> Requests<T> {
    /// Returns the next request from the client, if any.
    pub async fn next(&mut self) -> Result<Option<Request<T>>, ProtocolError<T::Error>>
    where
        for<'a> T::Decoder<'a>: Decoder<'a>,
    {
        let next = T::recv(&mut self.receiver).await.map_err(ProtocolError::TransportError)?;

        if let Some(buffer) = next {
            let (txid, buffer) = MessageBuffer::parse_header(buffer)?;
            if txid == 0 {
                Ok(Some(Request::OneWay { buffer }))
            } else {
                Ok(Some(Request::Transaction { responder: Responder { txid }, buffer }))
            }
        } else {
            Ok(None)
        }
    }
}

/// A request sent to the server.
pub enum Request<T: Transport> {
    /// A one-way request which does not expect a response.
    OneWay {
        /// The buffer containing the request.
        buffer: MessageBuffer<T>,
    },
    /// A transactional request which expects a response.
    Transaction {
        /// A responder which can be used to answer this request.
        responder: Responder,
        /// The buffer containing the request.
        buffer: MessageBuffer<T>,
    },
}

/// A responder for a transactional request.
pub struct Responder {
    txid: u32,
}