fidl_next/bind/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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.

use core::marker::PhantomData;

use crate::protocol::{self, ProtocolError, Transport};
use crate::{Encode, EncodeError};

use super::{Method, ServerEnd};

/// A storngly typed server sender.
pub struct ServerSender<T: Transport, P> {
    sender: protocol::ServerSender<T>,
    _protocol: PhantomData<P>,
}

impl<T: Transport, P> ServerSender<T, P> {
    /// Wraps an untyped sender reference, returning a typed sender reference.
    pub fn wrap_untyped(client: &protocol::ServerSender<T>) -> &Self {
        unsafe { &*(client as *const protocol::ServerSender<T>).cast() }
    }

    /// Returns the underlying untyped sender.
    pub fn as_untyped(&self) -> &protocol::ServerSender<T> {
        &self.sender
    }

    /// Closes the channel from the server end.
    pub fn close(&self) {
        self.as_untyped().close();
    }
}

impl<T: Transport, P> Clone for ServerSender<T, P> {
    fn clone(&self) -> Self {
        Self { sender: self.sender.clone(), _protocol: PhantomData }
    }
}

/// A protocol which supports servers.
pub trait ServerProtocol<T: Transport, H>: Sized {
    /// Handles a received server one-way message with the given handler.
    fn on_one_way(
        handler: &mut H,
        server: &ServerSender<T, Self>,
        ordinal: u64,
        buffer: T::RecvBuffer,
    );

    /// Handles a received server two-way message with the given handler.
    fn on_two_way(
        handler: &mut H,
        server: &ServerSender<T, Self>,
        ordinal: u64,
        buffer: T::RecvBuffer,
        responder: protocol::Responder,
    );
}

/// An adapter for a server protocol handler.
pub struct ServerAdapter<P, H> {
    handler: H,
    _protocol: PhantomData<P>,
}

impl<P, H> ServerAdapter<P, H> {
    /// Creates a new protocol server handler from a supported handler.
    pub fn from_untyped(handler: H) -> Self {
        Self { handler, _protocol: PhantomData }
    }
}

impl<T, P, H> protocol::ServerHandler<T> for ServerAdapter<P, H>
where
    T: Transport,
    P: ServerProtocol<T, H>,
{
    fn on_one_way(
        &mut self,
        server: &protocol::ServerSender<T>,
        ordinal: u64,
        buffer: T::RecvBuffer,
    ) {
        P::on_one_way(&mut self.handler, ServerSender::wrap_untyped(server), ordinal, buffer)
    }

    fn on_two_way(
        &mut self,
        server: &protocol::ServerSender<T>,
        ordinal: u64,
        buffer: <T as Transport>::RecvBuffer,
        responder: protocol::Responder,
    ) {
        P::on_two_way(
            &mut self.handler,
            ServerSender::wrap_untyped(server),
            ordinal,
            buffer,
            responder,
        )
    }
}

/// A strongly typed server.
pub struct Server<T: Transport, P> {
    server: protocol::Server<T>,
    _protocol: PhantomData<P>,
}

impl<T: Transport, P> Server<T, P> {
    /// Creates a new server from a server end.
    pub fn new(server_end: ServerEnd<T, P>) -> Self {
        Self { server: protocol::Server::new(server_end.into_untyped()), _protocol: PhantomData }
    }

    /// Returns the sender for the server.
    pub fn sender(&self) -> &ServerSender<T, P> {
        ServerSender::wrap_untyped(self.server.sender())
    }

    /// Creates a new server from an untyped server.
    pub fn from_untyped(server: protocol::Server<T>) -> Self {
        Self { server, _protocol: PhantomData }
    }

    /// Runs the server with the provided handler.
    pub async fn run<H>(&mut self, handler: H) -> Result<(), ProtocolError<T::Error>>
    where
        P: ServerProtocol<T, H>,
    {
        self.server.run(ServerAdapter { handler, _protocol: PhantomData::<P> }).await
    }
}

/// A strongly typed `Responder`.
#[must_use]
pub struct Responder<M> {
    responder: protocol::Responder,
    _method: PhantomData<M>,
}

impl<M> Responder<M> {
    /// Creates a new responder.
    pub fn from_untyped(responder: protocol::Responder) -> Self {
        Self { responder, _method: PhantomData }
    }

    /// Responds to the client.
    pub fn respond<'s, T, P, R>(
        self,
        server: &'s ServerSender<T, P>,
        response: &mut R,
    ) -> Result<T::SendFuture<'s>, EncodeError>
    where
        T: Transport,
        M: Method<Protocol = P>,
        for<'buf> R: Encode<T::Encoder<'buf>, Encoded<'buf> = M::Response<'buf>>,
    {
        server.as_untyped().send_response(self.responder, M::ORDINAL, response)
    }
}