[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [goptlib/master] More consistent style.



commit 2919f2cb00a8adcfa518438d69b149febbff0ee7
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Sat Dec 7 21:40:10 2013 -0800

    More consistent style.
---
 examples/dummy-client/dummy-client.go |   27 +++++++++------------------
 examples/dummy-server/dummy-server.go |   22 ++++++++++------------
 pt.go                                 |    3 ++-
 socks.go                              |    5 +++--
 4 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/examples/dummy-client/dummy-client.go b/examples/dummy-client/dummy-client.go
index de56d99..9497f99 100644
--- a/examples/dummy-client/dummy-client.go
+++ b/examples/dummy-client/dummy-client.go
@@ -43,26 +43,25 @@ func copyLoop(a, b net.Conn) {
 	wg.Wait()
 }
 
-func handleConnection(local *pt.SocksConn) error {
-	defer local.Close()
-
+func handleConnection(conn *pt.SocksConn) error {
 	handlerChan <- 1
 	defer func() {
 		handlerChan <- -1
 	}()
 
-	remote, err := net.Dial("tcp", local.Req.Target)
+	defer conn.Close()
+	remote, err := net.Dial("tcp", conn.Req.Target)
 	if err != nil {
-		local.Reject()
+		conn.Reject()
 		return err
 	}
-	err = local.Grant(remote.RemoteAddr().(*net.TCPAddr))
+	defer remote.Close()
+	err = conn.Grant(remote.RemoteAddr().(*net.TCPAddr))
 	if err != nil {
 		return err
 	}
 
-	defer remote.Close()
-	copyLoop(local, remote)
+	copyLoop(conn, remote)
 
 	return nil
 }
@@ -78,15 +77,6 @@ func acceptLoop(ln *pt.SocksListener) error {
 	return nil
 }
 
-func startListener(addr string) (net.Listener, error) {
-	ln, err := pt.ListenSocks("tcp", addr)
-	if err != nil {
-		return nil, err
-	}
-	go acceptLoop(ln)
-	return ln, nil
-}
-
 func main() {
 	var err error
 
@@ -97,11 +87,12 @@ func main() {
 
 	listeners := make([]net.Listener, 0)
 	for _, methodName := range ptInfo.MethodNames {
-		ln, err := startListener("127.0.0.1:0")
+		ln, err := pt.ListenSocks("tcp", "127.0.0.1:0")
 		if err != nil {
 			pt.CmethodError(methodName, err.Error())
 			continue
 		}
+		go acceptLoop(ln)
 		pt.Cmethod(methodName, "socks4", ln.Addr())
 		listeners = append(listeners, ln)
 	}
diff --git a/examples/dummy-server/dummy-server.go b/examples/dummy-server/dummy-server.go
index 0518737..7341b17 100644
--- a/examples/dummy-server/dummy-server.go
+++ b/examples/dummy-server/dummy-server.go
@@ -44,7 +44,9 @@ func copyLoop(a, b net.Conn) {
 	wg.Wait()
 }
 
-func handleConnection(conn net.Conn) {
+func handleConnection(conn net.Conn) error {
+	defer conn.Close()
+
 	handlerChan <- 1
 	defer func() {
 		handlerChan <- -1
@@ -52,9 +54,13 @@ func handleConnection(conn net.Conn) {
 
 	or, err := pt.ConnectOr(&ptInfo, conn.RemoteAddr(), "dummy")
 	if err != nil {
-		return
+		return err
 	}
+	defer or.Close()
+
 	copyLoop(conn, or)
+
+	return nil
 }
 
 func acceptLoop(ln net.Listener) error {
@@ -68,15 +74,6 @@ func acceptLoop(ln net.Listener) error {
 	return nil
 }
 
-func startListener(addr *net.TCPAddr) (net.Listener, error) {
-	ln, err := net.ListenTCP("tcp", addr)
-	if err != nil {
-		return nil, err
-	}
-	go acceptLoop(ln)
-	return ln, nil
-}
-
 func main() {
 	var err error
 
@@ -87,11 +84,12 @@ func main() {
 
 	listeners := make([]net.Listener, 0)
 	for _, bindaddr := range ptInfo.Bindaddrs {
-		ln, err := startListener(bindaddr.Addr)
+		ln, err := net.ListenTCP("tcp", bindaddr.Addr)
 		if err != nil {
 			pt.SmethodError(bindaddr.MethodName, err.Error())
 			continue
 		}
+		go acceptLoop(ln)
 		pt.Smethod(bindaddr.MethodName, ln.Addr())
 		listeners = append(listeners, ln)
 	}
diff --git a/pt.go b/pt.go
index ddecc29..345a7bd 100644
--- a/pt.go
+++ b/pt.go
@@ -34,11 +34,12 @@
 // 		os.Exit(1)
 // 	}
 // 	for _, bindaddr := range ptInfo.Bindaddrs {
-// 		ln, err := startListener(bindaddr.Addr, bindaddr.MethodName)
+// 		ln, err := net.ListenTCP("tcp", bindaddr.Addr)
 // 		if err != nil {
 // 			pt.SmethodError(bindaddr.MethodName, err.Error())
 // 			continue
 // 		}
+// 		go acceptLoop(ln)
 // 		pt.Smethod(bindaddr.MethodName, ln.Addr())
 // 	}
 // 	pt.SmethodsDone()
diff --git a/socks.go b/socks.go
index 8534636..c982c7a 100644
--- a/socks.go
+++ b/socks.go
@@ -43,19 +43,20 @@ func (conn *SocksConn) Reject() error {
 //
 // 	func handleConn(conn *pt.SocksConn) error {
 // 		defer conn.Close()
-//
 // 		remote, err := net.Dial("tcp", conn.Req.Target)
 // 		if err != nil {
 // 			conn.Reject()
 // 			return err
 // 		}
+// 		defer remote.Close()
 // 		err = conn.Grant(remote.RemoteAddr().(*net.TCPAddr))
 // 		if err != nil {
 // 			return err
 // 		}
-// 		defer remote.Close()
 //
 // 		// do something with conn and remote
+//
+// 		return nil
 // 	}
 // 	...
 // 	ln, err := pt.ListenSocks("tcp", "127.0.0.1:0")



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits