-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
196 lines (176 loc) · 4.03 KB
/
user.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
package main
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/ugorji/go/codec"
"io/ioutil"
"strconv"
"time"
)
const (
MAX_USERS_PER_LOCATION = 20
MAX_NICKNAME_LENGTH = 20
SEND_CHANNEL_SIZE = 256
// Time allowed to write a message to the peer.
WRITE_WAIT = 5 * time.Second
// Time allowed to read the next pong message from the peer.
PONG_WAIT = 20 * time.Second
// Send pings to client with this period. Must be less than PONG_WAIT.
PING_PERIOD = PONG_WAIT / 2
// Maximum message size allowed from peer.
// must be at least 4 * MAX_CHAT_MESSAGE_LENGTH (in script.js)
MAX_MESSAGE_SIZE = 1024
LAST_USER_ID_FILENAME = "data/last_user_id"
)
var (
userIdGenerator chan int
msgpackHandle codec.MsgpackHandle
)
type User struct {
sendData chan *[]byte
UserId int
Nickname string
MouseIsDown bool
PositionX int
PositionY int
ColorRed int
ColorGreen int
ColorBlue int
UsePen bool
kick chan string
}
func NewUser() *User {
user := new(User)
user.UserId = <-userIdGenerator
user.Nickname = strconv.Itoa(user.UserId)
user.UsePen = true
user.sendData = make(chan *[]byte, SEND_CHANNEL_SIZE)
user.kick = make(chan string)
return user
}
func init() {
userIdGenerator = make(chan int)
go func() {
i := ReadIntFromFile(LAST_USER_ID_FILENAME)
for {
userIdGenerator <- i
i += 1
}
}()
}
func encodeEvent(event *[]interface{}) (*[]byte, error) {
var result []byte
var err = codec.NewEncoderBytes(&result, &msgpackHandle).Encode(event)
if err != nil {
Log.Printf("Failed to encode event %v\n", event)
}
return &result, err
}
func (user *User) Kick(description string) {
select {
case user.kick <- description:
default:
// SocketHandler has already returned, avoid blocking
}
}
func (user *User) SendEvent(event *[]interface{}) {
data, err := encodeEvent(event)
if err != nil {
return
}
user.SendData(data)
}
func (user *User) SendData(data *[]byte) {
select {
case user.sendData <- data:
default:
user.Kick("Buffer full")
}
}
func (user *User) mouseMove(x int, y int, duration int) {
user.PositionX = x
user.PositionY = y
}
func (user *User) mouseUp() {
user.MouseIsDown = false
}
func (user *User) mouseDown() {
user.MouseIsDown = true
}
func (user *User) changeTool(use_pen bool) {
user.UsePen = use_pen
}
func (user *User) changeColor(red, green, blue int) {
user.ColorRed = red
user.ColorGreen = green
user.ColorBlue = blue
}
func (user *User) changeNickname(nickname string) {
user.Nickname = nickname
}
func write(ws *websocket.Conn, opCode int, payload []byte) error {
ws.SetWriteDeadline(time.Now().Add(WRITE_WAIT))
return ws.WriteMessage(opCode, payload)
}
func (user *User) Sender(ws *websocket.Conn) {
go func() {
ticker := time.NewTicker(PING_PERIOD)
defer func() {
ticker.Stop()
}()
for {
select {
case data, open := <-user.sendData:
if !open {
// channel is closed: user left
return
}
if err := write(ws, websocket.BinaryMessage, *data); err != nil {
user.Kick(err.Error())
return
}
case <-ticker.C:
if err := write(ws, websocket.PingMessage, []byte{}); err != nil {
user.Kick(err.Error())
return
}
}
}
}()
}
func (user *User) Receiver(location *Location, ws *websocket.Conn) {
// receives and decodes messages from users
go func() {
ws.SetReadLimit(MAX_MESSAGE_SIZE)
ws.SetReadDeadline(time.Now().Add(PONG_WAIT))
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(PONG_WAIT))
return nil
})
for {
op, r, err := ws.NextReader()
if err != nil {
user.Kick(err.Error())
return
}
switch op {
case websocket.BinaryMessage:
data, err := ioutil.ReadAll(r)
if err != nil {
user.Kick(err.Error())
return
}
var event []interface{}
err = codec.NewDecoderBytes(data, &msgpackHandle).Decode(&event)
if err != nil {
user.Kick(err.Error())
return
}
location.Message <- &UserAndEvent{user, &event}
default:
user.Kick(fmt.Sprintf("bad message type: %v", op))
return
}
}
}()
}