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

View File

@@ -475,19 +475,37 @@ namespace Subsurface
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();
wallAttackPos.Y = message.ReadFloat();
int targetID;
steeringManager.WanderAngle = message.ReadFloat();
updateTargetsTimer = message.ReadFloat();
raycastTimer = message.ReadFloat();
coolDownTimer = message.ReadFloat();
try
{
int targetID = message.ReadInt32();
if (targetID>-1)
newState = (AiState)(message.ReadByte());
newWallAttackPos = new Vector2(message.ReadFloat(), message.ReadFloat());
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;
}

View File

@@ -972,23 +972,35 @@ namespace Subsurface
return;
}
actionKeyDown.State = message.ReadBoolean();
secondaryKeyDown.State = message.ReadBoolean();
bool actionKeyState = false;
bool secondaryKeyState = false;
double sendingTime = 0.0f;
Vector2 targetMovement = Vector2.Zero;
bool targetDir = false;
Vector2 cursorPos = Vector2.Zero;
double sendingTime = message.ReadDouble();
Vector2 targetMovement = Vector2.Zero;
targetMovement.X = message.ReadFloat();
targetMovement.Y = message.ReadFloat();
try
{
actionKeyState = message.ReadBoolean();
secondaryKeyState = message.ReadBoolean();
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;
bool targetDir = message.ReadBoolean();
Vector2 cursorPos = Vector2.Zero;
cursorPos.X = message.ReadFloat();
cursorPos.Y = message.ReadFloat();
actionKeyDown.State = actionKeyState;
secondaryKeyDown.State = secondaryKeyState;
if (sendingTime <= LastNetworkUpdate) return;
@@ -1012,9 +1024,9 @@ namespace Subsurface
float rotation = 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)
{

View File

@@ -62,7 +62,11 @@ namespace Subsurface
public Vector2 TargetMovement
{
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

View File

@@ -4,7 +4,7 @@
linkable="true"
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>
<Sprite texture ="door.png" sourcerect="80,0,19,208" depth="0.4" origin="0.5,0.0"/>
@@ -26,7 +26,7 @@
linkable="true"
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">
<Sprite texture ="door.png" sourcerect="56,0,19,208" depth="0.4" origin="0.5,0.0"/>

View File

@@ -86,7 +86,7 @@ namespace Subsurface
Graphics.PreferredBackBufferWidth = graphicsWidth;
Graphics.PreferredBackBufferHeight = graphicsHeight;
Content.RootDirectory = "Content";
//graphics.SynchronizeWithVerticalRetrace = false;
//graphics.ApplyChanges();
@@ -132,7 +132,7 @@ namespace Subsurface
{
graphicsWidth = GraphicsDevice.Viewport.Width;
graphicsHeight = GraphicsDevice.Viewport.Height;
Sound.Init();
ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation);

View File

@@ -188,8 +188,19 @@ namespace Subsurface.Items.Components
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message)
{
flowPercentage = message.ReadFloat();
isActive = message.ReadBoolean();
float newFlow = 0.0f;
bool newActive;
try
{
newFlow = MathHelper.Clamp(message.ReadFloat(), -100.0f, 100.0f);
newActive = message.ReadBoolean();
}
catch { return; }
flowPercentage = newFlow;
isActive = newActive;
}
}
}

View File

@@ -393,12 +393,28 @@ namespace Subsurface.Items.Components
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
{
autoTemp = message.ReadBoolean();
temperature = message.ReadFloat();
shutDownTemp = message.ReadFloat();
bool newAutoTemp;
float newTemperature, newShutDownTemp;
float newCoolingRate, newFissionRate;
coolingRate = message.ReadFloat();
fissionRate = message.ReadFloat();
try
{
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;
}
}
}

View File

@@ -109,7 +109,7 @@ namespace Subsurface
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
@@ -561,20 +561,26 @@ namespace Subsurface
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 =
//if (newPosition == Position) return;
//if ((newPosition - Position).Length() > 500.0f)
//{
// System.Diagnostics.Debug.WriteLine("Submarine has moved over 500 pixels since last update");
// return;
//}
newTargetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
newSpeed = new Vector2(message.ReadFloat(), message.ReadFloat());
}
targetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
speed = new Vector2(message.ReadFloat(), message.ReadFloat());
catch
{
return;
}
targetPosition = newTargetPosition;
speed = newSpeed;
lastNetworkUpdate = sendingTime;
}

View File

@@ -27,16 +27,9 @@ namespace Subsurface
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)
to += MathHelper.TwoPi;
while (to >= MathHelper.TwoPi)
to -= MathHelper.TwoPi;
from = WrapAngleTwoPi(from);
to = WrapAngleTwoPi(to);
if (Math.Abs(from - to) < MathHelper.Pi)
{
@@ -59,9 +52,16 @@ namespace Subsurface
return retVal;
}
/// <summary>
/// wrap the angle between 0.0f and 2pi
/// </summary>
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)
angle += MathHelper.TwoPi;
while (angle >= MathHelper.TwoPi)
@@ -70,8 +70,15 @@ namespace Subsurface
return angle;
}
/// <summary>
/// wrap the angle between -pi and pi
/// </summary>
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"
while (angle < -MathHelper.Pi)
angle += MathHelper.TwoPi;

View File

@@ -188,13 +188,20 @@ namespace Subsurface.Networking
public override void Update()
{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (PlayerInput.KeyHit(Microsoft.Xna.Framework.Input.Keys.K))
{
SendRandomData();
}
if (!connected || updateTimer > DateTime.Now) return;
if (reconnectBox != null)
{
ConnectToServer(serverIP);
return;
}
//if (reconnectBox != null)
//{
// ConnectToServer(serverIP);
// return;
//}
if (Client.ConnectionStatus == NetConnectionStatus.Disconnected)
{
@@ -458,5 +465,39 @@ namespace Subsurface.Networking
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);
}
}
}

View File

@@ -91,11 +91,12 @@ namespace Subsurface.Networking
public static bool ReadData(NetIncomingMessage message)
{
NetworkEventType eventType = (NetworkEventType)message.ReadByte();
NetworkEventType eventType;
int id;
try
{
eventType = (NetworkEventType)message.ReadByte();
id = message.ReadInt32();
}
catch
@@ -107,18 +108,15 @@ namespace Subsurface.Networking
Entity e = Entity.FindEntityByID(id);
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;
}
//System.Diagnostics.Debug.WriteLine("new message: " + eventType +" - "+e);
e.ReadNetworkData(eventType, message);
return true;
}
}
}

View File

@@ -108,6 +108,7 @@ namespace Subsurface.Networking
public virtual void Update() { }
public virtual void Disconnect() { }
}
enum ChatMessageType

View File

@@ -46,19 +46,31 @@ namespace Subsurface
public Vector2 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
{
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
{
get { return targetRotation; }
set { targetRotation = value; }
set
{
if (float.IsNaN(value) || float.IsInfinity(value) || float.IsNegativeInfinity(value)) return;
targetRotation = value;
}
}
public float TargetAngularVelocity