加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 18:08:19 所属栏目:大数据 来源:网络整理
导读:新手学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

新手学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,封包,解包支持字符串,待续

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读