Skip to content

Commit

Permalink
- update nbio examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Jul 6, 2021
1 parent 047173a commit f957996
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
28 changes: 14 additions & 14 deletions examples/bench_nbio/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
}
Expand All @@ -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() {
Expand All @@ -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{
Expand Down
24 changes: 12 additions & 12 deletions examples/nbio/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
}
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion examples/protocols/websocket/jsclient/arpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function ArpcClient(url, codec) {
clearTimeout(session.timer);
}
session.resolve({ data: null, err: _ErrDisconnected });
}
}
}
// shutdown
if (client.state == _SOCK_STATE_CLOSED) {
Expand Down

0 comments on commit f957996

Please sign in to comment.