From 4d767f106c1f6ebcf23a712d003f4e1995d8415b Mon Sep 17 00:00:00 2001 From: scruzin Date: Sun, 6 Jan 2019 08:25:46 +1030 Subject: [PATCH] Make for Go idiomatic. --- rtmp/rtmp.go | 8 +++++--- rtmp/socket.go | 37 +++++++++++-------------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 88ea8db2..d4dcd18e 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -447,9 +447,8 @@ func C_WriteN(r *C_RTMP, buf []byte) (ok bool) { n, err := C_RTMPSockBuf_Send(&r.m_sb, buf) if n < 0 { if debugMode { - log.Println("C_WriteN, RTMP send error: " + err.Error()) + log.Printf("C_WriteN, RTMP send error: %v\n", err) } - C_RTMP_Close(r) return false } @@ -1476,7 +1475,10 @@ func C_CloseInternal(r *C_RTMP, reconnect bool) { } C_SendDeleteStream(r, float64(i)) } - C_RTMPSockBuf_Close(&r.m_sb) + err := C_RTMPSockBuf_Close(&r.m_sb) + if err != nil && debugMode { + log.Printf("C_RTMPSockBuf_Close error: %v\n", err) + } } r.m_stream_id = -1 diff --git a/rtmp/socket.go b/rtmp/socket.go index fd044e26..80b0e253 100644 --- a/rtmp/socket.go +++ b/rtmp/socket.go @@ -35,8 +35,8 @@ package rtmp import ( "errors" - "fmt" "net" + "strconv" "time" ) @@ -46,9 +46,7 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error { if r.Link.host == "" { return errors.New("Empty host") } - - hostPort := fmt.Sprintf("%s:%d", r.Link.host, r.Link.port) - addr, err := net.ResolveTCPAddr("tcp4", hostPort) + addr, err := net.ResolveTCPAddr("tcp4", r.Link.host+":"+strconv.Itoa(int(r.Link.port))) if err != nil { return err } @@ -56,13 +54,11 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error { if err != nil { return err } - r.m_sb.timeout = r.Link.timeout - if C_RTMP_Connect1(r, cp) { - return nil - } else { + if !C_RTMP_Connect1(r, cp) { return errors.New("RTMP connection failed") } + return nil } // int SocksNegotiate(RTMP* r); @@ -76,15 +72,12 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) (int, error) { sb.sb_start = 0 } err := sb.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout))) - if err != nil { - return -1, err - } - n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:]) if err != nil { return 0, err } + n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:]) sb.sb_size += n - return n, nil + return n, err } // int RTMPSockBuf_Send(RTMPSockBuf* sb, const char* buf, int len); @@ -93,25 +86,17 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) (int, error) { func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) (int, error) { err := sb.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout))) if err != nil { - return -1, err + return 0, err } - n, err := sb.conn.Write(buf) - if err != nil { - return -1, err - } - return n, nil + return sb.conn.Write(buf) } // int // RTMPSockBuf_Close(RTMPSockBuf *sb) // rtmp.c +4369 func C_RTMPSockBuf_Close(sb *C_RTMPSockBuf) error { - if sb.conn != nil { - err := sb.conn.Close() - sb.conn = nil - if err != nil { - return err - } + if sb.conn == nil { + return nil } - return nil + return sb.conn.Close() }