From 4d0ca39f97efb6f24f1935cfee4899d13ee4dafc Mon Sep 17 00:00:00 2001 From: David Wragg Date: Tue, 19 May 2015 11:00:57 +0100 Subject: [PATCH] Eliminate the Start method on LocalConnection --- router/connection.go | 21 +++++++++------------ router/local_peer.go | 3 +-- router/router.go | 3 +-- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/router/connection.go b/router/connection.go index 980de454..cbd1b71e 100644 --- a/router/connection.go +++ b/router/connection.go @@ -97,26 +97,23 @@ func (conn *RemoteConnection) String() string { return fmt.Sprint("Connection ", from, "->", to) } -func NewLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router) *LocalConnection { +// Does not return anything. If the connection is successful, it will +// end up in the local peer's connections map. +func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router, acceptNewPeer bool) { if connRemote.local != router.Ourself.Peer { log.Fatal("Attempt to create local connection from a peer which is not ourself") } // NB, we're taking a copy of connRemote here. - return &LocalConnection{ + actionChan := make(chan ConnectionAction, ChannelSize) + finished := make(chan struct{}) + conn := &LocalConnection{ RemoteConnection: *connRemote, Router: router, TCPConn: tcpConn, remoteUDPAddr: udpAddr, - effectivePMTU: DefaultPMTU} -} - -// Async. Does not return anything. If the connection is successful, -// it will end up in the local peer's connections map. -func (conn *LocalConnection) Start(acceptNewPeer bool) { - actionChan := make(chan ConnectionAction, ChannelSize) - conn.actionChan = actionChan - finished := make(chan struct{}) - conn.finished = finished + effectivePMTU: DefaultPMTU, + actionChan: actionChan, + finished: finished} go conn.run(actionChan, finished, acceptNewPeer) } diff --git a/router/local_peer.go b/router/local_peer.go index 09498629..0a15076d 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -132,8 +132,7 @@ func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) err return err } connRemote := NewRemoteConnection(peer.Peer, nil, tcpConn.RemoteAddr().String(), true, false) - connLocal := NewLocalConnection(connRemote, tcpConn, udpAddr, peer.router) - connLocal.Start(acceptNewPeer) + StartLocalConnection(connRemote, tcpConn, udpAddr, peer.router, acceptNewPeer) return nil } diff --git a/router/router.go b/router/router.go index 753853fc..c94ed30a 100644 --- a/router/router.go +++ b/router/router.go @@ -209,8 +209,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) { remoteAddrStr := tcpConn.RemoteAddr().String() log.Printf("->[%s] connection accepted\n", remoteAddrStr) connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false, false) - connLocal := NewLocalConnection(connRemote, tcpConn, nil, router) - connLocal.Start(true) + StartLocalConnection(connRemote, tcpConn, nil, router, true) } func (router *Router) listenUDP(localPort int, po PacketSink) *net.UDPConn {