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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use std::collections::HashMap;
use std::io;

use std::marker::Unpin;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures::channel::{mpsc, oneshot};
use futures::io::{AsyncRead, AsyncWrite};
use futures::{ready, Future, FutureExt, Sink, Stream, TryFutureExt};
use rmpv::Value;
use tokio_util::codec::{Decoder, Framed};
use tokio_util::compat::{Compat, FuturesAsyncWriteCompatExt};

use crate::codec::Codec;
use crate::message::Response as MsgPackResponse;
use crate::message::{Message, Notification, Request};
use crate::cakeservice::{ Service, ServiceWithClient };


struct Server<S> {
  service: S,
  pending_responses: mpsc::UnboundedReceiver<(u32, Result<Value, Value>)>,
  response_sender: mpsc::UnboundedSender<(u32, Result<Value, Value>)>,
}

impl<S: ServiceWithClient> Server<S> {
  fn new(service: S) -> Self {
    let (send, recv) = mpsc::unbounded();

    Server {
      service,
      pending_responses: recv,
      response_sender: send,
    }
  }

  fn send_responses<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    mut sink: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>> {
    trace!("Server: flushing responses");
    loop {
      ready!(sink.as_mut().poll_ready(cx)?);
      match Pin::new(&mut self.pending_responses).poll_next(cx) {
        Poll::Ready(Some((id, result))) => {
          let msg = Message::Response(MsgPackResponse { id, result });
          sink.as_mut().start_send(msg).unwrap();
        }
        Poll::Ready(None) => panic!("we store the sender, it can't be dropped"),
        Poll::Pending => return sink.as_mut().poll_flush(cx),
      }
    }
  }

  fn spawn_request_worker<F: Future<Output = Result<Value, Value>> + 'static + Send>(
    &self,
    id: u32,
    f: F,
  ) {
    trace!("spawning a new task");
    trace!("spawning the task on the event loop");
    let send = self.response_sender.clone();
    tokio::spawn(f.map(move |result| send.unbounded_send((id, result))));
  }
}

trait MessageHandler {
  fn handle_incoming(&mut self, msg: Message);

  fn send_outgoing<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    sink: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>>;

  fn is_finished(&self) -> bool {
    false
  }
}

type ResponseTx = oneshot::Sender<Result<Value, Value>>;

pub struct Response(oneshot::Receiver<Result<Value, Value>>);

type AckTx = oneshot::Sender<()>;

pub struct Ack(oneshot::Receiver<()>);

// TODO: perhaps make these bounded (for better backpressure)
type RequestTx = mpsc::UnboundedSender<(Request, ResponseTx)>;
type RequestRx = mpsc::UnboundedReceiver<(Request, ResponseTx)>;

type NotificationTx = mpsc::UnboundedSender<(Notification, AckTx)>;
type NotificationRx = mpsc::UnboundedReceiver<(Notification, AckTx)>;

impl Future for Response {
  type Output = Result<Value, Value>;

  fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
    trace!("Response: polling");
    Poll::Ready(match ready!(Pin::new(&mut self.0).poll(cx)) {
      Ok(Ok(v)) => Ok(v),
      Ok(Err(v)) => Err(v),
      Err(_) => Err(Value::Nil),
    })
  }
}

impl Future for Ack {
  type Output = Result<(), ()>;

  fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
    trace!("Ack: polling");
    Pin::new(&mut self.0).poll(cx).map_err(|_| ())
  }
}

struct InnerClient {
  client_closed: bool,
  request_id: u32,
  requests_rx: RequestRx,
  notifications_rx: NotificationRx,
  pending_requests: HashMap<u32, ResponseTx>,
  pending_notifications: Vec<AckTx>,
}

impl InnerClient {
  fn new() -> (Self, Client) {
    let (requests_tx, requests_rx) = mpsc::unbounded();
    let (notifications_tx, notifications_rx) = mpsc::unbounded();

    let client = Client {
      requests_tx,
      notifications_tx
    };

    let inner_client = InnerClient {
      client_closed: false,
      request_id: 0,
      requests_rx,
      notifications_rx,
      pending_requests: HashMap::new(),
      pending_notifications: Vec::new(),
    };

    (inner_client, client)
  }

  fn process_notifications<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    mut stream: Pin<&mut Transport<T>>,
  ) -> io::Result<()> {
    if self.client_closed {
      return Ok(());
    }

    trace!("Polling client notifications channel");

    while let Poll::Ready(()) = stream.as_mut().poll_ready(cx)? {
      match Pin::new(&mut self.notifications_rx).poll_next(cx) {
        Poll::Ready(Some((notification, ack_sender))) => {
          trace!("Got notification from client.");
          stream
            .as_mut()
            .start_send(Message::Notification(notification))?;
          self.pending_notifications.push(ack_sender);
        }
        Poll::Ready(None) => {
          trace!("Client closed the notifications channel.");
          self.client_closed = true;
          break;
        }
        Poll::Pending => {
          trace!("No new notification from client");
          break;
        }
      }
    }
    Ok(())
  }

  fn send_messages<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    mut stream: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>> {
    self.process_requests(cx, stream.as_mut())?;
    self.process_notifications(cx, stream.as_mut())?;

    match stream.poll_flush(cx)? {
      Poll::Ready(()) => {
        self.acknowledge_notifications();
        Poll::Ready(Ok(()))
      }
      Poll::Pending => Poll::Pending,
    }
  }

  fn process_requests<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    mut stream: Pin<&mut Transport<T>>,
  ) -> io::Result<()> {
    // Don't try to process requests after the requests channel was closed, because
    // trying to read from it might cause panics.
    if self.client_closed {
      return Ok(());
    }
    trace!("Polling client requests channel");
    while let Poll::Ready(()) = stream.as_mut().poll_ready(cx)? {
      match Pin::new(&mut self.requests_rx).poll_next(cx) {
        Poll::Ready(Some((mut request, response_sender))) => {
          self.request_id += 1;
          trace!("Got request from client: {:?}", request);
          request.id = self.request_id;
          stream.as_mut().start_send(Message::Request(request))?;
          self.pending_requests
            .insert(self.request_id, response_sender);
        }
        Poll::Ready(None) => {
          trace!("Client closed the requests channel.");
          self.client_closed = true;
          break;
        }
        Poll::Pending => {
          trace!("No new request from client");
          break;
        }
      }
    }
    Ok(())
  }

  fn process_response(&mut self, response: MsgPackResponse) {
    if let Some(response_tx) = self.pending_requests.remove(&response.id) {
      trace!("Forwarding response to the client.");
      if let Err(e) = response_tx.send(response.result) {
        warn!("Failed to send response to client: {:?}", e);
      }
    } else {
      warn!("no pending request found for response {}", &response.id);
    }
  }

  fn acknowledge_notifications(&mut self) {
    for chan in self.pending_notifications.drain(..) {
      trace!("Acknowledging notification.");
      if let Err(e) = chan.send(()) {
        warn!("Failed to send ack to client: {:?}", e);
      }
    }
  }
}

struct Transport<T>(Framed<Compat<T>, Codec>);
// pub struct Transport<T>(Framed<Compat<T>, Codec>);

impl<T> Transport<T>
  where
    T: AsyncRead + AsyncWrite,
{
  fn inner(self: Pin<&mut Self>) -> Pin<&mut Framed<Compat<T>, Codec>> {
    unsafe { self.map_unchecked_mut(|this| &mut this.0) }
  }
}

impl<T> Stream for Transport<T>
  where
    T: AsyncRead + AsyncWrite,
{
  type Item = io::Result<Message>;

  fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
    trace!("Transport: polling");
    self.inner().poll_next(cx)
  }
}

impl<T> Sink<Message> for Transport<T>
  where
    T: AsyncRead + AsyncWrite,
{
  type Error = io::Error;

  fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
    self.inner().poll_ready(cx)
  }

  fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
    self.inner().start_send(item)
  }

  fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
    self.inner().poll_flush(cx)
  }

  fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
    self.inner().poll_close(cx)
  }
}

impl<S: Service> MessageHandler for Server<S> {
  fn handle_incoming(&mut self, msg: Message) {
    match msg {
      Message::Request(req) => {
        let f = self.service.handle_request(&req.method, &req.params);
        self.spawn_request_worker(req.id, f);
      }
      Message::Notification(note) => {
        self.service.handle_notification(&note.method, &note.params);
      }
      Message::Response(_) => {
        trace!("This endpoint doesn't handle responses, ignoring the msg.");
      }
    };
  }

  fn send_outgoing<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    sink: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>> {
    self.send_responses(cx, sink)
  }
}

impl MessageHandler for InnerClient {
  fn handle_incoming(&mut self, msg: Message) {
    trace!("Received {:?}", msg);
    if let Message::Response(response) = msg {
      self.process_response(response);
    } else {
      trace!("This endpoint only handles reponses, ignoring the msg.");
    }
  }

  fn send_outgoing<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    sink: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>> {
    self.send_messages(cx, sink)
  }

  fn is_finished(&self) -> bool {
    self.client_closed
      && self.pending_requests.is_empty()
      && self.pending_notifications.is_empty()
  }
}

struct ClientAndServer<S> {
  inner_client: InnerClient,
  server: Server<S>,
  client: Client,
}

impl<S: ServiceWithClient> MessageHandler for ClientAndServer<S> {
  fn handle_incoming(&mut self, msg: Message) {
    match msg {
      Message::Request(req) => {
        let f =
          self.server
            .service
            .handle_request(&mut self.client, &req.method, &req.params);
        self.server.spawn_request_worker(req.id, f);
      }
      Message::Notification(note) => {
        self.server.service.handle_notification(
          &mut self.client,
          &note.method,
          &note.params,
        );
      }
      Message::Response(response) => self.inner_client.process_response(response),
    };
  }

  fn send_outgoing<T: AsyncRead + AsyncWrite>(
    &mut self,
    cx: &mut Context,
    mut sink: Pin<&mut Transport<T>>,
  ) -> Poll<io::Result<()>> {
    if let Poll::Ready(()) = self.server.send_responses(cx, sink.as_mut())? {
      self.inner_client.send_messages(cx, sink)
    } else {
      Poll::Pending
    }
  }
}

struct InnerEndpoint<MH, T> {
  handler: MH,
  stream: Transport<T>,
}

impl<MH: MessageHandler + Unpin, T: AsyncRead + AsyncWrite> Future for InnerEndpoint<MH, T> {
  type Output = io::Result<()>;

  fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
    trace!("InnerEndpoint: polling");
    let (handler, mut stream) = unsafe {
      let this = self.get_unchecked_mut();
      (&mut this.handler, Pin::new_unchecked(&mut this.stream))
    };
    if let Poll::Pending = handler.send_outgoing(cx, stream.as_mut())? {
      trace!("Sink not yet flushed, waiting...");
      return Poll::Pending;
    }

    trace!("Polling stream.");
    while let Poll::Ready(msg) = stream.as_mut().poll_next(cx)? {
      if let Some(msg) = msg {
        handler.handle_incoming(msg);
      } else {
        trace!("Stream closed by remote peer.");
        // FIXME: not sure if we should still continue sending responses here. Is it
        // possible that the client closed the stream only one way and is still waiting
        // for response? Not for TCP at least, but maybe for other transport types?
        return Poll::Ready(Ok(()));
      }
    }

    if handler.is_finished() {
      trace!("inner client finished, exiting...");
      Poll::Ready(Ok(()))
    } else {
      trace!("notifying the reactor that we're not done yet");
      Poll::Pending
    }
  }
}

/// Creates a future for running a `Service` on a stream.
///
/// The returned future will run until the stream is closed; if the stream encounters an error,
/// then the future will propagate it and terminate.
pub fn serve<'a, S: Service + Unpin + 'a, T: AsyncRead + AsyncWrite + 'a + Send>(
  stream: T,
  service: S,
) -> impl Future<Output = io::Result<()>> + 'a + Send {
  ServerEndpoint::new(stream, service)
}

struct ServerEndpoint<S, T> {
  inner: InnerEndpoint<Server<S>, T>,
}

impl<S: Service + Unpin, T: AsyncRead + AsyncWrite> ServerEndpoint<S, T> {
  pub fn new(stream: T, service: S) -> Self {
    let stream = FuturesAsyncWriteCompatExt::compat_write(stream);
    ServerEndpoint {
      inner: InnerEndpoint {
        stream: Transport(Codec.framed(stream)),
        handler: Server::new(service),
      },
    }
  }
}

impl<S: Service + Unpin, T: AsyncRead + AsyncWrite> Future for ServerEndpoint<S, T> {
  type Output = io::Result<()>;

  fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
    trace!("ServerEndpoint: polling");
    unsafe { self.map_unchecked_mut(|this| &mut this.inner) }.poll(cx)
  }
}

pub struct Endpoint<S, T> {
  inner: InnerEndpoint<ClientAndServer<S>, T>,
}

impl<S: ServiceWithClient + Unpin, T: AsyncRead + AsyncWrite> Endpoint<S, T> {
  /// Creates a new `Endpoint` on `stream`, using `service` to handle requests and notifications.
  pub fn new(stream: T, service: S) -> Self {
    let (inner_client, client) = InnerClient::new();
    let stream = FuturesAsyncWriteCompatExt::compat_write(stream);
    Endpoint {
      inner: InnerEndpoint {
        stream: Transport(Codec.framed(stream)),
        handler: ClientAndServer {
          inner_client,
          client,
          server: Server::new(service),
        },
      },
    }
  }

  /// Returns a handle to the client half of this `Endpoint`, which can be used for sending
  /// requests and notifications.
  pub fn client(&self) -> Client {
    self.inner.handler.client.clone()
  }
}

impl<S: ServiceWithClient + Unpin, T: AsyncRead + AsyncWrite> Future for Endpoint<S, T> {
  type Output = io::Result<()>;

  fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
    trace!("Endpoint: polling");
    unsafe { self.map_unchecked_mut(|this| &mut this.inner) }.poll(cx)
  }
}

/// A client that sends requests and notifications to a remote MessagePack-RPC server.
#[derive(Clone)]
pub struct Client {
  requests_tx: RequestTx,
  notifications_tx: NotificationTx,
}

impl Client {

  pub fn new<T: AsyncRead + AsyncWrite + 'static + Send>(stream: T) -> Self {
    let (inner_client, client) = InnerClient::new();
    let stream = FuturesAsyncWriteCompatExt::compat_write(stream);
    let endpoint = InnerEndpoint {
      stream: Transport(Codec.framed(stream)),
      handler: inner_client,
    };
    // We swallow io::Errors. The client will see an error if it has any outstanding requests
    // or if it tries to send anything, because the endpoint has terminated.
    // todo: 这里不是异步检查 endpoint是否有错误? 而且异步跑
    tokio::spawn(
      endpoint.map_err(|e| error!("Client endpoint closed because of an error: {}", e)),
    );

    client
  }

  // fn from_channels(requests_tx: RequestTx, notifications_tx: NotificationTx) -> Self {
  //   Client {
  //     requests_tx,
  //     notifications_tx,
  //   }
  // }

  /// Send a `MessagePack-RPC` request
  pub fn call(&self, method: &str, params: &[Value]) -> Response {
    trace!("New call (method={}, params={:?})", method, params);
    let request = Request {
      id: 0,
      method: method.to_owned(),
      params: Vec::from(params),
    };
    let (tx, rx) = oneshot::channel();
    // If send returns an Err, its because the other side has been dropped. By ignoring it,
    // we are just dropping the `tx`, which will mean the rx will return Canceled when
    // polled. In turn, that is translated into a BrokenPipe, which conveys the proper
    // error.
    let _ = mpsc::UnboundedSender::unbounded_send(&self.requests_tx, (request, tx));
    Response(rx)
  }

  /// Send a `MessagePack-RPC` notification
  pub fn call_notify(&self, method: &str, params: &[Value]) -> Ack {
    trace!("New notification (method={}, params={:?})", method, params);
    let notification = Notification {
      method: method.to_owned(),
      params: Vec::from(params),
    };
    let (tx, rx) = oneshot::channel();
    let _ = mpsc::UnboundedSender::unbounded_send(&self.notifications_tx, (notification, tx));
    Ack(rx)
  }
}

impl Future for Client {
  type Output = io::Result<()>;

  fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
    trace!("Client: polling");
    Poll::Ready(Ok(()))
  }
}