소스 검색

代码结构整理

skyfffire 2 년 전
부모
커밋
fa832493d1
2개의 변경된 파일33개의 추가작업 그리고 31개의 파일을 삭제
  1. 0 31
      p2p/server.go
  2. 33 0
      p2p/share_udp_conn.go

+ 0 - 31
p2p/server.go

@@ -24,11 +24,8 @@ var (
 
 // Server manages all peer connections.
 type Server struct {
-	// Config fields may not be modified while the server is running.
 	Config
 
-	// Hooks for testing. These are useful because we can inhibit
-	// the whole protocol stack.
 	//newTransport func(net.Conn, *ecdsa.PublicKey) transport
 	//newPeerHook  func(*Peer)
 	//listenFunc func(network, addr string) (net.Listener, error)
@@ -49,7 +46,6 @@ type Server struct {
 	discmix   *enode.FairMix
 	dialsched *dialScheduler
 
-	// Channels into the run loop.
 	quit chan struct{}
 	//addtrusted    chan *enode.Node
 	//removetrusted chan *enode.Node
@@ -59,7 +55,6 @@ type Server struct {
 	checkpointPostHandshake chan *conn
 	checkpointAddPeer       chan *conn
 
-	// State of run loop and listenLoop.
 	//inboundHistory expHeap
 }
 
@@ -385,29 +380,3 @@ func (server *Server) checkpoint(c *conn, stage chan<- *conn) error {
 	}
 	return <-c.cont
 }
-
-// sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
-// messages that were found unprocessable and sent to the unhandled channel by the primary listener.
-type sharedUDPConn struct {
-	*net.UDPConn
-	unhandled chan discover.ReadPacket
-}
-
-// ReadFromUDP implements discover.UDPConn
-func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
-	packet, ok := <-s.unhandled
-	if !ok {
-		return 0, nil, errors.New("connection was closed")
-	}
-	l := len(packet.Data)
-	if l > len(b) {
-		l = len(b)
-	}
-	copy(b[:l], packet.Data[:l])
-	return l, packet.Addr, nil
-}
-
-// Close implements discover.UDPConn
-func (s *sharedUDPConn) Close() error {
-	return nil
-}

+ 33 - 0
p2p/share_udp_conn.go

@@ -0,0 +1,33 @@
+package p2p
+
+import (
+	"blockchain-go/p2p/discover"
+	"errors"
+	"net"
+)
+
+// sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
+// messages that were found unprocessable and sent to the unhandled channel by the primary listener.
+type sharedUDPConn struct {
+	*net.UDPConn
+	unhandled chan discover.ReadPacket
+}
+
+// ReadFromUDP implements discover.UDPConn
+func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
+	packet, ok := <-s.unhandled
+	if !ok {
+		return 0, nil, errors.New("connection was closed")
+	}
+	l := len(packet.Data)
+	if l > len(b) {
+		l = len(b)
+	}
+	copy(b[:l], packet.Data[:l])
+	return l, packet.Addr, nil
+}
+
+// Close implements discover.UDPConn
+func (s *sharedUDPConn) Close() error {
+	return nil
+}