-
Notifications
You must be signed in to change notification settings - Fork 7
/
ipc.go
47 lines (37 loc) · 932 Bytes
/
ipc.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 open_in_mpv
import "net"
const defaultSocket = "/tmp/mpvsocket"
var defaultIpc = Ipc{
SocketAddress: defaultSocket,
}
// Defines an IPC connection with a UNIX socket
type Ipc struct {
conn net.Conn
SocketAddress string
}
// Send a byte-encoded command to the specified UNIX socket
func (i *Ipc) Send(cmd []byte) error {
var err error
i.conn, err = net.Dial("unix", i.SocketAddress)
if err != nil {
return err
}
defer i.conn.Close()
// The command has to be newline terminated
if cmd[len(cmd)-1] != '\n' {
cmd = append(cmd, '\n')
}
_, err = i.conn.Write(cmd)
if err != nil {
return err
}
return nil
}
// Generic public send string command for the default connection
func SendString(cmd string) error {
return defaultIpc.Send([]byte(cmd))
}
// Generic public send byte-encoded string command for the default connection
func SendBytes(cmd []byte) error {
return defaultIpc.Send(cmd)
}