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"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TCPTransport allows network communication over TCP.
|
|
|
|
type TCPTransport struct {
|
|
|
|
server *Server
|
|
|
|
listener net.Listener
|
|
|
|
bindAddr string
|
|
|
|
}
|
|
|
|
|
2019-02-19 13:22:33 +00:00
|
|
|
var reClosedNetwork = regexp.MustCompile(".* use of closed network connection")
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// NewTCPTransport return a new TCPTransport that will listen for
|
|
|
|
// new incoming peer connections.
|
|
|
|
func NewTCPTransport(s *Server, bindAddr string) *TCPTransport {
|
|
|
|
return &TCPTransport{
|
|
|
|
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 {
|
|
|
|
log.Fatalf("TCP listen error %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.listener = l
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("TCP accept error: %s", 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
|
|
|
|
|
|
|
for {
|
|
|
|
msg := &Message{}
|
|
|
|
if err = msg.Decode(p.conn); 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"
|
|
|
|
}
|