forked from SagerNet/cronet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_cgo.go
More file actions
58 lines (46 loc) · 1.82 KB
/
Copy pathbuffer_cgo.go
File metadata and controls
58 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:build !with_purego
package cronet
// #include <stdlib.h>
// #include <stdbool.h>
// #include <cronet_c.h>
import "C"
import (
"unsafe"
)
func NewBuffer() Buffer {
return Buffer{uintptr(unsafe.Pointer(C.Cronet_Buffer_Create()))}
}
func (b Buffer) Destroy() {
C.Cronet_Buffer_Destroy(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr)))
}
// InitWithDataAndCallback initialize Buffer with raw buffer |data| of |size| allocated by the app.
// The |callback| is invoked when buffer is destroyed.
func (b Buffer) InitWithDataAndCallback(data []byte, callback BufferCallback) {
C.Cronet_Buffer_InitWithDataAndCallback(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr)), C.Cronet_RawDataPtr(unsafe.Pointer(&data[0])), C.uint64_t(len(data)), C.Cronet_BufferCallbackPtr(unsafe.Pointer(callback.ptr)))
}
// InitWithAlloc initialize Buffer by allocating buffer of |size|.
// The content of allocated data is not initialized.
func (b Buffer) InitWithAlloc(size int64) {
C.Cronet_Buffer_InitWithAlloc(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr)), C.uint64_t(size))
}
// Size return size of data owned by this buffer.
func (b Buffer) Size() int64 {
return int64(C.Cronet_Buffer_GetSize(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr))))
}
// Data return raw pointer to |data| owned by this buffer.
func (b Buffer) Data() unsafe.Pointer {
return unsafe.Pointer(C.Cronet_Buffer_GetData(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr))))
}
func (b Buffer) DataSlice() []byte {
size := b.Size()
if size == 0 {
return nil
}
return unsafe.Slice((*byte)(b.Data()), size)
}
func (b Buffer) SetClientContext(context unsafe.Pointer) {
C.Cronet_Buffer_SetClientContext(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr)), C.Cronet_ClientContext(context))
}
func (b Buffer) ClientContext() unsafe.Pointer {
return unsafe.Pointer(C.Cronet_Buffer_GetClientContext(C.Cronet_BufferPtr(unsafe.Pointer(b.ptr))))
}