public void SendMessageToServer( MessageTypes type, MessagePackSerializableObject data ) { SendMessageToServer( new NetworkMessageContainer(data, type) ); }
/// <summary> /// Asynchronously set keyType to hold the string value. If keyType already holds a value, it is overwritten, regardless of its type. /// </summary> /// <param name="keyType">RedisDBKey value to convert to string and lookup.</param> /// <param name="obj">MsgPack object to serialize.</param> /// <param name="expiry">(optional) TimeSpan date to expire value at.</param> /// <param name="when">Allows you to specify criteria for setting object. Update only if not-exist, etc.</param> /// <param name="commandFlags">Flags to pass to Redis.</param> /// <returns>True or false depending on if value was successfully set.</returns> public async Task <bool> SetValueAsync( RedisDBKeyTypes keyType, MessagePackSerializableObject obj, TimeSpan?expiry = null, SetWhen when = SetWhen.Always, RedisCommandFlags commandFlags = RedisCommandFlags.None) { return(await SetValueAsync(keyType, "", obj, expiry, when, commandFlags)); }
/// <summary> /// Asynchronously set keyType to hold the string value. If keyType already holds a value, it is overwritten, regardless of its type. /// </summary> /// <param name="keyType">Redis keyType to set.</param> /// <param name="keyIdentifier">suffix added to keyType for unique keys. If unused, set to ""</param> /// <param name="obj">MsgPack object to serialize.</param> /// <param name="expiry">(optional) TimeSpan date to expire value at.</param> /// <param name="when">Allows you to specify criteria for setting object. Update only if not-exist, etc.</param> /// <param name="commandFlags">Flags to pass to Redis.</param> /// <returns>True or false depending on if value was successfully set.</returns> public Task <bool> SetValueAsync( RedisDBKeyTypes keyType, string keyIdentifier, MessagePackSerializableObject obj, TimeSpan?expiry = null, SetWhen when = SetWhen.Always, RedisCommandFlags commandFlags = RedisCommandFlags.None) { string keyString = keyType + "_" + keyIdentifier; #if DEBUG LogDebug("[Redis Async Set]: {0}" + expiry, keyString, obj.Serialize().ToString()); #endif return(_db.StringSetAsync(keyString, obj.Serialize(), expiry, (When)when, (CommandFlags)commandFlags)); }
/// <summary> /// Checks messages which should only be handled if the state is active. Returns true if a message is processed. /// </summary> /// <param name="?"></param> /// <returns>Returns true if a message is processed.</returns> bool _checkActiveMessages(MessagePackSerializableObject msg, MessageTypes messageType) { // All message cases must return true to signify that a message has been processed. switch (messageType) { #region Change ship location case (MessageTypes.ChangeShipLocation): { var data = msg as MessageChangeShipLocation; Ship s = _clientShipManager.GetShip(data.ShipID); if (s == null) { ConsoleManager.WriteLine("Warning: ShipID not found while processing ChangeShipLocation message.", ConsoleMessageType.Warning); break; } s.Position = new Vector2(data.PosX, data.PosY); s.LinearVelocity = new Vector2(data.VelX, data.VelY); s.Rotation = data.Rotation; return(true); } #endregion #region Receieve Position Update case (MessageTypes.PositionUpdateData): { var data = msg as MessagePositionUpdateData; if (data == null) { return(true); } data.UpdateDataObjects .Where(p => p.TargetType == PositionUpdateTargetType.Ship) .ForEach(UpdateShipPosition); return(true); } #endregion #region Add/Remove Ships to/from Team case (MessageTypes.AddRemoveShipsTeam): { MessageAddRemoveShipsTeam data = msg as MessageAddRemoveShipsTeam; _targetingService.DisableTargetSetting(); if (data.AddOrRemove) { foreach (var ID in data.IDs) { _teamManager.AddTeamToObject(ID, data.TeamID); } } else { foreach (var ID in data.IDs) { _teamManager.RemoveTeamFromObject(ID, data.TeamID); } } _targetingService.EnableTargetSetting(); return(true); } #endregion #region Receive New Ship // New connection, got a player ID case (MessageTypes.ReceiveNewShips): { var data = msg as MessageReceiveNewShips; foreach (var sd in data.Ships) { var s = MessageReader.InstantiateShip(sd, _physicsManager.World, _clientShipManager, false); if (s.Pilot.PilotType == PilotType.Player) { _numPlayerPilots++; if (_numPlayerPilots == 1 && IncreaseUpdateInterval != null) //Added the first player pilot { IncreaseUpdateInterval(this, MessageHandlerID.Value); } } } return(true); } #endregion #region NumOnlinePlayersChanged (Redis only) case MessageTypes.Redis_NumOnlinePlayersChanged: { var data = msg as MessageEmptyMessage; if (data.Data != null) { _numPlayerPilots = (int)data.Data; if (_numPlayerPilots >= 1 && IncreaseUpdateInterval != null) //Added the first player pilot { IncreaseUpdateInterval(this, MessageHandlerID.Value); } else if (_numPlayerPilots == 0 && DecreaseUpdateInterval != null) { DecreaseUpdateInterval(this, MessageHandlerID.Value); } } return(true); } #endregion #region Receive New Structure case MessageTypes.ReceiveNewStructure: { var data = msg as MessageReceiveNewStructure; StructureData sd = data.StructureData; InstantiateStructure(sd); return(true); } #endregion #region ReceiveFloatyAreaObjects case MessageTypes.ReceiveFloatyAreaObjects: { var data = msg as MessageReceiveFloatyAreaObjects; _floatyAreaObjectManager.InstantiateFloatyAreaObjects(data); break; } #endregion #region KillOrRemoveObjectsFromArea case MessageTypes.RemoveKillRevive: { MessageRemoveKillRevive data = msg as MessageRemoveKillRevive; switch (data.ObjectType) { case RemovableObjectTypes.FloatyAreaObject: { _floatyAreaObjectManager.RemoveFloatyAreaObjects(data); break; } case RemovableObjectTypes.Ship: { List <Ship> ships = new List <Ship>(); foreach (var ID in data.ObjectIDs) { Ship s = _clientShipManager.GetShip(ID); if (s == null) { ConsoleManager.WriteLine("Error: ShipID not found in MainNetworkingManager while processing MessageTypes.RemoveKillRevive", ConsoleMessageType.Error); } else { ships.Add(s); } } switch (data.ActionType) { case ActionType.Kill: { foreach (Ship s in ships) { s.Kill(); if (s != _clientShipManager.PlayerShip) { Debugging.DisposeStack.Push(this.ToString()); s.Body.Enabled = false; } _targetingService.DeRegisterObject(s); } break; } case ActionType.Revive: { float healthMultiplier = data.HealthMultiplier == null ? 1 : (float)data.HealthMultiplier; float shieldsMultiplier = data.ShieldMultiplier == null ? 1 : (float)data.ShieldMultiplier; foreach (Ship s in ships) { s.Revive((int)(s.MaxHealth * healthMultiplier), (int)(s.MaxShields * shieldsMultiplier)); _targetingService.RegisterObject(s); } break; } case ActionType.Remove: { foreach (Ship s in ships) { _clientShipManager.RemoveShip(s.Id); if (s.Pilot.PilotType == PilotType.Player) { _numPlayerPilots--; if (_numPlayerPilots == 0 && DecreaseUpdateInterval != null) //Last player pilot has been removed { DecreaseUpdateInterval(this, MessageHandlerID.Value); } } } break; } } break; } case RemovableObjectTypes.Structure: { switch (data.ActionType) { case ActionType.Kill: { foreach (var ID in data.ObjectIDs) { KillStructure(ID); } break; } case ActionType.Revive: { ConsoleManager.WriteLine("Error: Revive not implemented for structures.", ConsoleMessageType.Error); break; } case ActionType.Remove: { foreach (var ID in data.ObjectIDs) { RemoveStructure(ID); } break; } } break; } } return(true); } #endregion #region ObjectFired case MessageTypes.ObjectFired: { var data = msg as MessageObjectFired; ObjectFired(data); return(true); } #endregion #region COMMAND Change Ship Type case (MessageTypes.ChangeShipType): { var data = msg as ShipData; //This might not work...yolo MessageReader.InstantiateShip(data, _physicsManager.World, _clientShipManager, true); return(true); } #endregion #region Fire Denial case (MessageTypes.FireRequestResponse): { var data = msg as MessageFireRequestResponse; switch (data.FiringObjectType) { case FiringObjectTypes.Ship: { ShipFireResponse(data); break; } case FiringObjectTypes.Structure: { StructureFireResponse(data); break; } } return(true); } #endregion #region SelectorCommandMessage case MessageTypes.SelectorMessageType: { MessageSelectorCommand data = msg as MessageSelectorCommand; _selectionManager.RelayNetworkCommand(data); return(true); } #endregion #region Set Health case (MessageTypes.SetHealth): { var setHealthData = msg as MessageSetHealth; foreach (var d in setHealthData.HealthData) { Ship s = _clientShipManager.GetShip(d.ShipID); if (s == null) { ConsoleManager.WriteLine("Warning: ShipID not found while processing SetHealth message.", ConsoleMessageType.Warning); break; } if (!s.IsAlive) { s.Revive((int)d.Health, (int)d.Shields); } s.CurrentHealth = d.Health; s.Shields.CurrentShields = d.Shields; s.SetCurrentEnergy(d.Energy); for (int i = 0; i < d.DebuffTypesToAdd.Count; i++) { s.Debuffs.AddDebuff(d.DebuffTypesToAdd[i], d.DebuffCountsToAdd[i], TimeKeeper.MsSinceInitialization); } } return(true); } #endregion #region AddCargoToShip case MessageTypes.AddCargoToShip: { var data = msg as MessageAddCargoToShip; Ship s = _clientShipManager.GetShip(data.ShipID); if (s == null) { ConsoleManager.WriteLine("Warning: ShipID not found while processing AddStatefulCargoToShip message.", ConsoleMessageType.Warning); break; } foreach (var cargoData in data.StatefulCargoData) { var cargo = MessageReader.InstantiateStatefulCargo(cargoData); s.Cargo.AddStatefulCargo(cargo, true); if (cargo.CargoType == StatefulCargoTypes.Module) { s.AddModule((Module)cargo); } } foreach (var cargoData in data.StatelessCargoData) { s.Cargo.AddStatelessCargo(cargoData.CargoType, cargoData.Quantity, true); } break; } #endregion #region RemoveCargoFromPlayerShip case MessageTypes.RemoveCargoFromShip: { var data = msg as MessageRemoveCargoFromShip; Ship s = _clientShipManager.GetShip(data.ShipID); if (s == null) { ConsoleManager.WriteLine("Warning: ShipID not found while processing RemoveStatefulCargoFromShip message.", ConsoleMessageType.Warning); break; } foreach (var cargoID in data.StatefulCargoIDs) { var removedCargo = s.Cargo.RemoveStatefulCargo(cargoID); if (removedCargo == null) { ConsoleManager.WriteLine("Error: cargo not found while processing message " + MessageTypes.RemoveCargoFromShip + " for ID " + s.Id, ConsoleMessageType.Error); } else if (removedCargo.CargoType == StatefulCargoTypes.Module) { s.RemoveModule(removedCargo.Id); } foreach (var cargoData in data.StatelessCargoData) { s.Cargo.RemoveStatelessCargo(cargoData.CargoType, cargoData.Quantity); } } break; } #endregion } return(false);//No message processed }
public void SendMessage(MessagePackSerializableObject data, MessageTypes messageType, RoutingData routingData = null) { SendMessage(new NetworkMessageContainer(data, messageType, routingData)); }
public SimulatorBoundMessage(MessagePackSerializableObject data, MessageTypes messageType, int targetAreaId) : base(data, messageType) { TargetAreaId = targetAreaId; }