-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
47 lines (43 loc) · 1.03 KB
/
address.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
package enet
// #cgo CFLAGS: -I"src/"
// #cgo LDFLAGS: lib/libenet.a
// #include "enet/enetb.h"
import "C"
import (
"errors"
"net"
"strconv"
"unsafe"
)
func newAddress(addr string) (*C.ENetAddress, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
chost := C.CString(host)
defer C.free(unsafe.Pointer(chost))
p, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
enetAddr := C.create_enet_address(chost, C.enet_uint16(p))
if enetAddr == nil {
return nil, errors.New("failed to create enet address")
}
return enetAddr, nil
}
func getAddress(addr C.ENetAddress) string {
host := getHost(addr)
port := strconv.Itoa(int(addr.port))
return host + ":" + port
}
func getHost(addr C.ENetAddress) string {
size := 16
chost := (*C.char)(C.malloc(C.size_t(size)))
ret := C.enet_address_get_host_ip(&addr, chost, C.size_t(size))
if ret != 0 {
return ""
}
defer C.free(unsafe.Pointer(chost))
return C.GoStringN(chost, C.int(size))
}