1use core::future::Future;
6use core::marker::PhantomData;
7use core::ops::Deref;
8
9use fidl_next_codec::{Encode, Wire};
10use fidl_next_protocol::{
11 self as protocol, LocalServerHandler, Message, ProtocolError, ServerHandler, Transport,
12};
13
14use crate::{
15 HasConnectionHandles, HasTransport, Method, Respond, RespondErr, RespondFuture, ServerEnd,
16 TwoWayMethod,
17};
18
19#[repr(transparent)]
21pub struct Server<P, T: Transport = <P as HasTransport>::Transport> {
22 server: protocol::Server<T>,
23 _protocol: PhantomData<P>,
24}
25
26unsafe impl<P, T> Send for Server<P, T>
27where
28 protocol::Server<T>: Send,
29 T: Transport,
30{
31}
32
33impl<P, T: Transport> Server<P, T> {
34 pub fn from_untyped(server: protocol::Server<T>) -> Self {
36 Self { server, _protocol: PhantomData }
37 }
38
39 pub fn close(&self) {
41 self.server.close();
42 }
43
44 pub fn close_with_epitaph(&self, epitaph: i32) {
46 self.server.close_with_epitaph(epitaph);
47 }
48}
49
50impl<P, T: Transport> Clone for Server<P, T> {
51 fn clone(&self) -> Self {
52 Self { server: self.server.clone(), _protocol: PhantomData }
53 }
54}
55
56impl<P: HasConnectionHandles<T>, T: Transport> Deref for Server<P, T> {
57 type Target = P::Server;
58
59 fn deref(&self) -> &Self::Target {
60 unsafe { &*(self as *const Self).cast::<P::Server>() }
63 }
64}
65
66pub trait DispatchLocalServerMessage<H, T: Transport>: Sized + 'static {
71 fn on_one_way(
73 handler: &mut H,
74 message: Message<T>,
75 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>>;
76
77 fn on_two_way(
79 handler: &mut H,
80 message: Message<T>,
81 responder: protocol::Responder<T>,
82 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>>;
83}
84
85pub trait DispatchServerMessage<H, T: Transport>: Sized + 'static {
87 fn on_one_way(
89 handler: &mut H,
90 message: Message<T>,
91 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>> + Send;
92
93 fn on_two_way(
95 handler: &mut H,
96 message: Message<T>,
97 responder: protocol::Responder<T>,
98 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>> + Send;
99}
100
101pub struct ServerHandlerToProtocolAdapter<P, H> {
103 handler: H,
104 _protocol: PhantomData<P>,
105}
106
107unsafe impl<P, H> Send for ServerHandlerToProtocolAdapter<P, H> where H: Send {}
108
109impl<P, H> ServerHandlerToProtocolAdapter<P, H> {
110 pub fn from_untyped(handler: H) -> Self {
112 Self { handler, _protocol: PhantomData }
113 }
114}
115
116impl<P, H, T> LocalServerHandler<T> for ServerHandlerToProtocolAdapter<P, H>
117where
118 P: DispatchLocalServerMessage<H, T>,
119 T: Transport,
120{
121 fn on_one_way(
122 &mut self,
123 message: Message<T>,
124 ) -> impl Future<Output = Result<(), ProtocolError<<T as Transport>::Error>>> {
125 P::on_one_way(&mut self.handler, message)
126 }
127
128 fn on_two_way(
129 &mut self,
130 message: Message<T>,
131 responder: fidl_next_protocol::Responder<T>,
132 ) -> impl Future<Output = Result<(), ProtocolError<<T as Transport>::Error>>> {
133 P::on_two_way(&mut self.handler, message, responder)
134 }
135}
136
137impl<P, H, T> ServerHandler<T> for ServerHandlerToProtocolAdapter<P, H>
138where
139 P: DispatchServerMessage<H, T>,
140 H: Send,
141 T: Transport,
142{
143 fn on_one_way(
144 &mut self,
145 message: Message<T>,
146 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>> + Send {
147 P::on_one_way(&mut self.handler, message)
148 }
149
150 fn on_two_way(
151 &mut self,
152 message: Message<T>,
153 responder: protocol::Responder<T>,
154 ) -> impl Future<Output = Result<(), ProtocolError<T::Error>>> + Send {
155 P::on_two_way(&mut self.handler, message, responder)
156 }
157}
158
159pub struct ServerDispatcher<P, T: Transport = <P as HasTransport>::Transport> {
161 dispatcher: protocol::ServerDispatcher<T>,
162 _protocol: PhantomData<P>,
163}
164
165unsafe impl<P, T> Send for ServerDispatcher<P, T>
166where
167 protocol::Server<T>: Send,
168 T: Transport,
169{
170}
171
172impl<P, T: Transport> ServerDispatcher<P, T> {
173 pub fn new(server_end: ServerEnd<P, T>) -> Self {
175 Self {
176 dispatcher: protocol::ServerDispatcher::new(server_end.into_untyped()),
177 _protocol: PhantomData,
178 }
179 }
180
181 pub fn server(&self) -> Server<P, T> {
183 Server::from_untyped(self.dispatcher.server())
184 }
185
186 pub fn from_untyped(server: protocol::ServerDispatcher<T>) -> Self {
188 Self { dispatcher: server, _protocol: PhantomData }
189 }
190
191 pub async fn run<H>(self, handler: H) -> Result<H, ProtocolError<T::Error>>
193 where
194 P: DispatchServerMessage<H, T>,
195 H: Send,
196 {
197 self.dispatcher
198 .run(ServerHandlerToProtocolAdapter { handler, _protocol: PhantomData::<P> })
199 .await
200 .map(|adapter| adapter.handler)
201 }
202
203 pub async fn run_local<H>(self, handler: H) -> Result<H, ProtocolError<T::Error>>
205 where
206 P: DispatchLocalServerMessage<H, T>,
207 {
208 self.dispatcher
209 .run_local(ServerHandlerToProtocolAdapter { handler, _protocol: PhantomData::<P> })
210 .await
211 .map(|adapter| adapter.handler)
212 }
213}
214
215#[must_use]
217pub struct Responder<M, T: Transport = <<M as Method>::Protocol as HasTransport>::Transport> {
218 responder: protocol::Responder<T>,
219 _method: PhantomData<M>,
220}
221
222impl<M, T: Transport> Responder<M, T> {
223 pub fn from_untyped(responder: protocol::Responder<T>) -> Self {
225 Self { responder, _method: PhantomData }
226 }
227
228 pub fn respond<R>(self, response: R) -> RespondFuture<T>
233 where
234 M: TwoWayMethod + Respond<R>,
235 M::Response: Wire<Constraint = ()>,
236 <M as Respond<R>>::Output: Encode<M::Response, T::SendBuffer>,
237 {
238 self.respond_with(M::respond(response))
239 }
240
241 pub fn respond_err<R>(self, response: R) -> RespondFuture<T>
243 where
244 M: TwoWayMethod + RespondErr<R>,
245 M::Response: Wire<Constraint = ()>,
246 <M as RespondErr<R>>::Output: Encode<M::Response, T::SendBuffer>,
247 {
248 self.respond_with(M::respond_err(response))
249 }
250
251 pub fn respond_with<R>(self, response: R) -> RespondFuture<T>
253 where
254 M: TwoWayMethod,
255 M::Response: Wire<Constraint = ()>,
256 R: Encode<M::Response, T::SendBuffer>,
257 {
258 RespondFuture::from_untyped(self.responder.respond(M::ORDINAL, M::FLEXIBILITY, response))
259 }
260}