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

9 篇文章 1 订阅
6 篇文章 0 订阅

新手学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.go
old mode 100755
new mode 100644
index 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.go
old mode 100755
new mode 100644
index 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.go
index 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,
封包,解包支持字符串,待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值