neoneo-go/pkg/network/peer.go
2018-01-28 14:59:32 +01:00

47 lines
1 KiB
Go

package network
import (
"log"
"net"
)
// Peer represents a remote node, backed by TCP transport.
type Peer struct {
id uint32
// underlying TCP connection
conn net.Conn
// channel to coordinate message writes back to the connection.
send chan *Message
// verack is true if this node has sended it's version.
verack bool
// whether we or him made the initial connection.
initiator bool
}
// NewPeer returns a (TCP) Peer.
func NewPeer(conn net.Conn, init bool) *Peer {
return &Peer{
conn: conn,
send: make(chan *Message),
initiator: init,
}
}
// writeLoop writes messages to the underlying TCP connection.
// A goroutine writeLoop is started for each connection.
// There should be at most one writer to a connection executing
// all writes from this goroutine.
func (p *Peer) writeLoop() {
// clean up the connection.
defer func() {
p.conn.Close()
}()
for {
msg := <-p.send
rpcLogger.Printf("OUT :: %s", msg.commandType())
if err := msg.encode(p.conn); err != nil {
log.Printf("encode error: %s", err)
}
}
}