refactor: make LocalConnection.queryLoop do what it says

This commit is contained in:
Matthias Radestock committed 2015-01-14 17:08:38 +00:00
1 parent fd90212db4
commit 9cf5fc5331
1 file changed
+20 -15
+20 -15
View File
@@ -65,7 +65,7 @@ func NewLocalConnection(connRemote *RemoteConnection, acceptNewPeer bool, tcpCon
remoteUDPAddr: udpAddr, remoteUDPAddr: udpAddr,
effectivePMTU: DefaultPMTU, effectivePMTU: DefaultPMTU,
queryChan: queryChan} queryChan: queryChan}
go connLocal.queryLoop(queryChan, acceptNewPeer) go connLocal.run(queryChan, acceptNewPeer)
} }
func (conn *LocalConnection) Established() bool { func (conn *LocalConnection) Established() bool {
@@ -158,21 +158,34 @@ func (conn *LocalConnection) SendTCP(msg []byte) {
// ACTOR server // ACTOR server
func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, acceptNewPeer bool) { func (conn *LocalConnection) run(queryChan <-chan *ConnectionInteraction, acceptNewPeer bool) {
err := conn.handshake(acceptNewPeer) defer conn.handleShutdown()
if err != nil { if err := conn.handshake(acceptNewPeer); err != nil {
log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err) log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err)
conn.handleShutdown()
return return
} }
log.Printf("->[%s] completed handshake with %s\n", conn.remoteTCPAddr, conn.remote.Name) log.Printf("->[%s] completed handshake with %s\n", conn.remoteTCPAddr, conn.remote.Name)
conn.Router.Ourself.AddConnection(conn) conn.Router.Ourself.AddConnection(conn)
if conn.remoteUDPAddr != nil { if conn.remoteUDPAddr != nil {
if err = conn.ensureForwarders(); err == nil { if err := conn.ensureForwarders(); err == nil {
conn.heartbeat = time.NewTicker(FastHeartbeat) conn.heartbeat = time.NewTicker(FastHeartbeat)
conn.forwardHeartbeatFrame() // avoid initial wait conn.forwardHeartbeatFrame() // avoid initial wait
} else {
conn.log("connection shutting down due to error:", err)
return
} }
} }
err := conn.queryLoop(queryChan)
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && !conn.established {
conn.log("connection shutting down due to timeout; possibly caused by blocked UDP connectivity")
} else if err != nil {
conn.log("connection shutting down due to error:", err)
} else {
conn.log("connection shutting down")
}
}
func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction) (err error) {
terminate := false terminate := false
for !terminate && err == nil { for !terminate && err == nil {
select { select {
@@ -198,17 +211,9 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
case <-tickerChan(conn.fragTest): case <-tickerChan(conn.fragTest):
conn.setStackFrag(false) conn.setStackFrag(false)
err = conn.handleSendTCP(ProtocolStartFragmentationTestByte) err = conn.handleSendTCP(ProtocolStartFragmentationTestByte)
} }
} }
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && !conn.established { return
conn.log("connection shutting down due to timeout; possibly caused by blocked UDP connectivity")
} else if err != nil {
conn.log("connection shutting down due to error:", err)
} else {
conn.log("connection shutting down")
}
conn.handleShutdown()
} }
func (conn *LocalConnection) handleSetRemoteUDPAddr(remoteUDPAddr *net.UDPAddr) error { func (conn *LocalConnection) handleSetRemoteUDPAddr(remoteUDPAddr *net.UDPAddr) error {