public DeviceNetworkPacketEvent(int netId, string?address, uint frequency, string senderAddress, EntityUid sender, NetworkPayload data) { NetId = netId; Address = address; Frequency = frequency; SenderAddress = senderAddress; Sender = sender; Data = data; }
public async Task NetworkDeviceSendAndReceive() { await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings { NoClient = true, ExtraPrototypes = Prototypes }); var server = pairTracker.Pair.Server; var mapManager = server.ResolveDependency <IMapManager>(); var entityManager = server.ResolveDependency <IEntityManager>(); var deviceNetSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkSystem>(); var deviceNetTestSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkTestSystem>(); EntityUid device1 = default; EntityUid device2 = default; DeviceNetworkComponent networkComponent1 = null; DeviceNetworkComponent networkComponent2 = null; var testValue = "test"; var payload = new NetworkPayload { ["Test"] = testValue, ["testnumber"] = 1, ["testbool"] = true }; await server.WaitAssertion(() => { mapManager.CreateNewMapEntity(MapId.Nullspace); device1 = entityManager.SpawnEntity("DummyNetworkDevice", MapCoordinates.Nullspace); Assert.That(entityManager.TryGetComponent(device1, out networkComponent1), Is.True); Assert.That(networkComponent1.ReceiveFrequency != null, Is.True); Assert.That(networkComponent1.Address, Is.Not.EqualTo(string.Empty)); device2 = entityManager.SpawnEntity("DummyNetworkDevice", MapCoordinates.Nullspace); Assert.That(entityManager.TryGetComponent(device2, out networkComponent2), Is.True); Assert.That(networkComponent1.ReceiveFrequency != null, Is.True); Assert.That(networkComponent2.Address, Is.Not.EqualTo(string.Empty)); Assert.That(networkComponent1.Address, Is.Not.EqualTo(networkComponent2.Address)); deviceNetSystem.QueuePacket(device1, networkComponent2.Address, payload, networkComponent2.ReceiveFrequency.Value); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); await server.WaitAssertion(() => { CollectionAssert.AreEquivalent(deviceNetTestSystem.LastPayload, payload); }); await pairTracker.CleanReturnAsync(); }
// Sends camera information to all monitors currently interested. private void SendCameraInfo(EntityUid uid, NetworkPayload payload, SurveillanceCameraRouterComponent?router = null) { if (!Resolve(uid, ref router)) { return; } foreach (var address in router.MonitorRoutes) { _deviceNetworkSystem.QueuePacket(uid, address, payload); } }
private void PingCameraNetwork(EntityUid uid, SurveillanceCameraMonitorComponent?monitor = null) { if (!Resolve(uid, ref monitor)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraPingMessage } }; _deviceNetworkSystem.QueuePacket(uid, null, payload); }
// Pings a subnet to get all camera information. private void PingSubnet(EntityUid uid, SurveillanceCameraRouterComponent?router = null) { if (!Resolve(uid, ref router)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraPingMessage }, { SurveillanceCameraSystem.CameraSubnetData, router.SubnetName } }; _deviceNetworkSystem.QueuePacket(uid, null, payload, router.SubnetFrequency); }
private void ConnectCamera(EntityUid uid, string origin, string address, SurveillanceCameraRouterComponent?router = null) { if (!Resolve(uid, ref router)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraConnectMessage }, { SurveillanceCameraSystem.CameraAddressData, origin } }; _deviceNetworkSystem.QueuePacket(uid, address, payload, router.SubnetFrequency); }
private void SubnetPingResponse(EntityUid uid, string origin, SurveillanceCameraRouterComponent?router = null) { if (!Resolve(uid, ref router) || router.SubnetFrequencyId == null) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraSubnetData }, { SurveillanceCameraSystem.CameraSubnetData, router.SubnetFrequencyId } }; _deviceNetworkSystem.QueuePacket(uid, origin, payload); }
/// <summary> /// Broadcast a sync packet to an air alarm's local network. /// </summary> public void SyncAllDevices(EntityUid uid) { if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent monitor) && !monitor.NetEnabled) { return; } var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = AirAlarmSyncCmd }; _deviceNet.QueuePacket(uid, string.Empty, Freq, payload, true); }
private void DisconnectFromSubnet(EntityUid uid, string subnet, SurveillanceCameraMonitorComponent?monitor = null) { if (!Resolve(uid, ref monitor) || !monitor.KnownSubnets.TryGetValue(subnet, out var address)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraSubnetDisconnectMessage }, }; _deviceNetworkSystem.QueuePacket(uid, address, payload); }
private void RequestActiveSubnetInfo(EntityUid uid, SurveillanceCameraMonitorComponent?monitor = null) { if (!Resolve(uid, ref monitor) || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var address)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraPingSubnetMessage }, }; _deviceNetworkSystem.QueuePacket(uid, address, payload); }
/// <summary> /// Sync this air alarm's mode with the rest of the network. /// </summary> /// <param name="mode">The mode to sync with the rest of the network.</param> public void SyncMode(EntityUid uid, AirAlarmMode mode) { if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent? monitor) && !monitor.NetEnabled) { return; } var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = AirAlarmSetMode, [AirAlarmSetMode] = mode }; _deviceNet.QueuePacket(uid, null, payload); }
/// <summary> /// Send a sync packet to a specific device from an air alarm. /// </summary> /// <param name="address">The address of the device.</param> public void SyncDevice(EntityUid uid, string address) { if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent? monitor) && !monitor.NetEnabled) { return; } var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = AirAlarmSyncCmd }; _deviceNet.QueuePacket(uid, address, payload); }
private void SendHeartbeat(EntityUid uid, SurveillanceCameraMonitorComponent?monitor = null) { if (!Resolve(uid, ref monitor) || monitor.LastHeartbeatSent < _heartbeatDelay || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var subnetAddress)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraHeartbeatMessage }, { SurveillanceCameraSystem.CameraAddressData, monitor.ActiveCameraAddress } }; _deviceNetworkSystem.QueuePacket(uid, subnetAddress, payload); }
// -- API -- /// <summary> /// Set the data for an air alarm managed device. /// </summary> /// <param name="address">The address of the device.</param> /// <param name="data">The data to send to the device.</param> public void SetData(EntityUid uid, string address, IAtmosDeviceData data) { if (EntityManager.TryGetComponent(uid, out AtmosMonitorComponent monitor) && !monitor.NetEnabled) { return; } var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = AirAlarmSetData, // [AirAlarmTypeData] = type, [AirAlarmSetData] = data }; _deviceNet.QueuePacket(uid, address, Freq, payload); }
private void TrySwitchCameraByAddress(EntityUid uid, string address, SurveillanceCameraMonitorComponent?monitor = null) { if (!Resolve(uid, ref monitor) || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var subnetAddress)) { return; } var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraConnectMessage }, { SurveillanceCameraSystem.CameraAddressData, address } }; monitor.NextCameraAddress = address; _deviceNetworkSystem.QueuePacket(uid, subnetAddress, payload); }
/// <summary> /// Toggles the state of the switch and sents a <see cref="DeviceNetworkConstants.CmdSetState"/> command with the /// <see cref="DeviceNetworkConstants.StateEnabled"/> value set to state. /// </summary> private void OnInteracted(EntityUid uid, ApcNetSwitchComponent component, InteractHandEvent args) { if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? networkComponent)) { return; } component.State = !component.State; var payload = new NetworkPayload { [DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdSetState, [DeviceNetworkConstants.StateEnabled] = component.State, }; _deviceNetworkSystem.QueuePacket(uid, DeviceNetworkConstants.NullAddress, networkComponent.Frequency, payload, true); args.Handled = true; }
/// <summary> /// Serialize suit sensor status into device network package. /// </summary> public NetworkPayload SuitSensorToPacket(SuitSensorStatus status) { var payload = new NetworkPayload() { [DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdUpdatedState, [SuitSensorConstants.NET_NAME] = status.Name, [SuitSensorConstants.NET_JOB] = status.Job, [SuitSensorConstants.NET_IS_ALIVE] = status.IsAlive, }; if (status.TotalDamage != null) { payload.Add(SuitSensorConstants.NET_TOTAL_DAMAGE, status.TotalDamage); } if (status.Coordinates != null) { payload.Add(SuitSensorConstants.NET_CORDINATES, status.Coordinates); } return(payload); }
/// <summary> /// Try to deserialize device network message into suit sensor status /// </summary> public SuitSensorStatus?PacketToSuitSensor(NetworkPayload payload) { // check command if (!payload.TryGetValue(DeviceNetworkConstants.Command, out string?command)) { return(null); } if (command != DeviceNetworkConstants.CmdUpdatedState) { return(null); } // check name, job and alive if (!payload.TryGetValue(SuitSensorConstants.NET_NAME, out string?name)) { return(null); } if (!payload.TryGetValue(SuitSensorConstants.NET_JOB, out string?job)) { return(null); } if (!payload.TryGetValue(SuitSensorConstants.NET_IS_ALIVE, out bool?isAlive)) { return(null); } // try get total damage and cords (optionals) payload.TryGetValue(SuitSensorConstants.NET_TOTAL_DAMAGE, out int?totalDamage); payload.TryGetValue(SuitSensorConstants.NET_CORDINATES, out MapCoordinates? cords); var status = new SuitSensorStatus(name, job) { IsAlive = isAlive.Value, TotalDamage = totalDamage, Coordinates = cords }; return(status); }
private void OnPacketRecv(EntityUid uid, GasVentPumpComponent component, DeviceNetworkPacketEvent args) { if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? netConn) || !EntityManager.TryGetComponent(uid, out AtmosAlarmableComponent? alarmable) || !args.Data.TryGetValue(DeviceNetworkConstants.Command, out var cmd)) { return; } var payload = new NetworkPayload(); switch (cmd) { case AirAlarmSystem.AirAlarmSyncCmd: payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSyncData); payload.Add(AirAlarmSystem.AirAlarmSyncData, component.ToAirAlarmData()); _deviceNetSystem.QueuePacket(uid, args.SenderAddress, payload, device: netConn); return; case AirAlarmSystem.AirAlarmSetData: if (!args.Data.TryGetValue(AirAlarmSystem.AirAlarmSetData, out GasVentPumpData? setData)) { break; } component.FromAirAlarmData(setData); UpdateState(uid, component); alarmable.IgnoreAlarms = setData.IgnoreAlarms; payload.Add(DeviceNetworkConstants.Command, AirAlarmSystem.AirAlarmSetDataStatus); payload.Add(AirAlarmSystem.AirAlarmSetDataStatus, true); _deviceNetSystem.QueuePacket(uid, null, payload, device: netConn); return; } }
private void OnPacketReceived(EntityUid uid, DeviceNetworkComponent component, DeviceNetworkPacketEvent args) { LastPayload = args.Data; }
public async Task NetworkDeviceSendAndReceive() { var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes, ContentBeforeIoC = () => { IoCManager.Resolve <IEntitySystemManager>().LoadExtraSystemType <DeviceNetworkTestSystem>(); } }; var server = StartServerDummyTicker(options); await server.WaitIdleAsync(); var mapManager = server.ResolveDependency <IMapManager>(); var entityManager = server.ResolveDependency <IEntityManager>(); var deviceNetSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkSystem>(); var deviceNetTestSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkTestSystem>(); IEntity device1 = null; IEntity device2 = null; DeviceNetworkComponent networkComponent1 = null; DeviceNetworkComponent networkComponent2 = null; var testValue = "test"; var payload = new NetworkPayload { ["Test"] = testValue, ["testnumber"] = 1, ["testbool"] = true }; server.Assert(() => { mapManager.CreateNewMapEntity(MapId.Nullspace); device1 = entityManager.SpawnEntity("DummyNetworkDevice", MapCoordinates.Nullspace); Assert.That(device1.TryGetComponent(out networkComponent1), Is.True); Assert.That(networkComponent1.Open, Is.True); Assert.That(networkComponent1.Address, Is.Not.EqualTo(string.Empty)); device2 = entityManager.SpawnEntity("DummyNetworkDevice", MapCoordinates.Nullspace); Assert.That(device2.TryGetComponent(out networkComponent2), Is.True); Assert.That(networkComponent2.Open, Is.True); Assert.That(networkComponent2.Address, Is.Not.EqualTo(string.Empty)); Assert.That(networkComponent1.Address, Is.Not.EqualTo(networkComponent2.Address)); deviceNetSystem.QueuePacket(device1.Uid, networkComponent2.Address, networkComponent2.Frequency, payload); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { CollectionAssert.AreEquivalent(deviceNetTestSystem.LastPayload, payload); }); }
private void OnPacketReceived(EntityUid uid, SurveillanceCameraComponent component, DeviceNetworkPacketEvent args) { if (!component.Active) { return; } if (!TryComp(uid, out DeviceNetworkComponent? deviceNet)) { return; } if (args.Data.TryGetValue(DeviceNetworkConstants.Command, out string?command)) { var payload = new NetworkPayload() { { DeviceNetworkConstants.Command, string.Empty }, { CameraAddressData, deviceNet.Address }, { CameraNameData, component.CameraId }, { CameraSubnetData, string.Empty } }; var dest = string.Empty; switch (command) { case CameraConnectMessage: if (!args.Data.TryGetValue(CameraAddressData, out dest) || string.IsNullOrEmpty(args.Address)) { return; } payload[DeviceNetworkConstants.Command] = CameraConnectMessage; break; case CameraHeartbeatMessage: if (!args.Data.TryGetValue(CameraAddressData, out dest) || string.IsNullOrEmpty(args.Address)) { return; } payload[DeviceNetworkConstants.Command] = CameraHeartbeatMessage; break; case CameraPingMessage: if (!args.Data.TryGetValue(CameraSubnetData, out string?subnet)) { return; } dest = args.SenderAddress; payload[CameraSubnetData] = subnet; payload[DeviceNetworkConstants.Command] = CameraDataMessage; break; } _deviceNetworkSystem.QueuePacket( uid, dest, payload); } }
public async Task WiredNetworkDeviceSendAndReceive() { var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes, ContentBeforeIoC = () => { IoCManager.Resolve <IEntitySystemManager>().LoadExtraSystemType <DeviceNetworkTestSystem>(); } }; var server = StartServer(options); await server.WaitIdleAsync(); var mapManager = server.ResolveDependency <IMapManager>(); var entityManager = server.ResolveDependency <IEntityManager>(); var deviceNetSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkSystem>(); var deviceNetTestSystem = entityManager.EntitySysManager.GetEntitySystem <DeviceNetworkTestSystem>(); EntityUid device1 = default; EntityUid device2 = default; DeviceNetworkComponent networkComponent1 = null; DeviceNetworkComponent networkComponent2 = null; WiredNetworkComponent wiredNetworkComponent = null; IMapGrid grid = null; var testValue = "test"; var payload = new NetworkPayload { ["Test"] = testValue, ["testnumber"] = 1, ["testbool"] = true }; await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { var map = mapManager.CreateNewMapEntity(MapId.Nullspace); grid = mapManager.CreateGrid(MapId.Nullspace); device1 = entityManager.SpawnEntity("DummyWiredNetworkDevice", MapCoordinates.Nullspace); Assert.That(entityManager.TryGetComponent(device1, out networkComponent1), Is.True); Assert.That(entityManager.TryGetComponent(device1, out wiredNetworkComponent), Is.True); Assert.That(networkComponent1.Open, Is.True); Assert.That(networkComponent1.Address, Is.Not.EqualTo(string.Empty)); device2 = entityManager.SpawnEntity("DummyWiredNetworkDevice", new MapCoordinates(new Robust.Shared.Maths.Vector2(0, 2), MapId.Nullspace)); Assert.That(entityManager.TryGetComponent(device2, out networkComponent2), Is.True); Assert.That(networkComponent2.Open, Is.True); Assert.That(networkComponent2.Address, Is.Not.EqualTo(string.Empty)); Assert.That(networkComponent1.Address, Is.Not.EqualTo(networkComponent2.Address)); deviceNetSystem.QueuePacket(device1, networkComponent2.Address, networkComponent2.Frequency, payload); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { //CollectionAssert.AreNotEqual(deviceNetTestSystem.LastPayload, payload); entityManager.SpawnEntity("CableApcExtension", grid.MapToGrid(new MapCoordinates(new Robust.Shared.Maths.Vector2(0, 1), MapId.Nullspace))); deviceNetSystem.QueuePacket(device1, networkComponent2.Address, networkComponent2.Frequency, payload); }); await server.WaitRunTicks(1); await server.WaitIdleAsync(); server.Assert(() => { CollectionAssert.AreEqual(deviceNetTestSystem.LastPayload, payload); }); await server.WaitIdleAsync(); }