Skip to content

Commit

Permalink
Protocol.Packet‘s input parameter changed to Interface (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allenxuxu authored Jul 5, 2021
1 parent 2341c31 commit 979940f
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 29 deletions.
5 changes: 2 additions & 3 deletions benchmarks/gev-echo-server/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package main
import (
"flag"
"net/http"
_ "net/http/pprof"
"strconv"

"github.com/Allenxuxu/gev/log"

_ "net/http/pprof"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
)
Expand All @@ -17,7 +16,7 @@ type example struct {
}

func (s *example) OnConnect(c *connection.Connection) {}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {

out = data

Expand Down
8 changes: 4 additions & 4 deletions connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type CallBack interface {
OnMessage(c *Connection, ctx interface{}, data []byte) []byte
OnMessage(c *Connection, ctx interface{}, data []byte) interface{}
OnClose(c *Connection)
}

Expand Down Expand Up @@ -112,14 +112,14 @@ func (c *Connection) Connected() bool {
}

// Send 用来在非 loop 协程发送
func (c *Connection) Send(buffer []byte) error {
func (c *Connection) Send(data interface{}) error {
if !c.connected.Get() {
return ErrConnectionClosed
}

c.loop.QueueInLoop(func() {
if c.connected.Get() {
c.sendInLoop(c.protocol.Packet(c, buffer))
c.sendInLoop(c.protocol.Packet(c, data))
}
})
return nil
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *Connection) handlerProtocol(tmpBuffer *[]byte, buffer *ringbuffer.RingB
ctx, receivedData := c.protocol.UnPacket(c, buffer)
for ctx != nil || len(receivedData) != 0 {
sendData := c.callBack.OnMessage(c, ctx, receivedData)
if len(sendData) > 0 {
if sendData != nil {
*tmpBuffer = append(*tmpBuffer, c.protocol.Packet(c, sendData)...)
}

Expand Down
6 changes: 3 additions & 3 deletions connection/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var _ Protocol = &DefaultProtocol{}
// Protocol 自定义协议编解码接口
type Protocol interface {
UnPacket(c *Connection, buffer *ringbuffer.RingBuffer) (interface{}, []byte)
Packet(c *Connection, data []byte) []byte
Packet(c *Connection, data interface{}) []byte
}

// DefaultProtocol 默认 Protocol
Expand Down Expand Up @@ -38,6 +38,6 @@ func (d *DefaultProtocol) UnPacket(c *Connection, buffer *ringbuffer.RingBuffer)
}

// Packet 封包
func (d *DefaultProtocol) Packet(c *Connection, data []byte) []byte {
return data
func (d *DefaultProtocol) Packet(c *Connection, data interface{}) []byte {
return data.([]byte)
}
2 changes: 1 addition & 1 deletion example/bufferlength/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Server) OnConnect(c *connection.Connection) {
}

// OnMessage callback
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Printf("OnMessage, read buffer len %d, write buffer len %d \n", c.ReadBufferLength(), c.WriteBufferLength())

out = data
Expand Down
2 changes: 1 addition & 1 deletion example/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *example) OnConnect(c *connection.Connection) {
s.Count.Add(1)
//log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
//log.Println("OnMessage")
out = data
return
Expand Down
2 changes: 1 addition & 1 deletion example/idleconnection/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type example struct {
func (s *example) OnConnect(c *connection.Connection) {
log.Info(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Infof("OnMessage from : %s", c.PeerAddr())
out = data
return
Expand Down
2 changes: 1 addition & 1 deletion example/maxconnection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *Server) OnConnect(c *connection.Connection) {
}

// OnMessage callback
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Println("OnMessage")
out = data
return
Expand Down
2 changes: 1 addition & 1 deletion example/protobuf/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type example struct{}
func (s *example) OnConnect(c *connection.Connection) {
log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
msgType := ctx.(string)

switch msgType {
Expand Down
8 changes: 5 additions & 3 deletions example/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/binary"

"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/ringbuffer"
"github.com/gobwas/pool/pbytes"
Expand Down Expand Up @@ -31,10 +32,11 @@ func (d *ExampleProtocol) UnPacket(c *connection.Connection, buffer *ringbuffer.
return nil, nil
}

func (d *ExampleProtocol) Packet(c *connection.Connection, data []byte) []byte {
dataLen := len(data)
func (d *ExampleProtocol) Packet(c *connection.Connection, data interface{}) []byte {
dd := data.([]byte)
dataLen := len(dd)
ret := make([]byte, exampleHeaderLen+dataLen)
binary.BigEndian.PutUint32(ret, uint32(dataLen))
copy(ret[4:], data)
copy(ret[4:], dd)
return ret
}
2 changes: 1 addition & 1 deletion example/protocol/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type example struct{}
func (s *example) OnConnect(c *connection.Connection) {
log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Println("OnMessage:", data)
out = data
return
Expand Down
2 changes: 1 addition & 1 deletion example/pushmessage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *Server) OnConnect(c *connection.Connection) {
}

// OnMessage callback
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Println("OnMessage")
out = data
return
Expand Down
4 changes: 2 additions & 2 deletions plugins/protobuf/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func (p *Protocol) UnPacket(c *connection.Connection, buffer *ringbuffer.RingBuf
}

// Packet ...
func (p *Protocol) Packet(c *connection.Connection, data []byte) []byte {
return data
func (p *Protocol) Packet(c *connection.Connection, data interface{}) []byte {
return data.([]byte)
}
4 changes: 2 additions & 2 deletions plugins/websocket/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func (p *Protocol) UnPacket(c *connection.Connection, buffer *ringbuffer.RingBuf
}

// Packet 直接返回
func (p *Protocol) Packet(c *connection.Connection, data []byte) []byte {
return data
func (p *Protocol) Packet(c *connection.Connection, data interface{}) []byte {
return data.([]byte)
}
2 changes: 1 addition & 1 deletion plugins/websocket/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *HandlerWrap) OnConnect(c *connection.Connection) {
}

// OnMessage wrap
func (s *HandlerWrap) OnMessage(c *connection.Connection, ctx interface{}, payload []byte) []byte {
func (s *HandlerWrap) OnMessage(c *connection.Connection, ctx interface{}, payload []byte) interface{} {
header, ok := ctx.(*ws.Header)
if !ok && len(payload) != 0 { // 升级协议 握手
return payload
Expand Down
4 changes: 2 additions & 2 deletions server_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *example2) OnConnect(c *connection.Connection) {
}
}

func (s *example2) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example2) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Info("OnMessage")

return
Expand Down Expand Up @@ -71,7 +71,7 @@ func (s *example3) OnConnect(c *connection.Connection) {
//log.Info(" OnConnect : ", c.PeerAddr())
}

func (s *example3) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example3) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
//log.Info("OnMessage")

return
Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *example) OnConnect(c *connection.Connection) {
//log.Println(" OnConnect : ", c.PeerAddr())
}

func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
//log.Println("OnMessage")

//out = data
Expand Down Expand Up @@ -207,7 +207,7 @@ func (s *example1) OnConnect(c *connection.Connection) {
//log.Println(" OnConnect : ", c.PeerAddr())
}

func (s *example1) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
func (s *example1) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
//log.Println("OnMessage")

//out = data
Expand Down

0 comments on commit 979940f

Please sign in to comment.