def Disconnect(self, sReason): timer.Unschedule(self.m_ConnectedFlag) logger.Info("%s disconnect r=%s" % (self.m_ID, sReason)) oMgrServer = service.GetService("ServerManager") oTcpServer = oMgrServer.Get("client") oTcpServer.Disconnect(self.m_SocketID) oLinkMgr = service.GetService("LinkManager") oLinkMgr.DelLink(self.m_ID) oPlayerMgr = service.GetService('PlayerManager') oPlayerMgr.Disconnected(self.m_ProxyID, sReason)
def Disconnected(self, sReason, bForceQuit=False): if not bForceQuit: self.WaitReconnect() else: logger.Info("real disconnect %s, r=%s, force=%s" % (self.m_ID, sReason, bForceQuit)) self.OnDisconnected() service.GetService('PlayerManager').DelPlayer(self.m_ID, sReason)
def __init__(self, iSocketID): oLinkMgr = service.GetService("LinkManager") self.m_ID = oLinkMgr.UpdateID() self.m_SocketID = iSocketID self.m_ProxyID = 0 self.m_RecvData = bytes() self.m_HeartbeatFlag = "Heartbeat_%s" % (self.m_ID) self.m_ConnectedFlag = "Connected_%s" % (self.m_ID)
def TimerCheckHeartbeat(iLinkID): oLinkMgr = service.GetService("LinkManager") oLink = oLinkMgr.GetLink(iLinkID) if not oLink: return if int(time.time()) - oLink.m_Heartbeat < 10: return oLink.Disconnect("no heartbeat")
def OnLoginAccount(oLink, oAccountProtocol): if not database.CheckAccountExist(oAccountProtocol.m_User): uid = database.RegisterAccount(oAccountProtocol.m_User, oAccountProtocol.m_Password) else: ret = database.CheckAccountPassword(oAccountProtocol.m_User, oAccountProtocol.m_Password) if ret: oLink.Disconnect("check account fail %s" % str(ret)) return uid = database.RegisterAccount(oAccountProtocol.m_User, oAccountProtocol.m_Password) oPlayerMgr = service.GetService("PlayerManager") oPlayer = oPlayerMgr.CreatePlayer(oLink, uid) oPlayer.Login() oLink.ConnectedFinish()
def __init__(self, sAddress, iPort, sFlag): self.m_Address = sAddress self.m_Port = iPort self.m_Queue = Queue() self.m_SendQueue = Queue() self.m_QuitQueue = Queue() self.m_Epoll = None self.m_Sockets = {} self.m_SocketID2FD = {} self.m_SocketID = 0 self.m_ServerSocket = socket(AF_INET, SOCK_STREAM) self.m_NetThread = threading.Thread(target=NetworkThread, args=(self, )) self.m_CBFunc = {} self.m_Running = False self.m_Flag = sFlag oSMgr = service.GetService("ServerManager") oSMgr.Add(sFlag, self)
sys.path.append("../server") sys.path.append("../common") import serverapp import sys import entry import service import player class CCustomPlayer(player.CPlayer): def OnLogin(self, bRelink): import customprotocol.p_player oBaseInfo = customprotocol.p_player.P_BaseInfo() oBaseInfo.m_Uid = self.m_ID oBaseInfo.m_Name = self.GetName() self.SendProtocol(oBaseInfo) def OnDisconnected(self): pass app = serverapp.CServerApplication(sys.argv) app.SetEntry(entry.OnEntry) service.GetService("PlayerManager").SetCustomPlayerClass(CCustomPlayer) app.Listen(12349, "0.0.0.0") app.Exec() print("Bye!~")
def SendProtocol(self, oProtocol): oLinkMgr = service.GetService("LinkManager") oLink = oLinkMgr.GetLink(self.m_LinkID) if not oLink: return oLink.SendProtocol(oProtocol)
def CheckOffline(iUid): oPlayerMgr = service.GetService("PlayerManager") oPlayer = oPlayerMgr.GetPlayer(iUid) if not oPlayer: return oPlayer.Disconnected("wait reconnect failed.", True)
def SendProtocol(self, oProtocol): oMgrServer = service.GetService("ServerManager") oTcpServer = oMgrServer.Get("client") data = oProtocol.PacketData() oTcpServer.SendData(self.m_SocketID, data)
def GetLinkObjectBySocketID(iSocketID): oLinkMgr = service.GetService("LinkManager") return oLinkMgr.GetLinkBySocketID(iSocketID)
def GetLinkObject(iLinkID): oLinkMgr = service.GetService("LinkManager") return oLinkMgr.GetLink(iLinkID)
def OnConnected(iSocketID): oLinkObject = CLinkObject(iSocketID) oLinkMgr = service.GetService("LinkManager") oLinkMgr.AddLink(oLinkObject) oLinkObject.VerifyLink()
def ConnectTimeout(iLinkID): timer.Unschedule("Connected_%s" % (iLinkID)) logger.Info("connect timeout %s" % (iLinkID)) oLinkMgr = service.GetService("LinkManager") oLinkObject = oLinkMgr.GetLink(iLinkID) oLinkObject.Disconnect("timeout")