def __init__(self, ipc_path=None, testnet=False, timeout=10): if ipc_path is None: self.ipc_path = get_default_ipc_path(testnet) else: self.ipc_path = ipc_path self.timeout = timeout self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path)
def __init__(self, ipc_path): if isinstance(ipc_path, pathlib.Path): ipc_path = str(ipc_path.resolve()) self.ipc_path = ipc_path self.ipc_timeout = 60*10 self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path)
def __init__(self, websocket_url, ipc_path): self.websocket_url = websocket_url url = urlparse(websocket_url) assert url.scheme == 'ws' self.hostname, self.port = url.hostname, url.port if isinstance(ipc_path, pathlib.Path): ipc_path = str(ipc_path.resolve()) self.ipc_path = ipc_path self.keepalive_timeout = 30 self.server = None self.ipc_timeout = 10 self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path)
class BatchIPCProvider: """Class for making asynchronous ipc requests.""" _socket = None def __init__(self, ipc_path=None, testnet=False, timeout=10): """Initialization.""" if ipc_path is None: self.ipc_path = get_default_ipc_path(testnet) else: self.ipc_path = ipc_path self.timeout = timeout self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path) def make_request(self, text): """Make IPC request.""" request = text.encode('utf-8') with self._lock, self._socket as sock: try: sock.sendall(request) except BrokenPipeError: # one extra attempt, then give up sock = self._socket.reset() sock.sendall(request) raw_response = b"" with Timeout(self.timeout) as timeout: while True: try: raw_response += sock.recv(4096) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) elif has_valid_json_rpc_ending(raw_response): try: response = json.loads(raw_response.decode('utf-8')) except JSONDecodeError: timeout.sleep(0) continue else: return response else: timeout.sleep(0) continue
class BaseProxy: def __init__(self, ipc_path): if isinstance(ipc_path, pathlib.Path): ipc_path = str(ipc_path.resolve()) self.ipc_path = ipc_path self.ipc_timeout = 60*10 self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path) def process(self, request): with self._lock, self._socket as sock: try: sock.sendall(request) except BrokenPipeError: # one extra attempt, then give up sock = self._socket.reset() sock.sendall(request) raw_response = b'' with Timeout(self.ipc_timeout) as timeout: while True: try: raw_response += sock.recv(BUFSIZE) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) elif has_valid_json_rpc_ending(raw_response): try: json.loads(to_text(raw_response)) except JSONDecodeError: timeout.sleep(0) continue else: return raw_response else: timeout.sleep(0) continue return response
class Proxy: def __init__(self, websocket_url, ipc_path): self.websocket_url = websocket_url url = urlparse(websocket_url) assert url.scheme == 'ws' self.hostname, self.port = url.hostname, url.port if isinstance(ipc_path, pathlib.Path): ipc_path = str(ipc_path.resolve()) self.ipc_path = ipc_path self.keepalive_timeout = 30 self.server = None self.ipc_timeout = 10 self._lock = threading.Lock() self._socket = PersistantSocket(self.ipc_path) def process(self, request): with self._lock, self._socket as sock: try: sock.sendall(request) except BrokenPipeError: # one extra attempt, then give up sock = self._socket.reset() sock.sendall(request) raw_response = b'' with Timeout(self.ipc_timeout) as timeout: while True: try: raw_response += sock.recv(BUFSIZE) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) elif has_valid_json_rpc_ending(raw_response): try: json.loads(to_text(raw_response)) except JSONDecodeError: timeout.sleep(0) continue else: return raw_response else: timeout.sleep(0) continue return response async def interface(self, websocket, path): while websocket.open: try: request = await asyncio.wait_for( websocket.recv(), timeout=self.keepalive_timeout) except websockets.exceptions.ConnectionClosed: continue except asyncio.TimeoutError: continue print("request: {}".format(request)) response = self.process(request.encode('utf-8')) print("response: {}".format(response)) await websocket.send(response.decode('utf-8')) def run(self): if WEBSOCKET_USE_SSL: ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_cert_chain(WEBSOCKET_SSL_CERT_FILE_PATH, WEBSOCKET_SSL_KEY_FILE_PATH) self.server = websockets.serve(self.interface, self.hostname, self.port, ssl=ssl_context) print("JSON-RPC Secure Websocket Proxy: {} -> {}".format( self.ipc_path, self.websocket_url), file=sys.stderr, flush=True) else: self.server = websockets.serve(self.interface, self.hostname, self.port) print("JSON-RPC Websocket Proxy: {} -> {}".format( self.ipc_path, self.websocket_url), file=sys.stderr, flush=True) asyncio.get_event_loop().run_until_complete(self.server) asyncio.get_event_loop().run_forever()