forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoprocess_native.go
82 lines (62 loc) · 1.81 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
73
74
75
76
77
78
79
80
81
82
// +build coprocess
// +build !grpc
package main
/*
#cgo python CFLAGS: -DENABLE_PYTHON
#include <stdio.h>
#include <stdlib.h>
#include "coprocess/api.h"
#ifdef ENABLE_PYTHON
#include "coprocess/python/dispatcher.h"
#include "coprocess/python/binding.h"
#endif
*/
import "C"
import (
"errors"
"github.com/golang/protobuf/proto"
"github.com/TykTechnologies/tyk/coprocess"
"encoding/json"
"unsafe"
)
// Dispatch prepares a CoProcessMessage, sends it to the GlobalDispatcher and gets a reply.
func (c *CoProcessor) Dispatch(object *coprocess.Object) (*coprocess.Object, error) {
if GlobalDispatcher == nil {
return nil, errors.New("Dispatcher not initialized")
}
var objectMsg []byte
var err error
switch MessageType {
case coprocess.ProtobufMessage:
objectMsg, err = proto.Marshal(object)
case coprocess.JsonMessage:
objectMsg, err = json.Marshal(object)
}
if err != nil {
return nil, err
}
objectMsgStr := string(objectMsg)
CObjectStr := C.CString(objectMsgStr)
defer C.free(unsafe.Pointer(CObjectStr))
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))
defer C.free(unsafe.Pointer(objectPtr))
newObjectPtr := (*C.struct_CoProcessMessage)(GlobalDispatcher.Dispatch(unsafe.Pointer(objectPtr)))
defer C.free(unsafe.Pointer(newObjectPtr))
if newObjectPtr == nil {
return nil, errors.New("Dispatch error")
}
newObjectBytes := C.GoBytes(newObjectPtr.p_data, newObjectPtr.length)
newObject := &coprocess.Object{}
switch MessageType {
case coprocess.ProtobufMessage:
err = proto.Unmarshal(newObjectBytes, newObject)
case coprocess.JsonMessage:
err = json.Unmarshal(newObjectBytes, newObject)
}
if err != nil {
return nil, err
}
return newObject, nil
}