-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
308 lines (301 loc) · 6.28 KB
/
message.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (C) 2021 - 2023 iDigitalFlame
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package akcss
import (
"context"
"encoding/json"
"io"
"net"
"strconv"
"sync"
"time"
"github.com/iDigitalFlame/akcss/xerr"
)
const (
timeout = time.Second * 5
responseOk uint8 = iota
responseStatus
responseServerList
responseClientNew
responseClientList
responseNotifyList
responseOptionList
responseOptionClientList
responseShow
actionCRL
actionStop
actionStart
actionRenew
actionReload
actionStatus
actionUpdate
actionRestart
actionShow
actionNotifyNew
actionNotifyList
actionNotifyDelete
actionConnect
actionDisconnect
actionOptionNew
actionOptionList
actionOptionDelete
actionOptionClientNew
actionOptionClientList
actionOptionClientDelete
actionServerNew
actionClientNew
actionServerDelete
actionClientDelete
actionServerList
actionClientList
responseError
)
var (
bufs = sync.Pool{
New: func() interface{} {
b := make([]byte, 512)
return &b
},
}
local = new(net.Dialer)
)
type message struct {
e interface{}
Data []byte
Action uint8
}
type payload interface {
id() string
}
func (m *message) Parse() error {
if len(m.Data) == 0 || m.e != nil {
return nil
}
switch m.Action {
case responseShow:
m.e = new(typeServer)
case responseStatus:
m.e = new(typeStatus)
case actionClientNew:
m.e = new(typeClientNew)
case actionClientDelete:
m.e = new(typeClientDelete)
case responseNotifyList:
m.e = new(typeNotifyList)
case responseClientList:
m.e = new(typeClientList)
case responseServerList:
m.e = new(typeServerList)
case actionServerDelete:
m.e = new(typeServerDelete)
case actionUpdate, actionServerNew:
m.e = new(details)
case actionConnect, actionDisconnect:
m.e = new(typeConnect)
case actionNotifyNew, actionNotifyDelete:
m.e = new(typeNotify)
case responseOptionList, responseOptionClientList:
m.e = new(typeOptionList)
case actionOptionNew, actionOptionDelete, actionOptionClientNew, actionOptionClientDelete:
m.e = new(typeOption)
default:
return nil
}
return json.Unmarshal(m.Data, &m.e)
}
func (m message) String() string {
var s string
switch m.Action {
case responseOk:
return "Ok"
case responseStatus:
return "Status"
case responseServerList:
return "ServerList"
case responseClientNew:
return "ClientNew"
case responseClientList:
return "ClientList"
case responseNotifyList:
return "NotifyList"
case responseOptionList:
return "OptionList"
case responseOptionClientList:
return "OptionClientList"
case responseShow:
return "Show"
case actionConnect:
s = "Connect"
case actionDisconnect:
s = "Disconnect"
case actionCRL:
s = "CRL"
case actionStop:
s = "Stop"
case actionStart:
s = "Start"
case actionRenew:
s = "Renew"
case actionReload:
return "Reload"
case actionStatus:
s = "Status"
case actionUpdate:
return "Update"
case actionRestart:
s = "Restart"
case actionShow:
s = "Show"
case actionNotifyNew:
return "NotifyNew"
case actionNotifyList:
s = "NotifyList"
case actionNotifyDelete:
s = "NotifyDelete"
case actionOptionNew:
return "OptionNew"
case actionOptionList:
s = "OptionList"
case actionOptionDelete:
s = "OptionDelete"
case actionOptionClientNew:
return "OptionClientNew"
case actionOptionClientList:
s = "OptionClientList"
case actionOptionClientDelete:
s = "OptionClientDelete"
case actionServerNew:
return "ServerNew"
case actionClientNew:
return "ClientNew"
case actionServerDelete:
s = "ServerDelete"
case actionClientDelete:
s = "ClientDelete"
case actionServerList:
s = "ServerList"
case actionClientList:
s = "ClientList"
case responseError:
s = "Error"
default:
return "Unknown"
}
if len(m.Data) == 0 {
return s
}
return s + "(" + string(m.Data) + ")"
}
func (m *message) ID() (string, error) {
if len(m.Data) == 0 {
return "", errInvalidID
}
if err := m.Parse(); err != nil {
return "", err
}
if m.e == nil {
return string(m.Data), nil
}
if v, ok := m.e.(payload); ok {
return v.id(), nil
}
return "", errUnexpected
}
func (m message) Write(w io.Writer) error {
if m.e != nil {
return rawJSON(m.Action, m.e, w)
}
return raw(m.Action, m.Data, w)
}
func (m *message) Read(r io.Reader) error {
var (
b = bufs.Get().(*[]byte)
n, err = r.Read(*b)
)
if err != nil {
bufs.Put(b)
return err
}
if n == 0 {
bufs.Put(b)
return io.EOF
}
l := int(uint16((*b)[2]) | uint16((*b)[1])<<8)
m.Action, m.Data = (*b)[0], make([]byte, n-3, l)
if copy(m.Data, (*b)[3:n]); m.Action > responseError {
return xerr.New(`invalid action value "` + strconv.Itoa(int(m.Action)) + `"`)
}
for l -= len(m.Data); l > 0; {
if n, err = r.Read(*b); err == io.EOF && n == 0 {
err = nil
break
}
if err != nil {
break
}
m.Data = append(m.Data, (*b)[:n]...)
l -= n
}
bufs.Put(b)
return err
}
func raw(a uint8, d []byte, w io.Writer) error {
n := uint64(len(d))
if n > maxUint16 {
return xerr.New("message size is too large")
}
var (
b = bufs.Get().(*[]byte)
err error
)
(*b)[0], (*b)[1], (*b)[2] = a, byte(n>>8), byte(n)
if _, err = w.Write((*b)[0:3]); err == nil && len(d) > 0 {
_, err = w.Write(d)
}
bufs.Put(b)
return err
}
func rawJSON(a uint8, v interface{}, w io.Writer) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
return raw(a, b, w)
}
func write(x context.Context, s string, n message) (*message, error) {
var (
c net.Conn
err error
)
if isUnix(s) {
c, err = local.DialContext(x, "unix", s[5:])
} else {
c, err = local.DialContext(x, "tcp", s)
}
if err != nil {
return nil, err
}
if err = n.Write(c); err != nil {
c.Close()
return nil, err
}
var r message
if err = r.Read(c); err != nil {
c.Close()
return nil, err
}
return &r, c.Close()
}