我学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里面双击即可运行;
%~dp0
win下的当前目录
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 0
- 我学xingo golang服务器之-XingoApi调用
- 我学xingo golang服务器之-xingo_demo server.go start
- 我学xingo golang服务器之-xingo的单进程,集群服务器的配置和初始化过程
- 我学xingo golang服务器之-xingo_demo 服务器游戏逻辑处理初始化过程
- 我学xingo golang服务器之-Unity3d c# 协议api分解
- 新手学xingo golang服务器之-修改xingo 支持字符协议(一)
- 新手学xingo golang服务器之-修改xingo 支持字符协议(二)
- 我学xingo golang服务器之-Unity3d c#多玩家同屏出生过程和AI处理分解
- 新手学xingo golang服务器之-golang和unity3d的Protobuf生成(三)
- GoLang之搭建Web服务器
- 我学你也学之WEB服务器篇——安装JDK和APACHE服务器
- 系统调用跟我学之wait, waitpid函数
- golang中的RPC调用服务器方法
- 系统调用跟我学
- 系统调用跟我学
- 系统调用跟我学
- 系统调用跟我学
- Struts之我学
- Gson解析Json数据
- hibernate_持久化对象状态、方法
- 图象平滑消噪处理——均值滤波
- Android Service完全解析,关于服务你所需知道的一切(下)
- POJ1364[king]
- 我学xingo golang服务器之-XingoApi调用
- linux下查看某个文件属于哪个包
- Spark Streaming Backpressure分析
- UP board and Touch IC connection
- MYSQL如何开启远程访问连接HIVE
- lua基础---函数
- JAVA企业面试题精选 Java基础 31-40
- [技巧]UIView的hidden和alpha的妙用
- unity3d引用和实例化对象的方法