-
Notifications
You must be signed in to change notification settings - Fork 0
/
chitosocket.go
296 lines (261 loc) · 6.71 KB
/
chitosocket.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"reflect"
"sync"
"syscall"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
)
type Socket struct {
Epoller *Epoll
Hub *HubStruct
On map[string]func(subs *Subscriber, op ws.OpCode, data map[string]interface{})
}
// Socket Data structure
type socket_message struct {
Event string `json:"event"`
Data map[string]interface{} `json:"data"`
}
type HubSubStruct struct {
Room map[string]*Subscriber
lock *sync.RWMutex
}
// Room Hub Structure
type HubStruct struct {
Subs map[string]*HubSubStruct
lock *sync.RWMutex
}
// One Conenction has one subscriber
type Subscriber struct {
Id string
Connection *net.Conn
Data map[string]interface{}
Room []string
}
type Epoll struct {
Fd int
Connections map[int]*Subscriber
lock *sync.RWMutex
}
func MkEpoll() (*Epoll, error) {
fd, err := syscall.EpollCreate1(0)
if err != nil {
return nil, err
}
return &Epoll{
Fd: fd,
lock: &sync.RWMutex{},
Connections: make(map[int]*Subscriber),
}, nil
}
func MKHub() *HubStruct {
return &HubStruct{
Subs: make(map[string]*HubSubStruct),
lock: &sync.RWMutex{},
}
}
func StartUp() *Socket {
socket := Socket{}
var rLimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
panic(err)
}
rLimit.Cur = rLimit.Max
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
panic(err)
}
socket.Hub = MKHub()
var err error
socket.Epoller, err = MkEpoll()
if err != nil {
log.Printf("Error %e \n", err)
os.Exit(1)
}
socket.On = make(map[string]func(subs *Subscriber, op ws.OpCode, data map[string]interface{}))
go socket.start()
return &socket
}
func (socket *Socket) UpgradeConnection(r *http.Request, w http.ResponseWriter) (net.Conn, *bufio.ReadWriter, *Subscriber, error) {
conn, bufioRW, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return nil, nil, &Subscriber{}, err
}
new_subscriber := Subscriber{
Connection: &conn,
Data: make(map[string]interface{}),
}
fd := new_subscriber.websocketFD()
new_subscriber.Id = fmt.Sprint(fd)
if err := socket.Epoller.Add(&new_subscriber, fd); err != nil {
log.Printf("Failed to add connection %v", err)
conn.Close()
}
socket.AddToRoom(&new_subscriber, new_subscriber.Id)
return conn, bufioRW, &new_subscriber, nil
}
func (socket *Socket) AddToRoom(subs *Subscriber, room string) {
if socket.Hub.Subs[room] == nil {
socket.Hub.lock.Lock()
socket.Hub.Subs[room] = &HubSubStruct{
Room: make(map[string]*Subscriber),
lock: &sync.RWMutex{},
}
socket.Hub.lock.Unlock()
}
room_s := socket.Hub.Subs[room]
defer room_s.lock.Unlock()
subs.Room = append(subs.Room, room)
room_s.lock.Lock()
room_s.Room[subs.Id] = subs
}
func (e *Epoll) Add(new_subscriber *Subscriber, fd int) error {
err := syscall.EpollCtl(e.Fd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLHUP, Fd: int32(fd)})
if err != nil {
return err
}
e.lock.Lock()
defer e.lock.Unlock()
e.Connections[fd] = new_subscriber
return nil
}
func (socket *Socket) RemoveFromRoom(subs *Subscriber, room string) {
subs.Room = remove_element_from_array(subs.Room, room)
room_s := socket.Hub.Subs[room]
socket.Hub.lock.Lock()
delete(room_s.Room, subs.Id)
room_s.lock.Unlock()
}
func (socket *Socket) Emit(event string, data map[string]interface{}, room ...string) {
if event == "" {
log.Printf("Cannot have empty event")
return
}
send_event := socket_message{event, data}
msg, err := json.Marshal(send_event)
if err != nil {
panic(err)
}
for _, room := range room {
room_s := socket.Hub.Subs[room]
room_s.lock.RLock()
for _, subs := range room_s.Room {
err = wsutil.WriteServerMessage(*subs.Connection, 1, msg)
if err != nil {
log.Printf("Failed to send %v", err)
}
}
room_s.lock.RUnlock()
}
}
func (socket *Socket) Wait() ([]*Subscriber, error) {
events := make([]syscall.EpollEvent, 100)
n, err := syscall.EpollWait(socket.Epoller.Fd, events, 100)
if err != nil {
return nil, err
}
socket.Epoller.lock.RLock()
defer socket.Epoller.lock.RUnlock()
var connections []*Subscriber
for i := 0; i < n; i++ {
conn := socket.Epoller.Connections[int(events[i].Fd)]
connections = append(connections, conn)
}
return connections, nil
}
func (socket *Socket) Remove(sub **Subscriber) error {
su := *sub
fd := su.websocketFD()
err := syscall.EpollCtl(socket.Epoller.Fd, syscall.EPOLL_CTL_DEL, fd, nil)
if err != nil {
return err
}
for _, room := range su.Room {
room_s := socket.Hub.Subs[room]
if room_s == nil {
continue
}
room_s.lock.Lock()
delete(room_s.Room, su.Id)
room_s.lock.Unlock()
if room_s == nil {
socket.Hub.lock.Lock()
delete(socket.Hub.Subs, room)
socket.Hub.lock.Unlock()
}
}
*sub = nil
socket.Epoller.lock.Lock()
delete(socket.Epoller.Connections, fd)
socket.Epoller.lock.Unlock()
return nil
}
func remove_element_from_array(strings_in_array []string, string_to_remove string) []string {
deleteIndex := -1
for i, s := range strings_in_array {
if s == string_to_remove {
deleteIndex = i
break
}
}
if deleteIndex != -1 {
strings_in_array[deleteIndex] = strings_in_array[len(strings_in_array)]
strings_in_array = strings_in_array[:len(strings_in_array)-1]
}
return strings_in_array
}
func (new_subscriber *Subscriber) websocketFD() int {
tcpConn := reflect.Indirect(reflect.ValueOf(*new_subscriber.Connection)).FieldByName("conn")
fdVal := tcpConn.FieldByName("fd")
pfdVal := reflect.Indirect(fdVal).FieldByName("pfd")
return int(pfdVal.FieldByName("Sysfd").Int())
}
func unmarshal_msg(msg []byte) (string, map[string]interface{}, error) {
msg_un_mar := socket_message{
Event: "",
Data: make(map[string]interface{}),
}
json.Unmarshal(msg, &msg_un_mar)
if msg_un_mar.Event == "" {
return "", nil, fmt.Errorf("no event Found")
}
return msg_un_mar.Event, msg_un_mar.Data, nil
}
func (socket *Socket) start() {
for {
subscriber, err := socket.Wait()
if err != nil {
// log.Printf("Failed to epoll wait %v", err)
continue
}
for _, subs := range subscriber {
if subs.Connection == nil {
break
}
if msg, op, err := wsutil.ReadClientData(*subs.Connection); err != nil {
if disconnect_event, ok := socket.On["disconnect"]; ok {
disconnect_event(subs, op, map[string]interface{}{})
}
con := *subs.Connection
if err := socket.Remove(&subs); err != nil {
log.Printf("Failed to remove %v", err)
}
con.Close()
} else {
event, msg_from_client, err := unmarshal_msg(msg)
if err != nil {
log.Println(err)
}
if on_event, ok := socket.On[event]; ok {
on_event(subs, op, msg_from_client)
}
}
}
}
}