protocol/rtsp/rtsp.go: put tcp dial in new session

tcp dial is now in new session, firstly so that we don't have to do the s.conn == nil check and also so that we dont't have to pass around the url everywhere.
This commit is contained in:
Saxon 2019-04-17 12:44:54 +09:30
parent 85bb88c325
commit c9082c1b4c
1 changed files with 27 additions and 29 deletions

View File

@ -23,43 +23,53 @@ const (
setup = "SETUP"
)
type Session struct {
type session struct {
cSeq int
url *url.URL
conn net.Conn
session string
}
func NewSession() *Session {
return &Session{}
func NewSession(u string) (*session, error) {
s := &session{}
var err error
s.url, err = url.Parse(u)
if err != nil {
return nil, err
}
s.conn, err = net.Dial("tcp", s.url.Host)
if err != nil {
return nil, err
}
return s, nil
}
func (s *Session) Describe(urlStr string) (*Response, error) {
return s.writeRequest(describe, urlStr, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
func (s *session) Describe() (*Response, error) {
return s.writeRequest(describe, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
}
func (s *Session) Options(urlStr string) (*Response, error) {
return s.writeRequest(options, urlStr, nil, nil)
func (s *session) Options(urlStr string) (*Response, error) {
return s.writeRequest(options, nil, nil)
}
func (s *Session) Setup(urlStr, transport string) (*Response, error) {
func (s *session) Setup(transport string) (*Response, error) {
return s.writeRequest(
setup,
urlStr,
func(req *Request) {
req.Header.Add("Transport", transport)
},
func(resp *Response) {
s.session = resp.Header.Get("Session")
s.session = resp.Header.Get("session")
},
)
}
func (s *Session) Play(urlStr string) (*Response, error) {
return s.writeRequest(play, urlStr, func(req *Request) { req.Header.Add("Session", s.session) }, nil)
func (s *session) Play() (*Response, error) {
return s.writeRequest(play, func(req *Request) { req.Header.Add("session", s.session) }, nil)
}
func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), respOp func(*Response)) (*Response, error) {
req, err := NewRequest(method, urlStr, s.nextCSeq(), nil)
func (s *session) writeRequest(method string, headerOp func(*Request), respOp func(*Response)) (*Response, error) {
req, err := NewRequest(method, s.nextCSeq(), s.url, nil)
if err != nil {
return nil, err
}
@ -68,14 +78,7 @@ func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), r
headerOp(req)
}
if s.conn == nil {
s.conn, err = net.Dial("tcp", req.URL.Host)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(s.conn, (*Request)(req).String())
_, err = io.WriteString(s.conn, req.String())
if err != nil {
return nil, err
}
@ -91,7 +94,7 @@ func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), r
return res, nil
}
func (s *Session) nextCSeq() string {
func (s *session) nextCSeq() string {
s.cSeq++
return strconv.Itoa(s.cSeq)
}
@ -122,12 +125,7 @@ func (r Request) String() string {
return s
}
func NewRequest(method, urlStr, cSeq string, body io.ReadCloser) (*Request, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
func NewRequest(method, cSeq string, u *url.URL, body io.ReadCloser) (*Request, error) {
req := &Request{
Method: method,
URL: u,