forked from duoduo/Red.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
108 lines (93 loc) · 1.95 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
package main
import (
"fmt"
"net"
//"bytes"
"bufio"
"strconv"
//"os"
)
const (
CR_BYTE byte = byte('\r')
LF_BYTE byte = byte('\n')
COUNT_BYTE byte = byte('*')
ARG_BYTE byte = byte('$')
READ_BUF = (1024 * 16)
MAX_ARGC = (1024 * 1024)
)
// Request
type Request struct {
Argc uint64
Argv [][]byte
}
// Client
type Client struct {
Conn net.Conn
Request *Request
Response *Response
Command CommandFunc
Db *Db
Server *Server
}
func NewClient(server *Server, db *Db, conn net.Conn) *Client {
c := Client{
Server: server,
Db: db,
Conn: conn,
Response: NewResponse(conn),
}
return &c
}
func (c *Client) ReadRequest(reader *bufio.Reader) bool {
var lineBuf []byte
var isPrefix bool
var err error
lineBuf, isPrefix, err = reader.ReadLine()
if err != nil || isPrefix || lineBuf[0] != COUNT_BYTE {
return false
}
var request *Request = new(Request)
// Validate num of args.
request.Argc, err = strconv.ParseUint(string(lineBuf[1:]), 10, 64)
//fmt.Printf("\n\nARGC: ", request.Argc)
if err != nil || request.Argc > MAX_ARGC {
return false
}
// 2 lines per arg, 1st line is command, 2 line is value.
request.Argv = make([][]byte, request.Argc*2)
var line uint = 0
for {
if reader.Buffered() <= 0 {
break
}
lineBuf, isPrefix, err = reader.ReadLine()
if lineBuf[0] != ARG_BYTE {
request.Argv[line] = lineBuf
// New line if isPrefix == false
if !isPrefix {
line++
}
}
}
c.Request = request
return true
}
func (c *Client) ProcessRequest() {
reader := bufio.NewReaderSize(c.Conn, READ_BUF)
for {
// Read will block until something is ready to be ready.
ok := c.ReadRequest(reader)
if !ok {
continue
}
c.Command = CommandFromRequest(c.Request)
c.Command(c)
// For now, force close conn when there is no more data.
b, _ := reader.Peek(1)
if len(b) == 0 {
break
}
}
fmt.Printf("Closing conn %s\n", c.Conn.RemoteAddr())
c.Conn.Close()
}