-
Notifications
You must be signed in to change notification settings - Fork 14
/
ipc.go
160 lines (145 loc) · 3.67 KB
/
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package mpv
import (
"bufio"
"encoding/json"
"errors"
"io"
"math/rand"
"net"
"sync"
"time"
)
// Response received from mpv. Can be an event or a user requested response.
type Response struct {
Err string `json:"error"`
Data interface{} `json:"data"` // May contain float64, bool or string
Event string `json:"event"`
RequestID int `json:"request_id"`
}
// request sent to mpv. Includes request_id for mapping the response.
type request struct {
Command []interface{} `json:"command"`
RequestID int `json:"request_id"`
Response chan *Response `json:"-"`
}
func newRequest(cmd ...interface{}) *request {
return &request{
Command: cmd,
RequestID: rand.Intn(10000),
Response: make(chan *Response, 1),
}
}
// LLClient is the most low level interface
type LLClient interface {
Exec(command ...interface{}) (*Response, error)
}
// IPCClient is a low-level IPC client to communicate with the mpv player via socket.
type IPCClient struct {
socket string
timeout time.Duration
comm chan *request
mu sync.Mutex
reqMap map[int]*request // Maps RequestIDs to Requests for response association
}
// NewIPCClient creates a new IPCClient connected to the given socket.
func NewIPCClient(socket string) *IPCClient {
c := &IPCClient{
socket: socket,
timeout: 2 * time.Second,
comm: make(chan *request),
reqMap: make(map[int]*request),
}
c.run()
return c
}
// dispatch dispatches responses to the corresponding request
func (c *IPCClient) dispatch(resp *Response) {
if resp.Event == "" { // No Event
c.mu.Lock()
defer c.mu.Unlock()
if req, ok := c.reqMap[resp.RequestID]; ok { // Lookup requestID in request map
delete(c.reqMap, resp.RequestID)
req.Response <- resp
return
}
// Discard response
} else { // Event
// TODO: Implement Event support
}
}
func (c *IPCClient) run() {
conn, err := net.Dial("unix", c.socket)
if err != nil {
panic(err)
}
go c.readloop(conn)
go c.writeloop(conn)
// TODO: Close connection
}
func (c *IPCClient) writeloop(conn io.Writer) {
for {
req, ok := <-c.comm
if !ok {
panic("Communication channel closed")
}
b, err := json.Marshal(req)
if err != nil {
// TODO: Discard request, maybe send error downstream
// log.Printf("Discard request %v with error: %s", req, err)
continue
}
c.mu.Lock()
c.reqMap[req.RequestID] = req
c.mu.Unlock()
b = append(b, '\n')
_, err = conn.Write(b)
if err != nil {
// TODO: Discard request, maybe send error downstream
// TODO: Remove from reqMap?
}
}
}
func (c *IPCClient) readloop(conn io.Reader) {
rd := bufio.NewReader(conn)
for {
data, err := rd.ReadBytes('\n')
if err != nil {
// TODO: Handle error
continue
}
var resp Response
err = json.Unmarshal(data, &resp)
if err != nil {
// TODO: Handle error
continue
}
c.dispatch(&resp)
}
}
// Timeout errors while communicating via IPC
var (
ErrTimeoutSend = errors.New("Timeout while sending command")
ErrTimeoutRecv = errors.New("Timeout while receiving response")
)
// Exec executes a command via ipc and returns the response.
// A request can timeout while sending or while waiting for the response.
// An error is only returned if there was an error in the communication.
// The client has to check for `response.Error` in case the server returned
// an error.
func (c *IPCClient) Exec(command ...interface{}) (*Response, error) {
req := newRequest(command...)
select {
case c.comm <- req:
case <-time.After(c.timeout):
return nil, ErrTimeoutSend
}
select {
case res, ok := <-req.Response:
if !ok {
panic("Response channel closed")
}
return res, nil
case <-time.After(c.timeout):
return nil, ErrTimeoutRecv
}
}