forked from driftluo/tentacle
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from nervosnetwork/report-session-timeout
Report session timeout
- Loading branch information
Showing
3 changed files
with
176 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use futures::Future; | ||
use std::fmt; | ||
|
||
use crate::{ | ||
context::SessionContext, error::Error, multiaddr::Multiaddr, secio::PublicKey, | ||
yamux::session::SessionType, ProtocolId, SessionId, | ||
}; | ||
|
||
/// Error generated by the Service | ||
#[derive(Debug)] | ||
pub enum ServiceError<'a> { | ||
/// When dial remote error | ||
DialerError { | ||
/// Remote address | ||
address: Multiaddr, | ||
/// error | ||
error: Error<ServiceTask>, | ||
}, | ||
/// When listen error | ||
ListenError { | ||
/// Listen address | ||
address: Multiaddr, | ||
/// error | ||
error: Error<ServiceTask>, | ||
}, | ||
/// Protocol select fail | ||
ProtocolSelectError { | ||
/// Protocol name, if none, timeout or other net problem, | ||
/// if Some, don't support this proto | ||
proto_name: Option<String>, | ||
/// Session context | ||
session_context: &'a SessionContext, | ||
}, | ||
/// Protocol error during interaction | ||
ProtocolError { | ||
/// Session id | ||
id: SessionId, | ||
/// Protocol id | ||
proto_id: ProtocolId, | ||
/// Codec error | ||
error: Error<ServiceTask>, | ||
}, | ||
/// After initializing the connection, the session does not open any protocol, | ||
/// suspected fd attack | ||
SessionTimeout { | ||
/// Session context | ||
session_context: &'a SessionContext, | ||
}, | ||
} | ||
|
||
/// Event generated by the Service | ||
#[derive(Debug)] | ||
pub enum ServiceEvent { | ||
/// A session close | ||
SessionClose { | ||
/// Session id | ||
id: SessionId, | ||
}, | ||
/// A session open | ||
SessionOpen { | ||
/// Session id | ||
id: SessionId, | ||
/// Remote address | ||
address: Multiaddr, | ||
/// Outbound or Inbound | ||
ty: SessionType, | ||
/// Remote public key | ||
public_key: Option<PublicKey>, | ||
}, | ||
} | ||
|
||
/// Task received by the Service. | ||
/// | ||
/// An instruction that the outside world can send to the service | ||
pub enum ServiceTask { | ||
/// Send protocol data task | ||
ProtocolMessage { | ||
/// Specify which sessions to send to, | ||
/// None means broadcast | ||
session_ids: Option<Vec<SessionId>>, | ||
/// protocol id | ||
proto_id: ProtocolId, | ||
/// data | ||
data: Vec<u8>, | ||
}, | ||
/// Service-level notify task | ||
ProtocolNotify { | ||
/// Protocol id | ||
proto_id: ProtocolId, | ||
/// Notify token | ||
token: u64, | ||
}, | ||
/// Session-level notify task | ||
ProtocolSessionNotify { | ||
/// Session id | ||
session_id: SessionId, | ||
/// Protocol id | ||
proto_id: ProtocolId, | ||
/// Notify token | ||
token: u64, | ||
}, | ||
/// Future task | ||
FutureTask { | ||
/// Future | ||
task: Box<dyn Future<Item = (), Error = ()> + 'static + Send>, | ||
}, | ||
/// Disconnect task | ||
Disconnect { | ||
/// Session id | ||
session_id: SessionId, | ||
}, | ||
/// Dial task | ||
Dial { | ||
/// Remote address | ||
address: Multiaddr, | ||
}, | ||
/// Listen task | ||
Listen { | ||
/// Listen address | ||
address: Multiaddr, | ||
}, | ||
} | ||
|
||
impl fmt::Debug for ServiceTask { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use self::ServiceTask::*; | ||
|
||
match self { | ||
ProtocolMessage { | ||
session_ids, | ||
proto_id, | ||
data, | ||
} => write!( | ||
f, | ||
"id: {:?}, protoid: {}, message: {:?}", | ||
session_ids, proto_id, data | ||
), | ||
ProtocolNotify { proto_id, token } => { | ||
write!(f, "protocol id: {}, token: {}", proto_id, token) | ||
} | ||
ProtocolSessionNotify { | ||
session_id, | ||
proto_id, | ||
token, | ||
} => write!( | ||
f, | ||
"session id: {}, protocol id: {}, token: {}", | ||
session_id, proto_id, token | ||
), | ||
FutureTask { .. } => write!(f, "Future task"), | ||
Disconnect { session_id } => write!(f, "Disconnect session [{}]", session_id), | ||
Dial { address } => write!(f, "Dial address: {}", address), | ||
Listen { address } => write!(f, "Listen address: {}", address), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters