improve failed connection detection code

This commit is contained in:
Jacob
2024-03-08 10:09:02 -05:00
parent ea261ca186
commit e96ea2f4ab
2 changed files with 14 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ type Conn struct {
dataChannel *webrtc.DataChannel
raw datachannel.ReadWriteCloser
readChan chan []byte
// readChan chan []byte
errorChan chan error
closeOnce sync.Once
@@ -27,7 +27,6 @@ func newConn(peer *webrtc.PeerConnection) *Conn {
peerConn: peer,
errorChan: make(chan error, 16), //TODO! - Sizing
}
trace("conn: new: ")
return c
}
@@ -50,6 +49,12 @@ func (c *Conn) Read(b []byte) (int, error) {
}
func (c *Conn) Write(b []byte) (int, error) {
select {
case err := <-c.errorChan:
return 0, err // There was some error
default:
// Just exit
}
return c.raw.Write(b)
}

12
dial.go
View File

@@ -182,10 +182,11 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool, iceServers []stri
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
trace("Dial: Peer Connection State has changed: " + s.String())
// if s == webrtc.PeerConnectionStateClosed {
// // This means the webrtc was closed by one side. Just close it on the other side
// sock.Close()
// }
if s == webrtc.PeerConnectionStateClosed {
trace("Dial: webrtc.PeerConnectionStateClosed")
// This means the webrtc was closed by one side. Just close it on the other side
conn.Close()
}
if s == webrtc.PeerConnectionStateFailed {
trace("Dial: PeerConnectionStateFailed")
@@ -194,7 +195,8 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool, iceServers []stri
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
conn.pushErrorData(fmt.Errorf("Peer Connection has gone to failed"))
// conn.pushErrorData(fmt.Errorf("Peer Connection has gone to failed"))
conn.Close()
} else if s == webrtc.PeerConnectionStateDisconnected {
trace("Dial: PeerConnectionStateDisconnected")
// conn.errorChan <- fmt.Errorf("Peer Connection has gone to disconnected")