Make for Go idiomatic.

This commit is contained in:
scruzin 2019-01-06 08:25:46 +10:30
parent 513b84ad87
commit 4d767f106c
2 changed files with 16 additions and 29 deletions

View File

@ -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

View File

@ -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()
}