From 74078f61114e8ba83fe78aa70b55cf5f34a5ba40 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Mon, 24 Jul 2017 23:55:44 +0300 Subject: [PATCH] use newlines in communication protocol Mentioned in #12 and #47. --- client.go | 22 ++++++++++++---------- server.go | 21 +++++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/client.go b/client.go index 93f6e02e..cabbebb9 100644 --- a/client.go +++ b/client.go @@ -105,15 +105,18 @@ func saveFiles(list []string, copy bool) error { log.Printf("saving files: %v", list) - fmt.Fprint(c, "save ") + fmt.Fprintln(c, "save") if copy { - fmt.Fprint(c, "copy ") + fmt.Fprintln(c, "copy") } else { - fmt.Fprint(c, "move ") + fmt.Fprintln(c, "move") } - fmt.Fprintln(c, strings.Join(list, ":")) + for _, f := range list { + fmt.Fprintln(c, f) + } + fmt.Fprintln(c) return nil } @@ -132,20 +135,19 @@ func loadFiles() (list []string, copy bool, err error) { s.Scan() - word, rest := splitWord(s.Text()) - log.Printf("load: %s", s.Text()) - - switch word { + switch s.Text() { case "copy": copy = true case "move": copy = false default: - err = fmt.Errorf("unexpected option to copy file(s): %s", word) + err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text()) return } - list = strings.Split(rest, ":") + for s.Scan() && s.Text() != "" { + list = append(list, s.Text()) + } if s.Err() != nil { err = fmt.Errorf("scanning file list: %s", s.Err()) diff --git a/server.go b/server.go index 62ddafe9..c2765a6e 100644 --- a/server.go +++ b/server.go @@ -7,7 +7,6 @@ import ( "net" "os" "strconv" - "strings" ) var ( @@ -56,24 +55,30 @@ func handleConn(c net.Conn) { word, rest := splitWord(s.Text()) switch word { case "save": - word2, rest2 := splitWord(rest) - switch word2 { + s.Scan() + switch s.Text() { case "copy": gCopyFile = true case "move": gCopyFile = false default: - log.Printf("unexpected option to copy file(s): %s", word2) + log.Printf("unexpected option to copy file(s): %s", s.Text()) break } - gFileList = strings.Split(rest2, ":") + gFileList = nil + for s.Scan() && s.Text() != "" { + gFileList = append(gFileList, s.Text()) + } case "load": if gCopyFile { - fmt.Fprint(c, "copy ") + fmt.Fprintln(c, "copy") } else { - fmt.Fprint(c, "move ") + fmt.Fprintln(c, "move") } - fmt.Fprintln(c, strings.Join(gFileList, ":")) + for _, f := range gFileList { + fmt.Fprintln(c, f) + } + fmt.Fprintln(c) case "conn": if rest != "" { word2, _ := splitWord(rest)