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
use std::{
    io,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};
use tokio_util::codec::{Decoder, Encoder};

use crate::{
    context::{ProtocolContext, ProtocolContextMutRef, ServiceContext, SessionContext},
    service::{ServiceControl, ServiceError, ServiceEvent},
    substream::SubstreamReadPart,
};

/// Service handle
///
/// #### Note
///
/// All functions on this trait will block the entire server running, do not insert long-time tasks,
/// you can use the futures task instead.
///
/// #### Behavior
///
/// The handle that exists when the Service is created.
///
/// Mainly handle some Service-level errors thrown at runtime, such as listening errors.
///
/// At the same time, the session establishment and disconnection messages will also be perceived here.
pub trait ServiceHandle {
    /// Handling runtime errors
    fn handle_error(&mut self, _control: &mut ServiceContext, _error: ServiceError) {}
    /// Handling session establishment and disconnection events
    fn handle_event(&mut self, _control: &mut ServiceContext, _event: ServiceEvent) {}
}

/// Service level protocol handle
///
/// #### Note
///
/// All functions on this trait will block the entire server running, do not insert long-time tasks,
/// you can use the futures task instead.
///
/// #### Behavior
///
/// Define the behavior of each custom protocol in each state.
///
/// Depending on whether the user defines a service handle or a session exclusive handle,
/// the runtime has different performance.
///
/// The **important difference** is that some state values are allowed in the service handle,
/// and the handle exclusive to the session is "stateless", relative to the service handle,
/// it can only retain the information between a protocol stream on and off.
///
/// The opening and closing of the protocol will create and clean up the handle exclusive
/// to the session, but the service handle will remain in the state until the service is closed.
///
pub trait ServiceProtocol {
    /// This function is called when the service start.
    ///
    /// The service handle will only be called once
    fn init(&mut self, context: &mut ProtocolContext);
    /// Called when opening protocol
    fn connected(&mut self, _context: ProtocolContextMutRef, _version: &str) {}
    /// Called when closing protocol
    fn disconnected(&mut self, _context: ProtocolContextMutRef) {}
    /// Called when the corresponding protocol message is received
    fn received(&mut self, _context: ProtocolContextMutRef, _data: bytes::Bytes) {}
    /// Called when the Service receives the notify task
    fn notify(&mut self, _context: &mut ProtocolContext, _token: u64) {}
    /// Behave like `Stream::poll_next`, but nothing output
    /// if ready with Some, it will continue poll immediately
    /// if ready with None, it will don't try to call the function again
    #[inline]
    fn poll(
        self: Pin<&mut Self>,
        _cx: &mut Context,
        _context: &mut ProtocolContext,
    ) -> Poll<Option<()>> {
        Poll::Ready(None)
    }
}

/// Session level protocol handle
pub trait SessionProtocol {
    /// Called when opening protocol
    fn connected(&mut self, _context: ProtocolContextMutRef, _version: &str) {}
    /// Called when closing protocol
    fn disconnected(&mut self, _context: ProtocolContextMutRef) {}
    /// Called when the corresponding protocol message is received
    fn received(&mut self, _context: ProtocolContextMutRef, _data: bytes::Bytes) {}
    /// Called when the session receives the notify task
    fn notify(&mut self, _context: ProtocolContextMutRef, _token: u64) {}
    /// Behave like `Stream::poll_next`, but nothing output
    /// if ready with Some, it will continue poll immediately
    /// if ready with None, it will don't try to call the function again
    #[inline]
    fn poll(
        self: Pin<&mut Self>,
        _cx: &mut Context,
        _context: ProtocolContextMutRef,
    ) -> Poll<Option<()>> {
        Poll::Ready(None)
    }
}

/// When the negotiation is completed and the agreement is opened, will call the implementation,
/// allow users to implement the read processing of the protocol by themselves
///
/// Implementing this trait means that streaming reading directly from the underlying substream
/// will become possible, and at the same time, async methods that cannot be used due to Rust's
/// temporary lack of support on async trait will also become possible
///
/// This trait implementation and the callback implementation are mutually exclusive, and will be
/// checked during construction, if both exist, it will panic
#[cfg_attr(not(feature = "unstable"), doc(hidden))]
pub trait ProtocolSpawn {
    /// Call on protocol opened
    fn spawn(
        &self,
        context: Arc<SessionContext>,
        control: &ServiceControl,
        read_part: SubstreamReadPart,
    );
}

/// A trait can define codec, just wrapper `Decoder` and `Encoder`
pub trait Codec:
    Decoder<Item = bytes::BytesMut, Error = io::Error> + Encoder<bytes::Bytes, Error = io::Error>
{
}

impl<T> Codec for T where
    T: Decoder<Item = bytes::BytesMut, Error = io::Error>
        + Encoder<bytes::Bytes, Error = io::Error>
{
}

impl Decoder for Box<dyn Codec + Send + 'static> {
    type Item = bytes::BytesMut;
    type Error = io::Error;

    fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        Decoder::decode(&mut **self, src)
    }
}

impl Encoder<bytes::Bytes> for Box<dyn Codec + Send + 'static> {
    type Error = io::Error;

    fn encode(&mut self, item: bytes::Bytes, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> {
        Encoder::encode(&mut **self, item, dst)
    }
}

impl ServiceHandle for Box<dyn ServiceHandle + Send + 'static> {
    fn handle_error(&mut self, control: &mut ServiceContext, error: ServiceError) {
        (&mut **self).handle_error(control, error)
    }

    fn handle_event(&mut self, control: &mut ServiceContext, event: ServiceEvent) {
        (&mut **self).handle_event(control, event)
    }
}

impl ServiceHandle for Box<dyn ServiceHandle + Send + Sync + 'static> {
    fn handle_error(&mut self, control: &mut ServiceContext, error: ServiceError) {
        (&mut **self).handle_error(control, error)
    }

    fn handle_event(&mut self, control: &mut ServiceContext, event: ServiceEvent) {
        (&mut **self).handle_event(control, event)
    }
}

impl ServiceHandle for () {}

impl ServiceProtocol for Box<dyn ServiceProtocol + Send + 'static + Unpin> {
    fn init(&mut self, context: &mut ProtocolContext) {
        (&mut **self).init(context)
    }

    fn connected(&mut self, context: ProtocolContextMutRef, version: &str) {
        (&mut **self).connected(context, version)
    }

    fn disconnected(&mut self, context: ProtocolContextMutRef) {
        (&mut **self).disconnected(context)
    }

    fn received(&mut self, context: ProtocolContextMutRef, data: bytes::Bytes) {
        (&mut **self).received(context, data)
    }

    fn notify(&mut self, context: &mut ProtocolContext, token: u64) {
        (&mut **self).notify(context, token)
    }

    #[inline]
    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
        context: &mut ProtocolContext,
    ) -> Poll<Option<()>> {
        Pin::new(&mut **self).poll(cx, context)
    }
}

impl ServiceProtocol for Box<dyn ServiceProtocol + Send + Sync + 'static + Unpin> {
    fn init(&mut self, context: &mut ProtocolContext) {
        (&mut **self).init(context)
    }

    fn connected(&mut self, context: ProtocolContextMutRef, version: &str) {
        (&mut **self).connected(context, version)
    }

    fn disconnected(&mut self, context: ProtocolContextMutRef) {
        (&mut **self).disconnected(context)
    }

    fn received(&mut self, context: ProtocolContextMutRef, data: bytes::Bytes) {
        (&mut **self).received(context, data)
    }

    fn notify(&mut self, context: &mut ProtocolContext, token: u64) {
        (&mut **self).notify(context, token)
    }

    #[inline]
    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
        context: &mut ProtocolContext,
    ) -> Poll<Option<()>> {
        Pin::new(&mut **self).poll(cx, context)
    }
}

impl SessionProtocol for Box<dyn SessionProtocol + Send + 'static + Unpin> {
    fn connected(&mut self, context: ProtocolContextMutRef, version: &str) {
        (&mut **self).connected(context, version)
    }

    fn disconnected(&mut self, context: ProtocolContextMutRef) {
        (&mut **self).disconnected(context)
    }

    fn received(&mut self, context: ProtocolContextMutRef, data: bytes::Bytes) {
        (&mut **self).received(context, data)
    }

    fn notify(&mut self, context: ProtocolContextMutRef, token: u64) {
        (&mut **self).notify(context, token)
    }

    #[inline]
    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
        context: ProtocolContextMutRef,
    ) -> Poll<Option<()>> {
        Pin::new(&mut **self).as_mut().poll(cx, context)
    }
}

impl SessionProtocol for Box<dyn SessionProtocol + Send + Sync + 'static + Unpin> {
    fn connected(&mut self, context: ProtocolContextMutRef, version: &str) {
        (&mut **self).connected(context, version)
    }

    fn disconnected(&mut self, context: ProtocolContextMutRef) {
        (&mut **self).disconnected(context)
    }

    fn received(&mut self, context: ProtocolContextMutRef, data: bytes::Bytes) {
        (&mut **self).received(context, data)
    }

    fn notify(&mut self, context: ProtocolContextMutRef, token: u64) {
        (&mut **self).notify(context, token)
    }

    #[inline]
    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut Context,
        context: ProtocolContextMutRef,
    ) -> Poll<Option<()>> {
        Pin::new(&mut **self).as_mut().poll(cx, context)
    }
}