|
@@ -1,282 +0,0 @@
|
|
|
-
|
|
|
-
|
|
|
-import struct
|
|
|
-import asyncio
|
|
|
-import time
|
|
|
-
|
|
|
-
|
|
|
-import sys
|
|
|
-import importlib
|
|
|
-
|
|
|
-
|
|
|
-sys.path.append('/home/server/repositories/repositories/sri-project.demo-py/sri-server-bg03')
|
|
|
-sys.path.append('/home/server/repositories/repositories/sri-project.demo-py/3rdparty')
|
|
|
-
|
|
|
-protobuf = importlib.import_module(f"xprotobuf.protocol_pb2")
|
|
|
-methods = importlib.import_module(f"xlib")
|
|
|
-
|
|
|
-
|
|
|
-class SRIConnection(asyncio.Protocol):
|
|
|
- """"""
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- clients = {}
|
|
|
-
|
|
|
- def connection_made(self, client):
|
|
|
- self.client = client
|
|
|
- self.peername = client.get_extra_info('peername')
|
|
|
- print(f"Connection from {self.peername}")
|
|
|
-
|
|
|
-
|
|
|
- self.head_sequence = '<hh'
|
|
|
- self.head_size = struct.calcsize(self.head_sequence)
|
|
|
- self.connection_id = f"{self.peername[0].replace('.', '')}{self.peername[1]}"
|
|
|
-
|
|
|
-
|
|
|
- self.clients[self.connection_id] = client, methods.now_ts(), 'type=vehicle'
|
|
|
-
|
|
|
- def data_received(self, data):
|
|
|
- asyncio.create_task(self.handle_data(data))
|
|
|
-
|
|
|
- async def handle_data(self, data):
|
|
|
-
|
|
|
- loop = asyncio.get_running_loop()
|
|
|
-
|
|
|
- await loop.run_in_executor(None, self.process_data, data)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- def process_data(self, data):
|
|
|
-
|
|
|
- head_data = data[:self.head_size]
|
|
|
- body_data = data[self.head_size:]
|
|
|
-
|
|
|
- command_id, body_length = struct.unpack(self.head_sequence, head_data)
|
|
|
- methods.debug_log('SRIConnection.37', f"Received values: {command_id}, {body_length}")
|
|
|
-
|
|
|
- if command_id == protobuf.CS_KeepAlive:
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: None")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: None")
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Sign:
|
|
|
-
|
|
|
-
|
|
|
- object = protobuf.CSSign()
|
|
|
- object.ParseFromString(body_data)
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.100",
|
|
|
- f"Received Response: account={object.account}, password={object.password}")
|
|
|
-
|
|
|
-
|
|
|
- object = protobuf.SCSign()
|
|
|
- object.ret = True
|
|
|
- object.uid = 112233
|
|
|
- object.cid = 223344
|
|
|
- object.name = 'aabbcc'
|
|
|
- re_command_id = protobuf.SC_Sign
|
|
|
- re_body_length = object.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = object.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Add:
|
|
|
-
|
|
|
-
|
|
|
- """
|
|
|
- CSAdd: 消息体
|
|
|
- CSAdd.serial: string
|
|
|
- CSAdd.type: int32 EgoType::Car | EgoType::None EgoType::User EgoType::Car
|
|
|
- CSAdd.name: string
|
|
|
- """
|
|
|
- object0 = protobuf.CSAdd()
|
|
|
- object0.ParseFromString(body_data)
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.105", f"#serial: {object0.serial}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.105", f"#name: {object0.name}")
|
|
|
-
|
|
|
-
|
|
|
- """
|
|
|
- Robot: 消息体
|
|
|
- Robot.rid: int32
|
|
|
- Robot.name: string
|
|
|
- Robot.type: int32
|
|
|
- Robot.state: RobotState Offline Online Busy
|
|
|
- """
|
|
|
- o1 = protobuf.Robot()
|
|
|
- o1.rid = 112233
|
|
|
- o1.name = "aabbcc"
|
|
|
- o1.type = 123
|
|
|
- o1.state = protobuf.Robot.Online
|
|
|
- """
|
|
|
- SCAddRobot: 消息体
|
|
|
- SCAddRobot.robot: Robot
|
|
|
- """
|
|
|
- o2 = protobuf.SCAddRobot()
|
|
|
- o2.robot.CopyFrom(o1)
|
|
|
- re_command_id = protobuf.SC_NotifyAdd
|
|
|
- re_body_length = o2.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o2.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
-
|
|
|
- o1 = protobuf.SCAdd()
|
|
|
- o1.ret = True
|
|
|
- o1.uid = 112233
|
|
|
- o1.name = 'aabbcc'
|
|
|
- re_command_id = protobuf.SC_Add
|
|
|
- re_body_length = o1.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o1.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Offer:
|
|
|
-
|
|
|
-
|
|
|
- object = protobuf.Offer()
|
|
|
- object.ParseFromString(body_data)
|
|
|
- print(f"Received Response: peer={object.peer}")
|
|
|
-
|
|
|
-
|
|
|
- o1 = protobuf.Offer()
|
|
|
- o1.index = object.index
|
|
|
- o1.peer = object.peer
|
|
|
- o1.type = object.type
|
|
|
- o1.sdp = object.sdp
|
|
|
- re_command_id = protobuf.SC_NotifyOffer
|
|
|
- re_body_length = o1.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o1.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Answer:
|
|
|
-
|
|
|
-
|
|
|
- object = protobuf.Answer()
|
|
|
- object.ParseFromString(body_data)
|
|
|
- print(f"Received Response: peer={object.peer}")
|
|
|
-
|
|
|
-
|
|
|
- o1 = protobuf.Offer()
|
|
|
- o1.index = object.index
|
|
|
- o1.peer = object.peer
|
|
|
- o1.type = object.type
|
|
|
- o1.sdp = object.sdp
|
|
|
- re_command_id = protobuf.SC_NotifyAnswer
|
|
|
- re_body_length = o1.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o1.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Candidate:
|
|
|
-
|
|
|
-
|
|
|
- object = protobuf.Candidate()
|
|
|
- object.ParseFromString(body_data)
|
|
|
- print(f"Received Response: peer={object.peer}")
|
|
|
-
|
|
|
-
|
|
|
- o1 = protobuf.Offer()
|
|
|
- o1.index = object.index
|
|
|
- o1.peer = object.peer
|
|
|
- o1.type = object.type
|
|
|
- o1.candidate = object.candidate
|
|
|
- o1.sdpMLineIndex = object.sdpMLineIndex
|
|
|
- o1.sdpMid = object.sdpMid
|
|
|
- o1.egotype = object.egotype
|
|
|
-
|
|
|
- re_command_id = protobuf.SC_NotifyCandidate
|
|
|
- re_body_length = o1.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o1.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
- elif command_id == protobuf.CS_Req:
|
|
|
-
|
|
|
-
|
|
|
- """
|
|
|
- CSReq: 消息体
|
|
|
- CSReq.peer: int32(uid,用户标识)
|
|
|
- CSReq.index: int32(相机位置,RenderPosition)
|
|
|
- CSReq.egotype: int32(终端类型,舱端/车端)
|
|
|
- """
|
|
|
- o1 = protobuf.CSReq()
|
|
|
- o1.ParseFromString(body_data)
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"#peer: {o1.peer}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"#index: {o1.index}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"#egotype: {o1.egotype}")
|
|
|
-
|
|
|
-
|
|
|
- """
|
|
|
- CSReq: 消息体
|
|
|
- CSReq.peer: int32(uid,用户标识)
|
|
|
- CSReq.index: int32(相机位置,RenderPosition)
|
|
|
- CSReq.egotype: int32(终端类型,舱端/车端)
|
|
|
- """
|
|
|
- o2 = protobuf.CSReq()
|
|
|
- o2.peer = o1.peer
|
|
|
- o2.index = o1.index
|
|
|
- o2.type = o1.egotype
|
|
|
- re_command_id = protobuf.SC_NotifyReq
|
|
|
- re_body_length = o2.ByteSize()
|
|
|
- re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
|
|
|
- re_body_data = o2.SerializeToString()
|
|
|
- re_send_data = re_head_data + re_body_data
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_command_id: {re_command_id}")
|
|
|
- methods.debug_log(f"{self.connection_id} | SRIConnection.113", f"re_send_data: {re_send_data}")
|
|
|
- self.client.write(re_send_data)
|
|
|
-
|
|
|
- def connection_lost(self, exc):
|
|
|
- print(f"SRIConnection closed by {self.peername}")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- @classmethod
|
|
|
- async def run(cls):
|
|
|
- loop = asyncio.get_running_loop()
|
|
|
- server = await loop.create_server(
|
|
|
- lambda: SRIConnection(),
|
|
|
- '0.0.0.0', 20917
|
|
|
- )
|
|
|
- async with server:
|
|
|
- print(f"Server listening on 0.0.0.0:20917")
|
|
|
- methods.debug_log(f"SRIConnection.221", f"Server listening on 0.0.0.0:20917")
|
|
|
- await server.serve_forever()
|
|
|
-
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- asyncio.run(SRIConnection.run())
|
|
|
-
|