forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_native.go
72 lines (51 loc) · 1.73 KB
/
coprocess_native.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// +build coprocess
// +build !grpc
package main
/*
#cgo python CFLAGS: -DENABLE_PYTHON
#include <stdio.h>
#include <stdlib.h>
#include "coprocess/sds/sds.h"
#include "coprocess/api.h"
#ifdef ENABLE_PYTHON
#include "coprocess/python/dispatcher.h"
#include "coprocess/python/binding.h"
#endif
*/
import "C"
import (
"github.com/TykTechnologies/tyk/coprocess"
"github.com/golang/protobuf/proto"
"encoding/json"
"unsafe"
)
// Dispatch prepares a CoProcessMessage, sends it to the GlobalDispatcher and gets a reply.
func (c *CoProcessor) Dispatch(object *coprocess.Object) (newObject *coprocess.Object, err error) {
var objectMsg []byte
if MessageType == coprocess.ProtobufMessage {
objectMsg, _ = proto.Marshal(object)
} else if MessageType == coprocess.JsonMessage {
objectMsg, _ = json.Marshal(object)
}
objectMsgStr := string(objectMsg)
var CObjectStr *C.char
CObjectStr = C.CString(objectMsgStr)
var objectPtr *C.struct_CoProcessMessage
objectPtr = (*C.struct_CoProcessMessage)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_CoProcessMessage{}))))
objectPtr.p_data = unsafe.Pointer(CObjectStr)
objectPtr.length = C.int(len(objectMsg))
var newObjectPtr *C.struct_CoProcessMessage
newObjectPtr = (*C.struct_CoProcessMessage)(GlobalDispatcher.Dispatch(unsafe.Pointer(objectPtr)))
var newObjectBytes []byte
newObjectBytes = C.GoBytes(newObjectPtr.p_data, newObjectPtr.length)
newObject = &coprocess.Object{}
if MessageType == coprocess.ProtobufMessage {
proto.Unmarshal(newObjectBytes, newObject)
} else if MessageType == coprocess.JsonMessage {
json.Unmarshal(newObjectBytes, newObject)
}
C.free(unsafe.Pointer(CObjectStr))
C.free(unsafe.Pointer(objectPtr))
C.free(unsafe.Pointer(newObjectPtr))
return newObject, err
}