From ea3bef53e0672d4ce77078e3e414cd75e2e9b1b8 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Fri, 13 Jul 2018 16:34:28 +0800 Subject: [PATCH] pass go buffer as c buffer --- tls.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tls.go b/tls.go index 0a51f28..859d015 100644 --- a/tls.go +++ b/tls.go @@ -237,10 +237,11 @@ func (c *Conn) Write(buf []byte) (n int, err error) { return 0, nil } - cbuf := C.CBytes(buf) - defer C.free(cbuf) + //cbuf := C.CBytes(buf) + //defer C.free(cbuf) - ret := C.gnutls_record_send(c.sess.session, cbuf, C.size_t(len(buf))) + ret := C.gnutls_record_send(c.sess.session, + unsafe.Pointer(&buf[0]), C.size_t(len(buf))) n = int(ret) if n < 0 { @@ -380,10 +381,10 @@ func onDataReadCallback(d unsafe.Pointer, cbuf *C.char, bufLen C.int) C.int { log.Println(err) return -1 } - cbuf2 := C.CBytes(buf[:n]) + //cbuf2 := C.CBytes(buf[:n]) // d := C.CString(string(buf[:n])) - defer C.free(cbuf2) - C.memcpy(unsafe.Pointer(cbuf), unsafe.Pointer(cbuf2), C.size_t(n)) + //defer C.free(cbuf2) + C.memcpy(unsafe.Pointer(cbuf), unsafe.Pointer(&buf[0]), C.size_t(n)) return C.int(n) }