def load_lua_script(self, lua_alias, lua_script): sha = self.execute('script', 'load', lua_script) self.__sha_map__[lua_alias] = sha DbRedis.__LUA_ALIAS__[lua_alias] = lua_script Logger.info(self.__redis_key__, 'load_lua_script', lua_alias, sha, lua_script) return sha
class WebPage(IContext): def wait_for_page(self, url, postdata=None, query=None, method='POST', headers=None, cookies=None, timeout=6): try: tips = 'waitForPage' + repr(url[0:10]) except Exception, e: tips = 'waitForPage-errtips' if isinstance(url, unicode): url = url.encode('utf8') if isinstance(query, dict): query = urllib.urlencode(query) if query: url += '?' + query if not headers: headers = {'Content-type': 'application/x-www-form-urlencoded'} if isinstance(postdata, dict): postdata = urllib.urlencode(postdata) Logger.info('---->', url, method, headers, repr(postdata), timeout) d = client.getPage(url, method=method, headers=headers, postdata=postdata, cookies=cookies, timeout=timeout) tasklet = self.ctx.tasklet() response = tasklet.wait_for_deferred(d, tips) Logger.info('<----', url, repr(response)) return response
def wait_for_deferred(self, d, tip=None): try: d.addCallbacks(self.__callback, self.__errorback) return self._return_channel.receive() except Exception, e: Logger.exception(tip) raise e
def execute(self, *args, **kwargs): tasklet = TaskManager.current() # d = self.__redisPool__.execute_command(*args, **kwargs) d = self.__redisPool__.client.send(*args) result = tasklet.wait_for_deferred(d) Logger.debug_redis(self.__redis_key__, args, kwargs, '=>', result) return result
def run(self, *args, **kwargs): self._return_channel = Channel() current = stackless.getcurrent() current._class_instance = self self._tasklet_instance = current try: self.handle(*args, **kwargs) except: Logger.exception()
def receiveDone(self, request): try: tasklet = self.makeTasklet(request) TaskManager.add_task(tasklet.run) TaskManager.schedule() except Exception, e: Logger.exception() body, content_type = http_response_500(self.request) Logger.debug('<====', self.request.path, content_type, repr(body))
def connect(self, param_list): if not isinstance(param_list, list): raise Exception('param_list error' + str(param_list)) Logger.info('RedisCluster.__init__ ->', param_list) self.__dbs__ = [] for param in param_list: single = RedisSingle() single.connect(param) self.__dbs__.append(single)
def connectionMade(self): self.peer_host = str(self.transport.getPeer().host) self.peer_port = str(self.transport.getPeer().port) self.transport.setTcpNoDelay(1) self.transport.setTcpKeepAlive(1) try: sock = self.transport.getHandle() sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 30) sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 10) sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 180) except Exception, e: Logger.exception('not support TCP_KEEPIDLE')
def handle(self, *args, **kwargs): if Logger.is_debug_network(): peer = kwargs.get('peer', 'INNER') mode = kwargs.get('proto', 'TCP') Logger.debug('==== RECV %s FROM %s: %08X %s' % (mode, peer, self.cmd, repr(self.raw))) if Message.is_inner(self.cmd): cmd = Message.to_outer(self.cmd) mi = MsgLine.unpack(self.raw) self.onInnerMessage(cmd, mi, *args, **kwargs) else: self.onOuterMessage(self.cmd, self.raw, *args, **kwargs)
def handle(self, *args, **kwargs): Logger.debug('====>', self.request.path) try: mo = self.onMessage(*args, **kwargs) if self.request._disconnected: Logger.info('<====', self.request.path, 'connection lost') return if isinstance(mo, MsgPack): body, content_type = http_response( self.request, mo.pack(), content_type='application/json') else: body, content_type = http_response(self.request, mo) except SystemException, e: body, content_type = http_response_500(self.request)
def dataReceived(self, data): self._data += data while len(self._data) > 8: cmd, msg_len = struct.unpack('II', self._data[:8]) if msg_len > len(self._data) - 8: return body_data = self._data[8:8 + msg_len] self._data = self._data[8 + msg_len:] try: self.access_ts = Time.current_ts() tasklet = self.makeTasklet(cmd, body_data, self) TaskManager.add_task(tasklet.run, peer=self.peer_key) except Exception, e: Logger.exception(body_data) self.transport.loseConnection()
def sendMsg(self, cmd, data): try: Logger.debug_network('==== SEND TCP TO %s:' % self.peer_key, '%08X' % cmd, repr(data)) if self.transport and self.connected: header = struct.pack('II', cmd, len(data)) self.transport.write(header + data) return True else: Logger.error('==== ERROR: cannot connected !! protocol =', self, '%08X' % cmd, repr(data)) except Exception, e: Logger.exception(data)
def connectionLost(self, reason=connectionDone): try: Logger.info('ConnectionLost', 'userId =', self.userId) if self.userId > 0: msg = MsgPack(Message.MSG_INNER_BROKEN) if self.userId in Context.GData.map_client_connect: del Context.GData.map_client_connect[self.userId] msg.set_param('userId', self.userId) if self.gameId > 0: msg.set_param('gameId', self.gameId) tasklet = self.makeTasklet(Message.MSG_INNER_BROKEN, msg, self) TaskManager.add_task(tasklet.run) else: Logger.debug_network('empty user connection lost ... ') except Exception, e: Logger.exception()
def onOuterMessage(self, cmd, raw, *args, **kwargs): Logger.debug('HANDLE OUTER MESSAGE', raw, args, kwargs)
def onInnerMessage(self, cmd, mi, *args, **kwargs): Logger.debug('HANDLE INNER MESSAGE', self.raw, args, kwargs)
def __init__(self, request): self.request = request def handle(self, *args, **kwargs): Logger.debug('====>', self.request.path) try: mo = self.onMessage(*args, **kwargs) if self.request._disconnected: Logger.info('<====', self.request.path, 'connection lost') return if isinstance(mo, MsgPack): body, content_type = http_response( self.request, mo.pack(), content_type='application/json') else: body, content_type = http_response(self.request, mo) except SystemException, e: body, content_type = http_response_500(self.request) except NotFoundException, e: # body = resource.NoResource('Not Found').render(self.request) body, content_type = http_response_404(self.request) except ForbiddenException, e: # body = resource.ForbiddenResource('Forbidden Access').render(self.request) body, content_type = http_response_403(self.request) except Exception, e: Logger.exception(kwargs) body, content_type = http_response_500(self.request) Logger.debug('<====', self.request.path, content_type, repr(body)) def onMessage(self, *args, **kwargs): raise NotImplementedError
def __timeout(self, msg, task, *args, **kwargs): try: TaskManager.add_task(task) except Exception, e: Logger.exception(args)
def clientConnectionLost(self, connector, unused_reason): Logger.info('tcp client connect lost', self.peer_server_id) ReconnectingClientFactory.clientConnectionLost(self, connector, unused_reason)
def add_lua_alias(self, lua_alias, lua_sha): self.__sha_map__[lua_alias] = lua_sha Logger.info(self.__redis_key__, 'add_lua_alias', lua_alias, lua_sha)
def __errorback(self, fault): try: self._return_channel.send_exception_nowait(fault.type, fault.value) except Exception, e: Logger.exception(fault) self._return_channel.send_exception_nowait(Exception, e)
def connectionMade(self): BasicTcpProtocol.connectionMade(self) Logger.info('tcp client connect made', self.factory.peer_server_id) Context.GData.map_server_connect[self.factory.peer_server_id] = self
def __callback(self, msg): try: self._return_channel.send_nowait(msg) except Exception, e: Logger.exception(msg) self._return_channel.send_exception_nowait(Exception, e)
randint = (randint / 65536) % 32768 randchar = randint % 255 out.append(chr(ord(data[i]) ^ randchar)) return "".join(out) ffi = cffi.FFI() with open("code.h") as f: data = f.read() ffi.cdef(data) libc = ffi.dlopen("./code.so") char_buf = ffi.new("unsigned char[]", 65536) s = '{"birds":[{"t":205,"i":252,"n":3000,"s":60,"p":[1,1280,370,0,370]},{"t":104,"i":253,"n":3017,"s":30,"p":[1,0,356,1280,377]},{"t":107,"i":254,"n":3033,"s":30,"p":[1,0,301,1280,405]},{"t":115,"i":255,"n":3041,"s":30,"p":[3,147,720,213,362,578,0]},{"t":177,"i":256,"n":3054,"s":30,"p":[3,371,0,617,522,246,720]},{"t":176,"i":257,"n":3071,"s":30,"p":[3,138,0,237,220,451,720]},{"t":114,"i":258,"n":3083,"s":30,"p":[3,1280,484,822,560,242,0]},{"t":117,"i":259,"n":3089,"s":30,"p":[3,0,395,313,384,1005,0]},{"t":102,"i":260,"n":3096,"s":30,"p":[1,1045,720,0,113]},{"t":111,"i":261,"n":3103,"s":30,"p":[3,859,720,997,575,0,269]},{"t":116,"i":262,"n":3122,"s":30,"p":[1,1065,720,689,0]},{"t":109,"i":263,"n":3139,"s":30,"p":[3,1280,632,359,140,0,513]},{"t":109,"i":264,"n":3155,"s":30,"p":[3,1280,109,721,439,0,376]},{"t":112,"i":265,"n":3166,"s":30,"p":[3,1114,0,499,471,0,393]},{"t":106,"i":266,"n":3181,"s":30,"p":[1,1280,500,450,0]},{"t":182,"i":267,"n":3191,"s":30,"p":[3,1280,181,447,263,161,720]},{"t":104,"i":268,"n":3211,"s":30,"p":[1,272,720,205,0]},{"t":117,"i":269,"n":3229,"s":30,"p":[1,0,329,867,720]},{"t":117,"i":270,"n":3247,"s":30,"p":[3,1280,159,1163,516,0,75]},{"t":103,"i":271,"n":3252,"s":30,"p":[1,818,720,0,306]},{"t":102,"i":272,"n":3262,"s":30,"p":[1,327,720,1280,124]},{"t":113,"i":273,"n":3282,"s":30,"p":[3,0,642,983,146,1280,452]},{"t":178,"i":274,"n":3294,"s":30,"p":[3,175,0,713,194,862,720]}],"events":[{"in":3000,"type":1,"show":60}],"uptime":294102}' Logger.open_std_log() count = 1000000 start = time.time() Logger.debug("----------------------------") for i in xrange(count): libc.code(100, s, len(s), char_buf) data = ffi.buffer(char_buf, len(s))[:] # print code(100, s) Logger.debug("----------------------------") end = time.time() print(end - start) * 1000000 / count
def connect(self, *params): Logger.info('RedisSingle.__init__ ->', *params) DbRedis.connect(self, *params)
def clientConnectionFailed(self, connector, reason): Logger.error('tcp client connect failed', self.peer_server_id) ReconnectingClientFactory.clientConnectionFailed( self, connector, reason)