新手学xingo golang服务器之-修改xingo 支持字符协议(一)

来源:互联网 发布:包月无限流量软件 编辑:程序博客网 时间:2024/06/06 02:23

新手学xingo golang服务器之-修改xingo 支持字符协议

git diff

查看我的修改:

--- a/fnet/datapack.go+++ b/fnet/datapack.go@@ -11,7 +11,7 @@ import ( type PkgData struct {        Len   uint32-       MsgId uint32+       MsgId string        Data  []byte }diff --git a/fnet/msghandle.go b/fnet/msghandle.goold mode 100755new mode 100644index b1221a0..1d5aa91--- a/fnet/msghandle.go+++ b/fnet/msghandle.go@@ -7,7 +7,7 @@ import (        "fmt"        "github.com/viphxin/xingo/logger"        "github.com/viphxin/xingo/utils"-       "strconv"+       _ "strconv"        "time"        "github.com/viphxin/xingo/iface"        "runtime/debug"@@ -16,14 +16,14 @@ import ( type MsgHandle struct {        PoolSize  int32        TaskQueue []chan *Request-       Apis      map[uint32]iface.IRouter+       Apis      map[string]iface.IRouter } func NewMsgHandle() *MsgHandle {        return &MsgHandle{                PoolSize:  utils.GlobalObject.PoolSize,                TaskQueue: make([]chan *Request, utils.GlobalObject.PoolSize),-               Apis:      make(map[uint32]iface.IRouter),+               Apis:      make(map[string]iface.IRouter),        } }@@ -65,15 +65,16 @@ func (this *MsgHandle) DoMsgFromGoRoutine(pkg interface{}) { func (this *MsgHandle) AddRouter(name string, router iface.IRouter) {        api := name-       index, err := strconv.Atoi(api)-       if err != nil {-               panic("error api: " + api)-       }-       if _, ok := this.Apis[uint32(index)]; ok {+       // index, err := strconv.Atoi(api)+       index := name+       // if err != nil {+       //      panic("error api: " + api)+       // }+       if _, ok := this.Apis[(index)]; ok {                //存在                panic("repeated api " + string(index))        }-       this.Apis[uint32(index)] = router+       this.Apis[(index)] = router        logger.Info("add api " + api) }diff --git a/fnet/protocol.go b/fnet/protocol.goold mode 100755new mode 100644index e12d5af..e37759a--- a/fnet/protocol.go+++ b/fnet/protocol.go@@ -31,7 +31,7 @@ func (this *Request)GetData() []byte{        return this.Pdata.Data }-func (this *Request)GetMsgId() uint32{+func (this *Request)GetMsgId() string{        return this.Pdata.MsgId }diff --git a/iface/irouter.go b/iface/irouter.goindex 8cf16c9..ace54f9 100644--- a/iface/irouter.go+++ b/iface/irouter.go@@ -5,7 +5,7 @@ import "net/http" type IRequest interface {        GetConnection() Iconnection        GetData() []byte-       GetMsgId() uint32+       GetMsgId() string }

“-“号删除
“+”号添加、修改
项目github地址:

https://github.com/atgczcl/xingo

测试:
这里写图片描述

//测试字符串协议    s.AddRouter("msg_load_bg", &api.Api_msg_dead_info_Router{})

go build server.go
./server.exe
这里写图片描述

运行起来后添加 字符串 msg_load_bg 协议成功,添加协议支持string,
封包,解包支持字符串,待续

阅读全文
0 0