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

我学xingo golang服务器之-XingoApi调用

发布时间:2020-12-16 18:10:18 所属栏目:大数据 来源:网络整理
导读:XingoApi调用 先看看protobuf msg定义: `message MyTestMsg{` ? int32Code= 1 ; // Field numbers must be positive integers. 必须大于0 ? stringContent= 2 ; //依次递增 `}` 。。。可以有很多 ` message xxx{``xxx= 1 ;`` ... ``}` windows下面GenProtos.

XingoApi调用

先看看protobuf msg定义:

`message MyTestMsg{`

?       int32Code=1; // Field numbers must be positive integers. 必须大于0

?       stringContent=2; //依次递增

`}`

。。。可以有很多

`

message xxx{`

`xxx=1;`

`...`

`}`

windows下面GenProtos.bat批处理生成:

protoc.exe--plugin=protoc-gen-go=%GOPATH%binprotoc-gen-go.exe --go_out %~dp0 -I %~dp0%~dp0*.proto

pause

保存在文本.bat里面双击即可运行;

%~dp0win下的当前目录

api添加部分:

xingo_demoserver.go 里面添加api的列子:

`//add api ---------------start`

?    TestRouterObj := &api.TestRouter{}

?    s.AddRouter(TestRouterObj)

? //add api ---------------end

可以看到在github.comviphxinxingofnetmsghandle.go

AddRouter函数找到了,api添加的地方,通过split “_”进行了分解,变成了int型的index;

api进行读取部分在msghandle.go这里:

func (this *MsgHandle) StartWorkerLoop(poolSize int) {`

?    if utils.GlobalObject.IsThreadSafeMode(){

?        //线程安全模式所有的逻辑都在一个goroutine处理,这样可以实现无锁化服务

?        this.TaskQueue[0] = make(chan *PkgAll,utils.GlobalObject.MaxWorkerLen)

?        go func(){

?            logger.Info("init thread mode workpool.")

?            for{

?                select {

?                case data := <- this.TaskQueue[0]:

?                    if f,ok := this.Apis[data.Pdata.MsgId]; ok {

?                        //存在

?                        st := time.Now()

?                        //f.Call([]reflect.Value{reflect.ValueOf(data)})

?                        utils.XingoTry(f,[]reflect.Value{reflect.ValueOf(data)},this.HandleError)

?                        logger.Debug(fmt.Sprintf("Api_%d cost total time: %f ms",data.Pdata.MsgId,time.Now().Sub(st).Seconds()*1000))

?                    } else {

?                        logger.Error(fmt.Sprintf("not found api: %d",data.Pdata.MsgId))

?                    }

?                case delaytask := <- utils.GlobalObject.GetSafeTimer().GetTriggerChannel():

?                    delaytask.Call()

?                }

?            }

?        }()

?    }else{

?        for i := 0; i < poolSize; i += 1 {

?            c := make(chan *PkgAll,utils.GlobalObject.MaxWorkerLen)

?            this.TaskQueue[i] = c

?            go func(index int,taskQueue chan *PkgAll) {

?                logger.Info(fmt.Sprintf("init thread pool %d.",index))

?                for {

?                    data := <-taskQueue

?                    //can goroutine?

?                    if f,data.Pdata.MsgId))

?                    }

?                }

?            }(i,c)

?        }

?    }

`}`

处理流程图:

下发数据这块就是自己写相关逻辑,然后调用xingo接口即可;

over收工!理解不对的,可以指正

(编辑:李大同)

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

    推荐文章
      热点阅读