-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.go
78 lines (74 loc) · 2.09 KB
/
socket.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
package utilities
import (
"fmt"
"net"
"os"
)
// SendOverUnixSocket : starts a task that when receives a message on a channel can dispatch a message on socket
func SendOverUnixSocket(cancel chan interface{}, sock string, errx chan error) (chan []byte, func()) {
send := make(chan []byte, 10) // client code to use this channel to send desired message
return send, func() {
defer close(send)
for {
select {
case <-cancel:
return
case msg := <-send:
conn, err := net.Dial("unix", sock)
if err != nil {
errx <- fmt.Errorf("Autolumin: Error connecting to srvrelay over socket %s", err)
}
_, err = conn.Write(msg)
if err != nil {
errx <- fmt.Errorf("Error writing message to TCP sock %s", err)
}
conn.Close()
}
}
}
}
// ListenOnUnixSocket : sets up a listener, returns a function to start, stop and error incase the socket could not be listened
// https://eli.thegreenplace.net/2019/unix-domain-sockets-in-go/
func ListenOnUnixSocket(sock string, handler func(net.Conn)) (func(), func(), error) {
os.RemoveAll(sock)
l, err := net.Listen("unix", sock)
if err != nil {
return nil, nil, err
}
start := func() {
for {
fd, err := l.Accept()
if err != nil {
return
}
go handler(fd)
}
}
stop := func() {
l.Close()
}
return start, stop, nil
}
// AfterSocketEvent : this sets up a listener to a socket, and calls the play function only after the socket has received an event
// caller function can set the size of the expected event message - defaults to 512 if set to lesser than that
// returns a func to stop the event loop, and error incase any
func AfterSocketEvent(sock string, play func(msg []byte), size int) (func(), error) {
if size < 512 {
// incoming message size if not set by the caller code - it defaults to 512
size = 512
}
start, stop, err := ListenOnUnixSocket(sock, func(c net.Conn) {
buf := make([]byte, size)
nr, err := c.Read(buf)
if err != nil {
return
}
data := buf[0:nr]
play(data)
})
if err != nil {
return nil, fmt.Errorf("Failed to setup AfterSocket event :%s", err)
}
go start()
return stop, nil
}