From 0f47f624659646c646da906d9d815b861eac4675 Mon Sep 17 00:00:00 2001 From: John Brooks Date: Sun, 17 Sep 2017 23:01:22 +0200 Subject: [PATCH] Return the new channel from RequestOpenChannel This fixes a quirk where it would've been difficult to tell which of several channels of the same type+direction is the one you just created. It's also a fairly common pattern to want to interact with a channel right after opening it; for example, a chat channel is opened and can immediately send messages before getting the peer response. It's convenient to not have to do a separate lookup. --- connection/connection.go | 9 ++++++--- connection/outboundconnectionhandler.go | 2 +- examples/echobot/main.go | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/connection/connection.go b/connection/connection.go index 42af568..48f462d 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -109,9 +109,10 @@ func (rc *Connection) Do(do func() error) error { // An error is returned only if the requirements for opening this channel // are not met on the local side (a nil error return does not mean the // channel was opened successfully, because channels open asynchronously). -func (rc *Connection) RequestOpenChannel(ctype string, handler channels.Handler) error { +func (rc *Connection) RequestOpenChannel(ctype string, handler channels.Handler) (*channels.Channel, error) { rc.traceLog(fmt.Sprintf("requesting open channel of type %s", ctype)) - return rc.Do(func() error { + var channel *channels.Channel + err := rc.Do(func() error { // Check that we have the authentication already if handler.RequiresAuthentication() != "none" { // Enforce Authentication Check. @@ -121,7 +122,8 @@ func (rc *Connection) RequestOpenChannel(ctype string, handler channels.Handler) } } - channel, err := rc.channelManager.OpenChannelRequest(handler) + var err error + channel, err = rc.channelManager.OpenChannelRequest(handler) if err != nil { rc.traceLog(fmt.Sprintf("failed to request open channel of type %v", err)) @@ -148,6 +150,7 @@ func (rc *Connection) RequestOpenChannel(ctype string, handler channels.Handler) } return nil }) + return channel, err } // Process receives socket and protocol events for the connection. Methods diff --git a/connection/outboundconnectionhandler.go b/connection/outboundconnectionhandler.go index af81c24..c966140 100644 --- a/connection/outboundconnectionhandler.go +++ b/connection/outboundconnectionhandler.go @@ -48,7 +48,7 @@ func (och *OutboundConnectionHandler) ProcessAuthAsClient(privateKey *rsa.Privat och.connection.Break() } - err := och.connection.RequestOpenChannel("im.ricochet.auth.hidden-service", + _, err := och.connection.RequestOpenChannel("im.ricochet.auth.hidden-service", &channels.HiddenServiceAuthChannel{ PrivateKey: privateKey, ServerHostname: och.connection.RemoteHostname, diff --git a/examples/echobot/main.go b/examples/echobot/main.go index 485c803..8c4d1e7 100644 --- a/examples/echobot/main.go +++ b/examples/echobot/main.go @@ -64,7 +64,7 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string) go rc.Process(echobot) if !known { - err := rc.RequestOpenChannel("im.ricochet.contact.request", &channels.ContactRequestChannel{Handler: echobot}) + _, err := rc.RequestOpenChannel("im.ricochet.contact.request", &channels.ContactRequestChannel{Handler: echobot}) if err != nil { log.Printf("could not contact %s", err) }