defer conn.Close for simplicity and remove unnecessary goroutines, improve error handling (close #12)

This commit is contained in:
Serene Han 2016-03-19 22:33:54 -07:00
parent 598e2a3bfb
commit b8815627bd
2 changed files with 20 additions and 32 deletions

View file

@ -35,15 +35,12 @@ 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 copy loop recovery
go func() { go func() {
written, err := io.Copy(b, a) io.Copy(b, a)
log.Println("copy loop b-a break", err, written)
wg.Done() wg.Done()
}() }()
go func() { go func() {
written, err := io.Copy(a, b) io.Copy(a, b)
log.Println("copy loop a-b break", err, written)
wg.Done() wg.Done()
}() }()
wg.Wait() wg.Wait()
@ -66,11 +63,9 @@ func dialWebRTC() (*webRTCConn, error) {
if nil == broker { if nil == broker {
return nil, errors.New("Failed to prepare BrokerChannel") return nil, errors.New("Failed to prepare BrokerChannel")
} }
connection := NewWebRTCConnection(config, broker) connection := NewWebRTCConnection(config, broker)
go connection.ConnectLoop() err := connection.Connect()
return connection, err
return connection, nil
} }
func endWebRTC() { func endWebRTC() {
@ -97,14 +92,9 @@ func handler(conn *pt.SocksConn) error {
return err return err
} }
defer remote.Close() defer remote.Close()
defer conn.Close()
webrtcRemote = remote webrtcRemote = remote
// Induce another call to handler
go func() {
<-remote.reset
conn.Close()
}()
err = conn.Grant(&net.TCPAddr{IP: net.IPv4zero, Port: 0}) err = conn.Grant(&net.TCPAddr{IP: net.IPv4zero, Port: 0})
if err != nil { if err != nil {
return err return err
@ -112,8 +102,10 @@ func handler(conn *pt.SocksConn) error {
// TODO: Make SOCKS acceptance more independent from WebRTC so they can // TODO: Make SOCKS acceptance more independent from WebRTC so they can
// be more easily interchanged. // be more easily interchanged.
copyLoop(conn, remote) go copyLoop(conn, remote)
log.Println("----END---") // When WebRTC resets, close the SOCKS connection, which induces new handler.
<-remote.reset
log.Println("---- Closed ---")
return nil return nil
} }
@ -159,7 +151,6 @@ func readSignalingMessages(f *os.File) {
} }
func main() { func main() {
// var err error
webrtc.SetLoggingVerbosity(1) webrtc.SetLoggingVerbosity(1)
logFile, err := os.OpenFile("snowflake.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) logFile, err := os.OpenFile("snowflake.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil { if err != nil {
@ -174,8 +165,7 @@ func main() {
flag.Var(&iceServers, "ice", "comma-separated list of ICE servers") flag.Var(&iceServers, "ice", "comma-separated list of ICE servers")
flag.Parse() flag.Parse()
// Expect user to copy-paste if // TODO: Maybe just get rid of copy-paste option entirely.
// TODO: Maybe just get rid of copy-paste entirely.
if "" != brokerURL { if "" != brokerURL {
log.Println("Rendezvous using Broker at: ", brokerURL) log.Println("Rendezvous using Broker at: ", brokerURL)
if "" != frontDomain { if "" != frontDomain {

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"github.com/keroserene/go-webrtc" "github.com/keroserene/go-webrtc"
"io" "io"
@ -99,26 +100,24 @@ func NewWebRTCConnection(config *webrtc.Configuration,
return connection return connection
} }
// WebRTC re-establishment loop. Expected in own goroutine. // TODO: Multiplex.
func (c *webRTCConn) ConnectLoop() { func (c *webRTCConn) Connect() error {
log.Println("Establishing WebRTC connection...") log.Println("Establishing WebRTC connection...")
// TODO: When go-webrtc is more stable, it's possible that a new // TODO: When go-webrtc is more stable, it's possible that a new
// PeerConnection won't need to be re-prepared each time. // PeerConnection won't need to be re-prepared each time.
err := c.preparePeerConnection() err := c.preparePeerConnection()
if err != nil { if err != nil {
log.Println("WebRTC: Could not create PeerConnection.") return err
return
} }
err = c.establishDataChannel() err = c.establishDataChannel()
if err != nil { if err != nil {
log.Println("WebRTC: Could not establish DataChannel.") return errors.New("WebRTC: Could not establish DataChannel.")
} else {
go c.exchangeSDP()
<-c.reset
log.Println(" --- snowflake connection reset ---")
} }
<-time.After(time.Second * 1) err = c.exchangeSDP()
c.cleanup() if err != nil {
return err
}
return nil
} }
// Create and prepare callbacks on a new WebRTC PeerConnection. // Create and prepare callbacks on a new WebRTC PeerConnection.
@ -277,7 +276,6 @@ func (c *webRTCConn) exchangeSDP() error {
answer = nil answer = nil
} }
} }
log.Printf("Received Answer:\n\n%s\n", answer.Sdp) log.Printf("Received Answer:\n\n%s\n", answer.Sdp)
err := c.pc.SetRemoteDescription(answer) err := c.pc.SetRemoteDescription(answer)
if nil != err { if nil != err {