mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Simplify WebRTCPeer.exchangeSDP.
No need to run sendOfferToBroker in a goroutine.
This commit is contained in:
parent
8caa737700
commit
5787d5b8b0
1 changed files with 14 additions and 31 deletions
|
@ -24,12 +24,11 @@ type WebRTCPeer struct {
|
||||||
transport *webrtc.DataChannel
|
transport *webrtc.DataChannel
|
||||||
broker *BrokerChannel
|
broker *BrokerChannel
|
||||||
|
|
||||||
offerChannel chan *webrtc.SessionDescription
|
offerChannel chan *webrtc.SessionDescription
|
||||||
answerChannel chan *webrtc.SessionDescription
|
recvPipe *io.PipeReader
|
||||||
recvPipe *io.PipeReader
|
writePipe *io.PipeWriter
|
||||||
writePipe *io.PipeWriter
|
lastReceive time.Time
|
||||||
lastReceive time.Time
|
buffer bytes.Buffer
|
||||||
buffer bytes.Buffer
|
|
||||||
|
|
||||||
closed bool
|
closed bool
|
||||||
|
|
||||||
|
@ -53,7 +52,6 @@ func NewWebRTCPeer(config *webrtc.Configuration,
|
||||||
connection.config = config
|
connection.config = config
|
||||||
connection.broker = broker
|
connection.broker = broker
|
||||||
connection.offerChannel = make(chan *webrtc.SessionDescription, 1)
|
connection.offerChannel = make(chan *webrtc.SessionDescription, 1)
|
||||||
connection.answerChannel = make(chan *webrtc.SessionDescription, 1)
|
|
||||||
|
|
||||||
// Override with something that's not NullLogger to have real logging.
|
// Override with something that's not NullLogger to have real logging.
|
||||||
connection.BytesLogger = &BytesNullLogger{}
|
connection.BytesLogger = &BytesNullLogger{}
|
||||||
|
@ -256,34 +254,22 @@ func (c *WebRTCPeer) establishDataChannel() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebRTCPeer) sendOfferToBroker() {
|
|
||||||
if nil == c.broker {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
offer := c.pc.LocalDescription()
|
|
||||||
answer, err := c.broker.Negotiate(offer)
|
|
||||||
if nil != err || nil == answer {
|
|
||||||
log.Printf("BrokerChannel Error: %s", err)
|
|
||||||
answer = nil
|
|
||||||
}
|
|
||||||
c.answerChannel <- answer
|
|
||||||
}
|
|
||||||
|
|
||||||
// exchangeSDP blocks until an SDP offer is available, sends it to the Broker,
|
// exchangeSDP blocks until an SDP offer is available, sends it to the Broker,
|
||||||
// then awaits the SDP answer.
|
// then awaits the SDP answer.
|
||||||
func (c *WebRTCPeer) exchangeSDP() error {
|
func (c *WebRTCPeer) exchangeSDP() error {
|
||||||
<-c.offerChannel
|
<-c.offerChannel
|
||||||
// Keep trying the same offer until a valid answer arrives.
|
// Keep trying the same offer until a valid answer arrives.
|
||||||
var ok bool
|
|
||||||
var answer *webrtc.SessionDescription
|
var answer *webrtc.SessionDescription
|
||||||
for nil == answer {
|
for {
|
||||||
go c.sendOfferToBroker()
|
var err error
|
||||||
answer, ok = <-c.answerChannel // Blocks...
|
// Send offer to broker (blocks).
|
||||||
if !ok || nil == answer {
|
answer, err = c.broker.Negotiate(c.pc.LocalDescription())
|
||||||
log.Printf("Failed to retrieve answer. Retrying in %v", ReconnectTimeout)
|
if err == nil {
|
||||||
<-time.After(ReconnectTimeout)
|
break
|
||||||
answer = nil
|
|
||||||
}
|
}
|
||||||
|
log.Printf("BrokerChannel Error: %s", err)
|
||||||
|
log.Printf("Failed to retrieve answer. Retrying in %v", ReconnectTimeout)
|
||||||
|
<-time.After(ReconnectTimeout)
|
||||||
}
|
}
|
||||||
log.Printf("Received Answer.\n")
|
log.Printf("Received Answer.\n")
|
||||||
err := c.pc.SetRemoteDescription(*answer)
|
err := c.pc.SetRemoteDescription(*answer)
|
||||||
|
@ -299,9 +285,6 @@ func (c *WebRTCPeer) cleanup() {
|
||||||
if nil != c.offerChannel {
|
if nil != c.offerChannel {
|
||||||
close(c.offerChannel)
|
close(c.offerChannel)
|
||||||
}
|
}
|
||||||
if nil != c.answerChannel {
|
|
||||||
close(c.answerChannel)
|
|
||||||
}
|
|
||||||
// Close this side of the SOCKS pipe.
|
// Close this side of the SOCKS pipe.
|
||||||
if nil != c.writePipe {
|
if nil != c.writePipe {
|
||||||
c.writePipe.Close()
|
c.writePipe.Close()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue