forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
92 lines (83 loc) · 2.78 KB
/
types.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
83
84
85
86
87
88
89
90
91
92
// Copyright 2017-2019 Lei Ni ([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
//#include "dragonboat/binding.h"
import "C"
import (
"fmt"
"reflect"
"unsafe"
"github.com/lni/dragonboat"
)
func charToByte(data *C.char, len C.size_t) []byte {
var value []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
}
func ucharToByte(data *C.uchar, len C.size_t) []byte {
var value []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
}
func charArrayToString(data *C.char, len C.size_t) string {
value := charToByte(data, len)
str := string(value)
return str
}
func charToBool(c C.char) bool {
return c == 1
}
func cboolToBool(v C.Bool) bool {
return v == 1
}
func getErrorCode(err error) int {
if err == nil {
return int(C.StatusOK)
} else if err == dragonboat.ErrClusterNotFound {
return int(C.ErrClusterNotFound)
} else if err == dragonboat.ErrClusterAlreadyExist {
return int(C.ErrClusterAlreadyExist)
} else if err == dragonboat.ErrDeadlineNotSet {
return int(C.ErrDeadlineNotSet)
} else if err == dragonboat.ErrInvalidDeadline {
return int(C.ErrInvalidDeadline)
} else if err == dragonboat.ErrInvalidSession {
return int(C.ErrInvalidSession)
} else if err == dragonboat.ErrInvalidClusterSettings {
return int(C.ErrInvalidClusterSettings)
} else if err == dragonboat.ErrTimeoutTooSmall {
return int(C.ErrTimeoutTooSmall)
} else if err == dragonboat.ErrPayloadTooBig {
return int(C.ErrPayloadTooBig)
} else if err == dragonboat.ErrSystemBusy {
return int(C.ErrSystemBusy)
} else if err == dragonboat.ErrClusterClosed {
return int(C.ErrClusterClosed)
} else if err == dragonboat.ErrBadKey {
return int(C.ErrBadKey)
} else if err == dragonboat.ErrPendingConfigChangeExist {
return int(C.ErrPendingConfigChangeExist)
} else if err == dragonboat.ErrTimeout {
return int(C.ErrTimeout)
} else if err == dragonboat.ErrSystemStopped {
return int(C.ErrSystemStopped)
} else if err == dragonboat.ErrCanceled {
return int(C.ErrCanceled)
} else if err == dragonboat.ErrRejected {
return int(C.ErrRejected)
}
panic(fmt.Sprintf("unknown error %v", err))
}