-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
143 lines (111 loc) · 2.5 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
/* Gavin Langdon
* Network Programming
* Spring 2013
* Chat server
*/
package main
import (
"fmt"
"net"
"bytes"
"regexp"
"errors"
)
type ClientId struct {
username string
password []byte
}
type ClientInfo struct {
password []byte
loggedIn bool
client *Client
}
var clientregex = regexp.MustCompile("^[[:alnum:]]+$")
func NewClientId(username string, password []byte) (ClientId, error){
if !clientregex.MatchString(username) {
return ClientId{}, errors.New("Invalid username chars provided")
}
return ClientId{username, password}, nil
}
type Client struct {
net.Conn
responseCh chan *Response
requestCh chan Requestable
username string
loggedIn bool
loginTries int
messagesSent int
}
func (cl *Client) String() string {
str := cl.RemoteAddr().String()
if cl.loggedIn {
return cl.username + " (" + str + ")"
}
return str
}
func (cl *Client) HandleRequest(request []byte) (bool, error) {
if *VerboseMode {
fmt.Println("RCVD from", cl.String()+":", string(request))
}
temp := bytes.SplitN(request, []byte(" "), 2)
code := temp[0]
var data []byte
if len(temp) >= 2 {
data = temp[1]
}
for request_str := range Requests {
if bytes.Compare(bytes.ToUpper(code), []byte(request_str)) == 0 {
rq := Requests[request_str]()
rq.SetClient(cl)
if !rq.Validate() {
return false, errors.New("Not authorized to do this")
}
err := rq.Create(data)
if err != nil {
return false, err
}
cl.requestCh <-rq
response := <-cl.responseCh
response.WriteTo(cl)
return response.Quit, nil
}
}
return false, errors.New("Invalid request code")
}
func (cl *Client) Close() error {
// Defer connection closing
defer cl.Conn.Close()
// Create a dummy quit request and shuttle it to the other goroutine
// so its data is cleaned up
var qr QuitRequest
qr.SetClient(cl)
cl.requestCh <-&qr
return nil
}
func (cl *Client) Serve() {
defer cl.Close()
buf := make([]byte, 1024)
for {
count, err := cl.Read(buf)
if err != nil {
fmt.Println(err)
return
}
if count <= 0 {
continue
}
request := bytes.Trim(buf[:count], "\r\n")
disconn, err := cl.HandleRequest(request)
if err != nil {
response := NewErrorResponse(err)
response.WriteTo(cl)
if _, ok := err.(*DisconnectError); ok {
fmt.Println("Got disconnect error. Disconnecting")
return
}
}
if disconn {
return
}
}
}