| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package p2p
- import (
- "blockchain-go/p2p/enode"
- "net"
- "sync/atomic"
- )
- type connFlag int32
- func (f connFlag) String() string {
- s := ""
- if f&trustedConn != 0 {
- s += "-trusted"
- }
- if f&dynDialedConn != 0 {
- s += "-dyndial"
- }
- if f&staticDialedConn != 0 {
- s += "-staticdial"
- }
- if f&inboundConn != 0 {
- s += "-inbound"
- }
- if s != "" {
- s = s[1:]
- }
- return s
- }
- 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
- }
- func (c *conn) is(f connFlag) bool {
- flags := connFlag(atomic.LoadInt32((*int32)(&c.flags)))
- return flags&f != 0
- }
- func (c *conn) set(f connFlag, val bool) {
- for {
- oldFlags := connFlag(atomic.LoadInt32((*int32)(&c.flags)))
- flags := oldFlags
- if val {
- flags |= f
- } else {
- flags &= ^f
- }
- if atomic.CompareAndSwapInt32((*int32)(&c.flags), int32(oldFlags), int32(flags)) {
- return
- }
- }
- }
|