class TcpRpcClient(RpcClient): def __init__(self, host, port, keepconnect=True): RpcClient.__init__(self) self.host = host self.port = port self.keepconnect = keepconnect self.protocal = TcpProtocal(host, port) @retryTimes(retryTimes=3) def call(self, func, args=()): if args == None: args = () try: self.protocal.transport.connect() except Exception as ex: pass package = { 'func': func, 'args': args, } msg = self.protocal.serialize(package) self.protocal.transport.send(msg) respmsg = self.protocal.transport.recv() resp = self.protocal.unserialize(respmsg) if isinstance(resp, Exception): if not self.keepconnect: self.protocal.transport.close() raise resp if not self.keepconnect: self.protocal.transport.close() return resp def close(self): self.protocal.transport.close()
class TcpRpcClient(RpcClient): def __init__(self, host='', port=0, servers=[], timeout=10, keepconnect=True): RpcClient.__init__(self) self.host = host self.port = port self.keepconnect = keepconnect self.protocal = TcpProtocal(host, port, timeout=timeout) self.servers = servers self.timeout = timeout if len(self.servers) == 0 and self.host != '' and self.port != 0: self.servers = [host + ':' + str(port)] for server in self.servers: host, port = server.split(':') self.serversProtocalMap[server] = TcpProtocal(host, int(port), timeout=timeout) @retryTimes(retryTimes=3) def call(self, func, *args, **kwargs): try: self.beforeCall() self.protocal.transport.connect() package = { 'func': func, 'args': args, 'kwargs': kwargs, } msg = self.protocal.serialize(package) self.protocal.transport.send(msg) respmsg = self.protocal.transport.recv() resp = self.protocal.unserialize(respmsg) if isinstance(resp, Exception): if not self.keepconnect: self.protocal.transport.close() raise resp if not self.keepconnect: self.protocal.transport.close() return resp except socket.timeout as ex: self.protocal.transport.close() raise ex def getProtocalInstance(self, instance): key = "%s:%s:%s" % (instance.service, instance.address, instance.port) if key in self.protocalMap: return self.protocalMap.get(key) self.protocalMap[key] = TcpProtocal(instance.address, instance.port) return self.protocalMap[key]
class SimpleTcpRpcServer(RpcServer): def __init__(self, host, port): RpcServer.__init__(self) self.host = host self.port = port self.protocal = TcpProtocal(host, port) def serve(self): self.protocal.transport.bind(self.keepaliveTimeout) while 1: msg = self.protocal.transport.recv() request = self.protocal.unserialize(msg) func, args, kwargs = self.protocal.parseRequest(request) resp = self.run(func, args, kwargs) self.protocal.transport.send(self.protocal.serialize(resp))
def __init__(self, host='', port=0, servers=[], timeout=10, keepconnect=True): RpcClient.__init__(self) self.host = host self.port = port self.keepconnect = keepconnect self.protocal = TcpProtocal(host, port, timeout=timeout) self.servers = servers self.timeout = timeout if len(self.servers) == 0 and self.host != '' and self.port != 0: self.servers = [host + ':' + str(port)] for server in self.servers: host, port = server.split(':') self.serversProtocalMap[server] = TcpProtocal(host, int(port), timeout=timeout)
def __init__(self, host, port): RpcServer.__init__(self) self.host = host self.port = port self.protocal = TcpProtocal(host, port)
def getProtocalInstance(self, instance): key = "%s:%s:%s" % (instance.service, instance.address, instance.port) if key in self.protocalMap: return self.protocalMap.get(key) self.protocalMap[key] = TcpProtocal(instance.address, instance.port) return self.protocalMap[key]
def __init__(self, host, port, keepconnect=True): RpcClient.__init__(self) self.host = host self.port = port self.keepconnect = keepconnect self.protocal = TcpProtocal(host, port)