2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-03-23 20:36:59 +00:00
|
|
|
"regexp"
|
2018-03-14 09:36:59 +00:00
|
|
|
"time"
|
|
|
|
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2019-12-30 12:38:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TCPTransport allows network communication over TCP.
|
|
|
|
type TCPTransport struct {
|
2019-12-30 07:43:05 +00:00
|
|
|
log *zap.Logger
|
2018-03-14 09:36:59 +00:00
|
|
|
server *Server
|
|
|
|
listener net.Listener
|
|
|
|
bindAddr string
|
|
|
|
}
|
|
|
|
|
2019-02-19 13:22:33 +00:00
|
|
|
var reClosedNetwork = regexp.MustCompile(".* use of closed network connection")
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewTCPTransport returns a new TCPTransport that will listen for
|
2018-03-14 09:36:59 +00:00
|
|
|
// new incoming peer connections.
|
2019-12-30 07:43:05 +00:00
|
|
|
func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport {
|
2018-03-14 09:36:59 +00:00
|
|
|
return &TCPTransport{
|
2019-12-30 07:43:05 +00:00
|
|
|
log: log,
|
2018-03-14 09:36:59 +00:00
|
|
|
server: s,
|
|
|
|
bindAddr: bindAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Dial(addr string, timeout time.Duration) error {
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go t.handleConn(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Accept() {
|
|
|
|
l, err := net.Listen("tcp", t.bindAddr)
|
|
|
|
if err != nil {
|
2019-12-30 07:43:05 +00:00
|
|
|
t.log.Panic("TCP listen error", zap.Error(err))
|
2018-03-14 09:36:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.listener = l
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2019-12-30 07:43:05 +00:00
|
|
|
t.log.Warn("TCP accept error", zap.Error(err))
|
2018-03-23 20:36:59 +00:00
|
|
|
if t.isCloseError(err) {
|
|
|
|
break
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
go t.handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
func (t *TCPTransport) isCloseError(err error) bool {
|
|
|
|
if opErr, ok := err.(*net.OpError); ok {
|
2019-02-19 13:22:33 +00:00
|
|
|
if reClosedNetwork.Match([]byte(opErr.Error())) {
|
2018-03-23 20:36:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func (t *TCPTransport) handleConn(conn net.Conn) {
|
2018-04-13 10:14:08 +00:00
|
|
|
var (
|
|
|
|
p = NewTCPPeer(conn)
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
t.server.register <- p
|
2018-04-13 10:14:08 +00:00
|
|
|
|
2019-11-29 08:05:42 +00:00
|
|
|
// When a new peer is connected we send out our version immediately.
|
|
|
|
if err := t.server.sendVersion(p); err != nil {
|
2019-12-30 07:43:05 +00:00
|
|
|
t.log.Error("error on sendVersion", zap.Stringer("addr", p.RemoteAddr()), zap.Error(err))
|
2019-11-29 08:05:42 +00:00
|
|
|
}
|
2019-09-16 09:18:13 +00:00
|
|
|
r := io.NewBinReaderFromIO(p.conn)
|
2018-04-13 10:14:08 +00:00
|
|
|
for {
|
|
|
|
msg := &Message{}
|
2019-12-30 12:38:23 +00:00
|
|
|
err := msg.Decode(r)
|
|
|
|
|
|
|
|
if err == payload.ErrTooManyHeaders {
|
|
|
|
t.log.Warn("not all headers were processed")
|
|
|
|
r.Err = nil
|
|
|
|
} else if err != nil {
|
2019-09-13 12:36:53 +00:00
|
|
|
break
|
2018-04-13 10:14:08 +00:00
|
|
|
}
|
|
|
|
if err = t.server.handleMessage(p, msg); err != nil {
|
2019-09-13 12:36:53 +00:00
|
|
|
break
|
2018-04-13 10:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 12:36:53 +00:00
|
|
|
t.server.unregister <- peerDrop{p, err}
|
|
|
|
p.Disconnect(err)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Close() {
|
|
|
|
t.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proto implements the Transporter interface.
|
|
|
|
func (t *TCPTransport) Proto() string {
|
|
|
|
return "tcp"
|
|
|
|
}
|