rtmp: make C_ReadN take a []byte

This commit is contained in:
Dan Kortschak 2018-09-19 11:50:46 +09:30
parent 5f3f50373b
commit eeb1e1933b
1 changed files with 24 additions and 33 deletions

View File

@ -469,7 +469,7 @@ func C_SocksNegotiate(r *C_RTMP) (ok bool) {
C_WriteN(r, packet)
if C_ReadN(r, &packet[0], 8) != 8 {
if C_ReadN(r, packet[:8]) != 8 {
return false
}
@ -580,20 +580,15 @@ func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 {
// int ReadN(RTMP* r, char* buffer, int n);
// rtmp.c +1390
func C_ReadN(r *C_RTMP, buffer *byte, n int) int {
nOriginalSize := n
var avail int
var ptr *byte
func C_ReadN(r *C_RTMP, buf []byte) int {
nOriginalSize := len(buf)
r.m_sb.sb_timedout = false
ptr = buffer
for n > 0 {
for len(buf) != 0 {
nBytes := 0
var nRead int
avail = int(r.m_sb.sb_size)
avail := int(r.m_sb.sb_size)
if avail == 0 {
if C_RTMPSockBuf_Fill(&r.m_sb) < 1 {
if !r.m_sb.sb_timedout {
@ -603,14 +598,14 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int {
avail = int(r.m_sb.sb_size)
}
if n < avail {
nRead = n
if len(buf) < avail {
nRead = len(buf)
} else {
nRead = avail
}
if nRead > 0 {
memmove(unsafe.Pointer(ptr), unsafe.Pointer(&r.m_sb.sb_buf[r.m_sb.sb_start]), uintptr(nRead))
copy(buf, r.m_sb.sb_buf[r.m_sb.sb_start:][:nRead])
r.m_sb.sb_start += nRead
r.m_sb.sb_size -= nRead
nBytes = nRead
@ -628,11 +623,10 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int {
break
}
n -= nBytes
ptr = (*byte)(incBytePtr(unsafe.Pointer(ptr), nBytes))
buf = buf[nBytes:]
}
return nOriginalSize - n
return nOriginalSize - len(buf)
}
// int WriteN(RTMP* r, const char* buffer, int n);
@ -1257,13 +1251,13 @@ func C_EncodeInt32LE(dst []byte, v int32) int32 {
// int RTMP_ReadPacket(RTMP* r, RTMPPacket* packet);
// rtmp.c +3550
func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
var hbuf [RTMP_MAX_HEADER_SIZE]uint8
var hbuf [RTMP_MAX_HEADER_SIZE]byte
var header *byte
header = (*byte)(unsafe.Pointer(&hbuf[0]))
var nSize, hSize, nToRead, nChunk int32
var extendedTimestamp int32
if C_ReadN(r, &hbuf[0], 1) == 0 {
if C_ReadN(r, hbuf[:1]) != 1 {
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet header!")
return false
}
@ -1274,7 +1268,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
switch {
case packet.m_nChannel == 0:
if C_ReadN(r, &hbuf[1], 1) != 1 {
if C_ReadN(r, hbuf[1:2]) != 1 {
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header 2nd byte.")
return false
}
@ -1283,14 +1277,12 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
packet.m_nChannel += 64
header = (*byte)(incBytePtr(unsafe.Pointer(header), 1))
case packet.m_nChannel == 1:
var tmp int32
if C_ReadN(r, &hbuf[1], 2) != 2 {
if C_ReadN(r, hbuf[1:3]) != 2 {
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet 3rd byte")
return false
}
tmp = int32((hbuf[2] << 8) + hbuf[1])
tmp := int32((hbuf[2] << 8) + hbuf[1])
packet.m_nChannel = tmp + 64
header = (*byte)(incBytePtr(unsafe.Pointer(header), 2))
}
@ -1331,7 +1323,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
nSize--
if nSize > 0 && C_ReadN(r, header, int(nSize)) != int(nSize) {
if nSize > 0 && C_ReadN(r, pl2b(header, int(nSize))) != int(nSize) {
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header.")
return false
}
@ -1361,7 +1353,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
}
if extendedTimestamp != 0 {
if C_ReadN(r, (*byte)(incBytePtr(unsafe.Pointer(header), int(nSize))), 4) != 4 {
if C_ReadN(r, pl2b((*byte)(incBytePtr(unsafe.Pointer(header), int(nSize))), 4)) != 4 {
log.Println("RTMPRead_Packet: Failed to read extended timestamp")
return false
}
@ -1392,8 +1384,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
packet.m_chunk.c_chunk = (*[_Gi]byte)(unsafe.Pointer(packet.m_body))[packet.m_nBytesRead : packet.m_nBytesRead+uint32(nChunk)]
}
if C_ReadN(r, (*byte)(incBytePtr(unsafe.Pointer(packet.m_body), int(packet.m_nBytesRead))),
int(nChunk)) != int(nChunk) {
if C_ReadN(r, pl2b((*byte)(incBytePtr(unsafe.Pointer(packet.m_body), int(packet.m_nBytesRead))), int(nChunk))) != int(nChunk) {
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet body")
return false
}
@ -1431,7 +1422,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
// rtmp.c +3744
func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) {
var uptime, suptime uint32
var typ byte
var typ [1]byte
//clientbuf := make([]byte, RTMP_SIG_SIZE+1)
var clientbuf [RTMP_SIG_SIZE + 1]byte
clientsig := (*byte)(incBytePtr(unsafe.Pointer(&clientbuf[0]), 1))
@ -1454,18 +1445,18 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) {
return false
}
if C_ReadN(r, (*byte)(unsafe.Pointer(&typ)), 1) != 1 {
if C_ReadN(r, typ[:]) != 1 {
return false
}
if debugMode {
log.Printf("C_HandShake: Type answer: %v\n", typ)
log.Printf("C_HandShake: Type answer: %v\n", typ[0])
}
if typ != clientbuf[0] {
if typ[0] != clientbuf[0] {
log.Printf("C_HandShake: type mismatch: client sent %v, server sent: %v\n",
clientbuf[0], typ)
}
if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE {
if C_ReadN(r, serversig[:]) != RTMP_SIG_SIZE {
return false
}
@ -1478,7 +1469,7 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) {
return false
}
if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE {
if C_ReadN(r, serversig[:]) != RTMP_SIG_SIZE {
return false
}