mirror of
https://github.com/unitoftime/rtcnet.git
synced 2025-10-05 16:26:56 +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
|
dataChannel *webrtc.DataChannel
|
||||||
raw datachannel.ReadWriteCloser
|
raw datachannel.ReadWriteCloser
|
||||||
|
|
||||||
readChan chan []byte
|
// readChan chan []byte
|
||||||
errorChan chan error
|
errorChan chan error
|
||||||
|
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
@@ -27,7 +27,6 @@ func newConn(peer *webrtc.PeerConnection) *Conn {
|
|||||||
peerConn: peer,
|
peerConn: peer,
|
||||||
errorChan: make(chan error, 16), //TODO! - Sizing
|
errorChan: make(chan error, 16), //TODO! - Sizing
|
||||||
}
|
}
|
||||||
trace("conn: new: ")
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +49,12 @@ func (c *Conn) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Write(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)
|
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) {
|
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||||
trace("Dial: Peer Connection State has changed: " + s.String())
|
trace("Dial: Peer Connection State has changed: " + s.String())
|
||||||
|
|
||||||
// if s == webrtc.PeerConnectionStateClosed {
|
if s == webrtc.PeerConnectionStateClosed {
|
||||||
// // This means the webrtc was closed by one side. Just close it on the other side
|
trace("Dial: webrtc.PeerConnectionStateClosed")
|
||||||
// sock.Close()
|
// This means the webrtc was closed by one side. Just close it on the other side
|
||||||
// }
|
conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
if s == webrtc.PeerConnectionStateFailed {
|
if s == webrtc.PeerConnectionStateFailed {
|
||||||
trace("Dial: 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.
|
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
|
||||||
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
|
// 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 {
|
} else if s == webrtc.PeerConnectionStateDisconnected {
|
||||||
trace("Dial: PeerConnectionStateDisconnected")
|
trace("Dial: PeerConnectionStateDisconnected")
|
||||||
// conn.errorChan <- fmt.Errorf("Peer Connection has gone to disconnected")
|
// conn.errorChan <- fmt.Errorf("Peer Connection has gone to disconnected")
|
||||||
|
Reference in New Issue
Block a user