github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/net/netserver/common.go (about)

     1  package netserver
     2  
     3  import (
     4  	// "crypto/ecdsa"
     5  	// "errors"
     6  	// "fmt"
     7  	"net"
     8  	// "sync"
     9  	"crypto/ecdsa"
    10  	"time"
    11  
    12  	comm "github.com/sixexorg/magnetic-ring/common"
    13  	"github.com/sixexorg/magnetic-ring/p2pserver/common"
    14  	discover "github.com/sixexorg/magnetic-ring/p2pserver/discover"
    15  	"github.com/sixexorg/magnetic-ring/rlp"
    16  )
    17  
    18  const (
    19  	defaultDialTimeout      = 15 * time.Second
    20  	refreshPeersInterval    = 30 * time.Second
    21  	staticPeerCheckInterval = 15 * time.Second
    22  
    23  	// Maximum number of concurrently handshaking inbound connections.
    24  	maxAcceptConns = 50
    25  
    26  	// Maximum number of concurrently dialing outbound connections.
    27  	maxActiveDialTasks = 16
    28  
    29  	// Maximum time allowed for reading a complete message.
    30  	// This is effectively the amount of time a connection can be idle.
    31  	frameReadTimeout = 30 * time.Second
    32  
    33  	// Maximum amount of time allowed for writing a complete message.
    34  	frameWriteTimeout = 20 * time.Second
    35  )
    36  
    37  const (
    38  	// devp2p message codes
    39  	handshakeMsg = 0x00
    40  	discMsg      = 0x01
    41  	pingMsg      = 0x02
    42  	pongMsg      = 0x03
    43  	getPeersMsg  = 0x04
    44  	peersMsg     = 0x05
    45  )
    46  
    47  const (
    48  	baseProtocolVersion    = 5
    49  	baseProtocolLength     = uint64(16)
    50  	baseProtocolMaxMsgSize = 2 * 1024
    51  
    52  	snappyProtocolVersion = 5
    53  
    54  	pingInterval = 15 * time.Second
    55  )
    56  
    57  type conn struct {
    58  	fd net.Conn
    59  	transport
    60  	id   discover.NodeID // valid after the encryption handshake
    61  	name string          // valid after the protocol handshake
    62  }
    63  
    64  
    65  // protoHandshake is the RLP structure of the protocol handshake.
    66  type protoHandshake struct {
    67  	Version    uint64
    68  	Name       string
    69  	ListenPort uint64
    70  	ID         discover.NodeID
    71  
    72  	// Ignore additional fields (for forward compatibility).
    73  	Rest []rlp.RawValue `rlp:"tail"`
    74  }
    75  
    76  type transport interface {
    77  	// The two handshakes.
    78  	doEncHandshake(prv *ecdsa.PrivateKey, dialDest *discover.Node, orgid *comm.Address, bANode bool) (discover.NodeID, comm.Address, bool, error)
    79  	doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
    80  	// The MsgReadWriter can only be used after the encryption
    81  	// handshake has completed. The code uses conn.id to track this
    82  	// by setting it to a non-nil value after the encryption handshake.
    83  	common.MsgReadWriter
    84  	// transports must provide Close because we use MsgPipe in some of
    85  	// the tests. Closing the actual network connection doesn't do
    86  	// anything in those tests because NsgPipe doesn't use it.
    87  	// Close(err error)
    88  }