From bf3fd75c4bd8ebf8f8c72231dda68597c6601ae8 Mon Sep 17 00:00:00 2001 From: zhaoyuanli Date: Sat, 9 Oct 2021 16:40:17 +0800 Subject: [PATCH] add go.mod --- example/json_toy/{ => client}/main.go | 19 +---------- example/json_toy/service/main.go | 45 +++++++++++++++++++++++++++ go.mod | 5 +++ 3 files changed, 51 insertions(+), 18 deletions(-) rename example/json_toy/{ => client}/main.go (58%) create mode 100644 example/json_toy/service/main.go create mode 100644 go.mod diff --git a/example/json_toy/main.go b/example/json_toy/client/main.go similarity index 58% rename from example/json_toy/main.go rename to example/json_toy/client/main.go index 6d57e1b..c0a1624 100644 --- a/example/json_toy/main.go +++ b/example/json_toy/client/main.go @@ -20,28 +20,11 @@ func main() { json.Register(AddReq{}) json.Register(AddRsp{}) - server, err := link.Listen("tcp", "0.0.0.0:0", json, 0 /* sync send */, link.HandlerFunc(serverSessionLoop)) - checkErr(err) - addr := server.Listener().Addr().String() - go server.Serve() - - client, err := link.Dial("tcp", addr, json, 0) + client, err := link.Dial("tcp", "0.0.0.0:57278", json, 0) checkErr(err) clientSessionLoop(client) } -func serverSessionLoop(session *link.Session) { - for { - req, err := session.Receive() - checkErr(err) - - err = session.Send(&AddRsp{ - req.(*AddReq).A + req.(*AddReq).B, - }) - checkErr(err) - } -} - func clientSessionLoop(session *link.Session) { for i := 0; i < 10; i++ { err := session.Send(&AddReq{ diff --git a/example/json_toy/service/main.go b/example/json_toy/service/main.go new file mode 100644 index 0000000..2029c37 --- /dev/null +++ b/example/json_toy/service/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "log" + + "github.com/funny/link" + "github.com/funny/link/codec" +) + +type AddReq struct { + A, B int +} + +type AddRsp struct { + C int +} + +func main() { + json := codec.Json() + json.Register(AddReq{}) + json.Register(AddRsp{}) + + server, err := link.Listen("tcp", "0.0.0.0:57278", json, 0 /* sync send */, link.HandlerFunc(serverSessionLoop)) + checkErr(err) + server.Listener() + go server.Serve() + select {} +} + +func serverSessionLoop(session *link.Session) { + for { + req, err := session.Receive() + checkErr(err) + err = session.Send(&AddRsp{ + req.(*AddReq).A + req.(*AddReq).B, + }) + checkErr(err) + } +} + +func checkErr(err error) { + if err != nil { + log.Fatal(err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e60f130 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/funny/link + +go 1.15 + +require github.com/funny/utest v0.0.0-20161029064919-43870a374500