diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf592ec --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +ping-pong: pp-server pp-client + @ + +pp-client pp-server: + go build -o ./bin/$@ ./examples/ping-pong/$@/ diff --git a/examples/ping-pong/pp-client/main.go b/examples/ping-pong/pp-client/main.go new file mode 100644 index 0000000..e2f11ad --- /dev/null +++ b/examples/ping-pong/pp-client/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "time" + + "git.qowevisa.me/Qowevisa/tcpmachine/tcpclient" +) + +func main() { + conf := tcpclient.GetDefaultConfig() + client := tcpclient.CreateClient(conf) + go func() { + for { + if client.IsConnected { + break + } + time.Sleep(100 * time.Millisecond) + } + for i := 0; i < 10; i++ { + client.Server.Write([]byte("PING\n")) + time.Sleep(time.Second) + } + }() + go client.ErrorResolver(client.ErrorsChannel) + client.StartClient("127.0.0.1:10000") +} diff --git a/examples/ping-pong/pp-server/main.go b/examples/ping-pong/pp-server/main.go new file mode 100644 index 0000000..efdd4dc --- /dev/null +++ b/examples/ping-pong/pp-server/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "net" + "time" + + "git.qowevisa.me/Qowevisa/tcpmachine/tcpcommand" + "git.qowevisa.me/Qowevisa/tcpmachine/tcpserver" +) + +func main() { + bundle, err := tcpcommand.CreateCommandBundle([]tcpcommand.Command{ + { + Command: "PING", + Action: func(s []string, client net.Conn) { + fmt.Printf("todo..\n") + client.Write([]byte("PONG\n")) + }, + }, + }) + if err != nil { + panic(err) + } + conf := tcpserver.GetDefaultConfig() + handler, errorChannel := tcpserver.CreateHandleClientFuncFromCommands(bundle, conf) + go func() { + for err := range errorChannel { + fmt.Printf("Error:1: %v\n", err) + } + }() + conf.HandleClientFunc = handler + server := tcpserver.CreateServer(conf) + go func() { + time.Sleep(time.Minute) + server.Exit <- true + }() + server.StartServer("127.0.0.1:10000") +}