def createPool(): if not DBPool.pool: from sgLib.setting import Setting if Setting.getDatabase(): DB_SELECT = Setting.getDatabase() #在gateway设置的数据库 不配置的 else: DB_SELECT = cfg.DB_SELECT print '>>>创建连接池 ', DB_SELECT DBPool.pool = PooledDB.PooledDB(umysqldb, cfg.DBPoolCfg['mincached'], cfg.DBPoolCfg['maxcached'], cfg.DBPoolCfg['maxshared'], cfg.DBPoolCfg['maxconnections'], cfg.DBPoolCfg['blocking'], ping=0, host=cfg.DB_HOST, user=cfg.DB_USR, passwd=cfg.DB_PWD, db=DB_SELECT, port=int(cfg.DB_PORT), charset=cfg.DB_CHARSET) #创建连接池
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_GATEWAYSERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(('0.0.0.0', int(port)), handle=service.handle, spawn=_pool.spawn) #=========================================================== import signal logger = Gcore.getLogger('server', 'server') def close_server(): logger.warning('I got a signal.SIGUSR1 going to stop battle server') server.stop(180) def reload_server(): Gcore.reload() logger.warning('I got a signal.SIGUSR2, battle server Gcore reload()') def hook_signal(): '''捕获信号''' if os.name == 'nt': return #信号10-关服 gevent.signal(signal.SIGUSR1, close_server) #信号12-重载 gevent.signal(signal.SIGUSR2, reload_server) hook_signal() #===================================================================== message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print '=' * 50 print '=', ' ' * 15, 'Server Started', ' ' * 15, '=' print '=' * 50 app.run() if pid_file: with open(pid_file, 'wb') as fd: fd.write('0')
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_GATEWAYSERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(('0.0.0.0', int(port)), handle=service.handle, spawn=_pool.spawn) message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print '=' * 50 print '=', ' '*15, 'Server Started', ' '*15, '=' print '=' * 50 app.run()
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_BATTLESERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(('0.0.0.0', int(port) ), handle=service.handle, spawn=_pool.spawn) #=========================================================== import signal logger = Gcore.getLogger('server', 'server') def close_server(): logger.warning('I got a signal.SIGUSR1 going to stop server') server.stop(180) def reload_server(): Gcore.reload() logger.warning('I got a signal.SIGUSR2, Gcore reload()') def hook_signal(): '''捕获信号''' if os.name == 'nt': return #信号10-关服 gevent.signal(signal.SIGUSR1, close_server) #信号12-重载 gevent.signal(signal.SIGUSR2, reload_server) hook_signal() #===================================================================== message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print '=' * 50 print '=', ' '*15, 'Server Started', ' '*15, '=' print '=' * 50 app.run() if pid_file: with open(pid_file, 'wb') as fd: fd.write('0')
def handle(self, channel, request): #print 'ps '+ str(Cfg.CFG_BATTLESERV_PORT)+' Echo',request recData = comm.decodePacket(request) #(协议号,内容) if not recData: #参数异常 channel._send("xxxx Wu ask send xxxx") return ckey = channel.getpeername() if not ckey in self.Clients: self.Clients[ckey] = {} self.Clients[ckey]['uid'] = 0 self.Clients[ckey]['Channel'] = channel if Cfg.TEST: #获取开发人员指定用户ID uid_tmp = Setting.developer(ckey[0]) if uid_tmp: self.Clients[ckey]['uid'] = uid_tmp #print 'get developer uid',uid_tmp self.checkOpt(ckey, recData[0], recData[1], recData[2])
def _notifyServer(self,optId,para={}): '''通知服务器''' from sgCfg import config SECRET_DATA_OPEN = config.SECRET_DATA_OPEN #是否开启数据加密和压缩 from sgLib.pyMcrypt import encode from sgLib.setting import Setting import gevent.socket import json s = gevent.socket.socket() port = Setting.getGatewayPort() if not port: port = config.CFG_GATEWAYSERV_PORT else: port -= 1 #战斗端口起动的,上一个端口,调试才用到 print 'config.CFG_GATEWAYSERV_HOST',config.CFG_GATEWAYSERV_HOST print 'port',port sock=(config.CFG_GATEWAYSERV_HOST, int(port) ) #8082 8888 try: s.connect(sock) except gevent.socket.error, e: raise
def handle(self, channel, request): ckey = channel.getpeername() recData = comm.decodePacket(request) #(协议号,内容) if not recData: #参数异常 channel._send("xxxx Wu ask send xxxx") return if not ckey in self.Clients: self.Clients[ckey] = {} self.Clients[ckey]['uid'] = 0 self.Clients[ckey]['Channel'] = channel optId = recData[0] optKey = recData[1] para = recData[2] if 1001 <= optId <= 9999: #系统内部预留 if optId == 1001: #聊天监控 from sgLib.pyMcrypt import checkListenKey if checkListenKey(para.get('keyWord')): print '聊天接入成功' Gcore.setListenerData(ckey, channel, para.get('chatChannel')) response = Gcore.out(1001) channel._send(comm.json_encode(response)) else: print '聊天接入失败' else: if Cfg.TEST: #获取开发人员指定用户ID uid_tmp = Setting.developer(ckey[0]) if uid_tmp: self.Clients[ckey]['uid'] = uid_tmp self.checkOpt(ckey, optId, optKey, para)
def handle(self, channel, request): ckey = channel.getpeername() recData = comm.decodePacket(request) #(协议号,内容) if not recData: #参数异常 channel._send("xxxx Wu ask send xxxx") return if not ckey in self.Clients: self.Clients[ckey] = {} self.Clients[ckey]['uid'] = 0 self.Clients[ckey]['Channel'] = channel optId = recData[0] optKey = recData[1] para = recData[2] if 1001<=optId<=9999: #系统内部预留 if optId == 1001: #聊天监控 from sgLib.pyMcrypt import checkListenKey if checkListenKey(para.get('keyWord')): print '聊天接入成功' Gcore.setListenerData(ckey, channel, para.get('chatChannel')) response = Gcore.out(1001) channel._send( comm.json_encode(response) ) else: print '聊天接入失败' else: if Cfg.TEST: #获取开发人员指定用户ID uid_tmp = Setting.developer(ckey[0]) if uid_tmp: self.Clients[ckey]['uid'] = uid_tmp self.checkOpt(ckey, optId, optKey, para)
def handle(self, channel, request): # print '='*90 # comm.trace('<font color=green>Echo at Port 8888</font>') # print request ckey = channel.getpeername() recData = comm.decodePacket(request) #(协议号,内容) if not recData: #参数异常 channel._send("xxxx Wu ask send xxxx") return if not ckey in self.Clients: self.Clients[ckey] = {} self.Clients[ckey]['uid'] = 0 self.Clients[ckey]['Channel'] = channel if Cfg.TEST: #获取开发人员指定用户ID uid_tmp = Setting.developer(ckey[0]) if uid_tmp: self.Clients[ckey]['uid'] = uid_tmp #print 'get developer uid',uid_tmp self.checkOpt(ckey, recData[0], recData[1], recData[2])
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_GATEWAYSERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(('0.0.0.0', int(8099)), handle=service.handle, spawn=_pool.spawn) #=========================================================== import signal #对linux一些信号进行捕获 def hook_signal(): if os.name != "nt": signal.signal(signal.SIGBUS, signal_handle) signal.signal(signal.SIGTERM, signal_handle) signal.signal(signal.SIGUSR1, signal_handle) signal.signal(signal.SIGUSR2, signal_handle) signal.signal(signal.SIGABRT, signal_handle) #屏蔽与终端控制有关的信号 signal.signal(signal.SIGTTOU, signal.SIG_IGN) signal.signal(signal.SIGTTIN, signal.SIG_IGN) signal.signal(signal.SIGTSTP, signal.SIG_IGN) signal.signal(signal.SIGHUP, signal.SIG_IGN) #linux信号处理函数 def signal_handle(sign_num, frame): import signal logger = Gcore.getLogger('system', 'server') if sign_num == signal.SIGBUS: server.stop(180) logger.warning('I got a signal.SIGBUS server stop') sys.exit(-1) elif sign_num == signal.SIGTERM: server.stop(180) logger.warning('I got a signal.SIGTERM server stop') elif sign_num == signal.SIGHUP: logger.warning('I got a signal.SIGHUP pass') pass elif sign_num == signal.SIGUSR1: #用户发送停止服务的指令 10 logger.warning('I got a signal.SIGUSR1 going to stop server') server.stop(180) elif sign_num == signal.SIGUSR2: # 12 Gcore.reload() logger.warning('I got a signal.SIGUSR2, Gcore reload()') hook_signal() #===================================================================== message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print '=' * 50 print '=', ' ' * 15, 'Server Started', ' ' * 15, '=' print '=' * 50 app.run() Gcore.log.warning('I got a signal.SIGUSR1 have stoped server') if pid_file: with open(pid_file, 'wb') as fd: fd.write('0')
help='保存该进程的PID的文件') option, args = parser.parse_args() pid_file = option.pid_file #pid文件名 if pid_file: pid_file = dirname(abspath(__file__)) + '/' + pid_file with open(pid_file, 'wb') as fd: fd.write(str(os.getpid())) #python gateway.py 8084 2 开启2服 - 8084端口 数据库 gamesg2 (可省) #python gateway.py 8086 3 开启3服 - 8086端口 数据库 gamesg3 (未开) from sgLib.setting import Setting if args: port = args[0] Setting.setGatewayPort(port) if len(args) >= 2: Setting.setDatabase('gamesg%s' % args[1]) else: port = Cfg.CFG_GATEWAYSERV_PORT class Service(object): def __init__(self): #self.validate = Validate() self.Clients = {} #所有用户信息库 Gcore.objCfg.loadAllCfg() #读取所有配置 Gcore.IsServer = True #定义为服务器核心 Gcore.startNotice() #开启公告自动发布协程
#-------------------------------------------------------------------- from root_config import system_root import sys;sys.path.insert(0,'system_root') #把项目根目录加入默认库路径 import sgCfg.config as config serverid = config.CFG_SERVER_ID #本服serverid serverid = int(sys.argv[1]) if len(sys.argv) > 1 else serverid print 'ServerId', serverid from sgLib.setting import Setting try: if serverid == 1: database = 'gamesg' else: database = 'gamesg%s'%serverid Setting.setDatabase(database) except Exception, e: pass #-------------------------------------------------------------------- from sgLib.mqManager import MqManager print >>sys.stdout, 'pid is %s' % os.getpid() ##### global variables ##### recv_conn = None gpool = Pool(100) #coroutine pool ##### end define global variables ##### def ensure_chan(conn):
from os.path import dirname,abspath system_root = dirname(dirname( abspath( __file__ ) ) ) #定义上层目录为根目录 import sys;sys.path.insert(0,system_root) #把项目根目录加入默认库路径 import os pid = os.getpid() pidfile = dirname( abspath( __file__ ) )+'/gateway.pid' open(pidfile,'w').write(str(pid)) #python gateway.py 8084 2 开启2服 - 8084端口 数据库 gamesg2 (可省) #python gateway.py 8086 3 开启3服 - 8086端口 数据库 gamesg3 (未开) from sgLib.setting import Setting try: if sys.argv: port = int(sys.argv[1]) Setting.setGatewayPort(port) if len(sys.argv)>2: database = 'gamesg%s'%sys.argv[2] Setting.setDatabase(database) except Exception, e: print e pass #== START CONTENT START == import time import gevent.pool import message import sgCfg.config as Cfg import sgLib.common as comm
#-------------------------------------------------------------------- from root_config import system_root import sys sys.path.insert(0, 'system_root') #把项目根目录加入默认库路径 import sgCfg.config as config serverid = config.CFG_SERVER_ID #本服serverid serverid = int(sys.argv[1]) if len(sys.argv) > 1 else serverid print 'ServerId', serverid from sgLib.setting import Setting try: if serverid == 1: database = 'gamesg' else: database = 'gamesg%s' % serverid Setting.setDatabase(database) except Exception, e: pass #-------------------------------------------------------------------- from sgLib.mqManager import MqManager print >> sys.stdout, 'pid is %s' % os.getpid() ##### global variables ##### recv_conn = None gpool = Pool(100) #coroutine pool ##### end define global variables #####
parser = OptionParser() parser.add_option('-p', '--pid-file', action='store', dest='pid_file', help='保存该进程的PID的文件') option, args = parser.parse_args() pid_file = option.pid_file if pid_file: pid_file = abspath( __file__ )+'.pid' with open(pid_file, 'wb') as fd: fd.write(str(os.getpid())) from sgLib.setting import Setting if args: port = args[0] Setting.setGatewayPort(port) if len(args) >= 2: Setting.setDatabase('gamesg%s'%args[1]) else: port = Cfg.CFG_GATEWAYSERV_PORT #python gateway.py 8084 2 开启2服 - 8084端口 数据库 gamesg2 (可省) #python gateway.py 8086 3 开启3服 - 8086端口 数据库 gamesg3 (未开) #== START CONTENT START == import time import gevent.pool import message
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_GATEWAYSERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(('0.0.0.0', int(port)), handle=service.handle, spawn=_pool.spawn) #=========================================================== import signal ''' #对linux一些信号进行捕获 def hook_signal(): if os.name != "nt": gevent.signal(signal.SIGBUS,signal_handle) gevent.signal(signal.SIGTERM, signal_handle) gevent.signal(signal.SIGUSR1, signal_handle) gevent.signal(signal.SIGUSR2, signal_handle) gevent.signal(signal.SIGABRT, signal_handle) #屏蔽与终端控制有关的信号 gevent.signal(signal.SIGTTOU,signal.SIG_IGN) gevent.signal(signal.SIGTTIN,signal.SIG_IGN) gevent.signal(signal.SIGTSTP,signal.SIG_IGN) gevent.signal(signal.SIGHUP ,signal.SIG_IGN) #linux信号处理函数 def signal_handle(sign_num, frame): import signal logger = Gcore.getLogger('system', 'server') if sign_num == signal.SIGBUS: server.stop(180) logger.warning('I got a signal.SIGBUS server stop') sys.exit(-1) elif sign_num == signal.SIGTERM: server.stop(180) logger.warning('I got a signal.SIGTERM server stop') elif sign_num == signal.SIGHUP: logger.warning('I got a signal.SIGHUP pass') pass elif sign_num == signal.SIGUSR1: #用户发送停止服务的指令 10 logger.warning('I got a signal.SIGUSR1 going to stop server') server.stop(180) elif sign_num == signal.SIGUSR2: # 12 Gcore.reload() logger.warning('I got a signal.SIGUSR2, Gcore reload()') ''' def close_server(): logger.warning('I got a signal.SIGUSR1 going to stop server') server.stop(180) def reload_server(): Gcore.reload() logger.warning('I got a signal.SIGUSR2, Gcore reload()') def hook_signal(): '''捕获信号''' if os.name == 'nt': return #信号10-关服 gevent.signal(signal.SIGUSR1, close_server) #信号12-重载 gevent.signal(signal.SIGUSR2, reload_server) hook_signal() #===================================================================== message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print '=' * 50 print '=', ' '*15, 'Server Started', ' '*15, '=' print '=' * 50 app.run() Gcore.log.warning('I got a signal.SIGUSR1 have stoped server') if pid_file: with open(pid_file, 'wb') as fd: fd.write('0')
def main(): port = Setting.getGatewayPort() if not port: port = Cfg.CFG_GATEWAYSERV_PORT _pool = gevent.pool.Pool() service = Service() server = Server(("0.0.0.0", int(8099)), handle=service.handle, spawn=_pool.spawn) # =========================================================== import signal # 对linux一些信号进行捕获 def hook_signal(): if os.name != "nt": signal.signal(signal.SIGBUS, signal_handle) signal.signal(signal.SIGTERM, signal_handle) signal.signal(signal.SIGUSR1, signal_handle) signal.signal(signal.SIGUSR2, signal_handle) signal.signal(signal.SIGABRT, signal_handle) # 屏蔽与终端控制有关的信号 signal.signal(signal.SIGTTOU, signal.SIG_IGN) signal.signal(signal.SIGTTIN, signal.SIG_IGN) signal.signal(signal.SIGTSTP, signal.SIG_IGN) signal.signal(signal.SIGHUP, signal.SIG_IGN) # linux信号处理函数 def signal_handle(sign_num, frame): import signal logger = Gcore.getLogger("system", "server") if sign_num == signal.SIGBUS: server.stop(180) logger.warning("I got a signal.SIGBUS server stop") sys.exit(-1) elif sign_num == signal.SIGTERM: server.stop(180) logger.warning("I got a signal.SIGTERM server stop") elif sign_num == signal.SIGHUP: logger.warning("I got a signal.SIGHUP pass") pass elif sign_num == signal.SIGUSR1: # 用户发送停止服务的指令 10 logger.warning("I got a signal.SIGUSR1 going to stop server") server.stop(180) elif sign_num == signal.SIGUSR2: # 12 Gcore.reload() logger.warning("I got a signal.SIGUSR2, Gcore reload()") hook_signal() # ===================================================================== message.sub(Server.NEW_CONNECTION, service.new_connection) message.sub(Server.LOST_CONNECTION, service.lost_connection) app = Application(server) print "=" * 50 print "=", " " * 15, "Server Started", " " * 15, "=" print "=" * 50 app.run() Gcore.log.warning("I got a signal.SIGUSR1 have stoped server") if pid_file: with open(pid_file, "wb") as fd: fd.write("0")