mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-15 04:20:57 +08:00
Send RESP encoded message with double quotes and escaped delimiters to server
This commit is contained in:
@@ -112,7 +112,8 @@ func main() {
|
||||
defer conn.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
// connRW := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
|
||||
connRW := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
stdioRW := bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
|
||||
|
||||
go func() {
|
||||
@@ -131,8 +132,9 @@ func main() {
|
||||
break
|
||||
}
|
||||
|
||||
stdioRW.Write(serialization.Encode(in))
|
||||
stdioRW.Flush()
|
||||
enc := serialization.Encode(in)
|
||||
connRW.Write([]byte(fmt.Sprintf("\"%s\"\n", string(enc))))
|
||||
connRW.Flush()
|
||||
}
|
||||
}
|
||||
done <- struct{}{}
|
||||
|
@@ -66,15 +66,15 @@ func Encode(b []byte) []byte {
|
||||
}
|
||||
|
||||
if len(tokens) == 1 && bytes.Equal(bytes.ToLower(tokens[0]), []byte("ping")) {
|
||||
return []byte(fmt.Sprintf("+%s\r\n", string(bytes.ToUpper(tokens[0]))))
|
||||
return []byte(fmt.Sprintf("+%s\\r\\n", string(bytes.ToUpper(tokens[0]))))
|
||||
}
|
||||
|
||||
if len(tokens) > 1 && bytes.Equal(bytes.ToLower(tokens[0]), []byte("ping")) {
|
||||
enc := []byte(fmt.Sprintf("*%d\r\n$%d\r\n%s\r\n",
|
||||
enc := []byte(fmt.Sprintf("*%d\\r\\n$%d\\r\\n%s\\r\\n",
|
||||
len(tokens), len(tokens[0]), string(bytes.ToUpper(tokens[0]))))
|
||||
for i := 1; i < len(tokens); i++ {
|
||||
token := tokens[i]
|
||||
enc = append(enc, []byte(fmt.Sprintf("$%d\r\n%s\r\n", len(token), token))...)
|
||||
enc = append(enc, []byte(fmt.Sprintf("$%d\\r\\n%s\\r\\n", len(token), token))...)
|
||||
}
|
||||
return enc
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
@@ -29,6 +30,22 @@ type Server struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
func (server *Server) hanndleConnection(conn net.Conn) {
|
||||
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
sw := bufio.NewWriter(os.Stdout)
|
||||
|
||||
for {
|
||||
l, _, err := rw.ReadLine()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
sw.Write(l)
|
||||
sw.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (server *Server) StartTCP() {
|
||||
conf := server.config
|
||||
var listener net.Listener
|
||||
@@ -69,7 +86,7 @@ func (server *Server) StartTCP() {
|
||||
}
|
||||
|
||||
// Read loop for connection
|
||||
conn.Write([]byte("Hello, Client!\n"))
|
||||
go server.hanndleConnection(conn)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user