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

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%\bin\protoc-gen-go.exe --go_out %~dp0 -I %~dp0%~dp0\*.proto

pause

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

%~dp0win下的当前目录

api添加部分:

xingo_demo\server.go 里面添加api的列子:

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

​    TestRouterObj := &api.TestRouter{}

​    s.AddRouter(TestRouterObj)

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

这里写图片描述

可以看到在github.com\viphxin\xingo\fnet\msghandle.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, 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))

​                    }

​                }

​            }(i, c)

​        }

​    }

`}`

处理流程图:

这里写图片描述

这里写图片描述

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值