mirror of
https://github.com/unitoftime/rtcnet.git
synced 2025-09-26 20:31:17 +08:00
improve failed connection detection code
This commit is contained in:
9
conn.go
9
conn.go
@@ -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
12
dial.go
@@ -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")
|
||||
|
Reference in New Issue
Block a user