      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

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    use io::IoError;
    use rlp::*;
    use util::UtilError;
    use std::fmt;
    use ethkey::Error as KeyError;
    use crypto::Error as CryptoError;

    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum DisconnectReason
    {
    	DisconnectRequested,
    	TCPError,
    	BadProtocol,
    	UselessPeer,
    	TooManyPeers,
    	DuplicatePeer,
    	IncompatibleProtocol,
    	NullIdentity,
    	ClientQuit,
    	UnexpectedIdentity,
    	LocalIdentity,
    	PingTimeout,
    	Unknown,
    }

    impl DisconnectReason {
    	pub fn from_u8(n: u8) -> DisconnectReason {
    		match n {
    			0 => DisconnectReason::DisconnectRequested,
    			1 => DisconnectReason::TCPError,
    			2 => DisconnectReason::BadProtocol,
    			3 => DisconnectReason::UselessPeer,
    			4 => DisconnectReason::TooManyPeers,
    			5 => DisconnectReason::DuplicatePeer,
    			6 => DisconnectReason::IncompatibleProtocol,
    			7 => DisconnectReason::NullIdentity,
    			8 => DisconnectReason::ClientQuit,
    			9 => DisconnectReason::UnexpectedIdentity,
    			10 => DisconnectReason::LocalIdentity,
    			11 => DisconnectReason::PingTimeout,
    			_ => DisconnectReason::Unknown,
    		}
    	}
    }

    impl fmt::Display for DisconnectReason {
    	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    		use self::DisconnectReason::*;

    		let msg = match *self {
    			DisconnectRequested => "disconnect requested",
    			TCPError => "TCP error",
    			BadProtocol => "bad protocol",
    			UselessPeer => "useless peer",
    			TooManyPeers => "too many peers",
    			DuplicatePeer => "duplicate peer",
    			IncompatibleProtocol => "incompatible protocol",
    			NullIdentity => "null identity",
    			ClientQuit => "client quit",
    			UnexpectedIdentity => "unexpected identity",
    			LocalIdentity => "local identity",
    			PingTimeout => "ping timeout",
    			Unknown => "unknown",
    		};

    		f.write_str(msg)
    	}
    }

    #[derive(Debug)]
    /// Network error.
    pub enum NetworkError {
    	/// Authentication error.
    	Auth,
    	/// Unrecognised protocol.
    	BadProtocol,
    	/// Message expired.
    	Expired,
    	/// Peer not found.
    	PeerNotFound,
    	/// Peer is diconnected.
    	Disconnect(DisconnectReason),
    	/// Util error.
    	Util(UtilError),
    	/// Socket IO error.
    	Io(IoError),
    	/// Error concerning the network address parsing subsystem.
    	AddressParse(::std::net::AddrParseError),
    	/// Error concerning the network address resolution subsystem.
    	AddressResolve(Option<::std::io::Error>),
    	/// Error concerning the Rust standard library's IO subsystem.
    	StdIo(::std::io::Error),
    }

    impl fmt::Display for NetworkError {
    	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    		use self::NetworkError::*;

    		let msg = match *self {
    			Auth => "Authentication failure".into(),
    			BadProtocol => "Bad protocol".into(),
    			Expired => "Expired message".into(),
    			PeerNotFound => "Peer not found".into(),
    			Disconnect(ref reason) => format!("Peer disconnected: {}", reason),
    			Io(ref err) => format!("Socket I/O error: {}", err),
    			AddressParse(ref err) => format!("{}", err),
    			AddressResolve(Some(ref err)) => format!("{}", err),
    			AddressResolve(_) => "Failed to resolve network address.".into(),
    			StdIo(ref err) => format!("{}", err),
    			Util(ref err) => format!("{}", err),
    		};

    		f.write_fmt(format_args!("Network error ({})", msg))
    	}
    }

    impl From<DecoderError> for NetworkError {
    	fn from(_err: DecoderError) -> NetworkError {
    		NetworkError::Auth
    	}
    }

    impl From<::std::io::Error> for NetworkError {
    	fn from(err: ::std::io::Error) -> NetworkError {
    		NetworkError::StdIo(err)
    	}
    }

    impl From<IoError> for NetworkError {
    	fn from(err: IoError) -> NetworkError {
    		NetworkError::Io(err)
    	}
    }

    impl From<UtilError> for NetworkError {
    	fn from(err: UtilError) -> NetworkError {
    		NetworkError::Util(err)
    	}
    }

    impl From<KeyError> for NetworkError {
    	fn from(_err: KeyError) -> Self {
    		NetworkError::Auth
    	}
    }

    impl From<CryptoError> for NetworkError {
    	fn from(_err: CryptoError) -> NetworkError {
    		NetworkError::Auth
    	}
    }

    impl From<::std::net::AddrParseError> for NetworkError {
    	fn from(err: ::std::net::AddrParseError) -> NetworkError {
    		NetworkError::AddressParse(err)
    	}
    }

    #[test]
    fn test_errors() {
    	assert_eq!(DisconnectReason::ClientQuit, DisconnectReason::from_u8(8));
    	let mut r = DisconnectReason::DisconnectRequested;
    	for i in 0 .. 20 {
    		r = DisconnectReason::from_u8(i);
    	}
    	assert_eq!(DisconnectReason::Unknown, r);

    	match <NetworkError as From<DecoderError>>::from(DecoderError::RlpIsTooBig) {
    		NetworkError::Auth => {},
    		_ => panic!("Unexpeceted error"),
    	}

    	match <NetworkError as From<CryptoError>>::from(CryptoError::InvalidMessage) {
    		NetworkError::Auth => {},
    		_ => panic!("Unexpeceted error"),
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)