init.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package baseserver
  2. import (
  3. "rocommon"
  4. _ "rocommon/rpc"
  5. "rocommon/service"
  6. "rocommon/socket"
  7. "rocommon/util"
  8. "roserver/baseserver/model"
  9. "roserver/serverproto"
  10. "runtime"
  11. "sort"
  12. "strconv"
  13. "time"
  14. )
  15. var Queue rocommon.NetEventQueue
  16. type ServiceParam struct {
  17. ServiceName string
  18. ServiceType string //tcpAcceptor /tcpConnector
  19. ProcName string
  20. LisAddr string
  21. DiscoveryServiceName string //用于服务器发现
  22. DiscoveryServiceZone int //用于服务器发现
  23. }
  24. //loadConfig是否需要加载配置文件, update是否有主循环
  25. func Init(serverName string, loadConfig func(), update rocommon.UpdateModule) {
  26. //设置携程使用的线程数量(不会强制性生效)
  27. //The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously.
  28. // There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count
  29. // against the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes the limit.
  30. runtime.GOMAXPROCS(runtime.NumCPU() / 2)
  31. //dgnet -> init.go
  32. service.Init(serverName)
  33. util.InfoF("serverName serverName=%v finish", serverName)
  34. util.InfoF("////////////////////////Service GameVersion=%v DebugMode=%v",
  35. int32(serverproto.GameVersion_GameVersion_Main), service.DebugMode)
  36. if loadConfig != nil {
  37. loadConfig()
  38. }
  39. //事件回调队列
  40. Queue = service.NewEventQueue()
  41. //设置更新操作
  42. if update != nil {
  43. Queue.AttachUpdateModule(update)
  44. }
  45. Queue.StartQueue()
  46. //链接服务器发现 etcd 在service.Init中已经实现
  47. //log.Printf("server init ok...")
  48. util.InfoF("server init ok...")
  49. }
  50. func Wait() {
  51. //todo...
  52. //监听退出信号
  53. service.WaitExitSignal()
  54. Queue.StopQueue()
  55. Queue.Wait()
  56. }
  57. func Exit(node rocommon.ServerNode) {
  58. //todo... 停止所有在监听的协程
  59. util.InfoF("exit...")
  60. //停止etcd
  61. if node != nil {
  62. node.Stop()
  63. service.ETCDUnregister(node)
  64. }
  65. service.GetServiceDiscovery().Close()
  66. }
  67. var idxMap = map[int]*model.PerformKVTimeSt{}
  68. var performTime uint64 = 0
  69. func KVTimeTest(nowTime uint64) {
  70. var printfList []model.PerformKVTimeSt
  71. if performTime <= 0 {
  72. performTime = nowTime
  73. } else if nowTime-performTime >= 1000 {
  74. performTime = nowTime
  75. for _, val := range idxMap {
  76. printfList = append(printfList, *val)
  77. }
  78. }
  79. if len(printfList) > 0 {
  80. sort.Slice(printfList, func(i, j int) bool {
  81. return printfList[i].AckMsgId < printfList[j].AckMsgId
  82. })
  83. printfListStr := ""
  84. for idx := 0; idx < len(printfList); idx++ {
  85. //msgIdStr := strconv.Itoa(int(printfList[idx].AckMsgId))
  86. tmpTime := float64(printfList[idx].TotalTime) / float64(printfList[idx].TotalNum)
  87. tmpPerNum := printfList[idx].NowTime - printfList[idx].BeginTime
  88. if tmpPerNum > 0 {
  89. tmpPerNum = uint64(printfList[idx].TotalNum) * 1000 / tmpPerNum
  90. } else {
  91. tmpPerNum = uint64(printfList[idx].TotalNum)
  92. }
  93. printfListStr += " \n" + printfList[idx].MsgName + ":" +
  94. strconv.FormatInt(int64(tmpTime), 10) + "(ms) | " +
  95. strconv.FormatInt(int64(tmpPerNum), 10) + "(req-ack/s) | " +
  96. strconv.Itoa(int(printfList[idx].TotalNum)) + "(total)"
  97. }
  98. util.DebugF("printfListStr=%v", printfListStr)
  99. }
  100. }
  101. func CreateAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  102. if param.ServiceType == "" {
  103. param.ServiceType = "tcpAcceptor"
  104. }
  105. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  106. //消息处理函数,根据给定的服务器类型来获取,例如gate,game,db,auth
  107. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  108. //通过QueueEventCall函数加入直接函数的回调队列中
  109. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  110. if msgPrcFunc != nil {
  111. msgPrcFunc(e)
  112. //nowTime := util.GetTimeMilliseconds()
  113. //msgPrcFunc(e)
  114. //nowTime1 := util.GetTimeMilliseconds()
  115. //switch in := e.(type) {
  116. //case *model.RecvGateMsgEvent:
  117. // //tmpInfo := rocommon.MessageInfoByMsg(in.Msg())
  118. // //if tmpInfo != nil && tmpInfo.ID == 1066 {
  119. // // delTime := nowTime1 - nowTime
  120. // // recvTime := nowTime - in.KvTime
  121. // // util.DebugF("kvtime cid=%v idx=%v msg=%v deltime=%v recvtime=%v",
  122. // // in.ClientID, idxMap[in.ClientID], tmpInfo.ID, delTime, recvTime)
  123. // // //if delTime > 0 {
  124. // // // util.DebugF("kvtime cid=%v idx=%v deltime=%v msg=%T", in.ClientID, idxMap[in.ClientID], delTime, tmpInfo.ID)
  125. // // //}
  126. // //}
  127. // tmpInfo := rocommon.MessageInfoByMsg(in.Msg())
  128. // if tmpInfo != nil {
  129. // gateGameTime := nowTime - in.KvTime
  130. // gateGameProcTime := nowTime1 - in.KvTime
  131. // idxItem, ok := idxMap[tmpInfo.ID]
  132. // if ok {
  133. // idxItem.TotalNum++
  134. // idxItem.TotalTime += gateGameProcTime + gateGameTime
  135. // idxItem.NowTime = nowTime1
  136. // } else {
  137. // idxItem = &model.PerformKVTimeSt{
  138. // TotalNum: 1,
  139. // TotalTime: gateGameProcTime + gateGameTime,
  140. // MsgName: tmpInfo.Type.Name(),
  141. // BeginTime: nowTime1,
  142. // NowTime: nowTime1,
  143. // AckMsgId: int32(tmpInfo.ID),
  144. // }
  145. // }
  146. // idxMap[tmpInfo.ID] = idxItem
  147. // KVTimeTest(nowTime1)
  148. // }
  149. //}
  150. }
  151. })
  152. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  153. opt.SetSocketBuff(40960*1024, 40960*1024, true)
  154. }
  155. property := node.(rocommon.ServerNodeProperty)
  156. //服务器类型节点,区号,区号中的当前编号
  157. property.SetServerType(serverConfig.Node.Type)
  158. property.SetZone(serverConfig.Node.Zone)
  159. property.SetIndex(serverConfig.Node.Id)
  160. //session uuid 创建时的key
  161. node.(rocommon.SessionMagExport).SetUuidCreateKey(serverConfig.Node.Id)
  162. node.Start()
  163. //注册到服务器发现etcd中
  164. service.ETCDRegister(node)
  165. //todo...添加到总的服务器管理中
  166. return node
  167. }
  168. func CreateConnector(param ServiceParam) rocommon.ServerNode {
  169. if param.ServiceType == "" {
  170. param.ServiceType = "tcpConnector"
  171. }
  172. //通过服务器发现ETCD来实现连接
  173. service.DiscoveryService(param.DiscoveryServiceName, param.DiscoveryServiceZone,
  174. func(mn service.MultiServerNode, sd *service.ETCDServiceDesc) {
  175. //不连接自己
  176. serviceCfg := service.GetServiceConfig()
  177. if sd.Type == serviceCfg.Node.Type && sd.Zone == serviceCfg.Node.Zone && sd.Index == serviceCfg.Node.Id {
  178. return
  179. }
  180. node := socket.NewServerNode(param.ServiceType, param.ServiceName, sd.Host, Queue)
  181. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  182. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  183. if msgPrcFunc != nil {
  184. msgPrcFunc(e)
  185. //nowTime := util.GetTimeMilliseconds()
  186. //msgPrcFunc(e)
  187. //delTime := util.GetTimeMilliseconds() - nowTime
  188. //if delTime > 5 {
  189. // log.Printf("deltime=%v msg=%T", util.GetTimeMilliseconds()-nowTime, e.(rocommon.ProcEvent).Msg())
  190. //}
  191. }
  192. })
  193. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  194. opt.SetSocketBuff(40960*1024, 40960*1024, true)
  195. //15s无读写断开,服务器之间已经添加心跳来位置读写(非调试模式启动)
  196. if !service.DebugMode {
  197. opt.SetSocketDeadline(time.Second*15, time.Second*15)
  198. }
  199. }
  200. //通过etcd服务器发现来处理,如果发现重复的节点会解除当前正在重连或者执行的节点
  201. node.(rocommon.TCPConnector).SetReconnectTime(3 * time.Second)
  202. //如果连接的是服务器的节点那么需要加入当前本节点的信息,用来处理服务器节点之间的身份验证
  203. // ServerTCPEventHook中会使用
  204. node.(rocommon.ContextSet).SetContextData("sid", sd, "sid")
  205. //添加到服务器发现管理器中
  206. mn.AddNode(sd, node)
  207. //util.DebugF("NewServerNode:%v", node.TypeOfName())
  208. node.Start()
  209. //todo...添加到总的服务器管理中
  210. })
  211. if param.ServiceName != model.SERVICE_NODE_TYPE_CROSSROUTER_STR &&
  212. param.ServiceName != model.SERVICE_NODE_TYPE_CROSSSERVER_STR {
  213. service.InitServiceStartupTime(param.DiscoveryServiceZone)
  214. }
  215. return nil
  216. }
  217. //gate监听客户端处理
  218. func CreateClientAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  219. if param.ServiceType == "" {
  220. param.ServiceType = "tcpAcceptor"
  221. }
  222. //不需要要消息处理队列
  223. //node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  224. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  225. //没有具体的消息处理逻辑
  226. //socket.SetProcessorRPC(node, param.ProcName, nil)
  227. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  228. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  229. if msgPrcFunc != nil {
  230. msgPrcFunc(e)
  231. }
  232. })
  233. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  234. opt.SetSocketBuff(40960, 40960, true)
  235. //40秒无读,30秒无写断开 如果没有心跳了超时直接断开,调试期间可以不加
  236. // 通过该方法来模拟心跳保持连接
  237. if serverConfig.Node.Reconnect > 0 {
  238. opt.SetSocketDeadline(time.Second*40, time.Second*40)
  239. //读/写协程没有过滤超时事件,发生了操时操作就断开连接
  240. }
  241. }
  242. property := node.(rocommon.ServerNodeProperty)
  243. //服务器类型节点,区号,区号中的当前编号
  244. property.SetServerType(serverConfig.Node.Type)
  245. property.SetZone(serverConfig.Node.Zone)
  246. property.SetIndex(serverConfig.Node.Id)
  247. //session uuid 创建时的key
  248. node.(rocommon.SessionMagExport).SetUuidCreateKey(serverConfig.Node.Id)
  249. node.Start()
  250. //外层通过该对象来获取监听器上接收的链接
  251. model.ClientSessionMag = node.(socket.SessionManager)
  252. //注册到服务器发现etcd中
  253. service.ETCDRegister(node)
  254. //todo...添加到总的服务器管理中
  255. return node
  256. }
  257. func CreateHttpConnector(param ServiceParam) rocommon.ServerNode {
  258. if param.ServiceType == "" {
  259. param.ServiceType = "httpConnector"
  260. }
  261. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  262. return node
  263. }
  264. //func CreateHttpAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  265. // if param.ServiceType == "" {
  266. // param.ServiceType = "httpAcceptor"
  267. // }
  268. // node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  269. //
  270. // msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  271. // socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  272. // if matcher, ok := e.Session().(http.RequestProc); ok {
  273. // msg.WebGMMsgProcess(matcher, e)
  274. // } else {
  275. // msgPrcFunc(e)
  276. // }
  277. // })
  278. //
  279. // node.Start()
  280. //
  281. // return node
  282. //}
  283. //gate监听客户端处理
  284. func CreateWebSocketAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  285. if param.ServiceType == "" {
  286. param.ServiceType = "wsAcceptor"
  287. }
  288. //不需要要消息处理队列
  289. //node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  290. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  291. //没有具体的消息处理逻辑
  292. //socket.SetProcessorRPC(node, param.ProcName, nil)
  293. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  294. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  295. if msgPrcFunc != nil {
  296. msgPrcFunc(e)
  297. }
  298. })
  299. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  300. opt.SetSocketBuff(11264, 11264, true)
  301. //40秒无读,30秒无写断开 如果没有心跳了超时直接断开,调试期间可以不加
  302. // 通过该方法来模拟心跳保持连接
  303. if serverConfig.Node.Reconnect > 0 {
  304. opt.SetSocketDeadline(time.Second*40, time.Second*40)
  305. //读/写协程没有过滤超时事件,发生了操时操作就断开连接
  306. }
  307. }
  308. property := node.(rocommon.ServerNodeProperty)
  309. //服务器类型节点,区号,区号中的当前编号
  310. property.SetServerType(serverConfig.Node.Type)
  311. property.SetZone(serverConfig.Node.Zone)
  312. property.SetIndex(serverConfig.Node.Id)
  313. //session uuid 创建时的key
  314. node.(rocommon.SessionMagExport).SetUuidCreateKey(serverConfig.Node.Id)
  315. node.Start()
  316. //外层通过该对象来获取监听器上接收的链接
  317. model.ClientSessionMag = node.(socket.SessionManager)
  318. //注册到服务器发现etcd中
  319. service.ETCDRegister(node)
  320. //todo...添加到总的服务器管理中
  321. return node
  322. }