Disposal mailing (#2194)
* Implement device networking * Implement device configuration menu * Fix device network * Implement disposal mailing unit * Implement base network connection Implement wired and wireless network connection Implement device network metadata * Fix dereference null error * Fix wired network null checks * Change BaseNetworks enum to NetworkUtils class Add PingResponse function to NetworkUtils Change device network file structure * Add doc comments * Apply suggestions from code review Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Add tag validation to disposal mailing unit * Add tag validation to the mailing unit component * Address reviews Change WiredNetwork can connect check Change device networking string literals to constants * Address reviews Revert changes to PowerProvider and PowerReceiver Add new NodeGroup WELP * Fix recursive access to Owner property * Integrate suggested changes * Fix TryGetWireNet acting on NullPowerProvider Fix network connections not checking if their owner has been deleted * Close device network connection when the owning entity got deleted Fix mailing unit not closing the device network connection on remove * Remove GetWireNet from NullPowerProvider Co-authored-by: Julian Giebel <j.giebel@netrocks.info> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
198
Content.Server/DeviceNetwork/DeviceNetwork.cs
Normal file
198
Content.Server/DeviceNetwork/DeviceNetwork.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using Content.Server.Interfaces;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public delegate void OnReceiveNetMessage(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast);
|
||||
|
||||
public class DeviceNetwork : IDeviceNetwork
|
||||
{
|
||||
private const int PACKAGES_PER_TICK = 30;
|
||||
|
||||
private readonly IRobustRandom _random = IoCManager.Resolve<IRobustRandom>();
|
||||
private readonly Dictionary<int, List<NetworkDevice>> _devices = new Dictionary<int, List<NetworkDevice>>();
|
||||
private readonly Queue<NetworkPackage> _packages = new Queue<NetworkPackage>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public DeviceNetworkConnection Register(int netId, int frequency, OnReceiveNetMessage messageHandler, bool receiveAll = false)
|
||||
{
|
||||
var address = GenerateValidAddress(netId, frequency);
|
||||
|
||||
var device = new NetworkDevice
|
||||
{
|
||||
Address = address,
|
||||
Frequency = frequency,
|
||||
ReceiveAll = receiveAll,
|
||||
ReceiveNetMessage = messageHandler
|
||||
};
|
||||
|
||||
AddDevice(netId, device);
|
||||
|
||||
return new DeviceNetworkConnection(this, netId, address, frequency);
|
||||
}
|
||||
|
||||
public DeviceNetworkConnection Register(int netId, OnReceiveNetMessage messageHandler, bool receiveAll = false)
|
||||
{
|
||||
return Register(netId, 0, messageHandler, receiveAll);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var i = PACKAGES_PER_TICK;
|
||||
while (_packages.Count > 0 && i > 0)
|
||||
{
|
||||
i--;
|
||||
|
||||
var package = _packages.Dequeue();
|
||||
|
||||
if (package.Broadcast)
|
||||
{
|
||||
BroadcastPackage(package);
|
||||
continue;
|
||||
}
|
||||
|
||||
SendPackage(package);
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnqueuePackage(int netId, int frequency, string address, IReadOnlyDictionary<string, string> data, string sender, Metadata metadata, bool broadcast = false)
|
||||
{
|
||||
if (!_devices.ContainsKey(netId))
|
||||
return false;
|
||||
|
||||
var package = new NetworkPackage()
|
||||
{
|
||||
NetId = netId,
|
||||
Frequency = frequency,
|
||||
Address = address,
|
||||
Broadcast = broadcast,
|
||||
Data = data,
|
||||
Sender = sender,
|
||||
Metadata = metadata
|
||||
};
|
||||
|
||||
_packages.Enqueue(package);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void RemoveDevice(int netId, int frequency, string address)
|
||||
{
|
||||
var device = DeviceWithAddress(netId, frequency, address);
|
||||
_devices[netId].Remove(device);
|
||||
}
|
||||
|
||||
public void SetDeviceReceiveAll(int netId, int frequency, string address, bool receiveAll)
|
||||
{
|
||||
var device = DeviceWithAddress(netId, frequency, address);
|
||||
device.ReceiveAll = receiveAll;
|
||||
}
|
||||
|
||||
public bool GetDeviceReceiveAll(int netId, int frequency, string address)
|
||||
{
|
||||
var device = DeviceWithAddress(netId, frequency, address);
|
||||
return device.ReceiveAll;
|
||||
}
|
||||
|
||||
private string GenerateValidAddress(int netId, int frequency)
|
||||
{
|
||||
var unique = false;
|
||||
var devices = DevicesForFrequency(netId, frequency);
|
||||
var address = "";
|
||||
|
||||
while (!unique)
|
||||
{
|
||||
address = _random.Next().ToString("x");
|
||||
unique = !devices.Exists(device => device.Address == address);
|
||||
}
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
private void AddDevice(int netId, NetworkDevice networkDevice)
|
||||
{
|
||||
if(!_devices.ContainsKey(netId))
|
||||
_devices[netId] = new List<NetworkDevice>();
|
||||
|
||||
_devices[netId].Add(networkDevice);
|
||||
}
|
||||
|
||||
private List<NetworkDevice> DevicesForFrequency(int netId, int frequency)
|
||||
{
|
||||
if (!_devices.ContainsKey(netId))
|
||||
return new List<NetworkDevice>();
|
||||
|
||||
var result = _devices[netId].FindAll(device => device.Frequency == frequency);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private NetworkDevice DeviceWithAddress(int netId, int frequency, string address)
|
||||
{
|
||||
var devices = DevicesForFrequency(netId, frequency);
|
||||
|
||||
var device = devices.Find(device => device.Address == address);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
private List<NetworkDevice> DevicesWithReceiveAll(int netId, int frequency)
|
||||
{
|
||||
if (!_devices.ContainsKey(netId))
|
||||
return new List<NetworkDevice>();
|
||||
|
||||
var result = _devices[netId].FindAll(device => device.Frequency == frequency && device.ReceiveAll);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void BroadcastPackage(NetworkPackage package)
|
||||
{
|
||||
var devices = DevicesForFrequency(package.NetId, package.Frequency);
|
||||
SendToDevices(devices, package, true);
|
||||
}
|
||||
|
||||
private void SendPackage(NetworkPackage package)
|
||||
{
|
||||
var devices = DevicesWithReceiveAll(package.NetId, package.Frequency);
|
||||
var device = DeviceWithAddress(package.NetId, package.Frequency, package.Address);
|
||||
|
||||
devices.Add(device);
|
||||
|
||||
SendToDevices(devices, package, false);
|
||||
}
|
||||
|
||||
private void SendToDevices(List<NetworkDevice> devices, NetworkPackage package, bool broadcast)
|
||||
{
|
||||
for (var index = 0; index < devices.Count; index++)
|
||||
{
|
||||
var device = devices[index];
|
||||
if (device.Address == package.Sender)
|
||||
continue;
|
||||
|
||||
device.ReceiveNetMessage(package.Frequency, package.Sender, package.Data, package.Metadata, broadcast);
|
||||
}
|
||||
}
|
||||
|
||||
internal class NetworkDevice
|
||||
{
|
||||
public int Frequency;
|
||||
public string Address;
|
||||
public OnReceiveNetMessage ReceiveNetMessage;
|
||||
public bool ReceiveAll;
|
||||
}
|
||||
|
||||
internal class NetworkPackage
|
||||
{
|
||||
public int NetId;
|
||||
public int Frequency;
|
||||
public string Address;
|
||||
public bool Broadcast;
|
||||
public IReadOnlyDictionary<string, string> Data { get; set; }
|
||||
public Metadata Metadata;
|
||||
public string Sender;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
72
Content.Server/DeviceNetwork/DeviceNetworkConnection.cs
Normal file
72
Content.Server/DeviceNetwork/DeviceNetworkConnection.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Content.Server.Interfaces;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public class DeviceNetworkConnection : IDeviceNetworkConnection
|
||||
{
|
||||
private readonly DeviceNetwork _network;
|
||||
[ViewVariables]
|
||||
private readonly int _netId;
|
||||
|
||||
[ViewVariables]
|
||||
public bool Open { get; internal set; }
|
||||
[ViewVariables]
|
||||
public string Address { get; internal set; }
|
||||
[ViewVariables]
|
||||
public int Frequency { get; internal set; }
|
||||
|
||||
[ViewVariables]
|
||||
public bool RecieveAll
|
||||
{
|
||||
get => _network.GetDeviceReceiveAll(_netId, Frequency, Address);
|
||||
set => _network.SetDeviceReceiveAll(_netId, Frequency, Address, value);
|
||||
}
|
||||
|
||||
public DeviceNetworkConnection(DeviceNetwork network, int netId, string address, int frequency)
|
||||
{
|
||||
_network = network;
|
||||
_netId = netId;
|
||||
Open = true;
|
||||
Address = address;
|
||||
Frequency = frequency;
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, IReadOnlyDictionary<string, string> payload, Metadata metadata)
|
||||
{
|
||||
return Open && _network.EnqueuePackage(_netId, frequency, address, payload, Address, metadata);
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(frequency, address, payload);
|
||||
}
|
||||
|
||||
public bool Send(string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(0, address, payload);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, IReadOnlyDictionary<string, string> payload, Metadata metadata)
|
||||
{
|
||||
return Open && _network.EnqueuePackage(_netId, frequency, "", payload, Address, metadata, true);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(frequency, payload);
|
||||
}
|
||||
|
||||
public bool Broadcast(Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(0, payload);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_network.RemoveDevice(_netId, Frequency, Address);
|
||||
Open = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Content.Server/DeviceNetwork/Metadata.cs
Normal file
20
Content.Server/DeviceNetwork/Metadata.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public class Metadata : Dictionary<string, object>
|
||||
{
|
||||
public bool TryParseMetadata<T>(string key, [NotNullWhen(true)] out T data)
|
||||
{
|
||||
if(TryGetValue(key, out var value) && value is T typedValue)
|
||||
{
|
||||
data = typedValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
data = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Content.Server.Interfaces;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public abstract class BaseNetworkConnection : IDeviceNetworkConnection
|
||||
{
|
||||
protected readonly DeviceNetworkConnection Connection;
|
||||
|
||||
protected OnReceiveNetMessage MessageHandler;
|
||||
|
||||
[ViewVariables]
|
||||
public bool Open => Connection.Open;
|
||||
[ViewVariables]
|
||||
public string Address => Connection.Address;
|
||||
[ViewVariables]
|
||||
public int Frequency => Connection.Frequency;
|
||||
|
||||
protected BaseNetworkConnection(int netId, int frequency, OnReceiveNetMessage onReceive, bool receiveAll)
|
||||
{
|
||||
var network = IoCManager.Resolve<IDeviceNetwork>();
|
||||
Connection = network.Register(netId, frequency, OnReceiveNetMessage, receiveAll);
|
||||
MessageHandler = onReceive;
|
||||
|
||||
}
|
||||
|
||||
public bool Send(int frequency, string address, Dictionary<string, string> payload)
|
||||
{
|
||||
var data = ManipulatePayload(payload);
|
||||
var metadata = GetMetadata();
|
||||
return Connection.Send(frequency, address, data, metadata);
|
||||
}
|
||||
|
||||
public bool Send(string address, Dictionary<string, string> payload)
|
||||
{
|
||||
return Send(0, address, payload);
|
||||
}
|
||||
|
||||
public bool Broadcast(int frequency, Dictionary<string, string> payload)
|
||||
{
|
||||
var data = ManipulatePayload(payload);
|
||||
var metadata = GetMetadata();
|
||||
return Connection.Broadcast(frequency, data, metadata);
|
||||
}
|
||||
|
||||
public bool Broadcast(Dictionary<string, string> payload)
|
||||
{
|
||||
return Broadcast(0, payload);
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Connection.Close();
|
||||
}
|
||||
|
||||
private void OnReceiveNetMessage(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (CanReceive(frequency, sender, payload, metadata, broadcast))
|
||||
{
|
||||
MessageHandler(frequency, sender, payload, metadata, broadcast);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast);
|
||||
protected abstract Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload);
|
||||
protected abstract Metadata GetMetadata();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using Content.Server.GameObjects.Components.NodeContainer;
|
||||
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
|
||||
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public class WiredNetworkConnection : BaseNetworkConnection
|
||||
{
|
||||
public const string WIRENET = "powernet";
|
||||
|
||||
private readonly IEntity _owner;
|
||||
|
||||
public WiredNetworkConnection(OnReceiveNetMessage onReceive, bool receiveAll, IEntity owner) : base(NetworkUtils.WIRED, 0, onReceive, receiveAll)
|
||||
{
|
||||
_owner = owner;
|
||||
}
|
||||
|
||||
protected override bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_owner.TryGetComponent<PowerReceiverComponent>(out var powerReceiver)
|
||||
&& TryGetWireNet(powerReceiver, out var ownNet)
|
||||
&& metadata.TryParseMetadata<INodeGroup>(WIRENET, out var senderNet))
|
||||
{
|
||||
return ownNet.Equals(senderNet);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override Metadata GetMetadata()
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
if (_owner.TryGetComponent<PowerReceiverComponent>(out var powerReceiver)
|
||||
&& TryGetWireNet(powerReceiver, out var net))
|
||||
{
|
||||
var metadata = new Metadata
|
||||
{
|
||||
{WIRENET, net }
|
||||
};
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
protected override Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload)
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
private bool TryGetWireNet(PowerReceiverComponent powerReceiver, out INodeGroup net)
|
||||
{
|
||||
if (powerReceiver.Provider is PowerProviderComponent && powerReceiver.Provider.ProviderOwner.TryGetComponent<NodeContainerComponent>(out var nodeContainer))
|
||||
{
|
||||
var nodes = nodeContainer.Nodes;
|
||||
for (var index = 0; index < nodes.Count; index++)
|
||||
{
|
||||
if (nodes[index].NodeGroupID == NodeGroupID.WireNet)
|
||||
{
|
||||
net = nodes[index].NodeGroup;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
net = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Maths;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
public class WirelessNetworkConnection : BaseNetworkConnection
|
||||
{
|
||||
public const string WIRELESS_POSITION = "position";
|
||||
|
||||
private readonly IEntity _owner;
|
||||
|
||||
private float _range;
|
||||
public float Range { get => _range; set => _range = Math.Abs(value); }
|
||||
|
||||
public WirelessNetworkConnection(int frequency, OnReceiveNetMessage onReceive, bool receiveAll, IEntity owner, float range) : base(NetworkUtils.WIRELESS, frequency, onReceive, receiveAll)
|
||||
{
|
||||
_owner = owner;
|
||||
Range = range;
|
||||
}
|
||||
|
||||
protected override bool CanReceive(int frequency, string sender, IReadOnlyDictionary<string, string> payload, Metadata metadata, bool broadcast)
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (metadata.TryParseMetadata<Vector2>(WIRELESS_POSITION, out var position))
|
||||
{
|
||||
var ownPosition = _owner.Transform.WorldPosition;
|
||||
var distance = (ownPosition - position).Length;
|
||||
return distance <= Range;
|
||||
}
|
||||
//Only receive packages with the same frequency
|
||||
return frequency == Frequency;
|
||||
}
|
||||
|
||||
protected override Metadata GetMetadata()
|
||||
{
|
||||
if (_owner.Deleted)
|
||||
{
|
||||
Connection.Close();
|
||||
return new Metadata();
|
||||
}
|
||||
|
||||
var position = _owner.Transform.WorldPosition;
|
||||
var metadata = new Metadata
|
||||
{
|
||||
{WIRELESS_POSITION, position}
|
||||
};
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
protected override Dictionary<string, string> ManipulatePayload(Dictionary<string, string> payload)
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Content.Server/DeviceNetwork/NetworkUtils.cs
Normal file
38
Content.Server/DeviceNetwork/NetworkUtils.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Content.Server.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems.DeviceNetwork
|
||||
{
|
||||
/// <summary>
|
||||
/// A collection of utilities to help with using device networks
|
||||
/// </summary>
|
||||
public static class NetworkUtils
|
||||
{
|
||||
public const int PRIVATE = 0;
|
||||
public const int WIRED = 1;
|
||||
public const int WIRELESS = 2;
|
||||
|
||||
public const string COMMAND = "command";
|
||||
public const string MESSAGE = "message";
|
||||
public const string PING = "ping";
|
||||
|
||||
/// <summary>
|
||||
/// Handles responding to pings.
|
||||
/// </summary>
|
||||
public static void PingResponse<T>(T connection, string sender, IReadOnlyDictionary<string, string> payload, string message = "") where T : IDeviceNetworkConnection
|
||||
{
|
||||
if (payload.TryGetValue(COMMAND, out var command) && command == PING)
|
||||
{
|
||||
var response = new Dictionary<string, string>
|
||||
{
|
||||
{COMMAND, "ping_response"},
|
||||
{MESSAGE, message}
|
||||
};
|
||||
|
||||
connection.Send(connection.Frequency, sender, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user