mirror of
				https://github.com/aler9/gortsplib
				synced 2025-10-27 01:00:32 +08:00 
			
		
		
		
	update NewConnServer() and NewConnClient() signature
This commit is contained in:
		| @@ -7,32 +7,63 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // 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 | ||||||
|  | } | ||||||
|  |  | ||||||
| // ConnClient is a client-side RTSP connection. | // ConnClient is a client-side RTSP connection. | ||||||
| type ConnClient struct { | type ConnClient struct { | ||||||
| 	nconn        net.Conn | 	conf    ConnClientConf | ||||||
| 	br      *bufio.Reader | 	br      *bufio.Reader | ||||||
| 	bw      *bufio.Writer | 	bw      *bufio.Writer | ||||||
| 	readTimeout  time.Duration |  | ||||||
| 	writeTimeout time.Duration |  | ||||||
| 	session string | 	session string | ||||||
| 	curCSeq int | 	curCSeq int | ||||||
| 	auth    *AuthClient | 	auth    *AuthClient | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewConnClient allocates a ConnClient. | // NewConnClient allocates a ConnClient. See ConnClientConf for the options. | ||||||
| func NewConnClient(nconn net.Conn, readTimeout time.Duration, writeTimeout time.Duration) *ConnClient { | 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{ | 	return &ConnClient{ | ||||||
| 		nconn:        nconn, | 		conf: conf, | ||||||
| 		br:           bufio.NewReaderSize(nconn, 4096), | 		br:   bufio.NewReaderSize(conf.NConn, conf.ReadBufferSize), | ||||||
| 		bw:           bufio.NewWriterSize(nconn, 4096), | 		bw:   bufio.NewWriterSize(conf.NConn, conf.WriteBufferSize), | ||||||
| 		readTimeout:  readTimeout, |  | ||||||
| 		writeTimeout: writeTimeout, |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // NetConn returns the underlying net.Conn. | // NetConn returns the underlying net.Conn. | ||||||
| func (c *ConnClient) NetConn() 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. | // 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 | 	c.curCSeq += 1 | ||||||
| 	req.Header["CSeq"] = []string{strconv.FormatInt(int64(c.curCSeq), 10)} | 	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) | 	err := req.write(c.bw) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		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) | 	return readResponse(c.br) | ||||||
| } | } | ||||||
|  |  | ||||||
| // ReadInterleavedFrame reads an InterleavedFrame. | // ReadInterleavedFrame reads an InterleavedFrame. | ||||||
| func (c *ConnClient) ReadInterleavedFrame() (*InterleavedFrame, error) { | 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) | 	return readInterleavedFrame(c.br) | ||||||
| } | } | ||||||
|  |  | ||||||
| // WriteInterleavedFrame writes an InterleavedFrame. | // WriteInterleavedFrame writes an InterleavedFrame. | ||||||
| func (c *ConnClient) WriteInterleavedFrame(frame *InterleavedFrame) error { | 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) | 	return frame.write(c.bw) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,51 +6,82 @@ import ( | |||||||
| 	"time" | 	"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. | // ConnServer is a server-side RTSP connection. | ||||||
| type ConnServer struct { | type ConnServer struct { | ||||||
| 	nconn        net.Conn | 	conf ConnServerConf | ||||||
| 	br   *bufio.Reader | 	br   *bufio.Reader | ||||||
| 	bw   *bufio.Writer | 	bw   *bufio.Writer | ||||||
| 	readTimeout  time.Duration |  | ||||||
| 	writeTimeout time.Duration |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewConnServer allocates a ConnClient. | // 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{ | 	return &ConnServer{ | ||||||
| 		nconn:        nconn, | 		conf: conf, | ||||||
| 		br:           bufio.NewReaderSize(nconn, 4096), | 		br:   bufio.NewReaderSize(conf.NConn, conf.ReadBufferSize), | ||||||
| 		bw:           bufio.NewWriterSize(nconn, 4096), | 		bw:   bufio.NewWriterSize(conf.NConn, conf.ReadBufferSize), | ||||||
| 		readTimeout:  readTimeout, |  | ||||||
| 		writeTimeout: writeTimeout, |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| // NetConn returns the underlying net.Conn. | // NetConn returns the underlying net.Conn. | ||||||
| func (s *ConnServer) NetConn() net.Conn { | func (s *ConnServer) NetConn() net.Conn { | ||||||
| 	return s.nconn | 	return s.conf.NConn | ||||||
| } | } | ||||||
|  |  | ||||||
| // ReadRequest reads a Request. | // ReadRequest reads a Request. | ||||||
| func (s *ConnServer) ReadRequest() (*Request, error) { | 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) | 	return readRequest(s.br) | ||||||
| } | } | ||||||
|  |  | ||||||
| // WriteResponse writes a response. | // WriteResponse writes a response. | ||||||
| func (s *ConnServer) WriteResponse(res *Response) error { | 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) | 	return res.write(s.bw) | ||||||
| } | } | ||||||
|  |  | ||||||
| // ReadInterleavedFrame reads an InterleavedFrame. | // ReadInterleavedFrame reads an InterleavedFrame. | ||||||
| func (s *ConnServer) ReadInterleavedFrame() (*InterleavedFrame, error) { | 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) | 	return readInterleavedFrame(s.br) | ||||||
| } | } | ||||||
|  |  | ||||||
| // WriteInterleavedFrame writes an InterleavedFrame. | // WriteInterleavedFrame writes an InterleavedFrame. | ||||||
| func (s *ConnServer) WriteInterleavedFrame(frame *InterleavedFrame) error { | 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) | 	return frame.write(s.bw) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 aler9
					aler9