def _delconnection(sockfileno): ''' 删除连接 :return: ''' Lock2(ServerSocketManager._lock) del ServerSocketManager._dict[sockfileno]
def SendToUser(rid, data, forcesend=False): ''' 给玩家发送数据 :param rid: :param data: :param forcesend: :return: ''' # Log.Write("SendToUser", rid) Lock2(UserSocketManager._lock) if not UserSocketManager._id.has_key(rid): Log.Write("[Error]no user %d" % rid) return False sockfileno = UserSocketManager._id[rid] # todo fix chungeng cut off net resend data if not forcesend: sockfileno2 = None sock2 = data.Socket() if sock2: sockfileno2 = sock2.fileno() # Log.Write("SendToUser", sockfileno2, sockfileno) if sockfileno2 and sockfileno != sockfileno2: return False if not UserSocketManager._dict.has_key(sockfileno): Log.Write("[Error]no user %d sock" % rid) return False # UserSocketManager._seq[sockfileno] += 1 # data.setSeq(UserSocketManager._seq[sockfileno]) data.setMagicCode(UserSocketManager._magicCode) UserSocketManager._dict[sockfileno]["send"].put(data) # "send": Queue() return True
def _newconnection(sockfileno): ''' 创建一个连接 :return: ''' Lock2(ServerSocketManager._lock) ServerSocketManager._dict[sockfileno] = {"send": Queue()} ServerSocketManager._seq[sockfileno] = 0
def SendToOnlines(data): ''' 给在线的所有玩家发送数据 :param data: :return: ''' Lock2(UserSocketManager._lock) for sockfileno, userSocketData in UserSocketManager._dict.items(): userSocketData["send"].put(data.Clone())
def WriteToFile2(): Lock2(Log.Lock) try: for param in Log.LOG_CACHE: Log.FileLog.write(str(param) + "\r\n") Log.FileLog.flush() except Exception, e: print e traceback.print_exc()
def GameServerOnline(serverid, socket): ''' 设置区服在线的sock连接 :param serverid: :param socket: :return: ''' Lock2(ServerSocketManager._lock) Log.Write("[TIP]GameServer: %d online" % serverid) ServerSocketManager._id[serverid] = socket
def FindRoleIdBySock(sockfileno): ''' 通过sock找玩家rid :param sockfileno: :return: ''' Lock2(UserSocketManager._lock) if UserSocketManager._dict.has_key(sockfileno): return UserSocketManager._dict[sockfileno]["id"] return None
def ConnectionGet(sockfileno): ''' 根据sockfileno获取发送给玩家的数据 :param sockfileno: :return: ''' Lock2(UserSocketManager._lock) if not UserSocketManager._dict.has_key(sockfileno): return None return UserSocketManager._dict[sockfileno]["send"].get()
def Status(): ''' 统计在线的sock和玩家 :return: ''' Lock2(UserSocketManager._lock) c = len(UserSocketManager._dict) u = len(UserSocketManager._id) Log.Write("-status-usockcount-", c, u) t_log_connections.Now(c, u)
def CloseConnection(sockfileno): ''' 关闭用户的连接 :param sockfileno: :return: ''' Log.Write("CloseConnection", sockfileno) Lock2(UserSocketManager._lock) if UserSocketManager._dict.has_key(sockfileno): UserSocketManager._dict[sockfileno]["close"].set()
def FindSockByRoleId(roleid): ''' 通过rid找sockfileno :param roleid: :return: ''' Lock2(UserSocketManager._lock) if UserSocketManager._id.has_key(roleid): return UserSocketManager._id[roleid] return None
def _get(sockfileno): ''' 获取一个sock连接队列中的数据 :return: ''' Lock2(ServerSocketManager._lock) if not ServerSocketManager._dict.has_key(sockfileno): Log.Write("Error", "ServerSocketManager sockfileno cant find", sockfileno) return return ServerSocketManager._dict[sockfileno]["send"].get()
def UserOffline(rid): ''' 玩家离线 删除sockfileno和连接 :param rid: :return: ''' Lock2(UserSocketManager._lock) if UserSocketManager._id.has_key(rid): sockfileno = UserSocketManager._id[rid] if UserSocketManager._dict.has_key(sockfileno): UserSocketManager._dict[sockfileno]["close"].set() del UserSocketManager._id[rid]
def SendToAllOnline(data): ''' 给所有在线的玩家发送数据 :param data: :return: ''' Lock2(UserSocketManager._lock) for k, v in UserSocketManager._dict.items(): # UserSocketManager._seq[k] += 1 # data.setSeq(UserSocketManager._seq[k]) data.setMagicCode(UserSocketManager._magicCode) v["send"].put(data.Clone()) return True
def NewConnection(sockfileno, closeEvent): ''' 创建一个连接 :param sockfileno: :param closeEvent: :return: ''' Lock2(UserSocketManager._lock) UserSocketManager._dict[sockfileno] = { "send": Queue(), "id": None, # rid "close": closeEvent } UserSocketManager._seq[sockfileno] = 0
def _send(sockfileno, data): ''' 给请求的sock发送数据 :param data: :return: ''' Lock2(ServerSocketManager._lock) if not ServerSocketManager._dict.has_key(sockfileno): return False ServerSocketManager._seq[sockfileno] += 1 data.setSeq(ServerSocketManager._seq[sockfileno]) data.setMagicCode(ServerSocketManager._magicCode) ServerSocketManager._dict[sockfileno]["send"].put(data) return True
def SendToConnection(sockfileno, data): ''' 按照sockfileno给用户发送数据 :param sockfileno: :param data: :return: ''' Lock2(UserSocketManager._lock) if not UserSocketManager._dict.has_key(sockfileno): return False # UserSocketManager._seq[sockfileno] += 1 # data.setSeq(UserSocketManager._seq[sockfileno]) # data.setMagicCode(UserSocketManager._magicCode) UserSocketManager._dict[sockfileno]["send"].put(data) # "send": Queue() return True
def UserOnline(rid, sockfileno): ''' 设置玩家在线信息 玩家新的sockfileno和rid :param rid: :param sockfileno: :return: ''' Lock2(UserSocketManager._lock) Log.Write("useronline", ("roleid", rid, "socket", sockfileno)) if UserSocketManager._id.has_key(rid): oldsocket = UserSocketManager._id[rid] if sockfileno != oldsocket and UserSocketManager._dict.has_key(oldsocket): UserSocketManager._dict[oldsocket]["close"].set() UserSocketManager._id[rid] = sockfileno UserSocketManager._dict[sockfileno]["id"] = rid
def DelConnection(sockfileno): ''' 通过sockfileno删除玩家 :param sockfileno: :return: ''' Lock2(UserSocketManager._lock) # if UserDataManager._sock.has_key(sockfileno): # for acc # del UserDataManager._sock[sockfileno] # for acc if UserSocketManager._dict.has_key(sockfileno): rid = UserSocketManager._dict[sockfileno]["id"] InnerTask(50006, {"id": rid}) # for game t_log_onoffline.Off(UserSocketManager._dict[sockfileno]["id"], sockfileno) # for game del UserSocketManager._dict[sockfileno] if UserSocketManager._id.has_key(rid) and UserSocketManager._id[rid] == sockfileno: del UserSocketManager._id[rid] del UserSocketManager._seq[sockfileno]
def WriteToFile(param): Lock2(Log.Lock) if LOG_TIME and Log.LOG_FILE_TIME.day != LOG_TIME.day: Log.FileLog.close() Log.LOG_FILE_TIME = LOG_TIME if Log.LOG_FIXTAG: Log.LOG_FILE = Log.LOG_PATH + "/" + Log.LOG_FILE_TIME.strftime( "%F") + Log.LOG_FIXTAG + ".log" else: Log.LOG_FILE = Log.LOG_PATH + "/" + Log.LOG_FILE_TIME.strftime( "%F") + ".log" Log.FileLog = open(Log.LOG_FILE, "a+") try: Log.FileLog.write(str(param) + "\r\n") Log.FileLog.flush() except Exception, e: print e traceback.print_exc()
def _send2(serverid, data): ''' 给区服发送数据 :param data: :return: ''' Lock2(ServerSocketManager._lock) if not ServerSocketManager._id.has_key(serverid): Log.Write("[Error]Send To GameServer: %d offline" % serverid) return False sockfileno = ServerSocketManager._id[serverid] if not ServerSocketManager._dict.has_key(sockfileno): return False ServerSocketManager._seq[sockfileno] += 1 data.setSeq(ServerSocketManager._seq[sockfileno]) data.setMagicCode(ServerSocketManager._magicCode) ServerSocketManager._dict[sockfileno]["send"].put(data) return True
def FindDeviceBySocket(socket): Lock2(UserSocketManager._lock) if not UserSocketManager._device.has_key(socket): return None return UserSocketManager._device[socket]
def DeviceOnline(uuid, socket): Lock2(UserSocketManager._lock) UserSocketManager._device[socket] = uuid