| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package p2p
- import (
- "fmt"
- )
- const (
- errInvalidMsgCode = iota
- errInvalidMsg
- )
- var errorToString = map[int]string{
- errInvalidMsgCode: "invalid message code",
- errInvalidMsg: "invalid message",
- }
- type peerError struct {
- code int
- message string
- }
- func newPeerError(code int, format string, v ...interface{}) *peerError {
- desc, ok := errorToString[code]
- if !ok {
- panic("invalid error code")
- }
- err := &peerError{code, desc}
- if format != "" {
- err.message += ": " + fmt.Sprintf(format, v...)
- }
- return err
- }
- func (self *peerError) Error() string {
- return self.message
- }
- type DiscReason uint
- const (
- DiscRequested DiscReason = iota
- DiscNetworkError
- DiscProtocolError
- DiscUselessPeer
- DiscTooManyPeers
- DiscAlreadyConnected
- DiscIncompatibleVersion
- DiscInvalidIdentity
- DiscQuitting
- DiscUnexpectedIdentity
- DiscSelf
- DiscReadTimeout
- DiscSubprotocolError
- )
- var discReasonToString = [...]string{
- DiscRequested: "Disconnect requested",
- DiscNetworkError: "Network error",
- DiscProtocolError: "Breach of protocol",
- DiscUselessPeer: "Useless peer",
- DiscTooManyPeers: "Too many peers",
- DiscAlreadyConnected: "Already connected",
- DiscIncompatibleVersion: "Incompatible P2P protocol version",
- DiscInvalidIdentity: "Invalid node identity",
- DiscQuitting: "Client quitting",
- DiscUnexpectedIdentity: "Unexpected identity",
- DiscSelf: "Connected to self",
- DiscReadTimeout: "Read timeout",
- DiscSubprotocolError: "Subprotocol error",
- }
- func (d DiscReason) String() string {
- if len(discReasonToString) < int(d) {
- return fmt.Sprintf("Unknown Reason(%d)", d)
- }
- return discReasonToString[d]
- }
- func (d DiscReason) Error() string {
- return d.String()
- }
- func discReasonForError(err error) DiscReason {
- if reason, ok := err.(DiscReason); ok {
- return reason
- }
- peerError, ok := err.(*peerError)
- if ok {
- switch peerError.code {
- case errInvalidMsgCode, errInvalidMsg:
- return DiscProtocolError
- default:
- return DiscSubprotocolError
- }
- }
- return DiscSubprotocolError
- }
|