-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
48 lines (39 loc) · 845 Bytes
/
conn.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
package main
import (
"fmt"
"net"
"strings"
)
func handleConnection(conn net.Conn) {
defer fmt.Println("connection closed")
defer conn.Close()
for {
resp := NewResp(conn)
value, err := resp.Read()
if err != nil {
fmt.Println("-ERROR: unable to parse input")
conn.Close()
return
}
fmt.Println(value)
if value.typ != "array" {
fmt.Println("Invalid request, expected array")
continue
}
if len(value.array) == 0 {
fmt.Println("Invalid request, expected array length > 0")
continue
}
command := strings.ToUpper(value.array[0].bulk)
args := value.array[1:]
writer := NewWriter(conn)
handler, ok := Handlers[command]
if !ok {
fmt.Println("Invalid command: ", command)
writer.Write(Value{typ: "string", str: ""})
continue
}
result := handler(args)
writer.Write(result)
}
}