-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
46 lines (42 loc) · 1.08 KB
/
util.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
package mosquitto
/*
#cgo LDFLAGS: -lmosquitto
#include "mosquitto_ext.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
func bool_to_cint(b bool) C.int {
if b {
return C.int(1)
}
return C.int(0)
}
func ccode_to_error(code C.int) error {
switch code {
default:
return fmt.Errorf("Unknown error code: %d", code)
case C.MOSQ_ERR_SUCCESS:
return nil
case C.MOSQ_ERR_INVAL:
return Error{(int)(code), "The input parameters were invalid."}
case C.MOSQ_ERR_NOMEM:
panic("An out of memory condition occurred.")
case C.MOSQ_ERR_NO_CONN:
return Error{(int)(code), "The client isn't connected to a broker."}
case C.MOSQ_ERR_CONN_LOST:
return Error{(int)(code), "The connection to the broker was lost."}
case C.MOSQ_ERR_PROTOCOL:
return Error{(int)(code), "There is a protocol error communicating with the broker."}
case C.MOSQ_ERR_ERRNO:
cerr := C.CString("") // TODO: Is this safe?
defer C.free(unsafe.Pointer(cerr))
C.mosquitto_error(cerr)
return Error{(int)(code), C.GoString(cerr)}
case C.MOSQ_ERR_PAYLOAD_SIZE:
return Error{(int)(code), "Payload is too large."}
}
return nil
}