def lost_connection(self, server, channel): print '失去来自%s的连接' % (channel.peername, ) if channel in self.channels: user = self.channels.pop(channel).get('user') if user is not None: print '%d服%d玩家下线' % user del self.clients[user] Gcore.log_out(user[0], user[1]) #下线处理
def fight_end(self, fight_result): #内部用 '''战后处理''' # 1,将战果广播给客户端 # 2,将战果分发给各分服 # 3,更新总服的荣誉表 from core import Gcore for user in fight_result: Gcore.multicast(user, common.out(98005, fight_result[user])) RabbitMod.rm.send(98005, user[1], fight_result[user]) HonourMod.addHonour(user[0], user[1], fight_result[user]['gainHonour'], time.time()) self.zmq_sock_close() #释放端口
def sock_multicast(self, frm, content): '''用socket组播战斗信息''' from core import Gcore #0表示发起者是服务器:对该战斗对象的所有成员进行广播 if frm == 0: print '服务器发起的信息' return Gcore.multicast(self._userinfo.keys(), content) elif frm in self._userinfo: #发布者本人不广播 users = [user for user in self._userinfo if user != frm] return Gcore.multicast(users, content) else: #不是战斗成员,广播失败 return False
def checkOpt(self, channel, optId, optKey, para): '''协议号处理''' if optId == 10001: #登陆 try: tokenStr = para['tokenStr'] tokenDic = McryptMod.getTokenMsg(tokenStr) uid, sid = map(int, [tokenDic['UserId'], tokenDic['ServerId']]) print '%d服%d玩家已登陆' % (sid, uid) except Exception: print channel.peername, '登陆失败。断开channel。' return False self.clients[(uid, sid)] = {} self.clients[(uid, sid)]['channel'] = channel self.channels[channel]['user'] = (uid, sid) #登陆成功 res = {'opt_key':optKey, 'opt_id':optId, 'body':{"UserId":uid, "ServerId":sid}, 'flag':1} return res else: #非登陆 uid, sid = self.channels.get(channel, {}).get('user', (None, None)) if uid is None and sid is None: #没有登陆 return False #断开channel res = Gcore.pro_manager(uid, sid, optId, para) if isinstance(res, dict): #是字典 res['opt_key'] = optKey return res
#date:2013-5-9 #PVP服务器 from gevent import monkey; monkey.patch_all() import sys; reload(sys); sys.setdefaultencoding('utf8') import signal import os import time import gevent from gevent.pool import Pool import message from sgz import Server, Application from core import Gcore; Gcore.start() #开启核心 import common import config import McryptMod #服务器ip,端口 ADDR = (config.SERVER_IP, config.SERVER_PORT) print '进程ID:', os.getpid() print '服务器socket:', ADDR #绑定信号处理函数 gevent.signal(signal.SIGUSR1, Gcore.arena_start) #开启竞技场 gevent.signal(signal.SIGUSR2, Gcore.arena_end) #关闭竞技场 class Service(object):