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
-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;
}
}
}