-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
63 lines (52 loc) · 1.5 KB
/
wrapper.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
package libknot
// ErrCheck panics if there is an error
func (ctl KnotCtl) ErrCheck(e int) {
if e != 0 {
panic(ctl.StrErr(e))
}
}
// Init initializes the connection to the KnotDNS UNIX sockets. It allocates the control context and connects to the socket.
func (ctl *KnotCtl) Init(path string) int {
ctl.Alloc()
err := ctl.Connect(path)
return err
}
// Terminate Sends END, Closes the UNIX socket, deallocates (Free) the control socket.
func (ctl *KnotCtl) Terminate() {
ctl.Send(END, nil)
ctl.Close()
ctl.Free()
}
// SendBlock sends one control unit with the data and an other one with the BLOCK unit type telling the KnotDNS server that the message is finished and that it can respond to us.
func (ctl KnotCtl) SendBlock(data KnotCtlData) int {
x := data.ToCtl()
err := ctl.Send(DATA, &x)
if err != 0 {
return err
}
ctl.Send(BLOCK, nil)
return 0
}
// ReceiveBlock retrieves all data (DATA and EXTRA unit type) received by the server until it tells us (END or BLOCK unit type) that it is finished.
// Returns an error as int
func (ctl KnotCtl) ReceiveBlock() ([]KnotCtlData, int) {
var answers []KnotCtlData
for {
var receivedData KnotCtlData
msg := KnotCtlData{}.ToCtl()
err, reqType := ctl.Receive(&msg)
if err != 0 {
return nil, err
}
if reqType != DATA && reqType != EXTRA {
break
}
receivedData.FromCtl(msg)
if receivedData.Error != "" {
answers = append(answers, receivedData)
return answers, -1
}
answers = append(answers, receivedData)
}
return answers, 0
}