Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
all in all, the integration with twisted is limited and rather fake.
working with rpyc might block the reactor -- a bad thing -- but a necessary
evil if we wish to combine the two methodologies.
if you find a better solution, please tell me.
"""
import socket
import rpyc
from rpyc.core import SocketStream, Channel
import twisted.internet.protocol as tip
from twisted.internet import reactor
from twisted.python import log
class TwistedSocketStream(SocketStream):
def __init__(self, transport):
SocketStream.__init__(self, transport.socket)
self.transport = transport
self._buffer = ""
def push(self, data):
self._buffer += data
def poll(self, timeout):
if self._buffer:
return True
self.sock.setblocking(True)
try:
return SocketStream.poll(self, timeout)
finally:
try:
self.sock.setblocking(False)
def _serve_client(self, sock, credentials):
addrinfo = sock.getpeername()
h = addrinfo[0]
p = addrinfo[1]
if credentials:
self.logger.info("welcome [%s]:%s (%r)", h, p, credentials)
else:
self.logger.info("welcome [%s]:%s", h, p)
try:
config = dict(self.protocol_config, credentials = credentials,
endpoints = (sock.getsockname(), addrinfo), logger = self.logger)
conn = Connection(self.service, Channel(SocketStream(sock)),
config = config, _lazy = True)
conn._init_service()
conn.serve_all()
finally:
self.logger.info("goodbye [%s]:%s", h, p)
def _serve_client(self, sock, credentials):
addrinfo = sock.getpeername()
if credentials:
self.logger.info("welcome %s (%r)", addrinfo, credentials)
else:
self.logger.info("welcome %s", addrinfo)
try:
config = dict(self.protocol_config, credentials = credentials,
endpoints = (sock.getsockname(), addrinfo))
conn = Connection(self.service, Channel(SocketStream(sock)),
config = config, _lazy = True)
conn._init_service()
conn.serve_all()
finally:
self.logger.info("goodbye %s", addrinfo)
def _authenticate_and_build_connection(self, sock):
'''Authenticate a client and if it succees, wraps the socket in a connection object.
Note that this code is cut and paste from the rpyc internals and may have to be
changed if rpyc evolves'''
# authenticate
if self.authenticator:
sock, credentials = self.authenticator(sock)
else:
credentials = None
# build a connection
h, p = sock.getpeername()
config = dict(self.protocol_config, credentials=credentials, connid="%s:%d" % (h, p),
endpoints=(sock.getsockname(), (h, p)))
return sock, self.service._connect(Channel(SocketStream(sock)), config)