Connection_e1.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. """
  2. """
  3. # from hub import methods, Global
  4. import struct
  5. import asyncio
  6. import time
  7. import socket
  8. import sys
  9. import importlib
  10. # --- for linux
  11. # sys.path.append('/home/sri/repositories/repositories/SRI-DINO.Server-py/sri-server-bg03')
  12. # sys.path.append('/home/sri/repositories/repositories/SRI-DINO.Server-py/sri-pysdk')
  13. # --- for windows
  14. sys.path.append(r'C:\SRI-DINO.Server-py\sri-server-bg03')
  15. sys.path.append(r'C:\SRI-DINO.Server-py\sri-pysdk')
  16. protobuf = importlib.import_module(f"xprotobuf.protocol_pb2")
  17. methods = importlib.import_module(f"xlib")
  18. clients = {} # {<ipv4>: (socket, update_at, type)} | {<连接id>: (socket对象, 最后一次请求时间, 客户端类型)}
  19. serial_rid_dict = {
  20. '65F7171A-5585-46C7-A9D6-967ABA9EB223': 1000000,
  21. '7AF3F619-5067-4EE0-A710-89A6CB097EFE': 1000001,
  22. 'ECB93A87-560B-4022-8C5F-CBF9FE1E596A': 1000002,
  23. 'C0D14B6F-0FF0-4B68-877D-D2CB886FCD0E': 1000002,
  24. 'E537DDFB-6E3E-4E1A-AD18-AC21393BE300': 1000004, # 正在使用
  25. }
  26. account_uid_dict = {
  27. 'ego': 3
  28. }
  29. # live_relationship = {} # {<id-1>id-2>: True}
  30. class SRIConnection(asyncio.Protocol):
  31. """"""
  32. head_sequence = '<hh' # 字节序规则
  33. head_size = struct.calcsize(head_sequence)
  34. message_data = b''
  35. def __init__(self):
  36. self.lock = asyncio.Lock() # 初始化锁
  37. def connection_made(self, client):
  38. """
  39. 建立客户端连接
  40. """
  41. peername = client.get_extra_info('peername')
  42. # self.connection_id = f"{peername[0].replace('.', '')}-{peername[1]}" # 12700130000 客户端id
  43. self.connection_id = int(f"{peername[0].replace('.', '')}")
  44. self.client = client
  45. # self.data = b''
  46. self.client_type = None
  47. self.client_info = None
  48. self.update_at = methods.now_ts()
  49. clients[self.connection_id] = self
  50. # 获取底层 socket
  51. # sock = self.client.get_extra_info('socket')
  52. # 查看缓冲区大小
  53. # recv_buffer_size = sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
  54. # send_buffer_size = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
  55. # methods.debug_log(f'{self.connection_id}|SRIConnection70', f"Receive buffer size: {recv_buffer_size}")
  56. # methods.debug_log(f'{self.connection_id}|SRIConnection70', f"Send buffer size: {send_buffer_size}")
  57. def connection_lost(self, exc):
  58. """
  59. 关闭连接
  60. """
  61. methods.debug_log(f"{self.connection_id}|SRIConnection085", f"连接已关闭")
  62. # --- 处理车端掉线,通知所有舱端
  63. if self.client_type == 'vehicle':
  64. """
  65. SCDelRobot: 消息体
  66. SCDelRobot.peer: int32
  67. SCDelRobot.egotype: int32
  68. """
  69. o2 = protobuf.SCDelRobot()
  70. o2.peer = self.client_info.get('connection_id') # 车端id
  71. o2.egotype = self.client_info.get('egotype')
  72. re_command_id = protobuf.SC_NotifyDel # 4017
  73. re_body_length = o2.ByteSize()
  74. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  75. re_body_data = o2.SerializeToString()
  76. re_send_data = re_head_data + re_body_data
  77. methods.debug_log(f"{self.connection_id}|SRIConnection085", f"re_command_id: {re_command_id}")
  78. for item in clients.values():
  79. if item.client_type == 'cockpit':
  80. item.client.write(re_send_data)
  81. # --- 处理舱端掉线,通知所有车端
  82. if self.client_type == 'cockpit':
  83. """
  84. Leave: 消息体
  85. Leave.peer: int32(车端rid)
  86. Leave.egotype: int32
  87. """
  88. o2 = protobuf.Leave()
  89. o2.peer = self.client_info.get('connection_id') # 舱端id
  90. o2.egotype = 1 # 客户端类型
  91. re_command_id = protobuf.SC_NotifyLeave # 4014
  92. re_body_length = o2.ByteSize()
  93. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  94. re_body_data = o2.SerializeToString()
  95. re_send_data = re_head_data + re_body_data
  96. methods.debug_log(f"{self.connection_id}|SRIConnection515", f"re_command_id: {re_command_id}")
  97. for item in clients.values():
  98. if item.client_type == 'vehicle':
  99. item.client.write(re_send_data)
  100. # --- clean
  101. if self.connection_id in clients:
  102. del clients[self.connection_id]
  103. # --- clean live_relationship
  104. # keys = [key for key in live_relationship.keys() if self.connection_id in key]
  105. # for key in keys:
  106. # del live_relationship[key]
  107. def data_received(self, data):
  108. """
  109. 消息处理
  110. """
  111. asyncio.create_task(self.handle_data(data))
  112. async def handle_data(self, data):
  113. """
  114. 消息处理
  115. """
  116. # --- before
  117. # loop = asyncio.get_running_loop()
  118. # await loop.run_in_executor(None, self.process_data, data)
  119. # async with self.lock: # 加入锁,确保只有一个任务在处理数据
  120. # await self.process_data(data)
  121. async with self.lock: # 加入锁,确保只有一个任务在处理数据
  122. loop = asyncio.get_running_loop()
  123. await loop.run_in_executor(None, self.process_data, data) # 调用非异步函数
  124. def process_data(self, data):
  125. """
  126. 消息处理
  127. """
  128. try:
  129. # methods.debug_log(f"{self.connection_id}|SRIConnection114", f"receive: {len(data)}, message: {repr(data)}")
  130. self.message_data += data # 执行了这个以后
  131. # methods.debug_log(f"{self.connection_id}|SRIConnection114", f"--- 1")
  132. # methods.debug_log(f"{self.connection_id}|SRIConnection114", f"--- 2")
  133. # methods.debug_log(f"{self.connection_id}|SRIConnection114", f"--- 3")
  134. # count = 0
  135. while True:
  136. # --- print
  137. # count += 1
  138. # methods.debug_log(f"{self.connection_id}|SRIConnection123",
  139. # f"---------------------------------- while count: {count}")
  140. # 确保有足够的字节来处理消息头
  141. if len(self.message_data) < self.head_size:
  142. break
  143. # 获取消息头
  144. head_data = self.message_data[:self.head_size]
  145. command_id, body_length = struct.unpack(self.head_sequence, head_data)
  146. if command_id not in [2008]:
  147. methods.debug_log(f'{self.connection_id}|SRIConnection176',
  148. f"command_id: {command_id}, body_length: {body_length}")
  149. # 检查命令ID是否有效
  150. if not (2000 <= command_id < 5000):
  151. self.message_data = b'' # 清空无效数据
  152. break
  153. # 检查完整消息是否接收完毕
  154. total_length = self.head_size + body_length
  155. if len(self.message_data) < total_length:
  156. break # 数据不完整,等待更多数据
  157. # 调用相应处理方法
  158. method = getattr(self, f'message{command_id}', None)
  159. if method:
  160. method(self.message_data[self.head_size:total_length]) # 处理完整消息
  161. else:
  162. methods.debug_log(f"{self.connection_id}|SRIConnectionError",
  163. f"No handler for command_id: {command_id}")
  164. # 移除已处理的消息
  165. self.message_data = self.message_data[total_length:]
  166. except Exception as exception:
  167. methods.debug_log(f"{self.connection_id}|SRIConnection132", f"#exception: {exception}")
  168. methods.debug_log(f"{self.connection_id}|SRIConnection123", f"#traceback: {methods.trace_log()}")
  169. def message2008(self, body_data):
  170. # methods.debug_log(f"{self.connection_id}|SRIConnection80", f"message: 2008")
  171. pass
  172. def message2009(self, body_data):
  173. # --- 监听 2009
  174. """
  175. CSAdd: 消息体
  176. CSAdd.serial: string
  177. CSAdd.type: int32 EgoType::Car | 0 EgoType::None 1 EgoType::User 2 EgoType::Car
  178. CSAdd.name: string
  179. """
  180. object0 = protobuf.CSAdd()
  181. object0.ParseFromString(body_data)
  182. methods.debug_log(f"{self.connection_id}|SRIConnection219", f"#serial: {object0.serial}")
  183. # methods.debug_log(f"{self.connection_id}|SRIConnection90", f"#name: {object0.name}")
  184. # methods.debug_log(f"{self.connection_id}|SRIConnection90", f"#type: {object0.type}")
  185. # --- update ---
  186. self.client_type = 'vehicle'
  187. self.client_info = {
  188. 'connection_id': self.connection_id,
  189. 'rid': serial_rid_dict.get(object0.serial),
  190. 'name': object0.name,
  191. 'serial': object0.serial,
  192. 'egotype': 2, # 客户端类型
  193. }
  194. """
  195. SCAddRobot: 消息体
  196. SCAddRobot.robot: Robot
  197. Robot: 消息体
  198. Robot.rid: int32
  199. Robot.name: string
  200. Robot.type: int32
  201. Robot.state: RobotState Offline Online Busy
  202. """
  203. o1 = protobuf.Robot()
  204. # o1.rid = self.client_info.get('rid')
  205. o1.rid = self.client_info.get('connection_id')
  206. o1.name = object0.name
  207. o1.type = 2 # 0 EgoType::None 1 EgoType::User 2 EgoType::Car
  208. o1.state = protobuf.Robot.Online
  209. o2 = protobuf.SCAddRobot()
  210. o2.robot.CopyFrom(o1)
  211. re_command_id = protobuf.SC_NotifyAdd # 4016
  212. re_body_length = o2.ByteSize()
  213. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  214. re_body_data = o2.SerializeToString()
  215. re_send_data = re_head_data + re_body_data
  216. # --- send 4016 发送全部舱端
  217. for item in clients.values():
  218. if item.client_type == 'cockpit':
  219. methods.debug_log(f"{self.connection_id}|SRIConnection136", f"re_command_id: {re_command_id}")
  220. item.client.write(re_send_data)
  221. # --- send 4007 todo 凯强说并未用到
  222. # o1 = protobuf.SCAdd()
  223. # o1.ret = True
  224. # o1.uid = 112233 # todo 这个uid,应该是哪个舱端永辉在操作这个车 | 但是现在还没有人操控车
  225. # o1.name = object0.name
  226. # re_command_id = protobuf.SC_Add # 4007
  227. # re_body_length = o1.ByteSize()
  228. # re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  229. # re_body_data = o1.SerializeToString()
  230. # re_send_data = re_head_data + re_body_data
  231. # methods.debug_log(f"{self.connection_id} | SRIConnection150", f"re_command_id: {re_command_id}")
  232. # self.client.write(re_send_data)
  233. def message2000(self, body_data):
  234. # --- 解析消息体
  235. object = protobuf.CSSign()
  236. object.ParseFromString(body_data)
  237. methods.debug_log(f"{self.connection_id}|SRIConnection162", f"#account: {object.account}")
  238. methods.debug_log(f"{self.connection_id}|SRIConnection162", f"#password: {object.password}")
  239. # --- update ---
  240. self.client_type = 'cockpit'
  241. self.client_info = {
  242. 'connection_id': self.connection_id,
  243. 'uid': 3, # 对应数据库里的ego的id
  244. 'name': 'ego', # 对应数据库里
  245. 'egotype': 1, # 舱端类型
  246. }
  247. # --- send 4000
  248. object = protobuf.SCSign()
  249. object.ret = True
  250. # object.uid = self.client_info.get('uid')
  251. object.uid = self.client_info.get('connection_id')
  252. object.name = self.client_info.get('name')
  253. re_command_id = protobuf.SC_Sign # 4000
  254. re_body_length = object.ByteSize()
  255. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  256. re_body_data = object.SerializeToString()
  257. re_send_data = re_head_data + re_body_data
  258. methods.debug_log(f"{self.connection_id}|SRIConnection175", f"re_command_id: {re_command_id}")
  259. self.client.write(re_send_data)
  260. def message2010(self, body_data):
  261. # --- send 4008 发送全部车端信息列表
  262. """
  263. Robot: 消息体
  264. Robot.rid: string
  265. Robot.name: string
  266. Robot.type: int32 EgoType::Car | EgoType::None EgoType::User EgoType::Car
  267. Robot.state: enum Offline Online Busy
  268. """
  269. # --- 获取全部在线车辆列表
  270. o2 = protobuf.SCRobot()
  271. for item in clients.values():
  272. if item.client_type and item.client_type == 'vehicle':
  273. o1 = protobuf.Robot()
  274. # o1.rid = item.client_info.get('rid')
  275. o1.rid = item.client_info.get('connection_id')
  276. o1.name = item.client_info.get('name')
  277. o1.type = 2 # EgoType::Car
  278. o1.state = protobuf.Robot.RobotState.Value('Online')
  279. o2.robot.add().CopyFrom(o1)
  280. re_command_id = protobuf.SC_Robot # 4008
  281. re_body_length = o2.ByteSize()
  282. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  283. re_body_data = o2.SerializeToString()
  284. re_send_data = re_head_data + re_body_data
  285. methods.debug_log(f"{self.connection_id}|SRIConnection306", f"re_command_id: {re_command_id}")
  286. self.client.write(re_send_data)
  287. def message2001(self, body_data):
  288. # --- 解析消息体
  289. """
  290. CSReq: 消息体
  291. CSReq.peer: int32(rid,车端唯一标识)
  292. CSReq.index: int32(相机位置,RenderPosition)
  293. CSReq.egotype: int32(终端类型,舱端/车端)
  294. """
  295. o1 = protobuf.CSReq()
  296. o1.ParseFromString(body_data)
  297. methods.debug_log(f"{self.connection_id}|SRIConnection299.message2001", f"#peer: {o1.peer}")
  298. methods.debug_log(f"{self.connection_id}|SRIConnection299.message2001", f"#index: {o1.index}")
  299. # methods.debug_log(f"{self.connection_id}|SRIConnection299.message2001", f"#uid: {self.client_info.get('uid')}")
  300. # methods.debug_log(f"{self.connection_id}|SRIConnection299.message2001", f"#rid: {self.client_info.get('rid')}")
  301. # --- send 4009 指定车端
  302. for item in clients.values():
  303. if item.client_info.get('connection_id') == o1.peer:
  304. """
  305. CSReq: 消息体
  306. CSReq.peer: int32(rid,车端唯一标识)
  307. CSReq.index: int32(相机位置,RenderPosition)
  308. CSReq.egotype: int32(终端类型,舱端/车端)
  309. """
  310. o2 = protobuf.CSReq()
  311. o2.peer = self.client_info.get('connection_id') # 舱端id
  312. o2.index = o1.index
  313. o2.egotype = o1.egotype
  314. re_command_id = protobuf.SC_NotifyReq # 4009
  315. re_body_length = o2.ByteSize()
  316. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  317. re_body_data = o2.SerializeToString()
  318. re_send_data = re_head_data + re_body_data
  319. methods.debug_log(f"{self.connection_id}|SRIConnection217", f"re_command_id: {re_command_id}")
  320. item.client.write(re_send_data)
  321. def message2002(self, body_data):
  322. # --- 解析消息体
  323. """
  324. CSRep: 消息体
  325. CSRep.desc: VideoDesc
  326. CSRep.peer: int32
  327. CSRep.index: int32
  328. CSRep.egotype: int32
  329. """
  330. o1 = protobuf.CSRep()
  331. o1.ParseFromString(body_data)
  332. methods.debug_log(f"{self.connection_id}|SRIConnection319", f"#peer: {o1.peer}")
  333. # methods.debug_log(f"{self.connection_id}|SRIConnection235", f"#uid: {self.client_info.get('uid')}")
  334. # methods.debug_log(f"{self.connection_id}|SRIConnection235", f"#rid: {self.client_info.get('rid')}")
  335. # --- send 4010 指定舱端
  336. for item in clients.values():
  337. if item.client_info.get('connection_id') == o1.peer:
  338. o2 = protobuf.CSRep()
  339. o2.desc = o1.desc
  340. o2.peer = self.client_info.get('connection_id') # 车端id
  341. o2.index = o1.index
  342. o2.egotype = o1.egotype
  343. re_command_id = protobuf.SC_NotifyRep # 4010
  344. re_body_length = o2.ByteSize()
  345. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  346. re_body_data = o2.SerializeToString()
  347. re_send_data = re_head_data + re_body_data
  348. methods.debug_log(f"{self.connection_id}|SRIConnection217", f"re_command_id: {re_command_id}")
  349. item.client.write(re_send_data)
  350. def message2004(self, body_data):
  351. # --- 解析消息体
  352. """
  353. Offer: 消息体
  354. Offer.index: int32
  355. Offer.peer: int32(车端rid)
  356. Offer.type: string
  357. Offer.sdp: string
  358. """
  359. object = protobuf.Offer()
  360. object.ParseFromString(body_data)
  361. methods.debug_log(f"{self.connection_id}|SRIConnection348", f"#peer: {object.peer}")
  362. # methods.debug_log(f"{self.connection_id}|SRIConnection348", f"#uid: {self.client_info.get('uid')}")
  363. # methods.debug_log(f"{self.connection_id}|SRIConnection348", f"#rid: {self.client_info.get('rid')}")
  364. # --- send 4012 指定车端
  365. for item in clients.values():
  366. if item.client_info.get('connection_id') == object.peer:
  367. o1 = protobuf.Offer()
  368. o1.index = object.index
  369. o1.peer = self.client_info.get('connection_id') # 舱端id
  370. o1.type = object.type
  371. o1.sdp = object.sdp
  372. re_command_id = protobuf.SC_NotifyOffer # 4012
  373. re_body_length = o1.ByteSize()
  374. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  375. re_body_data = o1.SerializeToString()
  376. re_send_data = re_head_data + re_body_data
  377. methods.debug_log(f"{self.connection_id}|SRIConnection341", f"re_command_id: {re_command_id}")
  378. item.client.write(re_send_data)
  379. def message2005(self, body_data):
  380. # --- 解析消息体
  381. """
  382. Answer: 消息体
  383. Answer.index: int32
  384. Answer.peer: int32(舱端uid)
  385. Answer.type: string
  386. Answer.sdp: string
  387. """
  388. object = protobuf.Answer()
  389. object.ParseFromString(body_data)
  390. methods.debug_log(f"{self.connection_id}|SRIConnection411", f"#peer: {object.peer}")
  391. # methods.debug_log(f"{self.connection_id}|SRIConnection411", f"#uid: {self.client_info.get('uid')}")
  392. # methods.debug_log(f"{self.connection_id}|SRIConnection411", f"#rid: {self.client_info.get('rid')}")
  393. # --- send 4011 指定舱端
  394. for item in clients.values():
  395. if item.client_info.get('connection_id') == object.peer:
  396. o1 = protobuf.Answer()
  397. o1.index = object.index
  398. o1.peer = self.client_info.get('connection_id') # 车端id
  399. o1.type = object.type
  400. o1.sdp = object.sdp
  401. re_command_id = protobuf.SC_NotifyAnswer # 4011
  402. re_body_length = o1.ByteSize()
  403. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  404. re_body_data = o1.SerializeToString()
  405. re_send_data = re_head_data + re_body_data
  406. methods.debug_log(f"{self.connection_id}|SRIConnection428", f"re_command_id: {re_command_id}")
  407. methods.debug_log(f"{self.connection_id}|SRIConnection428", f"#index: {o1.index}")
  408. item.client.write(re_send_data)
  409. def message2006(self, body_data):
  410. # --- 解析消息体
  411. """
  412. Candidate: 消息体
  413. Candidate.index: int32
  414. Candidate.peer: int32(车端rid)
  415. Candidate.type: string
  416. Candidate.candidate: string
  417. Candidate.sdpMLineIndex: int32
  418. Candidate.sdpMid: string
  419. Candidate.egotype: int32
  420. """
  421. object = protobuf.Candidate()
  422. object.ParseFromString(body_data)
  423. methods.debug_log(f"{self.connection_id}|SRIConnection413", f"#peer: {object.peer}")
  424. # methods.debug_log(f"{self.connection_id}|SRIConnection413", f"#uid: {self.client_info.get('uid')}")
  425. # methods.debug_log(f"{self.connection_id}|SRIConnection413", f"#rid: {self.client_info.get('rid')}")
  426. for item in clients.values():
  427. # --- send 4013 舱端到车端
  428. if item.client_info.get('connection_id') == object.peer:
  429. o1 = protobuf.Candidate()
  430. o1.index = object.index
  431. o1.peer = self.client_info.get('connection_id') # 舱端id,
  432. o1.type = object.type
  433. o1.candidate = object.candidate
  434. o1.sdpMLineIndex = object.sdpMLineIndex
  435. o1.sdpMid = object.sdpMid
  436. o1.egotype = object.egotype
  437. re_command_id = protobuf.SC_NotifyCandidate # 4013
  438. re_body_length = o1.ByteSize()
  439. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  440. re_body_data = o1.SerializeToString()
  441. re_send_data = re_head_data + re_body_data
  442. methods.debug_log(f"{self.connection_id}|SRIConnection409", f"re_command_id: {re_command_id}")
  443. item.client.write(re_send_data)
  444. # --- send 4013 车端到舱端
  445. if item.client_info.get('connection_id') == object.peer:
  446. o1 = protobuf.Candidate()
  447. o1.index = object.index
  448. o1.peer = self.client_info.get('connection_id') # 车端id,
  449. o1.type = object.type
  450. o1.candidate = object.candidate
  451. o1.sdpMLineIndex = object.sdpMLineIndex
  452. o1.sdpMid = object.sdpMid
  453. o1.egotype = object.egotype
  454. re_command_id = protobuf.SC_NotifyCandidate # 4013
  455. re_body_length = o1.ByteSize()
  456. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  457. re_body_data = o1.SerializeToString()
  458. re_send_data = re_head_data + re_body_data
  459. methods.debug_log(f"{self.connection_id}|SRIConnection426", f"re_command_id: {re_command_id}")
  460. # methods.debug_log(f"{self.connection_id}|SRIConnection426", f"#candidate.out: {o1.candidate}")
  461. item.client.write(re_send_data)
  462. def message2007(self, body_data):
  463. # --- 解析消息体
  464. """
  465. Leave: 消息体
  466. Leave.peer: int32(车端rid)
  467. Leave.egotype: int32
  468. """
  469. o1 = protobuf.Leave()
  470. o1.ParseFromString(body_data)
  471. methods.debug_log(f"{self.connection_id}|SRIConnection497", f"#peer: {o1.peer}")
  472. # --- send 4009 指定车端
  473. for item in clients.values():
  474. if item.client_info.get('connection_id') == o1.peer:
  475. """
  476. Leave: 消息体
  477. Leave.peer: int32(车端rid)
  478. Leave.egotype: int32
  479. """
  480. o2 = protobuf.Leave()
  481. o2.peer = self.client_info.get('connection_id') # 舱端id
  482. o2.egotype = o1.egotype
  483. re_command_id = protobuf.SC_NotifyLeave # 4014
  484. re_body_length = o2.ByteSize()
  485. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  486. re_body_data = o2.SerializeToString()
  487. re_send_data = re_head_data + re_body_data
  488. methods.debug_log(f"{self.connection_id}|SRIConnection515", f"re_command_id: {re_command_id}")
  489. item.client.write(re_send_data)
  490. def message2014(self, body_data):
  491. # --- 解析消息体
  492. """
  493. message CSState
  494. {
  495. UserState state=1;
  496. int32 uid=2;
  497. };
  498. """
  499. o1 = protobuf.CSState()
  500. o1.ParseFromString(body_data)
  501. methods.debug_log(f"{self.connection_id}|SRIConnection|572", f"#state: {o1.state}")
  502. methods.debug_log(f"{self.connection_id}|SRIConnection|572", f"#uid: {o1.uid}")
  503. # --- send 4016 发送全部舱端
  504. for item in clients.values():
  505. if item.client_type == 'cockpit':
  506. """
  507. message SCState
  508. {
  509. UserState state=1;
  510. int32 uid=2;
  511. };
  512. """
  513. o2 = protobuf.SCState()
  514. o2.state = o1.state
  515. o2.uid = o1.uid # 车端id
  516. re_command_id = protobuf.SC_State # 4022
  517. re_body_length = o2.ByteSize()
  518. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  519. re_body_data = o2.SerializeToString()
  520. re_send_data = re_head_data + re_body_data
  521. methods.debug_log(f"{self.connection_id}|SRIConnection551", f"re_command_id: {re_command_id}")
  522. item.client.write(re_send_data)
  523. # --- send 6011 指发送车端操作用户的id
  524. for item in clients.values():
  525. if item.client_info.get('connection_id') == o1.uid:
  526. """
  527. Leave: 消息体
  528. Leave.peer: int32(车端rid)
  529. Leave.egotype: int32
  530. """
  531. o3 = protobuf.UserActivityInfo()
  532. o3.user_uuid = 'SSGGSSEEFFHHWWSS' # 用户uuid
  533. o3.cockpit_id = self.connection_id # 舱端id
  534. o3.vehicle_id = 112233 # 车端id
  535. re_command_id = protobuf.S2V_SendUserInfo # 6011
  536. re_body_length = o3.ByteSize()
  537. re_head_data = struct.pack(self.head_sequence, re_command_id, re_body_length)
  538. re_body_data = o3.SerializeToString()
  539. re_send_data = re_head_data + re_body_data
  540. methods.debug_log(f"{self.connection_id}|SRIConnection611", f"re_command_id: {re_command_id}")
  541. item.client.write(re_send_data)
  542. @staticmethod
  543. async def check_clients():
  544. """
  545. 剔除掉线连接
  546. """
  547. count = 0
  548. while True:
  549. count += 1
  550. print(f"#count: {count}", flush=True)
  551. now_at = methods.now_ts()
  552. for connection_id in list(clients.keys()):
  553. object = clients.get(connection_id)
  554. print(f"#client: {object.client}, #update_at: {object.update_at}, #client_type: {object.client_type}",
  555. flush=True)
  556. await asyncio.sleep(3) # 发送消息的间隔时间
  557. @staticmethod
  558. async def run():
  559. """
  560. """
  561. # --- define ---
  562. loop = asyncio.get_running_loop()
  563. server = await loop.create_server(
  564. lambda: SRIConnection(),
  565. '0.0.0.0', 20917
  566. )
  567. # --- start ---
  568. async with server:
  569. print("Server listening on 0.0.0.0:20917", flush=True)
  570. # await loop.create_task(SRIConnection.check_clients())
  571. await server.serve_forever()
  572. if __name__ == '__main__':
  573. asyncio.run(SRIConnection.run())