123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- import threading
- import socket
- import struct
- import time
- import sys
- import importlib
- sys.path.append('/home/server/repositories/repositories/sri-project.demo-py/sri-server-bg03')
- protobuf = importlib.import_module(f"protobuf.protocol_pb2")
- sys.path.append('/home/server/repositories/repositories/sri-project.demo-py/3rdparty')
- methods = importlib.import_module(f"xlib")
- class Connection(object):
- """"""
- tcp_server = None
- tcp_client = None
- all_connection_dict = {}
- @classmethod
- def get_method_by_command_id(cls, command_id):
- """获取方法名"""
- fn_dict = {
- protobuf.CS_Sign: cls.f1,
- }
- return fn_dict.get(command_id)
- @classmethod
- def f1(cls):
- pass
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @classmethod
- def create_tcp_server(cls, host='0.0.0.0', port=20916):
- cls.tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- cls.tcp_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- cls.tcp_server.bind((host, port))
- cls.tcp_server.listen(10)
- methods.debug_log('Connection.260', f"Server listening on {host}:{port}")
- @classmethod
- def start_listener(cls):
- cls.create_tcp_server()
- head_sequence = '<hh'
- head_size = struct.calcsize(head_sequence)
- while True:
- try:
-
- client, ipv4 = cls.tcp_server.accept()
-
- methods.debug_log('Connection.260', f"Connected by {ipv4}")
-
- head_bytestream = client.recv(head_size)
- if not head_bytestream:
- continue
- command_id, body_length = struct.unpack(head_sequence, head_bytestream)
- methods.debug_log('Connection.284', f"Received values: {command_id, body_length}")
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if command_id == protobuf.CS_KeepAlive:
- continue
- elif command_id == protobuf.CS_Sign:
-
- body_bytestream = client.recv(body_length)
- object = protobuf.CSSign()
- object.ParseFromString(body_bytestream)
- print(f"Received Response: account={object.account}, password={object.password}")
-
- object = protobuf.SCSign()
- object.ret = True
- object.uid = 112233
- object.cid = 223344
- object.name = 'aabbcc'
- command_id = protobuf.SC_Sign
- body_length = object.ByteSize()
- head_bytestream = struct.pack(head_sequence, command_id, body_length)
- body_bytestream = object.SerializeToString()
- send_bytestream = head_bytestream + body_bytestream
- client.sendall(send_bytestream)
- print(f"Sent data: {send_bytestream}")
-
-
-
-
-
-
-
- except Exception as exception:
- methods.debug_log('Connection.287', f"#exception: {exception}")
- methods.debug_log('Connection.287', f"#traceback: {methods.trace_log()}")
- finally:
- client.close()
- @classmethod
- def run(cls, background_is=True):
- thread_list = [
- threading.Thread(target=cls.start_listener),
- ]
- for thread in thread_list:
- thread.setDaemon(True)
- thread.start()
- if background_is:
- return
- for thread in thread_list:
- thread.join()
- if __name__ == '__main__':
- print(protobuf.CS_Offer)
- Connection.run(background_is=False)
|