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.
This commit is contained in:
John Brooks 2017-09-17 23:01:22 +02:00 committed by Sarah Jamie Lewis
parent d19102b257
commit 0f47f62465
3 changed files with 8 additions and 5 deletions

View File

@ -109,9 +109,10 @@ func (rc *Connection) Do(do func() error) error {
// An error is returned only if the requirements for opening this channel // 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 // are not met on the local side (a nil error return does not mean the
// channel was opened successfully, because channels open asynchronously). // 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)) 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 // Check that we have the authentication already
if handler.RequiresAuthentication() != "none" { if handler.RequiresAuthentication() != "none" {
// Enforce Authentication Check. // 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 { if err != nil {
rc.traceLog(fmt.Sprintf("failed to request open channel of type %v", err)) 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 nil
}) })
return channel, err
} }
// Process receives socket and protocol events for the connection. Methods // Process receives socket and protocol events for the connection. Methods

View File

@ -48,7 +48,7 @@ func (och *OutboundConnectionHandler) ProcessAuthAsClient(privateKey *rsa.Privat
och.connection.Break() och.connection.Break()
} }
err := och.connection.RequestOpenChannel("im.ricochet.auth.hidden-service", _, err := och.connection.RequestOpenChannel("im.ricochet.auth.hidden-service",
&channels.HiddenServiceAuthChannel{ &channels.HiddenServiceAuthChannel{
PrivateKey: privateKey, PrivateKey: privateKey,
ServerHostname: och.connection.RemoteHostname, ServerHostname: och.connection.RemoteHostname,

View File

@ -64,7 +64,7 @@ func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string)
go rc.Process(echobot) go rc.Process(echobot)
if !known { 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 { if err != nil {
log.Printf("could not contact %s", err) log.Printf("could not contact %s", err)
} }