Removed Fill/ReadNetworkData

These functions needed to be replaced because they rely heavily on reliability.

Instead, new functions called (Write/Read)Data(Server/Client) will be added. 

Separating client and server code into completely separate functions will help ensure that proper security checks are performed.
This commit is contained in:
juanjp600
2016-08-29 20:02:46 -03:00
parent a00ceb5b9f
commit af220dbc2a
28 changed files with 29 additions and 2297 deletions
@@ -575,74 +575,6 @@ namespace Barotrauma
spriteBatch.DrawString(GUI.Font, "updatetargets: "+updateTargetsTimer, pos - Vector2.UnitY * 100.0f, Color.Red);
spriteBatch.DrawString(GUI.Font, "cooldown: " + coolDownTimer, pos - Vector2.UnitY * 120.0f, Color.Red);
}
public override void FillNetworkData(NetBuffer message)
{
message.WriteRangedInteger(0, Enum.GetValues(typeof(AiState)).Length-1, (int)state);
//bool wallAttack = (wallAttackPos != Vector2.Zero && state == AiState.Attack);
//message.Write(wallAttack);
message.Write(MathUtils.AngleToByte(steeringManager.WanderAngle));
coolDownTimer = MathHelper.Clamp(coolDownTimer, 0.0f, 30.0f);
message.WriteRangedSingle(coolDownTimer, 0.0f, 30.0f, 8);
message.Write(targetEntity==null ? (ushort)0 : (targetEntity as Entity).ID);
}
public override void ReadNetworkData(NetIncomingMessage message)
{
AiState newState = AiState.None;
Vector2 newWallAttackPos = Vector2.Zero;
float wanderAngle;
ushort targetID;
try
{
newState = (AiState)message.ReadRangedInteger(0, Enum.GetValues(typeof(AiState)).Length - 1);
//bool wallAttack = message.ReadBoolean();
//if (wallAttack)
//{
// newWallAttackPos = new Vector2(
// message.ReadRangedSingle(-50.0f, 50.0f, 10),
// message.ReadRangedSingle(-50.0f, 50.0f, 10));
// newWallAttackPos += Submarine.Loaded.SimPosition;
//}
//newVelocity = new Vector2(message.ReadFloat(), message.ReadFloat());
//targetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
wanderAngle = MathUtils.ByteToAngle(message.ReadByte());
coolDownTimer = message.ReadRangedSingle(0.0f, 30.0f, 8);
targetID = message.ReadUInt16();
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("Failed to read enemy ai update message", e);
#endif
return;
}
steeringManager.WanderAngle = wanderAngle;
if (targetID > 0) targetEntity = Entity.FindEntityByID(targetID) as IDamageable;
//updateTargetsTimer = UpdateTargetsInterval;
}
}
//the "memory" of the Character
-175
View File
@@ -86,180 +86,5 @@ namespace Barotrauma
return result;
}
public override bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
{
switch (type)
{
case NetworkEventType.KillCharacter:
return true;
case NetworkEventType.ImportantEntityUpdate:
message.Write(AnimController.RefLimb.SimPosition.X);
message.Write(AnimController.RefLimb.SimPosition.Y);
//message.Write(AnimController.RefLimb.Rotation);
message.Write((byte)((health / maxHealth) * 255.0f));
message.Write(AnimController.StunTimer > 0.0f);
if (AnimController.StunTimer > 0.0f)
{
message.WriteRangedSingle(MathHelper.Clamp(AnimController.StunTimer, 0.0f, 60.0f), 0.0f, 60.0f, 8);
}
if (DoesBleed)
{
Bleeding = MathHelper.Clamp(Bleeding, 0.0f, 5.0f);
message.WriteRangedSingle(Bleeding, 0.0f, 5.0f, 8);
}
aiController.FillNetworkData(message);
return true;
case NetworkEventType.EntityUpdate:
message.Write(AnimController.Dir > 0.0f);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.X, -1.0f, 1.0f), -1.0f, 1.0f, 4);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.Y, -1.0f, 1.0f), -1.0f, 1.0f, 4);
if (AnimController.CanEnterSubmarine) message.Write(Submarine != null);
message.Write(AnimController.RefLimb.SimPosition.X);
message.Write(AnimController.RefLimb.SimPosition.Y);
return true;
case NetworkEventType.InventoryUpdate:
return base.FillNetworkData(type, message, data);
default:
#if DEBUG
DebugConsole.ThrowError("AICharacter network event had a wrong type ("+type+")");
#endif
return false;
}
}
public override bool ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
{
data = null;
Enabled = true;
//server doesn't accept AICharacter updates from the clients
if (GameMain.Server != null) return false;
switch (type)
{
case NetworkEventType.KillCharacter:
Kill(CauseOfDeath.Damage, true);
break;
case NetworkEventType.InventoryUpdate:
return base.ReadNetworkData(type, message, sendingTime, out data);
case NetworkEventType.ImportantEntityUpdate:
Vector2 limbPos = AnimController.RefLimb.SimPosition;
float rotation = AnimController.RefLimb.Rotation;
try
{
limbPos.X = message.ReadFloat();
limbPos.Y = message.ReadFloat();
//rotation = message.ReadFloat();
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("Failed to read AICharacter update message", e);
#endif
return false;
}
if (AnimController.RefLimb.body != null)
{
AnimController.RefLimb.body.TargetPosition = limbPos;
//AnimController.RefLimb.body.TargetRotation = rotation;
}
float newStunTimer = 0.0f, newHealth = 0.0f, newBleeding = 0.0f;
try
{
newHealth = (message.ReadByte() / 255.0f) * maxHealth;
if (message.ReadBoolean())
{
newStunTimer = message.ReadRangedSingle(0.0f, 60.0f, 8);
}
if (DoesBleed)
{
newBleeding = message.ReadRangedSingle(0.0f, 5.0f, 8);
}
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("Failed to read AICharacter update message", e);
#endif
return false;
}
AnimController.StunTimer = newStunTimer;
health = newHealth;
Bleeding = newBleeding;
aiController.ReadNetworkData(message);
break;
case NetworkEventType.EntityUpdate:
if (sendingTime <= LastNetworkUpdate) return false;
Vector2 targetMovement = Vector2.Zero, pos = Vector2.Zero;
bool targetDir = false,inSub = false;
try
{
targetDir = message.ReadBoolean();
targetMovement.X = message.ReadRangedSingle(-1.0f, 1.0f, 4);
targetMovement.Y = message.ReadRangedSingle(-1.0f, 1.0f, 4);
if (AnimController.CanEnterSubmarine) inSub = message.ReadBoolean();
pos.X = message.ReadFloat();
pos.Y = message.ReadFloat();
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("Failed to read AICharacter update message", e);
#endif
return false;
}
AnimController.TargetDir = (targetDir) ? Direction.Right : Direction.Left;
AnimController.TargetMovement = targetMovement;
AnimController.RefLimb.body.TargetPosition = pos;
//AnimController.EstimateCurrPosition(pos, (float)(NetTime.Now) - sendingTime);
if (inSub)
{
Hull newHull = Hull.FindHull(ConvertUnits.ToDisplayUnits(pos), AnimController.CurrentHull, false);
if (newHull != null)
{
AnimController.CurrentHull = newHull;
Submarine = newHull.Submarine;
}
}
LastNetworkUpdate = sendingTime;
break;
}
return true;
}
}
}
+1 -430
View File
@@ -1448,436 +1448,7 @@ namespace Barotrauma
GameMain.GameSession.ReviveCharacter(this);
}
}
public override bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
{
switch (type)
{
case NetworkEventType.PickItem:
int[] pickData = (int[])data;
if (pickData.Length != 3) return false;
message.Write((ushort)pickData[0]);
message.Write((int)pickData[1] == 1);
message.Write((int)pickData[2] == 1);
var pickedItem = Entity.FindEntityByID((ushort)pickData[0]);
message.Write(pickedItem != null && selectedConstruction == pickedItem);
message.WritePadBits();
return true;
case NetworkEventType.SelectCharacter:
message.Write(AnimController.Anim == AnimController.Animation.CPR);
message.Write((ushort)data);
return true;
case NetworkEventType.KillCharacter:
CauseOfDeath causeOfDeath = CauseOfDeath.Damage;
try
{
causeOfDeath = (CauseOfDeath)data;
}
catch
{
causeOfDeath = CauseOfDeath.Damage;
}
message.Write((byte)causeOfDeath);
return true;
case NetworkEventType.InventoryUpdate:
if (inventory == null) return false;
return inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
case NetworkEventType.ApplyStatusEffect:
message.Write((ushort)data);
return true;
case NetworkEventType.ImportantEntityUpdate:
message.WriteRangedSingle(health, minHealth, maxHealth, 8);
//if (health > 0.0f)
//{
// message.Write(Math.Max((byte)((health / maxHealth) * 255.0f), (byte)1));
//}
//else
//{
// message.Write((byte)0);
// message.WriteRangedInteger(0, Enum.GetValues(typeof(CauseOfDeath)).Length-1, (int)lastAttackCauseOfDeath);
//}
if (AnimController.StunTimer <= 0.0f && bleeding <= 0.0f && oxygen > 99.0f)
{
message.Write(true);
}
else
{
message.Write(false);
message.WriteRangedSingle(MathHelper.Clamp(AnimController.StunTimer, 0.0f, 60.0f), 0.0f, 60.0f, 8);
message.WriteRangedSingle(oxygen, -100.0f, 100.0f, 8);
bleeding = MathHelper.Clamp(bleeding, 0.0f, 5.0f);
message.WriteRangedSingle(bleeding, 0.0f, 5.0f, 8);
}
return true;
case NetworkEventType.EntityUpdate:
message.Write(keys[(int)InputType.Use].GetHeldQueue);
bool secondaryHeld = keys[(int)InputType.Aim].GetHeldQueue;
message.Write(secondaryHeld);
message.Write(keys[(int)InputType.Left].Held);
message.Write(keys[(int)InputType.Right].Held);
message.Write(keys[(int)InputType.Up].Held);
message.Write(keys[(int)InputType.Down].Held);
message.Write(keys[(int)InputType.Run].Held);
message.Write(((HumanoidAnimController)AnimController).Crouching);
if (secondaryHeld)
{
if (Character.controlled==this)
{
ViewTarget = Lights.LightManager.ViewTarget == null ? this : Lights.LightManager.ViewTarget;
}
if (ViewTarget == null) ViewTarget = this;
Vector2 relativeCursorPosition = cursorPosition;
relativeCursorPosition -= ViewTarget.Position;
if (relativeCursorPosition.Length()>500.0f)
{
relativeCursorPosition = Vector2.Normalize(relativeCursorPosition) * 495.0f;
}
message.Write(ViewTarget.ID);
message.WriteRangedSingle(relativeCursorPosition.X, -500.0f, 500.0f, 8);
message.WriteRangedSingle(relativeCursorPosition.Y, -500.0f, 500.0f, 8);
}
else
{
message.Write(AnimController.Dir > 0.0f);
}
if (GameMain.Server != null)
{
message.Write(Submarine != null);
//Vector2 position = Submarine == null ? SimPosition : SimPosition - Submarine.SimPosition;
//if ((AnimController.RefLimb.SimPosition - Submarine.Loaded.SimPosition).Length() > NetConfig.CharacterIgnoreDistance) return true;
message.Write(SimPosition.X);
message.Write(SimPosition.Y);
}
networkUpdateSent = true;
return true;
default:
#if DEBUG
DebugConsole.ThrowError("Character "+this+" tried to fill a networkevent of the wrong type: "+type);
#endif
return false;
}
}
public override bool ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
{
Enabled = true;
data = null;
if (GameMain.Server != null && type != NetworkEventType.InventoryUpdate)
{
Client sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender == null || sender.Character != this)
{
#if DEBUG
DebugConsole.ThrowError("Received a character update message from someone else than the client controlling the Character!");
#endif
return false;
}
}
switch (type)
{
case NetworkEventType.PickItem:
ushort itemId = message.ReadUInt16();
bool pickHit = message.ReadBoolean();
bool actionHit = message.ReadBoolean();
bool isSelected = message.ReadBoolean();
data = new int[] { (int)itemId, pickHit ? 1 : 0, actionHit ? 1: 0 };
System.Diagnostics.Debug.WriteLine("item id: "+itemId);
Item pickedItem = FindEntityByID(itemId) as Item;
if (pickedItem != null)
{
if (!CanAccessItem(pickedItem)) return false;
if (pickedItem == selectedConstruction)
{
GameServer.Log(Name + " deselected " + pickedItem.Name, Color.Orange);
}
else
{
GameServer.Log(Name + " selected " + pickedItem.Name, Color.Orange);
}
pickedItem.Pick(this, false, pickHit, actionHit);
selectedConstruction = isSelected ? pickedItem : null;
}
break;
case NetworkEventType.SelectCharacter:
bool performingCPR = message.ReadBoolean();
ushort characterId = message.ReadUInt16();
data = characterId;
if (characterId==0)
{
DeselectCharacter(false);
return true;
}
Character character = FindEntityByID(characterId) as Character;
if (character == null || !character.IsHumanoid) return true;
SelectCharacter(character, false);
if (performingCPR)
{
AnimController.Anim = AnimController.Animation.CPR;
foreach (Limb limb in selectedCharacter.AnimController.Limbs)
{
limb.pullJoint.Enabled = false;
}
}
else if (AnimController.Anim == AnimController.Animation.CPR)
{
AnimController.Anim = AnimController.Animation.None;
}
break;
case NetworkEventType.KillCharacter:
CauseOfDeath causeOfDeath = CauseOfDeath.Damage;
try
{
byte causeOfDeathByte = message.ReadByte();
causeOfDeath = (CauseOfDeath)causeOfDeathByte;
}
catch
{
causeOfDeath = CauseOfDeath.Damage;
}
data = causeOfDeath;
if (causeOfDeath == CauseOfDeath.Pressure)
{
Implode(true);
}
else
{
Kill(causeOfDeath, true);
}
break;
case NetworkEventType.InventoryUpdate:
if (inventory == null) return false;
inventory.ReadNetworkData(NetworkEventType.InventoryUpdate, message, sendingTime);
return true;
case NetworkEventType.ApplyStatusEffect:
ushort id = message.ReadUInt16();
data = id;
var item = FindEntityByID(id) as Item;
if (item == null) return false;
item.ApplyStatusEffects(ActionType.OnUse, 1.0f, this);
break;
case NetworkEventType.ImportantEntityUpdate:
if (GameMain.Server != null)
{
return false;
}
health = message.ReadRangedSingle(minHealth, 100.0f, 8);
bool allOk = message.ReadBoolean();
if (allOk)
{
bleeding = 0.0f;
Oxygen = 100.0f;
AnimController.StunTimer = 0.0f;
return true;
}
float newStunTimer = message.ReadRangedSingle(0.0f, 60.0f, 8);
StartStun(newStunTimer, true);
Oxygen = message.ReadRangedSingle(-100.0f,100.0f, 8);
Bleeding = message.ReadRangedSingle(0.0f, 5.0f, 8);
break;
case NetworkEventType.EntityUpdate:
Vector2 relativeCursorPos = Vector2.Zero;
bool actionKeyState, secondaryKeyState;
bool leftKeyState, rightKeyState, upKeyState, downKeyState;
bool runState, crouchState;
try
{
if (sendingTime > LastNetworkUpdate) ClearInputs();
actionKeyState = message.ReadBoolean();
secondaryKeyState = message.ReadBoolean();
leftKeyState = message.ReadBoolean();
rightKeyState = message.ReadBoolean();
upKeyState = message.ReadBoolean();
downKeyState = message.ReadBoolean();
runState = message.ReadBoolean();
crouchState = message.ReadBoolean();
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("Error in Character.ReadNetworkData: " + e.Message);
#endif
return false;
}
if (GameMain.Server != null && (isDead || IsUnconscious)) return false;
keys[(int)InputType.Use].Held = actionKeyState;
keys[(int)InputType.Use].SetState(false, actionKeyState);
keys[(int)InputType.Aim].Held = secondaryKeyState;
keys[(int)InputType.Aim].SetState(false, secondaryKeyState);
if (sendingTime <= LastNetworkUpdate) return false;
keys[(int)InputType.Left].Held = leftKeyState;
keys[(int)InputType.Right].Held = rightKeyState;
keys[(int)InputType.Up].Held = upKeyState;
keys[(int)InputType.Down].Held = downKeyState;
keys[(int)InputType.Run].Held = runState;
keys[(int)InputType.Crouch].Held = crouchState;
float dir = 1.0f;
Vector2 pos = Vector2.Zero;
ushort viewTargetID = 0;
ViewTarget = null;
try
{
if (secondaryKeyState)
{
viewTargetID = message.ReadUInt16();
relativeCursorPos = new Vector2(
message.ReadRangedSingle(-500.0f, 500.0f, 8),
message.ReadRangedSingle(-500.0f, 500.0f, 8));
}
else
{
dir = message.ReadBoolean() ? 1.0f : -1.0f;
}
}
catch
{
#if DEBUG
DebugConsole.ThrowError("Failed to read networkevent for "+this.ToString());
#endif
return false;
}
if (GameMain.Server == null)
{
bool inSub = message.ReadBoolean();
pos.X = message.ReadFloat();
pos.Y = message.ReadFloat();
if (inSub != (Submarine != null))
{
AnimController.Teleport(pos - SimPosition, Vector2.Zero);
}
if (inSub)
{
//AnimController.FindHull(ConvertUnits.ToDisplayUnits(pos) - Submarine.Loaded.WorldPosition);
Hull newHull = Hull.FindHull(ConvertUnits.ToDisplayUnits(pos), AnimController.CurrentHull, false);
if (newHull != null)
{
AnimController.CurrentHull = newHull;
Submarine = newHull.Submarine;
}
}
else
{
AnimController.CurrentHull = null;
Submarine = null;
}
}
if (secondaryKeyState)
{
cursorPosition = MathUtils.IsValid(relativeCursorPos) ? relativeCursorPos : Vector2.Zero;
ViewTarget = viewTargetID == 0 ? this : Entity.FindEntityByID(viewTargetID);
if (ViewTarget == null) ViewTarget = this;
cursorPosition += ViewTarget.Position;
}
else
{
cursorPosition = Position + new Vector2(1000.0f, 0.0f) * dir;
AnimController.TargetDir = dir < 0 ? Direction.Left : Direction.Right;
}
if (GameMain.Server == null)
{
AnimController.RefLimb.body.TargetPosition =
AnimController.EstimateCurrPosition(pos, (float)(NetTime.Now) - sendingTime);
}
LastNetworkUpdate = sendingTime;
break;
default:
#if DEBUG
DebugConsole.ThrowError("Character " + this + " tried to read a networkevent of the wrong type: " + type);
#endif
return false;
}
return true;
}
public override void Remove()
{
base.Remove();