-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_plugin.go
189 lines (155 loc) · 4.02 KB
/
server_plugin.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
package rpcng
import (
"io"
"net"
"github.com/iqoption/rpcng/plugin"
)
type (
// Before start
ServerBeforeStart interface {
BeforeStart(listener net.Listener, methods []string) error
}
// After start
ServerAfterStart interface {
AfterStart(listener net.Listener, methods []string) error
}
// Before stop
ServerBeforeStop interface {
BeforeStop(listener net.Listener, methods []string) error
}
// After stop
ServerAfterStop interface {
AfterStop(listener net.Listener, methods []string) error
}
// Accept connection
ServerAcceptConnection interface {
AcceptConnection(conn io.ReadWriteCloser, err error) error
}
// Client connected
ServerClientConnected interface {
ClientConnected(conn io.ReadWriteCloser) error
}
// Client disconnected
ServerClientDisconnected interface {
ClientDisconnected(err error) error
}
// Any errors handler
ServerErrorHandler interface {
ErrorHandle(err error)
}
// Server plugins registry
serverPluginRegistry struct {
beforeStart []ServerBeforeStart
afterStart []ServerAfterStart
beforeStop []ServerBeforeStop
afterStop []ServerAfterStop
acceptConn []ServerAcceptConnection
clientConn []ServerClientConnected
clientDisc []ServerClientDisconnected
errorHandlers []ServerErrorHandler
}
)
// Add plugins to registry
func (r *serverPluginRegistry) Add(p plugin.Plugin) {
// before start
if v, ok := p.(ServerBeforeStart); ok {
r.beforeStart = append(r.beforeStart, v)
}
// after start
if v, ok := p.(ServerAfterStart); ok {
r.afterStart = append(r.afterStart, v)
}
// before stop
if v, ok := p.(ServerBeforeStop); ok {
r.beforeStop = append(r.beforeStop, v)
}
// after start
if v, ok := p.(ServerAfterStop); ok {
r.afterStop = append(r.afterStop, v)
}
// accept connection
if v, ok := p.(ServerAcceptConnection); ok {
r.acceptConn = append(r.acceptConn, v)
}
// client connected
if v, ok := p.(ServerClientConnected); ok {
r.clientConn = append(r.clientConn, v)
}
// client disconnected
if v, ok := p.(ServerClientDisconnected); ok {
r.clientDisc = append(r.clientDisc, v)
}
// any error or panic handler
if v, ok := p.(ServerErrorHandler); ok {
r.errorHandlers = append(r.errorHandlers, v)
}
}
// Trigger before start
func (r *serverPluginRegistry) doBeforeStart(listener net.Listener, methods []string) (err error) {
for _, p := range r.beforeStart {
if err = p.BeforeStart(listener, methods); err != nil {
return err
}
}
return nil
}
// Trigger after start
func (r *serverPluginRegistry) doAfterStart(listener net.Listener, methods []string) (err error) {
for _, p := range r.afterStart {
if err = p.AfterStart(listener, methods); err != nil {
return err
}
}
return nil
}
// Trigger before stop
func (r *serverPluginRegistry) doBeforeStop(listener net.Listener, methods []string) (err error) {
for _, p := range r.beforeStop {
if err = p.BeforeStop(listener, methods); err != nil {
return err
}
}
return nil
}
// Trigger after stop
func (r *serverPluginRegistry) doAfterStop(listener net.Listener, methods []string) (err error) {
for _, p := range r.afterStop {
if err = p.AfterStop(listener, methods); err != nil {
return err
}
}
return nil
}
// Trigger accept connection
func (r *serverPluginRegistry) doAcceptConnection(conn io.ReadWriteCloser, err error) error {
for _, p := range r.acceptConn {
if err := p.AcceptConnection(conn, err); err != nil {
return err
}
}
return nil
}
// Trigger client connected
func (r *serverPluginRegistry) doClientConnected(conn io.ReadWriteCloser) (err error) {
for _, p := range r.clientConn {
if err = p.ClientConnected(conn); err != nil {
return err
}
}
return nil
}
// Trigger client disconnected
func (r *serverPluginRegistry) doClientDisconnected(err error) error {
for _, p := range r.clientDisc {
if err := p.ClientDisconnected(err); err != nil {
return err
}
}
return nil
}
// Trigger if any error
func (r *serverPluginRegistry) doError(err error) {
for _, p := range r.errorHandlers {
p.ErrorHandle(err)
}
}