mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
ServerConn: remove mutex around request handler
This commit is contained in:
@@ -34,7 +34,8 @@ type ServerConn struct {
|
|||||||
nconn net.Conn
|
nconn net.Conn
|
||||||
br *bufio.Reader
|
br *bufio.Reader
|
||||||
bw *bufio.Writer
|
bw *bufio.Writer
|
||||||
mutex sync.Mutex
|
writeMutex sync.Mutex
|
||||||
|
nextFramesEnabled bool
|
||||||
framesEnabled bool
|
framesEnabled bool
|
||||||
readTimeoutEnabled bool
|
readTimeoutEnabled bool
|
||||||
}
|
}
|
||||||
@@ -51,7 +52,7 @@ func (sc *ServerConn) NetConn() net.Conn {
|
|||||||
|
|
||||||
// EnableFrames allows reading and writing TCP frames.
|
// EnableFrames allows reading and writing TCP frames.
|
||||||
func (sc *ServerConn) EnableFrames(v bool) {
|
func (sc *ServerConn) EnableFrames(v bool) {
|
||||||
sc.framesEnabled = v
|
sc.nextFramesEnabled = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnableReadTimeout sets or removes the timeout on incoming packets.
|
// EnableReadTimeout sets or removes the timeout on incoming packets.
|
||||||
@@ -247,17 +248,16 @@ func (sc *ServerConn) backgroundRead(handlers ServerConnReadHandlers, done chan
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleRequestOuter := func(req *base.Request) error {
|
handleRequestOuter := func(req *base.Request) error {
|
||||||
sc.mutex.Lock()
|
|
||||||
defer sc.mutex.Unlock()
|
|
||||||
|
|
||||||
// check cseq
|
// check cseq
|
||||||
cseq, ok := req.Header["CSeq"]
|
cseq, ok := req.Header["CSeq"]
|
||||||
if !ok || len(cseq) != 1 {
|
if !ok || len(cseq) != 1 {
|
||||||
|
sc.writeMutex.Lock()
|
||||||
sc.nconn.SetWriteDeadline(time.Now().Add(sc.s.conf.WriteTimeout))
|
sc.nconn.SetWriteDeadline(time.Now().Add(sc.s.conf.WriteTimeout))
|
||||||
base.Response{
|
base.Response{
|
||||||
StatusCode: base.StatusBadRequest,
|
StatusCode: base.StatusBadRequest,
|
||||||
Header: base.Header{},
|
Header: base.Header{},
|
||||||
}.Write(sc.bw)
|
}.Write(sc.bw)
|
||||||
|
sc.writeMutex.Unlock()
|
||||||
return ErrServerMissingCseq
|
return ErrServerMissingCseq
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,9 +277,19 @@ func (sc *ServerConn) backgroundRead(handlers ServerConnReadHandlers, done chan
|
|||||||
handlers.OnResponse(res)
|
handlers.OnResponse(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sc.writeMutex.Lock()
|
||||||
|
|
||||||
sc.nconn.SetWriteDeadline(time.Now().Add(sc.s.conf.WriteTimeout))
|
sc.nconn.SetWriteDeadline(time.Now().Add(sc.s.conf.WriteTimeout))
|
||||||
res.Write(sc.bw)
|
res.Write(sc.bw)
|
||||||
|
|
||||||
|
// set framesEnabled after sending the response
|
||||||
|
// in order to start sending frames after the response
|
||||||
|
if sc.framesEnabled != sc.nextFramesEnabled {
|
||||||
|
sc.framesEnabled = sc.nextFramesEnabled
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.writeMutex.Unlock()
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,8 +357,8 @@ func (sc *ServerConn) Read(handlers ServerConnReadHandlers) chan error {
|
|||||||
|
|
||||||
// WriteFrame writes a frame.
|
// WriteFrame writes a frame.
|
||||||
func (sc *ServerConn) WriteFrame(trackID int, streamType StreamType, content []byte) error {
|
func (sc *ServerConn) WriteFrame(trackID int, streamType StreamType, content []byte) error {
|
||||||
sc.mutex.Lock()
|
sc.writeMutex.Lock()
|
||||||
defer sc.mutex.Unlock()
|
defer sc.writeMutex.Unlock()
|
||||||
|
|
||||||
if !sc.framesEnabled {
|
if !sc.framesEnabled {
|
||||||
return ErrServerFramesDisabled
|
return ErrServerFramesDisabled
|
||||||
|
Reference in New Issue
Block a user