This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
/
client.go
209 lines (174 loc) · 3.75 KB
/
client.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
package iguagile
import (
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
)
// Client is a middleman between the connection and the room.
type Client struct {
id int
idByte []byte
conn io.ReadWriteCloser
room *Room
send chan []byte
}
// NewClient is Client constructed.
func NewClient(room *Room, conn io.ReadWriteCloser) (*Client, error) {
id, err := room.generator.Generate()
if err != nil {
return nil, err
}
idByte := make([]byte, 2)
binary.LittleEndian.PutUint16(idByte, uint16(id))
client := &Client{
id: id,
idByte: idByte,
conn: conn,
room: room,
send: make(chan []byte),
}
return client, nil
}
func (c *Client) read(buf []byte) (int, error) {
_, err := c.conn.Read(buf[:2])
if err != nil {
return 0, err
}
size := int(binary.LittleEndian.Uint16(buf))
receivedSizeSum := 0
for receivedSizeSum < size {
receivedSize, err := c.conn.Read(buf[receivedSizeSum:size])
if err != nil {
return 0, err
}
receivedSizeSum += receivedSize
}
return size, nil
}
func (c *Client) readStart() {
buf := make([]byte, maxMessageSize)
for {
n, err := c.read(buf)
if err != nil {
c.room.log.Println(err)
c.room.CloseConnection(c)
break
}
if err = c.room.service.Receive(c.id, buf[:n]); err != nil {
c.room.log.Println(err)
c.room.CloseConnection(c)
break
}
}
}
func (c *Client) write(message []byte) error {
size := len(message)
sizeByte := make([]byte, 2, size+2)
binary.LittleEndian.PutUint16(sizeByte, uint16(size))
message = append(sizeByte, message...)
if _, err := c.conn.Write(message); err != nil {
return err
}
return nil
}
func (c *Client) writeStart() {
for {
if err := c.write(<-c.send); err != nil {
c.room.log.Println(err)
c.room.CloseConnection(c)
break
}
}
}
// GetID is getter for id.
func (c *Client) GetID() int {
return c.id
}
// GetIDByte is getter for idByte.
func (c *Client) GetIDByte() []byte {
return c.idByte
}
// Send is enqueue outbound messages.
func (c *Client) Send(message []byte) {
c.send <- message
}
// Close closes the connection.
func (c *Client) Close() error {
return c.conn.Close()
}
// ClientManager manages clients.
type ClientManager struct {
clients map[int]*Client
count int
*sync.Mutex
}
// NewClientManager is ClientManager constructed.
func NewClientManager() *ClientManager {
return &ClientManager{
clients: make(map[int]*Client),
Mutex: &sync.Mutex{},
}
}
// Get the client.
func (m *ClientManager) Get(clientID int) (*Client, error) {
m.Lock()
client, ok := m.clients[clientID]
m.Unlock()
if !ok {
return nil, fmt.Errorf("client not exists %v", clientID)
}
return client, nil
}
// Add the client.
func (m *ClientManager) Add(client *Client) error {
m.Lock()
defer m.Unlock()
if _, ok := m.clients[client.GetID()]; ok {
return fmt.Errorf("client already exists %v", client.GetID())
}
m.clients[client.GetID()] = client
m.count++
return nil
}
// Remove the client.
func (m *ClientManager) Remove(clientID int) {
m.Lock()
defer m.Unlock()
if _, ok := m.clients[clientID]; !ok {
return
}
delete(m.clients, clientID)
m.count--
}
// Exist checks the client exists.
func (m *ClientManager) Exist(clientID int) bool {
m.Lock()
_, ok := m.clients[clientID]
m.Unlock()
return ok
}
// GetAllClients returns all clients.
func (m *ClientManager) GetAllClients() map[int]*Client {
return m.clients
}
// Clear all clients.
func (m *ClientManager) Clear() {
m.Lock()
m.clients = make(map[int]*Client)
m.Unlock()
}
// Count clients.
func (m *ClientManager) Count() int {
return m.count
}
// First returns a first element.
func (m *ClientManager) First() (*Client, error) {
m.Lock()
defer m.Unlock()
for _, client := range m.clients {
return client, nil
}
return nil, errors.New("clients not exist")
}