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

前端之家收集整理的这篇文章主要介绍了新手学xingo golang服务器之-修改xingo 支持字符协议(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

原文链接:https://www.f2er.com/go/188152.html

猜你在找的Go相关文章