diff --git a/conn-client.go b/conn-client.go index 1232d3c6..712c6ef5 100644 --- a/conn-client.go +++ b/conn-client.go @@ -7,32 +7,63 @@ import ( "time" ) -// ConnClient is a client-side RTSP connection. -type ConnClient struct { - nconn net.Conn - br *bufio.Reader - bw *bufio.Writer - readTimeout time.Duration - writeTimeout time.Duration - session string - curCSeq int - auth *AuthClient +// ConnClientConf allows to configure a ConnClient. +type ConnClientConf struct { + // pre-existing TCP connection that will be wrapped + NConn net.Conn + + // (optional) timeout for read requests. + // It defaults to 5 seconds + ReadTimeout time.Duration + + // (optional) timeout for write requests. + // It defaults to 5 seconds + WriteTimeout time.Duration + + // (optional) size of the read buffer. + // It defaults to 4096 bytes + ReadBufferSize int + + // (optional) size of the write buffer. + // It defaults to 4096 bytes + WriteBufferSize int } -// NewConnClient allocates a ConnClient. -func NewConnClient(nconn net.Conn, readTimeout time.Duration, writeTimeout time.Duration) *ConnClient { +// ConnClient is a client-side RTSP connection. +type ConnClient struct { + conf ConnClientConf + br *bufio.Reader + bw *bufio.Writer + session string + curCSeq int + auth *AuthClient +} + +// NewConnClient allocates a ConnClient. See ConnClientConf for the options. +func NewConnClient(conf ConnClientConf) *ConnClient { + if conf.ReadTimeout == time.Duration(0) { + conf.ReadTimeout = 5 * time.Second + } + if conf.WriteTimeout == time.Duration(0) { + conf.WriteTimeout = 5 * time.Second + } + if conf.ReadBufferSize == 0 { + conf.ReadBufferSize = 4096 + } + if conf.WriteBufferSize == 0 { + conf.WriteBufferSize = 4096 + } + return &ConnClient{ - nconn: nconn, - br: bufio.NewReaderSize(nconn, 4096), - bw: bufio.NewWriterSize(nconn, 4096), - readTimeout: readTimeout, - writeTimeout: writeTimeout, + conf: conf, + br: bufio.NewReaderSize(conf.NConn, conf.ReadBufferSize), + bw: bufio.NewWriterSize(conf.NConn, conf.WriteBufferSize), } } // NetConn returns the underlying net.Conn. func (c *ConnClient) NetConn() net.Conn { - return c.nconn + return c.conf.NConn } // SetSession sets a Session header that is automatically inserted into every outgoing request. @@ -71,24 +102,24 @@ func (c *ConnClient) WriteRequest(req *Request) (*Response, error) { c.curCSeq += 1 req.Header["CSeq"] = []string{strconv.FormatInt(int64(c.curCSeq), 10)} - c.nconn.SetWriteDeadline(time.Now().Add(c.writeTimeout)) + c.conf.NConn.SetWriteDeadline(time.Now().Add(c.conf.WriteTimeout)) err := req.write(c.bw) if err != nil { return nil, err } - c.nconn.SetReadDeadline(time.Now().Add(c.readTimeout)) + c.conf.NConn.SetReadDeadline(time.Now().Add(c.conf.ReadTimeout)) return readResponse(c.br) } // ReadInterleavedFrame reads an InterleavedFrame. func (c *ConnClient) ReadInterleavedFrame() (*InterleavedFrame, error) { - c.nconn.SetReadDeadline(time.Now().Add(c.readTimeout)) + c.conf.NConn.SetReadDeadline(time.Now().Add(c.conf.ReadTimeout)) return readInterleavedFrame(c.br) } // WriteInterleavedFrame writes an InterleavedFrame. func (c *ConnClient) WriteInterleavedFrame(frame *InterleavedFrame) error { - c.nconn.SetWriteDeadline(time.Now().Add(c.writeTimeout)) + c.conf.NConn.SetWriteDeadline(time.Now().Add(c.conf.WriteTimeout)) return frame.write(c.bw) } diff --git a/conn-server.go b/conn-server.go index 69a06f46..b4a6fb9d 100644 --- a/conn-server.go +++ b/conn-server.go @@ -6,51 +6,82 @@ import ( "time" ) +// ConnServerConf allows to configure a ConnServer. +type ConnServerConf struct { + // pre-existing TCP connection that will be wrapped + NConn net.Conn + + // (optional) timeout for read requests. + // It defaults to 5 seconds + ReadTimeout time.Duration + + // (optional) timeout for write requests. + // It defaults to 5 seconds + WriteTimeout time.Duration + + // (optional) size of the read buffer. + // It defaults to 4096 bytes + ReadBufferSize int + + // (optional) size of the write buffer. + // It defaults to 4096 bytes + WriteBufferSize int +} + // ConnServer is a server-side RTSP connection. type ConnServer struct { - nconn net.Conn - br *bufio.Reader - bw *bufio.Writer - readTimeout time.Duration - writeTimeout time.Duration + conf ConnServerConf + br *bufio.Reader + bw *bufio.Writer } // NewConnServer allocates a ConnClient. -func NewConnServer(nconn net.Conn, readTimeout time.Duration, writeTimeout time.Duration) *ConnServer { +func NewConnServer(conf ConnServerConf) *ConnServer { + if conf.ReadTimeout == time.Duration(0) { + conf.ReadTimeout = 5 * time.Second + } + if conf.WriteTimeout == time.Duration(0) { + conf.WriteTimeout = 5 * time.Second + } + if conf.ReadBufferSize == 0 { + conf.ReadBufferSize = 4096 + } + if conf.WriteBufferSize == 0 { + conf.WriteBufferSize = 4096 + } + return &ConnServer{ - nconn: nconn, - br: bufio.NewReaderSize(nconn, 4096), - bw: bufio.NewWriterSize(nconn, 4096), - readTimeout: readTimeout, - writeTimeout: writeTimeout, + conf: conf, + br: bufio.NewReaderSize(conf.NConn, conf.ReadBufferSize), + bw: bufio.NewWriterSize(conf.NConn, conf.ReadBufferSize), } } // NetConn returns the underlying net.Conn. func (s *ConnServer) NetConn() net.Conn { - return s.nconn + return s.conf.NConn } // ReadRequest reads a Request. func (s *ConnServer) ReadRequest() (*Request, error) { - s.nconn.SetReadDeadline(time.Time{}) // disable deadline + s.conf.NConn.SetReadDeadline(time.Time{}) // disable deadline return readRequest(s.br) } // WriteResponse writes a response. func (s *ConnServer) WriteResponse(res *Response) error { - s.nconn.SetWriteDeadline(time.Now().Add(s.writeTimeout)) + s.conf.NConn.SetWriteDeadline(time.Now().Add(s.conf.WriteTimeout)) return res.write(s.bw) } // ReadInterleavedFrame reads an InterleavedFrame. func (s *ConnServer) ReadInterleavedFrame() (*InterleavedFrame, error) { - s.nconn.SetReadDeadline(time.Now().Add(s.readTimeout)) + s.conf.NConn.SetReadDeadline(time.Now().Add(s.conf.ReadTimeout)) return readInterleavedFrame(s.br) } // WriteInterleavedFrame writes an InterleavedFrame. func (s *ConnServer) WriteInterleavedFrame(frame *InterleavedFrame) error { - s.nconn.SetWriteDeadline(time.Now().Add(s.writeTimeout)) + s.conf.NConn.SetWriteDeadline(time.Now().Add(s.conf.WriteTimeout)) return frame.write(s.bw) }