From f9579966ee65c39df593b1069becee1494f50686 Mon Sep 17 00:00:00 2001 From: lesismal <40462947+lesismal@users.noreply.github.com> Date: Tue, 6 Jul 2021 12:35:07 +0800 Subject: [PATCH] - update nbio examples --- examples/bench_nbio/server/server.go | 28 +++++++++---------- examples/nbio/server/server.go | 24 ++++++++-------- examples/protocols/websocket/jsclient/arpc.js | 2 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/examples/bench_nbio/server/server.go b/examples/bench_nbio/server/server.go index c9aa818..cdca68a 100644 --- a/examples/bench_nbio/server/server.go +++ b/examples/bench_nbio/server/server.go @@ -8,9 +8,8 @@ import ( "github.com/lesismal/arpc" "github.com/lesismal/arpc/codec" "github.com/lesismal/arpc/log" - "github.com/lesismal/llib/bytes" "github.com/lesismal/nbio" - nlog "github.com/lesismal/nbio/log" + nlog "github.com/lesismal/nbio/logging" ) var ( @@ -31,14 +30,14 @@ type HelloRsp struct { // Session . type Session struct { Client *arpc.Client - Buffer *bytes.Buffer + Buffer []byte } func onOpen(c *nbio.Conn) { - client := &arpc.Client{Conn: c, Codec: codec.DefaultCodec} + client := &arpc.Client{Conn: c, Codec: codec.DefaultCodec, IsAsync: true} session := &Session{ Client: client, - Buffer: bytes.NewBuffer(), + Buffer: nil, } c.SetSession(session) } @@ -50,19 +49,20 @@ func onData(c *nbio.Conn, data []byte) { return } session := iSession.(*Session) - buffer := session.Buffer - buffer.Push(append([]byte{}, data...)) - buf, err := buffer.Head(4) - if err != nil { + session.Buffer = append(session.Buffer, data...) + if len(session.Buffer) < arpc.HeadLen { return } - header := arpc.Header(buf) - buf, err = buffer.Pop(arpc.HeadLen + header.BodyLen()) - if err != nil { + + headBuf := session.Buffer[:4] + header := arpc.Header(headBuf) + if len(session.Buffer) < arpc.HeadLen+header.BodyLen() { return } - handler.OnMessage(session.Client, &arpc.Message{Buffer: append([]byte{}, buf...)}) + msg := &arpc.Message{Buffer: session.Buffer[:arpc.HeadLen+header.BodyLen()]} + session.Buffer = session.Buffer[arpc.HeadLen+header.BodyLen():] + handler.OnMessage(session.Client, msg) } func main() { @@ -72,7 +72,7 @@ func main() { handler.Handle("Hello", func(ctx *arpc.Context) { req := &HelloReq{} ctx.Bind(req) - ctx.Write2(&HelloRsp{Msg: req.Msg}) + ctx.Write(&HelloRsp{Msg: req.Msg}) }) g := nbio.NewGopher(nbio.Config{ diff --git a/examples/nbio/server/server.go b/examples/nbio/server/server.go index e18eafa..574e089 100644 --- a/examples/nbio/server/server.go +++ b/examples/nbio/server/server.go @@ -8,9 +8,8 @@ import ( "github.com/lesismal/arpc" "github.com/lesismal/arpc/codec" "github.com/lesismal/arpc/log" - "github.com/lesismal/llib/bytes" "github.com/lesismal/nbio" - nlog "github.com/lesismal/nbio/loging" + nlog "github.com/lesismal/nbio/logging" ) var ( @@ -21,14 +20,14 @@ var ( // Session . type Session struct { Client *arpc.Client - Buffer *bytes.Buffer + Buffer []byte } func onOpen(c *nbio.Conn) { client := &arpc.Client{Conn: c, Codec: codec.DefaultCodec, IsAsync: true} session := &Session{ Client: client, - Buffer: bytes.NewBuffer(), + Buffer: nil, } c.SetSession(session) } @@ -40,19 +39,20 @@ func onData(c *nbio.Conn, data []byte) { return } session := iSession.(*Session) - buffer := session.Buffer - buffer.Push(append([]byte{}, data...)) - buf, err := buffer.Head(4) - if err != nil { + session.Buffer = append(session.Buffer, data...) + if len(session.Buffer) < arpc.HeadLen { return } - header := arpc.Header(buf) - buf, err = buffer.Pop(arpc.HeadLen + header.BodyLen()) - if err != nil { + + headBuf := session.Buffer[:4] + header := arpc.Header(headBuf) + if len(session.Buffer) < arpc.HeadLen+header.BodyLen() { return } - handler.OnMessage(session.Client, &arpc.Message{Buffer: buf}) + msg := &arpc.Message{Buffer: session.Buffer[:arpc.HeadLen+header.BodyLen()]} + session.Buffer = session.Buffer[arpc.HeadLen+header.BodyLen():] + handler.OnMessage(session.Client, msg) } func main() { diff --git a/examples/protocols/websocket/jsclient/arpc.js b/examples/protocols/websocket/jsclient/arpc.js index 99f4ba0..68dcf78 100644 --- a/examples/protocols/websocket/jsclient/arpc.js +++ b/examples/protocols/websocket/jsclient/arpc.js @@ -280,7 +280,7 @@ function ArpcClient(url, codec) { clearTimeout(session.timer); } session.resolve({ data: null, err: _ErrDisconnected }); - } + } } // shutdown if (client.state == _SOCK_STATE_CLOSED) {