本文整理匯總了Python中xpra.net.protocol.Protocol類的典型用法代碼示例。如果您正苦於以下問題:Python Protocol類的具體用法?Python Protocol怎麽用?Python Protocol使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Protocol類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _new_connection
def _new_connection(self, listener, *args):
socktype = self.socket_types.get(listener, "")
sock, address = listener.accept()
if len(self._potential_protocols)>=self._max_connections:
log.error("too many connections (%s), ignoring new one", len(self._potential_protocols))
sock.close()
return True
try:
peername = sock.getpeername()
except:
peername = str(address)
sockname = sock.getsockname()
target = peername or sockname
sock.settimeout(self._socket_timeout)
log("new_connection(%s) sock=%s, sockname=%s, address=%s, peername=%s", args, sock, sockname, address, peername)
sc = SocketConnection(sock, sockname, address, target, socktype)
log.info("New connection received: %s", sc)
protocol = Protocol(self, sc, self.process_packet)
protocol.large_packets.append("info-response")
protocol.authenticator = None
protocol.invalid_header = self.invalid_header
protocol.receive_aliases.update(self._aliases)
self._potential_protocols.append(protocol)
protocol.start()
self.timeout_add(SOCKET_TIMEOUT*1000, self.verify_connection_accepted, protocol)
return True
示例2: run
def run(self):
log("ProxyProcess.run() pid=%s, uid=%s, gid=%s", os.getpid(), getuid(), getgid())
setuidgid(self.uid, self.gid)
if self.env_options:
#TODO: whitelist env update?
os.environ.update(self.env_options)
self.video_init()
log.info("new proxy instance started")
log.info(" for client %s", self.client_conn)
log.info(" and server %s", self.server_conn)
signal.signal(signal.SIGTERM, self.signal_quit)
signal.signal(signal.SIGINT, self.signal_quit)
log("registered signal handler %s", self.signal_quit)
start_thread(self.server_message_queue, "server message queue")
if not self.create_control_socket():
#TODO: should send a message to the client
return
self.control_socket_thread = start_thread(self.control_socket_loop, "control")
self.main_queue = Queue()
#setup protocol wrappers:
self.server_packets = Queue(PROXY_QUEUE_SIZE)
self.client_packets = Queue(PROXY_QUEUE_SIZE)
self.client_protocol = Protocol(self, self.client_conn, self.process_client_packet, self.get_client_packet)
self.client_protocol.restore_state(self.client_state)
self.server_protocol = Protocol(self, self.server_conn, self.process_server_packet, self.get_server_packet)
#server connection tweaks:
self.server_protocol.large_packets.append("draw")
self.server_protocol.large_packets.append("window-icon")
self.server_protocol.large_packets.append("keymap-changed")
self.server_protocol.large_packets.append("server-settings")
if self.caps.boolget("file-transfer"):
self.client_protocol.large_packets.append("send-file")
self.client_protocol.large_packets.append("send-file-chunk")
self.server_protocol.large_packets.append("send-file")
self.server_protocol.large_packets.append("send-file-chunk")
self.server_protocol.set_compression_level(self.session_options.get("compression_level", 0))
self.server_protocol.enable_default_encoder()
self.lost_windows = set()
self.encode_queue = Queue()
self.encode_thread = start_thread(self.encode_loop, "encode")
log("starting network threads")
self.server_protocol.start()
self.client_protocol.start()
self.send_hello()
self.timeout_add(VIDEO_TIMEOUT*1000, self.timeout_video_encoders)
try:
self.run_queue()
except KeyboardInterrupt as e:
self.stop(str(e))
finally:
log("ProxyProcess.run() ending %s", os.getpid())
示例3: SimpleClient
class SimpleClient(object):
def init(self, exit_cb, packets=[]):
self.packets = packets
sock = socket.socket(socket.AF_UNIX)
sock.settimeout(5)
sock.connect(TEST_SOCKFILE)
sock.settimeout(None)
sc = makeSocketConnection(sock, "test-client-socket")
self.protocol = Protocol(gobject, sc, self.process_packet, None)
self.protocol.start()
if len(self.packets)>0:
gobject.timeout_add(1000, self.send_packet)
def send_packet(self):
self.protocol.send_now(self.packets[0])
self.packets = self.packets[1:]
return len(self.packets)>0
def process_packet(self, proto, packet):
log.info("process_packet(%s, %s)", proto, packet)
def get_packet(self, *args):
log.info("get_packet(%s)", args)
return None
示例4: make_protocol
def make_protocol(self):
#make a connection using the process stdin / stdout
conn = TwoFileConnection(self.process.stdin, self.process.stdout, abort_test=self.abort_test, target=self.description, socktype=self.description, close_cb=self.subprocess_exit)
conn.timeout = 0
protocol = Protocol(self, conn, self.process_packet, get_packet_cb=self.get_packet)
setup_fastencoder_nocompression(protocol)
protocol.large_packets = self.large_packets
return protocol
示例5: run
def run(self):
debug("ProxyProcess.run() pid=%s, uid=%s, gid=%s", os.getpid(), os.getuid(), os.getgid())
#change uid and gid:
if os.getgid()!=self.gid:
os.setgid(self.gid)
if os.getuid()!=self.uid:
os.setuid(self.uid)
debug("ProxyProcess.run() new uid=%s, gid=%s", os.getuid(), os.getgid())
if self.env_options:
#TODO: whitelist env update?
os.environ.update(self.env_options)
log.info("new proxy started for client %s and server %s", self.client_conn, self.server_conn)
signal.signal(signal.SIGTERM, self.signal_quit)
signal.signal(signal.SIGINT, self.signal_quit)
debug("registered signal handler %s", self.signal_quit)
make_daemon_thread(self.server_message_queue, "server message queue").start()
self.main_queue = Queue()
#setup protocol wrappers:
self.server_packets = Queue(PROXY_QUEUE_SIZE)
self.client_packets = Queue(PROXY_QUEUE_SIZE)
self.client_protocol = Protocol(self, self.client_conn, self.process_client_packet, self.get_client_packet)
self.client_protocol.restore_state(self.client_state)
self.server_protocol = Protocol(self, self.server_conn, self.process_server_packet, self.get_server_packet)
#server connection tweaks:
self.server_protocol.large_packets.append("draw")
self.server_protocol.large_packets.append("window-icon")
self.server_protocol.large_packets.append("keymap-changed")
self.server_protocol.large_packets.append("server-settings")
self.server_protocol.set_compression_level(self.session_options.get("compression_level", 0))
self.lost_windows = set()
self.encode_queue = Queue()
self.encode_thread = make_daemon_thread(self.encode_loop, "encode")
self.encode_thread.start()
debug("starting network threads")
self.server_protocol.start()
self.client_protocol.start()
#forward the hello packet:
hello_packet = ("hello", self.filter_client_caps(self.caps))
self.queue_server_packet(hello_packet)
try:
try:
self.run_queue()
except KeyboardInterrupt, e:
self.stop(str(e))
finally:
debug("ProxyProcess.run() ending %s", os.getpid())
示例6: make_protocol
def make_protocol(self):
#figure out where we read from and write to:
if self.input_filename=="-":
#disable stdin buffering:
self._input = os.fdopen(sys.stdin.fileno(), 'rb', 0)
setbinarymode(self._input.fileno())
else:
self._input = open(self.input_filename, 'rb')
if self.output_filename=="-":
#disable stdout buffering:
self._output = os.fdopen(sys.stdout.fileno(), 'wb', 0)
setbinarymode(self._output.fileno())
else:
self._output = open(self.output_filename, 'wb')
#stdin and stdout wrapper:
conn = TwoFileConnection(self._output, self._input, abort_test=None, target=self.name, info=self.name, close_cb=self.net_stop)
conn.timeout = 0
protocol = Protocol(gobject, conn, self.process_packet, get_packet_cb=self.get_packet)
try:
protocol.enable_encoder("rencode")
except Exception as e:
log.warn("failed to enable rencode: %s", e)
protocol.enable_encoder("bencode")
protocol.enable_compressor("none")
protocol.large_packets = self.large_packets
return protocol
示例7: setup_connection
def setup_connection(self, conn):
log.debug("setup_connection(%s)", conn)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.set_compression_level(self.compression_level)
self.have_more = self._protocol.source_has_more
示例8: init
def init(self, exit_cb, packets=[]):
self.packets = packets
sock = socket.socket(socket.AF_UNIX)
sock.settimeout(5)
sock.connect(TEST_SOCKFILE)
sock.settimeout(None)
sc = makeSocketConnection(sock, "test-client-socket")
self.protocol = Protocol(gobject, sc, self.process_packet, None)
self.protocol.start()
if len(self.packets)>0:
gobject.timeout_add(1000, self.send_packet)
示例9: setup_connection
def setup_connection(self, conn):
log.debug("setup_connection(%s)", conn)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.set_compression_level(self.compression_level)
self._protocol.receive_aliases.update(self._aliases)
self._protocol.enable_default_encoder()
self._protocol.enable_default_compressor()
self.have_more = self._protocol.source_has_more
if conn.timeout>0:
self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000, self.verify_connected)
示例10: new_control_connection
def new_control_connection(self, sock, address):
if len(self.potential_protocols)>=self.max_connections:
log.error("too many connections (%s), ignoring new one", len(self.potential_protocols))
sock.close()
return True
try:
peername = sock.getpeername()
except:
peername = str(address)
sockname = sock.getsockname()
target = peername or sockname
#sock.settimeout(0)
log("new_control_connection() sock=%s, sockname=%s, address=%s, peername=%s", sock, sockname, address, peername)
sc = SocketConnection(sock, sockname, address, target, "unix-domain")
log.info("New proxy instance control connection received: %s", sc)
protocol = Protocol(self, sc, self.process_control_packet)
protocol.large_packets.append("info-response")
self.potential_protocols.append(protocol)
protocol.start()
self.timeout_add(SOCKET_TIMEOUT*1000, self.verify_connection_accepted, protocol)
return True
示例11: _new_connection
def _new_connection(self, listener, *args):
socktype = self.socket_types.get(listener)
assert socktype, "cannot find socket type for %s" % listener
sock, address = listener.accept()
if len(self._potential_protocols) >= self._max_connections:
netlog.error("too many connections (%s), ignoring new one", len(self._potential_protocols))
sock.close()
return True
try:
peername = sock.getpeername()
except:
peername = str(address)
sockname = sock.getsockname()
target = peername or sockname
sock.settimeout(self._socket_timeout)
netlog(
"new_connection(%s) sock=%s, timeout=%s, sockname=%s, address=%s, peername=%s",
args,
sock,
self._socket_timeout,
sockname,
address,
peername,
)
sc = SocketConnection(sock, sockname, address, target, socktype)
netlog("socket connection: %s", sc)
frominfo = ""
if peername:
frominfo = " from %s" % str(peername)
netlog.info("New %s connection received%s", socktype, frominfo)
protocol = Protocol(self, sc, self.process_packet)
self._potential_protocols.append(protocol)
protocol.large_packets.append("info-response")
protocol.challenge_sent = False
protocol.authenticator = None
if socktype == "tcp":
protocol.auth_class = self.tcp_auth_class
else:
protocol.auth_class = self.auth_class
protocol.socket_type = socktype
protocol.invalid_header = self.invalid_header
protocol.receive_aliases.update(self._aliases)
protocol.start()
self.timeout_add(SOCKET_TIMEOUT * 1000, self.verify_connection_accepted, protocol)
return True
示例12: setup_connection
def setup_connection(self, conn):
netlog("setup_connection(%s) timeout=%s", conn, conn.timeout)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.large_packets.append("logging")
self._protocol.set_compression_level(self.compression_level)
self._protocol.receive_aliases.update(self._aliases)
self._protocol.enable_default_encoder()
self._protocol.enable_default_compressor()
if self.encryption and ENCRYPT_FIRST_PACKET:
key = self.get_encryption_key()
self._protocol.set_cipher_out(self.encryption, DEFAULT_IV, key, DEFAULT_SALT, DEFAULT_ITERATIONS, INITIAL_PADDING)
self.have_more = self._protocol.source_has_more
if conn.timeout>0:
self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000, self.verify_connected)
netlog("setup_connection(%s) protocol=%s", conn, self._protocol)
示例13: new_connection
def new_connection(self, *args):
log.info("new_connection(%s)", args)
sock, address = self.listener.accept()
log.info("new_connection(%s) sock=%s, address=%s", args, sock, address)
sock.settimeout(None)
sock.setblocking(1)
sc = makeSocketConnection(sock, str(address)+"server")
protocol = Protocol(gobject, sc, self.process_packet)
protocol.salt = None
protocol.set_compression_level(1)
protocol.start()
return True
示例14: setup_connection
def setup_connection(self, conn):
netlog("setup_connection(%s) timeout=%s", conn, conn.timeout)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.large_packets.append("logging")
self._protocol.set_compression_level(self.compression_level)
self._protocol.receive_aliases.update(self._aliases)
self._protocol.enable_default_encoder()
self._protocol.enable_default_compressor()
if self.encryption and ENCRYPT_FIRST_PACKET:
key = self.get_encryption_key()
self._protocol.set_cipher_out(
self.encryption, DEFAULT_IV, key, DEFAULT_SALT, DEFAULT_ITERATIONS, INITIAL_PADDING
)
self.have_more = self._protocol.source_has_more
if conn.timeout > 0:
self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000, self.verify_connected)
process = getattr(conn, "process", None) # ie: ssh is handled by anotherprocess
if process:
proc, name, command = process
getChildReaper().add_process(proc, name, command, ignore=True, forget=False)
netlog("setup_connection(%s) protocol=%s", conn, self._protocol)
示例15: XpraClientBase
#.........這裏部分代碼省略.........
pass
def disconnect_and_quit(self, exit_code, reason):
#make sure that we set the exit code early,
#so the protocol shutdown won't set a different one:
if self.exit_code is None:
self.exit_code = exit_code
#try to tell the server we're going, then quit
log("disconnect_and_quit(%s, %s)", exit_code, reason)
p = self._protocol
if p is None or p._closed:
self.quit(exit_code)
return
def protocol_closed():
log("disconnect_and_quit: protocol_closed()")
self.idle_add(self.quit, exit_code)
if p:
p.flush_then_close(["disconnect", reason], done_callback=protocol_closed)
self.timeout_add(1000, self.quit, exit_code)
def exit(self):
sys.exit()
def client_type(self):
#overriden in subclasses!
return "Python"
def get_scheduler(self):
raise NotImplementedError()
def setup_connection(self, conn):
netlog("setup_connection(%s) timeout=%s", conn, conn.timeout)
self._protocol = Protocol(self.get_scheduler(), conn, self.process_packet, self.next_packet)
self._protocol.large_packets.append("keymap-changed")
self._protocol.large_packets.append("server-settings")
self._protocol.large_packets.append("logging")
self._protocol.set_compression_level(self.compression_level)
self._protocol.receive_aliases.update(self._aliases)
self._protocol.enable_default_encoder()
self._protocol.enable_default_compressor()
if self.encryption and ENCRYPT_FIRST_PACKET:
key = self.get_encryption_key()
self._protocol.set_cipher_out(self.encryption, DEFAULT_IV, key, DEFAULT_SALT, DEFAULT_ITERATIONS, INITIAL_PADDING)
self.have_more = self._protocol.source_has_more
if conn.timeout>0:
self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000, self.verify_connected)
netlog("setup_connection(%s) protocol=%s", conn, self._protocol)
def remove_packet_handlers(self, *keys):
for k in keys:
for d in (self._packet_handlers, self._ui_packet_handlers):
try:
del d[k]
except:
pass
def set_packet_handlers(self, to, defs):
""" configures the given packet handlers,
and make sure we remove any existing ones with the same key
(which can be useful for subclasses, not here)
"""
log("set_packet_handlers(%s, %s)", to, defs)
self.remove_packet_handlers(*defs.keys())
for k,v in defs.items():