From a9799c7cf1071e4ce79b8a4c6ed7b4ee9b72b74d Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 21 Jan 2020 09:29:02 +0100 Subject: [PATCH] fix: add SetWriteDeadline() and prevent timeout when receiving via tcp --- client.go | 5 ----- main.go | 9 +++++++++ server_udpl.go | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 2684c687..2e9ff47c 100644 --- a/client.go +++ b/client.go @@ -13,11 +13,6 @@ import ( "gortc.io/sdp" ) -const ( - _READ_TIMEOUT = 5 * time.Second - _WRITE_TIMEOUT = 5 * time.Second -) - func interleavedChannelToTrack(channel int) (int, trackFlow) { if (channel % 2) == 0 { return (channel / 2), _TRACK_FLOW_RTP diff --git a/main.go b/main.go index 2cdd9aec..6e5bf5c9 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,18 @@ import ( "regexp" "strings" "sync" + "time" "gopkg.in/alecthomas/kingpin.v2" ) var Version string = "v0.0.0" +const ( + _READ_TIMEOUT = 5 * time.Second + _WRITE_TIMEOUT = 5 * time.Second +) + type trackFlow int const ( @@ -124,11 +130,13 @@ func (p *program) forwardTrack(path string, id int, flow trackFlow, frame []byte if c.path == path && c.state == _CLIENT_STATE_PLAY { if c.streamProtocol == _STREAM_PROTOCOL_UDP { if flow == _TRACK_FLOW_RTP { + p.rtpl.nconn.SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT)) p.rtpl.nconn.WriteTo(frame, &net.UDPAddr{ IP: c.ip, Port: c.streamTracks[id].rtpPort, }) } else { + p.rtcpl.nconn.SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT)) p.rtcpl.nconn.WriteTo(frame, &net.UDPAddr{ IP: c.ip, Port: c.streamTracks[id].rtcpPort, @@ -136,6 +144,7 @@ func (p *program) forwardTrack(path string, id int, flow trackFlow, frame []byte } } else { + c.conn.NetConn().SetWriteDeadline(time.Now().Add(_WRITE_TIMEOUT)) c.conn.WriteInterleavedFrame(trackToInterleavedChannel(id, flow), frame) } } diff --git a/server_udpl.go b/server_udpl.go index bd7748b3..b8bec15a 100644 --- a/server_udpl.go +++ b/server_udpl.go @@ -53,7 +53,7 @@ func (l *serverUdpListener) run() { l.p.mutex.RLock() defer l.p.mutex.RUnlock() - // find path and track id + // find path and track id from ip and port path, trackId := func() (string, int) { for _, pub := range l.p.publishers { for i, t := range pub.streamTracks {