DeviceNetworkSystem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using Content.Server.DeviceNetwork.Components;
  2. using Content.Shared.DeviceNetwork;
  3. using JetBrains.Annotations;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Random;
  6. using System.Buffers;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Numerics;
  9. using Content.Shared.DeviceNetwork.Components;
  10. using Content.Shared.Examine;
  11. namespace Content.Server.DeviceNetwork.Systems
  12. {
  13. /// <summary>
  14. /// Entity system that handles everything device network related.
  15. /// Device networking allows machines and devices to communicate with each other while adhering to restrictions like range or being connected to the same powernet.
  16. /// </summary>
  17. [UsedImplicitly]
  18. public sealed class DeviceNetworkSystem : EntitySystem
  19. {
  20. [Dependency] private readonly IRobustRandom _random = default!;
  21. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  22. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  23. [Dependency] private readonly DeviceListSystem _deviceLists = default!;
  24. [Dependency] private readonly NetworkConfiguratorSystem _configurator = default!;
  25. private readonly Dictionary<int, DeviceNet> _networks = new(4);
  26. private readonly Queue<DeviceNetworkPacketEvent> _queueA = new();
  27. private readonly Queue<DeviceNetworkPacketEvent> _queueB = new();
  28. /// <summary>
  29. /// The queue being processed in the current tick
  30. /// </summary>
  31. private Queue<DeviceNetworkPacketEvent> _activeQueue = null!;
  32. /// <summary>
  33. /// The queue that will be processed in the next tick
  34. /// </summary>
  35. private Queue<DeviceNetworkPacketEvent> _nextQueue = null!;
  36. public override void Initialize()
  37. {
  38. SubscribeLocalEvent<DeviceNetworkComponent, MapInitEvent>(OnMapInit);
  39. SubscribeLocalEvent<DeviceNetworkComponent, ComponentShutdown>(OnNetworkShutdown);
  40. SubscribeLocalEvent<DeviceNetworkComponent, ExaminedEvent>(OnExamine);
  41. _activeQueue = _queueA;
  42. _nextQueue = _queueB;
  43. }
  44. public override void Update(float frameTime)
  45. {
  46. while (_activeQueue.TryDequeue(out var packet))
  47. {
  48. SendPacket(packet);
  49. }
  50. SwapQueues();
  51. }
  52. /// <summary>
  53. /// Sends the given payload as a device network packet to the entity with the given address and frequency.
  54. /// Addresses are given to the DeviceNetworkComponent of an entity when connecting.
  55. /// </summary>
  56. /// <param name="uid">The EntityUid of the sending entity</param>
  57. /// <param name="address">The address of the entity that the packet gets sent to. If null, the message is broadcast to all devices on that frequency (except the sender)</param>
  58. /// <param name="frequency">The frequency to send on</param>
  59. /// <param name="data">The data to be sent</param>
  60. /// <returns>Returns true when the packet was successfully enqueued.</returns>
  61. public bool QueuePacket(EntityUid uid, string? address, NetworkPayload data, uint? frequency = null, int? network = null, DeviceNetworkComponent? device = null)
  62. {
  63. if (!Resolve(uid, ref device, false))
  64. return false;
  65. if (device.Address == string.Empty)
  66. return false;
  67. frequency ??= device.TransmitFrequency;
  68. if (frequency == null)
  69. return false;
  70. network ??= device.DeviceNetId;
  71. _nextQueue.Enqueue(new DeviceNetworkPacketEvent(network.Value, address, frequency.Value, device.Address, uid, data));
  72. return true;
  73. }
  74. /// <summary>
  75. /// Swaps the active queue.
  76. /// Queues are swapped so that packets being sent in the current tick get processed in the next tick.
  77. /// </summary>
  78. /// <remarks>
  79. /// This prevents infinite loops while sending packets
  80. /// </remarks>
  81. private void SwapQueues()
  82. {
  83. _nextQueue = _activeQueue;
  84. _activeQueue = _activeQueue == _queueA ? _queueB : _queueA;
  85. }
  86. private void OnExamine(EntityUid uid, DeviceNetworkComponent device, ExaminedEvent args)
  87. {
  88. if (device.ExaminableAddress)
  89. {
  90. args.PushText(Loc.GetString("device-address-examine-message", ("address", device.Address)));
  91. }
  92. }
  93. /// <summary>
  94. /// Automatically attempt to connect some devices when a map starts.
  95. /// </summary>
  96. private void OnMapInit(EntityUid uid, DeviceNetworkComponent device, MapInitEvent args)
  97. {
  98. if (device.ReceiveFrequency == null
  99. && device.ReceiveFrequencyId != null
  100. && _protoMan.TryIndex<DeviceFrequencyPrototype>(device.ReceiveFrequencyId, out var receive))
  101. {
  102. device.ReceiveFrequency = receive.Frequency;
  103. }
  104. if (device.TransmitFrequency == null
  105. && device.TransmitFrequencyId != null
  106. && _protoMan.TryIndex<DeviceFrequencyPrototype>(device.TransmitFrequencyId, out var xmit))
  107. {
  108. device.TransmitFrequency = xmit.Frequency;
  109. }
  110. if (device.AutoConnect)
  111. ConnectDevice(uid, device);
  112. }
  113. private DeviceNet GetNetwork(int netId)
  114. {
  115. if (_networks.TryGetValue(netId, out var deviceNet))
  116. return deviceNet;
  117. var newDeviceNet = new DeviceNet(netId, _random);
  118. _networks[netId] = newDeviceNet;
  119. return newDeviceNet;
  120. }
  121. /// <summary>
  122. /// Automatically disconnect when an entity with a DeviceNetworkComponent shuts down.
  123. /// </summary>
  124. private void OnNetworkShutdown(EntityUid uid, DeviceNetworkComponent component, ComponentShutdown args)
  125. {
  126. foreach (var list in component.DeviceLists)
  127. {
  128. _deviceLists.OnDeviceShutdown(list, (uid, component));
  129. }
  130. foreach (var list in component.Configurators)
  131. {
  132. _configurator.OnDeviceShutdown(list, (uid, component));
  133. }
  134. GetNetwork(component.DeviceNetId).Remove(component);
  135. }
  136. /// <summary>
  137. /// Connect an entity with a DeviceNetworkComponent. Note that this will re-use an existing address if the
  138. /// device already had one configured. If there is a clash, the device cannot join the network.
  139. /// </summary>
  140. public bool ConnectDevice(EntityUid uid, DeviceNetworkComponent? device = null)
  141. {
  142. if (!Resolve(uid, ref device, false))
  143. return false;
  144. return GetNetwork(device.DeviceNetId).Add(device);
  145. }
  146. /// <summary>
  147. /// Disconnect an entity with a DeviceNetworkComponent.
  148. /// </summary>
  149. public bool DisconnectDevice(EntityUid uid, DeviceNetworkComponent? device, bool preventAutoConnect = true)
  150. {
  151. if (!Resolve(uid, ref device, false))
  152. return false;
  153. // If manually disconnected, don't auto reconnect when a game state is loaded.
  154. if (preventAutoConnect)
  155. device.AutoConnect = false;
  156. return GetNetwork(device.DeviceNetId).Remove(device);
  157. }
  158. /// <summary>
  159. /// Checks if a device is already connected to its network
  160. /// </summary>
  161. /// <returns>True if the device was found in the network with its corresponding network id</returns>
  162. public bool IsDeviceConnected(EntityUid uid, DeviceNetworkComponent? device)
  163. {
  164. if (!Resolve(uid, ref device, false))
  165. return false;
  166. if (!_networks.TryGetValue(device.DeviceNetId, out var deviceNet))
  167. return false;
  168. return deviceNet.Devices.ContainsValue(device);
  169. }
  170. /// <summary>
  171. /// Checks if an address exists in the network with the given netId
  172. /// </summary>
  173. public bool IsAddressPresent(int netId, string? address)
  174. {
  175. if (address == null || !_networks.TryGetValue(netId, out var network))
  176. return false;
  177. return network.Devices.ContainsKey(address);
  178. }
  179. public void SetReceiveFrequency(EntityUid uid, uint? frequency, DeviceNetworkComponent? device = null)
  180. {
  181. if (!Resolve(uid, ref device, false))
  182. return;
  183. if (device.ReceiveFrequency == frequency) return;
  184. var deviceNet = GetNetwork(device.DeviceNetId);
  185. deviceNet.Remove(device);
  186. device.ReceiveFrequency = frequency;
  187. deviceNet.Add(device);
  188. }
  189. public void SetTransmitFrequency(EntityUid uid, uint? frequency, DeviceNetworkComponent? device = null)
  190. {
  191. if (Resolve(uid, ref device, false))
  192. device.TransmitFrequency = frequency;
  193. }
  194. public void SetReceiveAll(EntityUid uid, bool receiveAll, DeviceNetworkComponent? device = null)
  195. {
  196. if (!Resolve(uid, ref device, false))
  197. return;
  198. if (device.ReceiveAll == receiveAll) return;
  199. var deviceNet = GetNetwork(device.DeviceNetId);
  200. deviceNet.Remove(device);
  201. device.ReceiveAll = receiveAll;
  202. deviceNet.Add(device);
  203. }
  204. public void SetAddress(EntityUid uid, string address, DeviceNetworkComponent? device = null)
  205. {
  206. if (!Resolve(uid, ref device, false))
  207. return;
  208. if (device.Address == address && device.CustomAddress) return;
  209. var deviceNet = GetNetwork(device.DeviceNetId);
  210. deviceNet.Remove(device);
  211. device.CustomAddress = true;
  212. device.Address = address;
  213. deviceNet.Add(device);
  214. }
  215. public void RandomizeAddress(EntityUid uid, DeviceNetworkComponent? device = null)
  216. {
  217. if (!Resolve(uid, ref device, false))
  218. return;
  219. var deviceNet = GetNetwork(device.DeviceNetId);
  220. deviceNet.Remove(device);
  221. device.CustomAddress = false;
  222. device.Address = "";
  223. deviceNet.Add(device);
  224. }
  225. /// <summary>
  226. /// Try to find a device on a network using its address.
  227. /// </summary>
  228. private bool TryGetDevice(int netId, string address, [NotNullWhen(true)] out DeviceNetworkComponent? device) =>
  229. GetNetwork(netId).Devices.TryGetValue(address, out device);
  230. private void SendPacket(DeviceNetworkPacketEvent packet)
  231. {
  232. var network = GetNetwork(packet.NetId);
  233. if (packet.Address == null)
  234. {
  235. // Broadcast to all listening devices
  236. if (network.ListeningDevices.TryGetValue(packet.Frequency, out var devices) && CheckRecipientsList(packet, ref devices))
  237. {
  238. var deviceCopy = ArrayPool<DeviceNetworkComponent>.Shared.Rent(devices.Count);
  239. devices.CopyTo(deviceCopy);
  240. SendToConnections(deviceCopy.AsSpan(0, devices.Count), packet);
  241. ArrayPool<DeviceNetworkComponent>.Shared.Return(deviceCopy);
  242. }
  243. }
  244. else
  245. {
  246. var totalDevices = 0;
  247. var hasTargetedDevice = false;
  248. if (network.ReceiveAllDevices.TryGetValue(packet.Frequency, out var devices))
  249. {
  250. totalDevices += devices.Count;
  251. }
  252. if (TryGetDevice(packet.NetId, packet.Address, out var device) &&
  253. !device.ReceiveAll &&
  254. device.ReceiveFrequency == packet.Frequency)
  255. {
  256. totalDevices += 1;
  257. hasTargetedDevice = true;
  258. }
  259. var deviceCopy = ArrayPool<DeviceNetworkComponent>.Shared.Rent(totalDevices);
  260. if (devices != null)
  261. {
  262. devices.CopyTo(deviceCopy);
  263. }
  264. if (hasTargetedDevice)
  265. {
  266. deviceCopy[totalDevices - 1] = device!;
  267. }
  268. SendToConnections(deviceCopy.AsSpan(0, totalDevices), packet);
  269. ArrayPool<DeviceNetworkComponent>.Shared.Return(deviceCopy);
  270. }
  271. }
  272. /// <summary>
  273. /// Sends the <see cref="BeforeBroadcastAttemptEvent"/> to the sending entity if the packets SendBeforeBroadcastAttemptEvent field is set to true.
  274. /// The recipients is set to the modified recipient list.
  275. /// </summary>
  276. /// <returns>false if the broadcast was canceled</returns>
  277. private bool CheckRecipientsList(DeviceNetworkPacketEvent packet, ref HashSet<DeviceNetworkComponent> recipients)
  278. {
  279. if (!_networks.ContainsKey(packet.NetId) || !_networks[packet.NetId].Devices.ContainsKey(packet.SenderAddress))
  280. return false;
  281. var sender = _networks[packet.NetId].Devices[packet.SenderAddress];
  282. if (!sender.SendBroadcastAttemptEvent)
  283. return true;
  284. var beforeBroadcastAttemptEvent = new BeforeBroadcastAttemptEvent(recipients);
  285. RaiseLocalEvent(packet.Sender, beforeBroadcastAttemptEvent, true);
  286. if (beforeBroadcastAttemptEvent.Cancelled || beforeBroadcastAttemptEvent.ModifiedRecipients == null)
  287. return false;
  288. recipients = beforeBroadcastAttemptEvent.ModifiedRecipients;
  289. return true;
  290. }
  291. private void SendToConnections(ReadOnlySpan<DeviceNetworkComponent> connections, DeviceNetworkPacketEvent packet)
  292. {
  293. if (Deleted(packet.Sender))
  294. {
  295. return;
  296. }
  297. var xform = Transform(packet.Sender);
  298. var senderPos = _transformSystem.GetWorldPosition(xform);
  299. foreach (var connection in connections)
  300. {
  301. if (connection.Owner == packet.Sender)
  302. continue;
  303. BeforePacketSentEvent beforeEv = new(packet.Sender, xform, senderPos, connection.NetIdEnum.ToString());
  304. RaiseLocalEvent(connection.Owner, beforeEv, false);
  305. if (!beforeEv.Cancelled)
  306. RaiseLocalEvent(connection.Owner, packet, false);
  307. else
  308. beforeEv.Uncancel();
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// Event raised before a device network packet is send.
  314. /// Subscribed to by other systems to prevent the packet from being sent.
  315. /// </summary>
  316. public sealed class BeforePacketSentEvent : CancellableEntityEventArgs
  317. {
  318. /// <summary>
  319. /// The EntityUid of the entity the packet was sent from.
  320. /// </summary>
  321. public readonly EntityUid Sender;
  322. public readonly TransformComponent SenderTransform;
  323. /// <summary>
  324. /// The senders current position in world coordinates.
  325. /// </summary>
  326. public readonly Vector2 SenderPosition;
  327. /// <summary>
  328. /// The network the packet will be sent to.
  329. /// </summary>
  330. public readonly string NetworkId;
  331. public BeforePacketSentEvent(EntityUid sender, TransformComponent xform, Vector2 senderPosition, string networkId)
  332. {
  333. Sender = sender;
  334. SenderTransform = xform;
  335. SenderPosition = senderPosition;
  336. NetworkId = networkId;
  337. }
  338. }
  339. /// <summary>
  340. /// Sent to the sending entity before broadcasting network packets to recipients
  341. /// </summary>
  342. public sealed class BeforeBroadcastAttemptEvent : CancellableEntityEventArgs
  343. {
  344. public readonly IReadOnlySet<DeviceNetworkComponent> Recipients;
  345. public HashSet<DeviceNetworkComponent>? ModifiedRecipients;
  346. public BeforeBroadcastAttemptEvent(IReadOnlySet<DeviceNetworkComponent> recipients)
  347. {
  348. Recipients = recipients;
  349. }
  350. }
  351. /// <summary>
  352. /// Event raised when a device network packet gets sent.
  353. /// </summary>
  354. public sealed class DeviceNetworkPacketEvent : EntityEventArgs
  355. {
  356. /// <summary>
  357. /// The id of the network that this packet is being sent on.
  358. /// </summary>
  359. public int NetId;
  360. /// <summary>
  361. /// The frequency the packet is sent on.
  362. /// </summary>
  363. public readonly uint Frequency;
  364. /// <summary>
  365. /// Address of the intended recipient. Null if the message was broadcast.
  366. /// </summary>
  367. public string? Address;
  368. /// <summary>
  369. /// The device network address of the sending entity.
  370. /// </summary>
  371. public readonly string SenderAddress;
  372. /// <summary>
  373. /// The entity that sent the packet.
  374. /// </summary>
  375. public EntityUid Sender;
  376. /// <summary>
  377. /// The data that is being sent.
  378. /// </summary>
  379. public readonly NetworkPayload Data;
  380. public DeviceNetworkPacketEvent(int netId, string? address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data)
  381. {
  382. NetId = netId;
  383. Address = address;
  384. Frequency = frequency;
  385. SenderAddress = senderAddress;
  386. Sender = sender;
  387. Data = data;
  388. }
  389. }
  390. }