rtmp: use C.CString

Now GODEBUG=cgocheck=0 is not needed.
This commit is contained in:
Dan Kortschak 2018-09-22 17:38:15 +09:30
parent d93357bb6f
commit e5faa396c1
3 changed files with 2 additions and 28 deletions

View File

@ -1677,18 +1677,6 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int {
return size return size
} }
// Creates a new C style string from a go string
func goStrToCStr(str string) *byte {
l := len(str)
slice := make([]byte, l+1)
ptr := unsafe.Pointer(&[]byte(str)[0])
for i := 0; i < l; i++ {
slice[i] = (*[_Gi]byte)(ptr)[i]
}
slice[l] = '\x00'
return &slice[0]
}
// incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions // incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions
// from the passed ptr // from the passed ptr
func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer {

View File

@ -53,21 +53,6 @@ var (
errMsg = "Obtained: %v, but wanted: %v" errMsg = "Obtained: %v, but wanted: %v"
) )
func TestGoStrToCStr(t *testing.T) {
goStr := "string\000"
bStr := goStrToCStr(goStr)
testData := []byte{'s', 't', 'r', 'i', 'n', 'g', '\000'}
for i := 0; i < len(goStr); i++ {
val := (*[_Gi]byte)(unsafe.Pointer(bStr))[i]
testVal := testData[i]
if val != testVal {
t.Errorf("Wanted: %v, but got: %v", testVal, val)
}
}
}
func TestIncPtr(t *testing.T) { func TestIncPtr(t *testing.T) {
// Test how it deals with bytes // Test how it deals with bytes
bytePtr := unsafe.Pointer(&byteArr[arrStart]) bytePtr := unsafe.Pointer(&byteArr[arrStart])

View File

@ -56,7 +56,8 @@ import (
// int add_addr_info(struct sockaddr_in *service, AVal *host, int port) // int add_addr_info(struct sockaddr_in *service, AVal *host, int port)
// rtmp.c +869 // rtmp.c +869
func C_add_addr_info(service *C.sockaddr_in, hostname string, port uint16) (ok bool) { func C_add_addr_info(service *C.sockaddr_in, hostname string, port uint16) (ok bool) {
h := (*C.char)(unsafe.Pointer(goStrToCStr(hostname))) h := C.CString(hostname)
defer C.free(unsafe.Pointer(h))
service.sin_addr.s_addr = C.inet_addr(h) service.sin_addr.s_addr = C.inet_addr(h)
if service.sin_addr.s_addr == C.INADDR_NONE { if service.sin_addr.s_addr == C.INADDR_NONE {
host := C.gethostbyname(h) host := C.gethostbyname(h)