fix SendData channel syncing

This commit is contained in:
Serene Han 2016-02-21 22:00:51 -08:00
parent 26f5caf404
commit ef5cb162c0

View file

@ -36,7 +36,7 @@ const (
func copyLoop(a, b net.Conn) { func copyLoop(a, b net.Conn) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
// TODO fix the copy loop. // TODO fix copy loop recovery
go func() { go func() {
io.Copy(b, a) io.Copy(b, a)
log.Println("copy loop b-a break") log.Println("copy loop b-a break")
@ -168,24 +168,22 @@ func (c *webRTCConn) EstablishDataChannel() error {
} }
dc.OnOpen = func() { dc.OnOpen = func() {
log.Println("WebRTC: DataChannel.OnOpen") log.Println("WebRTC: DataChannel.OnOpen")
// if nil != c.snowflake { if nil != c.snowflake {
// panic("PeerConnection snowflake already exists.") panic("PeerConnection snowflake already exists.")
// } }
// Flush the buffer, then enable datachannel. // Flush the buffer, then enable datachannel.
// TODO: Make this more safe dc.Send(c.buffer.Bytes())
// dc.Send(c.buffer.Bytes()) log.Println("Flushed", c.buffer.Len(), "bytes")
// log.Println("Flushed", c.buffer.Len(), "bytes") c.buffer.Reset()
// c.buffer.Reset()
c.snowflake = dc c.snowflake = dc
c.SendData(nil)
} }
dc.OnClose = func() { dc.OnClose = func() {
// Disable the DataChannel as a write destination. // Disable the DataChannel as a write destination.
// Future writes will go to the buffer until a new DataChannel is available. // Future writes will go to the buffer until a new DataChannel is available.
log.Println("WebRTC: DataChannel.OnClose") log.Println("WebRTC: DataChannel.OnClose")
// Only reset if this OnClose was triggered remotely.
if nil != c.snowflake { if nil != c.snowflake {
c.snowflake = nil c.snowflake = nil
// Only reset if this OnClose was triggered remotely.
c.Reset() c.Reset()
} }
} }
@ -253,15 +251,14 @@ func (c *webRTCConn) ReceiveAnswer() {
} }
func (c *webRTCConn) SendData(data []byte) { func (c *webRTCConn) SendData(data []byte) {
c.BytesInfo.AddOutbound(len(data))
// Buffer the data in case datachannel isn't available yet. // Buffer the data in case datachannel isn't available yet.
if nil == c.snowflake { if nil == c.snowflake {
log.Printf("Buffered %d bytes --> WebRTC", len(data)) log.Printf("Buffered %d bytes --> WebRTC", len(data))
c.buffer.Write(data) c.buffer.Write(data)
return return
} }
go func() {
c.writeChannel <- data c.writeChannel <- data
}()
} }
// Expected in own goroutine. // Expected in own goroutine.
@ -274,8 +271,6 @@ func (c *webRTCConn) SendLoop() {
log.Println("Flushed", c.buffer.Len(), "bytes") log.Println("Flushed", c.buffer.Len(), "bytes")
c.buffer.Reset() c.buffer.Reset()
} }
c.BytesInfo.AddOutbound(len(data))
c.snowflake.Send(data) c.snowflake.Send(data)
} }
} }
@ -293,6 +288,8 @@ func (c *webRTCConn) ConnectLoop() {
c.ReceiveAnswer() c.ReceiveAnswer()
<-c.reset <-c.reset
log.Println(" --- snowflake connection reset ---") log.Println(" --- snowflake connection reset ---")
} else {
log.Println("WebRTC: Could not establish DataChannel.")
} }
} }
} }
@ -353,7 +350,7 @@ func handler(conn *pt.SocksConn) error {
handlerChan <- -1 handlerChan <- -1
}() }()
defer conn.Close() defer conn.Close()
log.Println("handler", conn) log.Println("handler fired:", conn)
// TODO: [#3] Fetch ICE server information from Broker. // TODO: [#3] Fetch ICE server information from Broker.
// TODO: [#18] Consider TURN servers here too. // TODO: [#18] Consider TURN servers here too.
@ -377,6 +374,8 @@ func handler(conn *pt.SocksConn) error {
return err return err
} }
// TODO: Make SOCKS acceptance more independent from WebRTC so they can
// be more easily interchanged.
copyLoop(conn, remote) copyLoop(conn, remote)
log.Println("----END---") log.Println("----END---")
return nil return nil