init.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. util.ErrorF("927 DiscoveryService :%+v", *sd)
  176. //不连接自己
  177. serviceCfg := service.GetServiceConfig()
  178. if sd.Type == serviceCfg.Node.Type && sd.Zone == serviceCfg.Node.Zone && sd.Index == serviceCfg.Node.Id {
  179. return
  180. }
  181. // game2会发现game20,过滤掉这种
  182. if sd.Zone != serviceCfg.Node.Zone {
  183. return
  184. }
  185. node := socket.NewServerNode(param.ServiceType, param.ServiceName, sd.Host, Queue)
  186. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  187. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  188. if msgPrcFunc != nil {
  189. msgPrcFunc(e)
  190. //nowTime := util.GetTimeMilliseconds()
  191. //msgPrcFunc(e)
  192. //delTime := util.GetTimeMilliseconds() - nowTime
  193. //if delTime > 5 {
  194. // log.Printf("deltime=%v msg=%T", util.GetTimeMilliseconds()-nowTime, e.(rocommon.ProcEvent).Msg())
  195. //}
  196. }
  197. })
  198. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  199. opt.SetSocketBuff(40960*1024, 40960*1024, true)
  200. //15s无读写断开,服务器之间已经添加心跳来位置读写(非调试模式启动)
  201. if !service.DebugMode {
  202. opt.SetSocketDeadline(time.Second*15, time.Second*15)
  203. }
  204. }
  205. //通过etcd服务器发现来处理,如果发现重复的节点会解除当前正在重连或者执行的节点
  206. node.(rocommon.TCPConnector).SetReconnectTime(3 * time.Second)
  207. //如果连接的是服务器的节点那么需要加入当前本节点的信息,用来处理服务器节点之间的身份验证
  208. // ServerTCPEventHook中会使用
  209. node.(rocommon.ContextSet).SetContextData("sid", sd, "sid")
  210. //添加到服务器发现管理器中
  211. mn.AddNode(sd, node)
  212. //util.DebugF("NewServerNode:%v", node.TypeOfName())
  213. node.Start()
  214. //todo...添加到总的服务器管理中
  215. })
  216. if param.ServiceName != model.SERVICE_NODE_TYPE_CROSSROUTER_STR &&
  217. param.ServiceName != model.SERVICE_NODE_TYPE_CROSSSERVER_STR {
  218. service.InitServiceStartupTime(param.DiscoveryServiceZone)
  219. }
  220. return nil
  221. }
  222. // gate监听客户端处理
  223. func CreateClientAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  224. if param.ServiceType == "" {
  225. param.ServiceType = "tcpAcceptor"
  226. }
  227. //不需要要消息处理队列
  228. //node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  229. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  230. //没有具体的消息处理逻辑
  231. //socket.SetProcessorRPC(node, param.ProcName, nil)
  232. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  233. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  234. if msgPrcFunc != nil {
  235. msgPrcFunc(e)
  236. }
  237. })
  238. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  239. opt.SetSocketBuff(40960, 40960, true)
  240. //40秒无读,30秒无写断开 如果没有心跳了超时直接断开,调试期间可以不加
  241. // 通过该方法来模拟心跳保持连接
  242. if serverConfig.Node.Reconnect > 0 {
  243. opt.SetSocketDeadline(time.Second*40, time.Second*40)
  244. //读/写协程没有过滤超时事件,发生了操时操作就断开连接
  245. }
  246. }
  247. property := node.(rocommon.ServerNodeProperty)
  248. //服务器类型节点,区号,区号中的当前编号
  249. property.SetServerType(serverConfig.Node.Type)
  250. property.SetZone(serverConfig.Node.Zone)
  251. property.SetIndex(serverConfig.Node.Id)
  252. //session uuid 创建时的key
  253. node.(rocommon.SessionMagExport).SetUuidCreateKey(serverConfig.Node.Id)
  254. node.Start()
  255. //外层通过该对象来获取监听器上接收的链接
  256. model.ClientSessionMag = node.(socket.SessionManager)
  257. //注册到服务器发现etcd中
  258. service.ETCDRegister(node)
  259. //todo...添加到总的服务器管理中
  260. return node
  261. }
  262. func CreateHttpConnector(param ServiceParam) rocommon.ServerNode {
  263. if param.ServiceType == "" {
  264. param.ServiceType = "httpConnector"
  265. }
  266. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  267. return node
  268. }
  269. //func CreateHttpAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  270. // if param.ServiceType == "" {
  271. // param.ServiceType = "httpAcceptor"
  272. // }
  273. // node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  274. //
  275. // msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  276. // socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  277. // if matcher, ok := e.Session().(http.RequestProc); ok {
  278. // msg.WebGMMsgProcess(matcher, e)
  279. // } else {
  280. // msgPrcFunc(e)
  281. // }
  282. // })
  283. //
  284. // node.Start()
  285. //
  286. // return node
  287. //}
  288. // gate监听客户端处理
  289. func CreateWebSocketAcceptor(param ServiceParam, serverConfig service.ConfigServerNode) rocommon.ServerNode {
  290. if param.ServiceType == "" {
  291. param.ServiceType = "wsAcceptor"
  292. }
  293. //不需要要消息处理队列
  294. //node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  295. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, Queue)
  296. //没有具体的消息处理逻辑
  297. //socket.SetProcessorRPC(node, param.ProcName, nil)
  298. msgPrcFunc := serverproto.GetMessageHandler(param.ServiceName)
  299. socket.SetProcessorRPC(node, param.ProcName, func(e rocommon.ProcEvent) {
  300. if msgPrcFunc != nil {
  301. msgPrcFunc(e)
  302. }
  303. })
  304. if opt, ok := node.(rocommon.TCPSocketOption); ok {
  305. opt.SetSocketBuff(11264, 11264, true)
  306. //40秒无读,30秒无写断开 如果没有心跳了超时直接断开,调试期间可以不加
  307. // 通过该方法来模拟心跳保持连接
  308. if serverConfig.Node.Reconnect > 0 {
  309. opt.SetSocketDeadline(time.Second*40, time.Second*40)
  310. //读/写协程没有过滤超时事件,发生了操时操作就断开连接
  311. }
  312. }
  313. property := node.(rocommon.ServerNodeProperty)
  314. //服务器类型节点,区号,区号中的当前编号
  315. property.SetServerType(serverConfig.Node.Type)
  316. property.SetZone(serverConfig.Node.Zone)
  317. property.SetIndex(serverConfig.Node.Id)
  318. //session uuid 创建时的key
  319. node.(rocommon.SessionMagExport).SetUuidCreateKey(serverConfig.Node.Id)
  320. node.Start()
  321. //外层通过该对象来获取监听器上接收的链接
  322. model.ClientSessionMag = node.(socket.SessionManager)
  323. //注册到服务器发现etcd中
  324. service.ETCDRegister(node)
  325. //todo...添加到总的服务器管理中
  326. return node
  327. }