This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredgo.go
123 lines (108 loc) · 2.18 KB
/
redgo.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
package redgo
import (
"bufio"
"bytes"
"errors"
"net"
"strconv"
"time"
// "fmt"
)
type Conn struct {
conn net.Conn
reader *bufio.Reader
}
const (
Sep = "\r\n"
MaxArgSize = 64000
MaxArgCount = 64
)
var (
errFormat = errors.New("format error")
errSize = errors.New("size out of bounds")
)
type Status string
type Error string
type Integer int
func Dial(addr string) (*Conn, error) {
conn, err := net.DialTimeout("tcp", addr, time.Second*5)
if err != nil {
return nil, err
}
return &Conn{conn: conn , reader: bufio.NewReader(conn)}, nil
}
func (conn *Conn) Close() error {
return conn.conn.Close()
}
func (conn *Conn) Excute(args ...string) error {
var buf bytes.Buffer
buf.WriteString("*")
buf.WriteString(strconv.Itoa(len(args)))
buf.WriteString(Sep)
for _, arg := range args {
buf.WriteString("$")
buf.WriteString(strconv.Itoa(len(arg)))
buf.WriteString(Sep)
buf.WriteString(arg)
buf.WriteString(Sep)
}
_, err := conn.conn.Write(buf.Bytes())
// fmt.Println(buf.Bytes())
return err
}
func (conn *Conn) ReadRaw() (string, error) {
b := make([]byte, 4096)
n, err := conn.conn.Read(b)
// fmt.Println(n)
return string(b[0:n]), err
}
//func (conn *Conn) Response()(res interface{}, error){
// b, err := conn.reader.ReadByte()
// if err != nil {
// return nil, err
// }
// switch b {
// // OK
// case '+':
// line, isPrefix, err := conn.reader.ReadLine()
// if err != nil {
// return nil, err
// }
// if isPrefix{
// return nil, errSize
// }
// return Status(line), nil
// // Error
// case '-':
// line, isPrefix, err := c.r.ReadLine()
// if err != nil {
// return nil, err
// }
// if isPrefix {
// return nil, ErrSize
// }
// return Error(line), nil
// //Integer
// case ':':
// line, isPrefix, err := c.r.ReadLine()
// if err != nil {
// return nil, err
// }
// if isPrefix {
// return nil, ErrSize
// }
// i, err := strconv.Atoi(string(line))
// if err != nil {
// return nil, ErrFormat
// }
// return Integer(i), nil
// case '$':
// conn.reader.UnreadByte()
// return conn.ReadString()
// case '*':
// conn.reader.UnreadByte()
// return conn.ReadStrings()
// }
//}
//func (conn *Conn)ReadString()(string, error){
//}