Bläddra i källkod

加入conn结构

skyfffire 2 år sedan
förälder
incheckning
c520a89758
4 ändrade filer med 134 tillägg och 3 borttagningar
  1. 0 3
      p2p/config.go
  2. 33 0
      p2p/conn.go
  3. 89 0
      p2p/peer.go
  4. 12 0
      p2p/server.go

+ 0 - 3
p2p/config.go

@@ -4,7 +4,6 @@ import (
 	"blockchain-go/common/mclock"
 	"blockchain-go/log"
 	"crypto/ecdsa"
-	"errors"
 	"time"
 )
 
@@ -34,8 +33,6 @@ const (
 	stopTimeout = 5 * time.Second
 )
 
-var errServerStopped = errors.New("server stopped")
-
 // Config holds Server options.
 type Config struct {
 	// This field must be set to a valid secp256k1 private key.

+ 33 - 0
p2p/conn.go

@@ -0,0 +1,33 @@
+package p2p
+
+import "net"
+
+type connFlag int32
+
+const (
+	dynDialedConn connFlag = 1 << iota
+	staticDialedConn
+	inboundConn
+	trustedConn
+)
+
+// conn wraps a network connection with information gathered
+// during the two handshakes.
+type conn struct {
+	fd net.Conn
+	//transport
+	//node  *enode.Node
+	flags connFlag
+	cont  chan error // The run loop uses cont to signal errors to SetupConn.
+	//caps  []Cap      // valid after the protocol handshake
+	name string // valid after the protocol handshake
+}
+
+//func (c *conn) String() string {
+//    s := c.flags.String()
+//    if (c.node.ID() != enode.ID{}) {
+//        s += " " + c.node.ID().String()
+//    }
+//    s += " " + c.fd.RemoteAddr().String()
+//    return s
+//}

+ 89 - 0
p2p/peer.go

@@ -0,0 +1,89 @@
+package p2p
+
+import (
+	"errors"
+	"time"
+)
+
+var (
+	ErrShuttingDown = errors.New("shutting down")
+)
+
+const (
+	baseProtocolVersion    = 5
+	baseProtocolLength     = uint64(16)
+	baseProtocolMaxMsgSize = 2 * 1024
+
+	snappyProtocolVersion = 5
+
+	pingInterval = 15 * time.Second
+)
+
+const (
+	// devp2p message codes
+	handshakeMsg = 0x00
+	discMsg      = 0x01
+	pingMsg      = 0x02
+	pongMsg      = 0x03
+)
+
+// protoHandshake is the RLP structure of the protocol handshake.
+type protoHandshake struct {
+	Version uint64
+	Name    string
+	//Caps       []Cap
+	ListenPort uint64
+	ID         []byte // secp256k1 public key
+
+	// Ignore additional fields (for forward compatibility).
+	//Rest []rlp.RawValue `rlp:"tail"`
+}
+
+type PeerEventType string
+
+const (
+	// PeerEventTypeAdd is the type of event emitted when a peer is added
+	// to a p2p.Server
+	PeerEventTypeAdd PeerEventType = "add"
+
+	// PeerEventTypeDrop is the type of event emitted when a peer is
+	// dropped from a p2p.Server
+	PeerEventTypeDrop PeerEventType = "drop"
+
+	// PeerEventTypeMsgSend is the type of event emitted when a
+	// message is successfully sent to a peer
+	PeerEventTypeMsgSend PeerEventType = "msgsend"
+
+	// PeerEventTypeMsgRecv is the type of event emitted when a
+	// message is received from a peer
+	PeerEventTypeMsgRecv PeerEventType = "msgrecv"
+)
+
+// PeerEvent is an event emitted when peers are either added or dropped from
+// a p2p.Server or when a message is sent or received on a peer connection
+type PeerEvent struct {
+	Type PeerEventType `json:"type"`
+	//Peer          enode.ID      `json:"peer"`
+	Error         string  `json:"error,omitempty"`
+	Protocol      string  `json:"protocol,omitempty"`
+	MsgCode       *uint64 `json:"msg_code,omitempty"`
+	MsgSize       *uint32 `json:"msg_size,omitempty"`
+	LocalAddress  string  `json:"local,omitempty"`
+	RemoteAddress string  `json:"remote,omitempty"`
+}
+
+// Peer represents a connected remote node.
+type Peer struct {
+	//rw      *conn
+	//running map[string]*protoRW
+	//log     log.Logger
+	//created mclock.AbsTime
+
+	//wg       sync.WaitGroup
+	protoErr chan error
+	closed   chan struct{}
+	//disc     chan DiscReason
+
+	// events receives message send / receive events if set
+	//events *event.Feed
+}

+ 12 - 0
p2p/server.go

@@ -2,10 +2,15 @@ package p2p
 
 import (
 	"blockchain-go/log"
+	"errors"
 	"net"
 	"sync"
 )
 
+var (
+	ErrServerStopped = errors.New("server stopped")
+)
+
 // Server manages all peer connections.
 type Server struct {
 	// Config fields may not be modified while the server is running.
@@ -46,3 +51,10 @@ type Server struct {
 	// State of run loop and listenLoop.
 	//inboundHistory expHeap
 }
+
+//type peerOpFunc func(map[enode.ID]*Peer)
+type peerDrop struct {
+	*Peer
+	err       error
+	requested bool // true if signaled by the peer
+}