From 99525ebf85891fbcfa3f2c5599bcc4816e581d35 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 21 Jan 2015 21:23:59 +0000 Subject: [PATCH] refactor: LocalConnection.CheckFatal be gone! use Shutdown instead, which is in the Connection interface, thus further reducing dependency on LocalConnection. --- router/connection.go | 17 ++++------------- router/crypto.go | 6 ++++-- router/forwarder.go | 2 +- router/local_peer.go | 12 ++++++------ router/router.go | 2 +- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/router/connection.go b/router/connection.go index d07d2548..8ccaaa11 100644 --- a/router/connection.go +++ b/router/connection.go @@ -149,17 +149,6 @@ func (conn *LocalConnection) Established() bool { return conn.established } -// Called by the forwarder processes in a few places (including -// crypto), but the connection TCP receiver process, and by the local -// peer actor process. Do not call this from the connection's actor -// process itself. -func (conn *LocalConnection) CheckFatal(err error) error { - if err != nil { - conn.Shutdown(err) - } - return err -} - // Called by forwarder processes, read in Forward (by sniffer and udp // listener process in router). func (conn *LocalConnection) setEffectivePMTU(pmtu int) { @@ -538,7 +527,8 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { for { var msg []byte conn.extendReadDeadline() - if conn.CheckFatal(decoder.Decode(&msg)) != nil { + if err = decoder.Decode(&msg); err != nil { + conn.Shutdown(err) return } msg, err = receiver.Decode(msg) @@ -597,7 +587,8 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder) { conn.SendProtocolMsg(ProtocolMsg{ProtocolFetchAll, nil}) continue } - if conn.CheckFatal(err) != nil { + if err != nil { + conn.Shutdown(err) return } if len(newUpdate) != 0 { diff --git a/router/crypto.go b/router/crypto.go index 8b57722c..f019502e 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -204,7 +204,8 @@ func (ne *NaClEncryptor) Bytes() []byte { nonce := ne.nonce if nonce == nil { freshNonce, encodedNonce, err := EncodeNonce(ne.df) - if err = ne.conn.CheckFatal(err); err != nil { + if err != nil { + ne.conn.Shutdown(err) return []byte{} } ne.conn.SendProtocolMsg(ProtocolMsg{ProtocolNonce, encodedNonce}) @@ -222,7 +223,8 @@ func (ne *NaClEncryptor) Bytes() []byte { ne.nonce = <-ne.nonceChan } else if offset == 1<<14 { // half way through range, send new nonce nonce, encodedNonce, err := EncodeNonce(ne.df) - if err = ne.conn.CheckFatal(err); err != nil { + if err != nil { + ne.conn.Shutdown(err) return []byte{} } ne.nonceChan <- nonce diff --git a/router/forwarder.go b/router/forwarder.go index 2e4f0905..d33cd701 100644 --- a/router/forwarder.go +++ b/router/forwarder.go @@ -349,7 +349,7 @@ func (fwd *Forwarder) flush() { } else if PosixError(err) == syscall.ENOBUFS { // TODO handle this better } else { - fwd.conn.CheckFatal(err) + fwd.conn.Shutdown(err) } } } diff --git a/router/local_peer.go b/router/local_peer.go index 33c49de3..f7b707c6 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -192,21 +192,21 @@ func (peer *LocalPeer) handleAddConnection(conn *LocalConnection) { dupConnLocal := dupConn.(*LocalConnection) switch conn.BreakTie(dupConnLocal) { case TieBreakWon: - dupConnLocal.CheckFatal(dupErr) + dupConn.Shutdown(dupErr) peer.handleDeleteConnection(dupConnLocal) case TieBreakLost: - conn.CheckFatal(dupErr) + conn.Shutdown(dupErr) return case TieBreakTied: // oh good grief. Sod it, just kill both of them. - conn.CheckFatal(dupErr) - dupConnLocal.CheckFatal(dupErr) + conn.Shutdown(dupErr) + dupConn.Shutdown(dupErr) peer.handleDeleteConnection(dupConnLocal) return } } if err := peer.checkConnectionLimit(); err != nil { - conn.CheckFatal(err) + conn.Shutdown(err) return } peer.addConnection(conn) @@ -239,7 +239,7 @@ func (peer *LocalPeer) handleConnectionEstablished(conn *LocalConnection) { log.Fatal("Peer informed of active connection where peer is not the source of connection") } if dupConn, found := peer.connections[conn.Remote().Name]; !found || conn != dupConn { - conn.CheckFatal(fmt.Errorf("Cannot set unknown connection active")) + conn.Shutdown(fmt.Errorf("Cannot set unknown connection active")) return } peer.connectionEstablished(conn) diff --git a/router/router.go b/router/router.go index bc6170b3..65865578 100644 --- a/router/router.go +++ b/router/router.go @@ -252,7 +252,7 @@ func (router *Router) udpReader(conn *net.UDPConn, po PacketSink) { err = relayConn.Decryptor.IterateFrames(handleUDPPacket, udpPacket) if pde, ok := err.(PacketDecodingError); ok { if pde.Fatal { - relayConn.CheckFatal(pde) + relayConn.Shutdown(pde) } else { relayConn.log(pde.Error()) }