From 08c84e9f855db7c581e55fd0d096eb82409f8498 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 22 Oct 2018 20:34:04 -0700 Subject: [PATCH] connect to tcp in parallel --- src/cli/cli.go | 2 ++ src/croc/recipient.go | 24 +++++++++++++++--------- src/croc/sender.go | 24 +++++++++++++++--------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index 1059dbf..344357e 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -8,6 +8,7 @@ import ( "log" "os" "path/filepath" + "runtime" "strings" "time" @@ -22,6 +23,7 @@ var Version string var cr *croc.Croc func Run() { + runtime.GOMAXPROCS(runtime.NumCPU()) app := cli.NewApp() app.Name = "croc" if Version == "" { diff --git a/src/croc/recipient.go b/src/croc/recipient.go index dd4b225..d41cf75 100644 --- a/src/croc/recipient.go +++ b/src/croc/recipient.go @@ -172,17 +172,23 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, if !useWebsockets { log.Debugf("connecting to server") tcpConnections = make([]comm.Comm, len(tcpPorts)) + var wg sync.WaitGroup + wg.Add(len(tcpPorts)) for i, tcpPort := range tcpPorts { - log.Debugf("connecting to %d", i) - var message string - tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort) - if err != nil { - log.Error(err) - } - if message != "recipient" { - log.Errorf("got wrong message: %s", message) - } + go func(i int, tcpPort string) { + defer wg.Done() + log.Debugf("connecting to %d", i) + var message string + tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort) + if err != nil { + log.Error(err) + } + if message != "recipient" { + log.Errorf("got wrong message: %s", message) + } + }(i, tcpPort) } + wg.Wait() log.Debugf("fully connected") } isConnectedIfUsingTCP <- true diff --git a/src/croc/sender.go b/src/croc/sender.go index 925ba5e..b6f4248 100644 --- a/src/croc/sender.go +++ b/src/croc/sender.go @@ -284,17 +284,23 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL go func() { if !useWebsockets { log.Debugf("connecting to server") + var wg sync.WaitGroup + wg.Add(len(tcpPorts)) for i, tcpPort := range tcpPorts { - log.Debugf("connecting to %s on connection %d", tcpPort, i) - var message string - tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort) - if err != nil { - log.Error(err) - } - if message != "sender" { - log.Errorf("got wrong message: %s", message) - } + go func(i int, tcpPort string) { + defer wg.Done() + log.Debugf("connecting to %s on connection %d", tcpPort, i) + var message string + tcpConnections[i], message, err = connectToTCPServer(utils.SHA256(fmt.Sprintf("%d%x", i, sessionKey)), serverAddress+":"+tcpPort) + if err != nil { + log.Error(err) + } + if message != "sender" { + log.Errorf("got wrong message: %s", message) + } + }(i, tcpPort) } + wg.Wait() } isConnectedIfUsingTCP <- true }()