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

来源:互联网 发布:男士帽子品牌知乎 编辑:程序博客网 时间:2024/06/12 19:32

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收工!理解不对的,可以指正

原创粉丝点击