mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-14 20:16:05 +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()
|
defer conn.Close()
|
||||||
|
|
||||||
done := make(chan struct{})
|
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))
|
stdioRW := bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -131,8 +132,9 @@ func main() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
stdioRW.Write(serialization.Encode(in))
|
enc := serialization.Encode(in)
|
||||||
stdioRW.Flush()
|
connRW.Write([]byte(fmt.Sprintf("\"%s\"\n", string(enc))))
|
||||||
|
connRW.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
|
@@ -66,15 +66,15 @@ func Encode(b []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(tokens) == 1 && bytes.Equal(bytes.ToLower(tokens[0]), []byte("ping")) {
|
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")) {
|
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]))))
|
len(tokens), len(tokens[0]), string(bytes.ToUpper(tokens[0]))))
|
||||||
for i := 1; i < len(tokens); i++ {
|
for i := 1; i < len(tokens); i++ {
|
||||||
token := 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
|
return enc
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
@@ -29,6 +30,22 @@ type Server struct {
|
|||||||
config Config
|
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() {
|
func (server *Server) StartTCP() {
|
||||||
conf := server.config
|
conf := server.config
|
||||||
var listener net.Listener
|
var listener net.Listener
|
||||||
@@ -69,7 +86,7 @@ func (server *Server) StartTCP() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read loop for connection
|
// Read loop for connection
|
||||||
conn.Write([]byte("Hello, Client!\n"))
|
go server.hanndleConnection(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user