Merged Session.start()/close() into Open()/Close() respectively.

This commit is contained in:
scruzin 2019-01-11 00:29:51 +10:30
parent d4a0fad2be
commit b3b1b04814
1 changed files with 16 additions and 30 deletions

View File

@ -127,54 +127,37 @@ func NewSession(url string, timeout uint, log Log) *Session {
// Open establishes an rtmp connection with the url passed into the constructor. // Open establishes an rtmp connection with the url passed into the constructor.
func (s *Session) Open() error { func (s *Session) Open() error {
s.log(DebugLevel, pkg+"Session.Open")
if s.isConnected() { if s.isConnected() {
return errConnected return errConnected
} }
err := s.start()
if err != nil {
return err
}
return nil
}
// start does the heavylifting for Open().
func (s *Session) start() error {
s.log(DebugLevel, pkg+"Session.start")
err := setupURL(s) err := setupURL(s)
if err != nil { if err != nil {
s.close()
return err return err
} }
s.enableWrite() s.enableWrite()
err = connect(s) err = connect(s)
if err != nil { if err != nil {
s.close() s.Close()
return err return err
} }
err = connectStream(s) err = connectStream(s)
if err != nil { if err != nil {
s.close() s.Close()
return err return err
} }
return nil return nil
} }
// Close terminates the rtmp connection, // Close terminates the rtmp connection.
// NB: The session object is cleared completely.
func (s *Session) Close() error { func (s *Session) Close() error {
s.log(DebugLevel, pkg+"Session.Close")
if !s.isConnected() { if !s.isConnected() {
return errNotConnected return errNotConnected
} }
s.close()
return nil
}
// close does the heavylifting for Close().
// Any errors are ignored as it is often called in response to an earlier error.
func (s *Session) close() {
s.log(DebugLevel, pkg+"Session.close")
if s.isConnected() {
if s.streamID > 0 { if s.streamID > 0 {
if s.link.protocol&RTMP_FEATURE_WRITE != 0 { if s.link.protocol&RTMP_FEATURE_WRITE != 0 {
sendFCUnpublish(s) sendFCUnpublish(s)
@ -182,8 +165,8 @@ func (s *Session) close() {
sendDeleteStream(s, float64(s.streamID)) sendDeleteStream(s, float64(s.streamID))
} }
s.link.conn.Close() s.link.conn.Close()
}
*s = Session{} *s = Session{}
return nil
} }
// Write writes a frame (flv tag) to the rtmp connection. // Write writes a frame (flv tag) to the rtmp connection.
@ -216,7 +199,10 @@ func (s *Session) Write(data []byte) (int, error) {
} }
// I/O functions // I/O functions
// read from an RTMP connection.
// read from an RTMP connection. Sends a bytes received message if the
// number of bytes received (nBytesIn) is greater than the number sent
// (nBytesInSent) by 10% of the bandwidth.
func (s *Session) read(buf []byte) error { func (s *Session) read(buf []byte) error {
err := s.link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(s.link.timeout))) err := s.link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(s.link.timeout)))
if err != nil { if err != nil {