WIP network message validation

This commit is contained in:
Regalis11
2015-07-12 18:25:34 +03:00
parent d4c308003b
commit a2636133ca
13 changed files with 199 additions and 73 deletions
+28 -10
View File
@@ -475,19 +475,37 @@ namespace Subsurface
public override void ReadNetworkData(NetIncomingMessage message) public override void ReadNetworkData(NetIncomingMessage message)
{ {
state = (AiState)(message.ReadByte()); AiState newState = AiState.None;
Vector2 newWallAttackPos;
float wanderAngle;
float updateTargetsTimer, raycastTimer, coolDownTimer;
wallAttackPos.X = message.ReadFloat(); int targetID;
wallAttackPos.Y = message.ReadFloat();
steeringManager.WanderAngle = message.ReadFloat(); try
updateTargetsTimer = message.ReadFloat(); {
raycastTimer = message.ReadFloat();
coolDownTimer = message.ReadFloat();
int targetID = message.ReadInt32(); newState = (AiState)(message.ReadByte());
newWallAttackPos = new Vector2(message.ReadFloat(), message.ReadFloat());
if (targetID>-1)
wanderAngle = MathUtils.WrapAngleTwoPi(message.ReadFloat());
updateTargetsTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, UpdateTargetsInterval);
raycastTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, RaycastInterval);
coolDownTimer = MathHelper.Clamp(message.ReadFloat(), 0.0f, attackCoolDown);
targetID = message.ReadInt32();
}
catch { return; }
wallAttackPos = newWallAttackPos;
steeringManager.WanderAngle = wanderAngle;
this.updateTargetsTimer = updateTargetsTimer;
this.raycastTimer = raycastTimer;
this.coolDownTimer = coolDownTimer;
if (targetID > -1)
targetEntity = Entity.FindEntityByID(targetID) as IDamageable; targetEntity = Entity.FindEntityByID(targetID) as IDamageable;
} }
+27 -15
View File
@@ -972,23 +972,35 @@ namespace Subsurface
return; return;
} }
actionKeyDown.State = message.ReadBoolean(); bool actionKeyState = false;
secondaryKeyDown.State = message.ReadBoolean(); bool secondaryKeyState = false;
double sendingTime = 0.0f;
Vector2 targetMovement = Vector2.Zero;
bool targetDir = false;
Vector2 cursorPos = Vector2.Zero;
double sendingTime = message.ReadDouble(); try
{
Vector2 targetMovement = Vector2.Zero; actionKeyState = message.ReadBoolean();
secondaryKeyState = message.ReadBoolean();
targetMovement.X = message.ReadFloat();
targetMovement.Y = message.ReadFloat();
sendingTime = message.ReadDouble();
targetMovement = new Vector2 (message.ReadFloat(), message.ReadFloat());
targetDir = message.ReadBoolean();
cursorPos = new Vector2(message.ReadFloat(), message.ReadFloat());
}
catch
{
return;
}
AnimController.IsStanding = true; AnimController.IsStanding = true;
bool targetDir = message.ReadBoolean(); actionKeyDown.State = actionKeyState;
secondaryKeyDown.State = secondaryKeyState;
Vector2 cursorPos = Vector2.Zero;
cursorPos.X = message.ReadFloat();
cursorPos.Y = message.ReadFloat();
if (sendingTime <= LastNetworkUpdate) return; if (sendingTime <= LastNetworkUpdate) return;
@@ -1012,9 +1024,9 @@ namespace Subsurface
float rotation = message.ReadFloat(); float rotation = message.ReadFloat();
float angularVel = message.ReadFloat(); float angularVel = message.ReadFloat();
if (vel != Vector2.Zero && vel.Length() > 100.0f) { } //if (vel != Vector2.Zero && vel.Length() > 100.0f) { }
if (pos != Vector2.Zero && pos.Length() > 100.0f) { } //if (pos != Vector2.Zero && pos.Length() > 100.0f) { }
if (limb.body != null) if (limb.body != null)
{ {
+5 -1
View File
@@ -62,7 +62,11 @@ namespace Subsurface
public Vector2 TargetMovement public Vector2 TargetMovement
{ {
get { return (correctionMovement == Vector2.Zero) ? targetMovement : correctionMovement; } get { return (correctionMovement == Vector2.Zero) ? targetMovement : correctionMovement; }
set { targetMovement = value; } set
{
targetMovement.X = MathHelper.Clamp(value.X, -3.0f, 3.0f);
targetMovement.Y = MathHelper.Clamp(value.Y, -3.0f, 3.0f);
}
} }
public float HeadPosition public float HeadPosition
+2 -2
View File
@@ -4,7 +4,7 @@
linkable="true" linkable="true"
pickdistance="150.0"> pickdistance="150.0">
<Sprite texture ="door.png" sourcerect="0,0,48,208" depth="0.8" origin="0.5,0.5"/> <Sprite texture ="doorframe.png" sourcerect="0,0,48,208" depth="0.8" origin="0.5,0.5"/>
<Door> <Door>
<Sprite texture ="door.png" sourcerect="80,0,19,208" depth="0.4" origin="0.5,0.0"/> <Sprite texture ="door.png" sourcerect="80,0,19,208" depth="0.4" origin="0.5,0.0"/>
@@ -26,7 +26,7 @@
linkable="true" linkable="true"
pickdistance="150.0"> pickdistance="150.0">
<Sprite texture ="door.png" sourcerect="0,0,48,208" depth="0.8" origin="0.5,0.5"/> <Sprite texture ="doorframe.png" sourcerect="0,0,48,208" depth="0.8" origin="0.5,0.5"/>
<Door window="0,-18,10,89"> <Door window="0,-18,10,89">
<Sprite texture ="door.png" sourcerect="56,0,19,208" depth="0.4" origin="0.5,0.0"/> <Sprite texture ="door.png" sourcerect="56,0,19,208" depth="0.4" origin="0.5,0.0"/>
+2 -2
View File
@@ -86,7 +86,7 @@ namespace Subsurface
Graphics.PreferredBackBufferWidth = graphicsWidth; Graphics.PreferredBackBufferWidth = graphicsWidth;
Graphics.PreferredBackBufferHeight = graphicsHeight; Graphics.PreferredBackBufferHeight = graphicsHeight;
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
//graphics.SynchronizeWithVerticalRetrace = false; //graphics.SynchronizeWithVerticalRetrace = false;
//graphics.ApplyChanges(); //graphics.ApplyChanges();
@@ -132,7 +132,7 @@ namespace Subsurface
{ {
graphicsWidth = GraphicsDevice.Viewport.Width; graphicsWidth = GraphicsDevice.Viewport.Width;
graphicsHeight = GraphicsDevice.Viewport.Height; graphicsHeight = GraphicsDevice.Viewport.Height;
Sound.Init(); Sound.Init();
ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation); ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation);
+13 -2
View File
@@ -188,8 +188,19 @@ namespace Subsurface.Items.Components
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message) public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
{ {
flowPercentage = message.ReadFloat(); float newFlow = 0.0f;
isActive = message.ReadBoolean(); bool newActive;
try
{
newFlow = MathHelper.Clamp(message.ReadFloat(), -100.0f, 100.0f);
newActive = message.ReadBoolean();
}
catch { return; }
flowPercentage = newFlow;
isActive = newActive;
} }
} }
} }
@@ -393,12 +393,28 @@ namespace Subsurface.Items.Components
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message) public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
{ {
autoTemp = message.ReadBoolean(); bool newAutoTemp;
temperature = message.ReadFloat(); float newTemperature, newShutDownTemp;
shutDownTemp = message.ReadFloat(); float newCoolingRate, newFissionRate;
coolingRate = message.ReadFloat(); try
fissionRate = message.ReadFloat(); {
newAutoTemp = message.ReadBoolean();
newTemperature = message.ReadFloat();
newShutDownTemp = message.ReadFloat();
newCoolingRate = message.ReadFloat();
newFissionRate = message.ReadFloat();
}
catch { return; }
autoTemp = newAutoTemp;
Temperature = newTemperature;
shutDownTemp = newShutDownTemp;
CoolingRate = newCoolingRate;
FissionRate = newFissionRate;
} }
} }
} }
+18 -12
View File
@@ -109,7 +109,7 @@ namespace Subsurface
public Vector2 Position public Vector2 Position
{ {
get { return (Level.Loaded==null) ? Vector2.Zero : -Level.Loaded.Position; } get { return (Level.Loaded == null) ? Vector2.Zero : -Level.Loaded.Position; }
} }
public string FilePath public string FilePath
@@ -561,20 +561,26 @@ namespace Subsurface
public override void ReadNetworkData(Networking.NetworkEventType type, NetIncomingMessage message) public override void ReadNetworkData(Networking.NetworkEventType type, NetIncomingMessage message)
{ {
double sendingTime = message.ReadDouble(); double sendingTime;
Vector2 newTargetPosition, newSpeed;
try
{
sendingTime = message.ReadDouble();
if (sendingTime <= lastNetworkUpdate) return; if (sendingTime <= lastNetworkUpdate) return;
//Vector2 newPosition = newTargetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
//if (newPosition == Position) return; newSpeed = new Vector2(message.ReadFloat(), message.ReadFloat());
//if ((newPosition - Position).Length() > 500.0f) }
//{
// System.Diagnostics.Debug.WriteLine("Submarine has moved over 500 pixels since last update");
// return;
//}
targetPosition = new Vector2(message.ReadFloat(), message.ReadFloat()); catch
speed = new Vector2(message.ReadFloat(), message.ReadFloat()); {
return;
}
targetPosition = newTargetPosition;
speed = newSpeed;
lastNetworkUpdate = sendingTime; lastNetworkUpdate = sendingTime;
} }
+17 -10
View File
@@ -27,16 +27,9 @@ namespace Subsurface
public static float CurveAngle(float from, float to, float step) public static float CurveAngle(float from, float to, float step)
{ {
// Ensure that 0 <= angle < 2pi for both "from" and "to"
while (from < 0)
from += MathHelper.TwoPi;
while (from >= MathHelper.TwoPi)
from -= MathHelper.TwoPi;
while (to < 0) from = WrapAngleTwoPi(from);
to += MathHelper.TwoPi; to = WrapAngleTwoPi(to);
while (to >= MathHelper.TwoPi)
to -= MathHelper.TwoPi;
if (Math.Abs(from - to) < MathHelper.Pi) if (Math.Abs(from - to) < MathHelper.Pi)
{ {
@@ -59,9 +52,16 @@ namespace Subsurface
return retVal; return retVal;
} }
/// <summary>
/// wrap the angle between 0.0f and 2pi
/// </summary>
public static float WrapAngleTwoPi(float angle) public static float WrapAngleTwoPi(float angle)
{ {
// Ensure that 0 <= angle < 2pi for both "from" and "to" if (float.IsInfinity(angle) || float.IsNegativeInfinity(angle) || float.IsNaN(angle))
{
return 0.0f;
}
while (angle < 0) while (angle < 0)
angle += MathHelper.TwoPi; angle += MathHelper.TwoPi;
while (angle >= MathHelper.TwoPi) while (angle >= MathHelper.TwoPi)
@@ -70,8 +70,15 @@ namespace Subsurface
return angle; return angle;
} }
/// <summary>
/// wrap the angle between -pi and pi
/// </summary>
public static float WrapAnglePi(float angle) public static float WrapAnglePi(float angle)
{ {
if (float.IsInfinity(angle) || float.IsNegativeInfinity(angle) || float.IsNaN(angle))
{
return 0.0f;
}
// Ensure that -pi <= angle < pi for both "from" and "to" // Ensure that -pi <= angle < pi for both "from" and "to"
while (angle < -MathHelper.Pi) while (angle < -MathHelper.Pi)
angle += MathHelper.TwoPi; angle += MathHelper.TwoPi;
+46 -5
View File
@@ -188,13 +188,20 @@ namespace Subsurface.Networking
public override void Update() public override void Update()
{ {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (PlayerInput.KeyHit(Microsoft.Xna.Framework.Input.Keys.K))
{
SendRandomData();
}
if (!connected || updateTimer > DateTime.Now) return; if (!connected || updateTimer > DateTime.Now) return;
if (reconnectBox != null) //if (reconnectBox != null)
{ //{
ConnectToServer(serverIP); // ConnectToServer(serverIP);
return; // return;
} //}
if (Client.ConnectionStatus == NetConnectionStatus.Disconnected) if (Client.ConnectionStatus == NetConnectionStatus.Disconnected)
{ {
@@ -458,5 +465,39 @@ namespace Subsurface.Networking
Client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered); Client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
} }
/// <summary>
/// sends some random data to the server (can be a networkevent or just something completely random)
/// use for debugging purposes
/// </summary>
public void SendRandomData()
{
NetOutgoingMessage msg = Client.CreateMessage();
switch (Rand.Int(4))
{
case 0:
msg.Write((byte)PacketTypes.NetworkEvent);
msg.Write((byte)NetworkEventType.UpdateEntity);
msg.Write(Rand.Int(MapEntity.mapEntityList.Count));
break;
case 1:
msg.Write((byte)PacketTypes.NetworkEvent);
msg.Write((byte)Enum.GetNames(typeof(NetworkEventType)).Length);
msg.Write(Rand.Int(MapEntity.mapEntityList.Count));
break;
case 2:
msg.Write((byte)Enum.GetNames(typeof(PacketTypes)).Length);
break;
}
int byteCount = Rand.Int(100);
for (int i = 0; i<byteCount; i++)
{
msg.Write((Rand.Int(2)==0) ? true : false);
}
Client.SendMessage(msg, (Rand.Int(2)==0) ? NetDeliveryMethod.ReliableOrdered : NetDeliveryMethod.Unreliable);
}
} }
} }
+4 -6
View File
@@ -91,11 +91,12 @@ namespace Subsurface.Networking
public static bool ReadData(NetIncomingMessage message) public static bool ReadData(NetIncomingMessage message)
{ {
NetworkEventType eventType = (NetworkEventType)message.ReadByte(); NetworkEventType eventType;
int id; int id;
try try
{ {
eventType = (NetworkEventType)message.ReadByte();
id = message.ReadInt32(); id = message.ReadInt32();
} }
catch catch
@@ -107,18 +108,15 @@ namespace Subsurface.Networking
Entity e = Entity.FindEntityByID(id); Entity e = Entity.FindEntityByID(id);
if (e == null) if (e == null)
{ {
//DebugConsole.ThrowError("Couldn't find an entity matching the ID ''" + id + "''"); //DebugConsole.ThrowError("Couldn't find an entity matching the ID ''" + id + "''");
return false; return false;
} }
//System.Diagnostics.Debug.WriteLine("new message: " + eventType +" - "+e); //System.Diagnostics.Debug.WriteLine("new message: " + eventType +" - "+e);
e.ReadNetworkData(eventType, message); e.ReadNetworkData(eventType, message);
return true; return true;
} }
} }
} }
+1
View File
@@ -108,6 +108,7 @@ namespace Subsurface.Networking
public virtual void Update() { } public virtual void Update() { }
public virtual void Disconnect() { } public virtual void Disconnect() { }
} }
enum ChatMessageType enum ChatMessageType
+15 -3
View File
@@ -46,19 +46,31 @@ namespace Subsurface
public Vector2 TargetPosition public Vector2 TargetPosition
{ {
get { return targetPosition; } get { return targetPosition; }
set { targetPosition = value; } set
{
targetPosition.X = MathHelper.Clamp(value.X, -10000.0f, 10000.0f);
targetPosition.Y = MathHelper.Clamp(value.Y, -10000.0f, 10000.0f);
}
} }
public Vector2 TargetVelocity public Vector2 TargetVelocity
{ {
get { return targetVelocity; } get { return targetVelocity; }
set { targetVelocity = value; } set
{
targetVelocity.X = MathHelper.Clamp(value.X, -100.0f, 100.0f);
targetVelocity.Y = MathHelper.Clamp(value.Y, -100.0f, 100.0f);
}
} }
public float TargetRotation public float TargetRotation
{ {
get { return targetRotation; } get { return targetRotation; }
set { targetRotation = value; } set
{
if (float.IsNaN(value) || float.IsInfinity(value) || float.IsNegativeInfinity(value)) return;
targetRotation = value;
}
} }
public float TargetAngularVelocity public float TargetAngularVelocity