Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed packet sequencing #13

Merged
merged 1 commit into from Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions Assets/Server/Client.cs
Expand Up @@ -77,7 +77,7 @@ public void Init()
EntityUpdateMessage.Action.CREATE,
kv.Key
);
MovementMessage move = new MovementMessage(0, kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);
MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

this.SendMessage(entity);
this.SendMessage(move);
Expand All @@ -91,7 +91,7 @@ public void Init()
EntityUpdateMessage.Action.CREATE,
kv.Key
);
MovementMessage move = new MovementMessage(0, kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);
MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

this.SendMessage(entity);
this.SendMessage(move);
Expand Down Expand Up @@ -122,7 +122,7 @@ public void Init()
throw new Exception("Weapon type not found");
}
Debug.Log("Creating a weapon move message with x: " + e.X + " y: " + e.Y);
MovementMessage move = new MovementMessage(0, kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);
MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY);

this.SendMessage(entity);
this.SendMessage(move);
Expand All @@ -143,7 +143,7 @@ public void Init()
);
UDPServer.getInstance().BroadcastMessage(NewClient);

MovementMessage ClientMovement = new MovementMessage(0, this.PlayerID, 0, 0, 0, 0, 0, 0, 0);
MovementMessage ClientMovement = new MovementMessage(this.PlayerID, 0, 0, 0, 0, 0, 0, 0);
UDPServer.getInstance().BroadcastMessage(ClientMovement);

EntityUpdateMessage control = new EntityUpdateMessage(
Expand Down Expand Up @@ -265,15 +265,21 @@ private int WrapArray(int a)
/// Acks incoming packet
/// </summary>
/// <param name="packet">the packet to ack</param>
public void AckIncomingPacket(UDPPacket packet)
public bool AckIncomingPacket(UDPPacket packet)
{
bool result = false;
int i = packet.SequenceNumber % BufferSize;

if(packet.SequenceNumber > this.RemoteSeqNum)
{
this.RemoteSeqNum = packet.SequenceNumber;
}

if (this.ReceiveSequenceBuffer[i] != packet.SequenceNumber
&& !this.ReceiveBuffer[i].Acked)
{
result = true;
}
this.ReceiveSequenceBuffer[i] = packet.SequenceNumber;
this.ReceiveBuffer[i].Acked = true;
this.ReceiveBuffer[i].Packet = packet;
Expand All @@ -295,6 +301,8 @@ public void AckIncomingPacket(UDPPacket packet)
}
}
}

return result;
}

public UDPAckPacket GetSentPacket(UInt16 SequenceNumber)
Expand Down
1 change: 0 additions & 1 deletion Assets/Server/Entities/Enemy.cs
Expand Up @@ -237,7 +237,6 @@ private void FixedUpdate()
base.DY = body.velocity.y;

Assets.Server.MovementMessage m = new Assets.Server.MovementMessage(
0,
this.ID,
0,
0,
Expand Down
2 changes: 0 additions & 2 deletions Assets/Server/Entities/Player.cs
Expand Up @@ -112,7 +112,6 @@ private void FixedUpdate()
//base.DY = body.velocity.y;

Assets.Server.MovementMessage m = new Assets.Server.MovementMessage(
0,
this.ID,
0,
0,
Expand All @@ -135,7 +134,6 @@ public void ForceUpdateClientPosition()
base.DY = body.velocity.y;

Assets.Server.MovementMessage m = new Assets.Server.MovementMessage(
0,
this.ID,
0,
0,
Expand Down
6 changes: 3 additions & 3 deletions Assets/Server/GameState.cs
Expand Up @@ -207,7 +207,7 @@ public void PlayerAttack(UInt32 playerId, short weaponId, float clickPositionX,

Player player = (Player) GetEntity(playerId);

AttackMessage AttackInit = new AttackMessage(0, playerId, 0, 0, 0, weaponId, 0, 0, clickPositionX, clickPositionY, 1);
AttackMessage AttackInit = new AttackMessage(playerId, 0, 0, 0, weaponId, 0, 0, clickPositionX, clickPositionY, 1);
Debug.Log("Broadcasting in PlayerATtack");
UDPServer.getInstance().BroadcastMessage(AttackInit);

Expand All @@ -227,7 +227,7 @@ public void PlayerAttack(UInt32 playerId, short weaponId, float clickPositionX,
Enemy enemy = (Enemy)GetEntity(enemyID);
Player player = (Player)GetEntity(targetPlayerID);

AttackMessage AttackInit = new AttackMessage(0, enemyID, 0, 0, targetPlayerID, 0, 0, 0, player.X, player.Y, 1);
AttackMessage AttackInit = new AttackMessage(enemyID, 0, 0, targetPlayerID, 0, 0, 0, player.X, player.Y, 1);
Debug.Log("Broadcasting in EnemyAttack");
UDPServer.getInstance().BroadcastMessage(AttackInit);
}
Expand All @@ -241,7 +241,7 @@ public void AttackValid(UInt32 targetCharacterID, float damageAmount)
{
Character targetEntity = (Character) GetEntity(targetCharacterID);
targetEntity.TakeDamage(damageAmount);
AttackMessage newAttack = new AttackMessage(0, targetCharacterID, 0, 0, 0, 0, 1, damageAmount, 0, 0, 0);
AttackMessage newAttack = new AttackMessage(targetCharacterID, 0, 0, 0, 0, 1, damageAmount, 0, 0, 0);
//targetEntity.Client.MessageQueue.Enqueue(newAttack);
UDPServer.getInstance().BroadcastMessage(newAttack);
}
Expand Down
34 changes: 12 additions & 22 deletions Assets/Server/Message/AttackMessage.cs
Expand Up @@ -13,35 +13,33 @@ namespace Assets.Server
public class AttackMessage : Message
{
// Total size of message
public static readonly int MESSAGE_SIZE = 26;
public static readonly int MESSAGE_SIZE = 23;

// Indices for the values in the message
// Bytes | Description
// --------------------------
// 0 | Type ID
private static readonly int TYPE_ID = 0;
// 1-2 | Sequence number
private static readonly int SEQUENCE_NUMBER = 1;
// 3-4 | Entity ID
private static readonly int ENTITY_ID = 3;
private static readonly int ENTITY_ID = 1;
// 5 | Entity action type
private static readonly int ACTION_TYPE = 5;
private static readonly int ACTION_TYPE = 3;
// 6 | Entity action descriptor
private static readonly int ACTION_DESCRIPTOR = 6;
private static readonly int ACTION_DESCRIPTOR = 4;
// 7-8 | Entity X coordinate
private static readonly int TARGET_ENTITY_ID = 7;
private static readonly int TARGET_ENTITY_ID = 5;
// 9 | Type of weapon to use
private static readonly int WEAPON_TYPE = 9;
private static readonly int WEAPON_TYPE = 7;
// 10 | 0 is an invalid attack 1 is valid
private static readonly int ATTACK_VALID = 10;
private static readonly int ATTACK_VALID = 8;
// 11-14 | Attack damage amount
private static readonly int DAMAGE_AMOUNT = 11;
private static readonly int DAMAGE_AMOUNT = 9;
// 15-18 | Attack vector direction
private static readonly int ATTACK_POSITION_X = 15;
private static readonly int ATTACK_POSITION_X = 13;
// 19-22 | Attack vector direction
private static readonly int ATTACK_POSITION_Y = 19;
private static readonly int ATTACK_POSITION_Y = 17;
// 23 | Attack initiated 0 for false 1 for true
private static readonly int ATTACK_INITIATED = 24;
private static readonly int ATTACK_INITIATED = 21;
// Byte array containing the information

private byte[] message = new byte[MESSAGE_SIZE];
Expand All @@ -58,10 +56,9 @@ public AttackMessage(byte[] bytes, int cursor)
message[TYPE_ID] = ATTACK;
}

public AttackMessage(short seqNum, UInt32 entityId, byte actionType, byte actionDescriptor, UInt32 targetEntityId, short weaponType, short attackValid, float damageAmount, float attackPositionX, float attackPositionY, short attackInit)
public AttackMessage(UInt32 entityId, byte actionType, byte actionDescriptor, UInt32 targetEntityId, short weaponType, short attackValid, float damageAmount, float attackPositionX, float attackPositionY, short attackInit)
{
message[TYPE_ID] = ATTACK;
SetSequenceNumber(seqNum);
SetEntityId(entityId);
SetActionType(actionType);
SetActionDescriptor(actionDescriptor);
Expand All @@ -76,11 +73,6 @@ public AttackMessage(short seqNum, UInt32 entityId, byte actionType, byte action

// The Setters will convert the argument to bytes and copy them into the message buffer

public void SetSequenceNumber(short sn)
{
Array.Copy(BitConverter.GetBytes(sn), 0, message, SEQUENCE_NUMBER, 2);
}

public void SetEntityId(UInt32 ei)
{
Array.Copy(BitConverter.GetBytes(ei), 0, message, ENTITY_ID, 2);
Expand Down Expand Up @@ -127,7 +119,6 @@ public void SetAttackInitiated(short ati)

// Getters

public short GetSequenceNumber() => BitConverter.ToInt16(message, SEQUENCE_NUMBER);
public uint GetEntityId() => BitConverter.ToUInt32(message, ENTITY_ID);
public byte GetActionType() => message[ACTION_TYPE];
public byte GetActionDescriptor() => message[ACTION_DESCRIPTOR];
Expand All @@ -145,7 +136,6 @@ public void SetAttackInitiated(short ati)
public override string ToString()
{
string s = "\n";
s += "Sequence nr\t" + GetSequenceNumber() + "\n";
s += "Entity id \t" + GetEntityId() + "\n";
s += "Action type\t" + GetActionType() + "\n";
s += "Action desc\t" + GetActionDescriptor() + "\n";
Expand Down
9 changes: 8 additions & 1 deletion Assets/Server/Message/Message.cs
@@ -1,4 +1,3 @@

using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -36,6 +35,14 @@ public static Message Deserialize(byte[] bytes, int cursor)
return (Message) typeConstructors[type].DynamicInvoke(bytes, cursor);
}

public static Message Deserialize(byte[] bytes, int cursor, UInt16 seqnum)
{
byte type = bytes[cursor];
Message m = (Message)typeConstructors[type].DynamicInvoke(bytes, cursor);
m.SequenceNumber = seqnum;
return m;
}

// Add packet sequence number when deserializing
public UInt16 SequenceNumber;

Expand Down
32 changes: 11 additions & 21 deletions Assets/Server/Message/MovementMessage.cs
Expand Up @@ -8,36 +8,34 @@
using System.Runtime;
using System.Security.Cryptography;

namespace Assets.Server
namespace Assets.Server
{
public class MovementMessage : Message
{
// Total size of message
public static readonly int MESSAGE_SIZE = 29;
public static readonly int MESSAGE_SIZE = 27;

// Indices for the values in the message
// Bytes | Description
// --------------------------
// 0 | Type ID
private static readonly int TYPE_ID = 0;
// 1-2 | Sequence number
private static readonly int SEQUENCE_NUMBER = 1;
// 3-4 | Entity ID
private static readonly int ENTITY_ID = 3;
private static readonly int ENTITY_ID = 1;
// 5 | Entity action type
private static readonly int ACTION_TYPE = 7;
private static readonly int ACTION_TYPE = 5;
// 6 | Entity action descriptor
private static readonly int ACTION_DESCRIPTOR = 8;
private static readonly int ACTION_DESCRIPTOR = 6;
// 7-10 | Entity X coordinate
private static readonly int X_COORDINATE = 9;
private static readonly int X_COORDINATE = 7;
// 11-14 | Entity Y coordinate
private static readonly int Y_COORDINATE = 13;
private static readonly int Y_COORDINATE = 11;
// 15-18 | Entity rotation
private static readonly int ROTATION = 17;
private static readonly int ROTATION = 15;
// 19-22 | Entity X velocity
private static readonly int X_VELOCITY = 21;
private static readonly int X_VELOCITY = 19;
// 23-26 | Entity Y velocity
private static readonly int Y_VELOCITY = 25;
private static readonly int Y_VELOCITY = 23;

// Byte array containing the information
private byte[] message = new byte[MESSAGE_SIZE];
Expand All @@ -52,10 +50,9 @@ public MovementMessage(byte[] bytes, int cursor)
Array.Copy(bytes, cursor, message, 0, MESSAGE_SIZE);
}

public MovementMessage(ushort seqNum, UInt32 entityId, byte actionType, byte actionDescriptor, float x, float y, float r, float xd, float yd)
public MovementMessage(UInt32 entityId, byte actionType, byte actionDescriptor, float x, float y, float r, float xd, float yd)
{
message[TYPE_ID] = MOVEMENT;
SetSequenceNumber(seqNum);
SetEntityId(entityId);
SetActionType(actionType);
SetActionDescriptor(actionDescriptor);
Expand All @@ -68,11 +65,6 @@ public MovementMessage(ushort seqNum, UInt32 entityId, byte actionType, byte act

// The Setters will convert the argument to bytes and copy them into the message buffer

public void SetSequenceNumber(ushort sn)
{
Array.Copy(BitConverter.GetBytes(sn), 0, message, SEQUENCE_NUMBER, 2);
}

public void SetEntityId(UInt32 ei)
{
Array.Copy(BitConverter.GetBytes(ei), 0, message, ENTITY_ID, 2);
Expand Down Expand Up @@ -115,7 +107,6 @@ public void SetYVelocity(float yd)

// Getters

public ushort GetSequenceNumber() => BitConverter.ToUInt16(message, SEQUENCE_NUMBER);

public UInt32 GetEntityId() => BitConverter.ToUInt32(message, ENTITY_ID);

Expand All @@ -140,7 +131,6 @@ public void SetYVelocity(float yd)
public override string ToString()
{
string s = "\n";
s += "Sequence nr\t" + GetSequenceNumber() + "\n";
s += "Entity id \t" + GetEntityId() + "\n";
s += "Action type\t" + GetActionType() + "\n";
s += "Action desc\t" + GetActionDescriptor() + "\n";
Expand Down
9 changes: 1 addition & 8 deletions Assets/Server/MessageVisitorGameStateUpdater.cs
Expand Up @@ -11,19 +11,12 @@ class MessageVisitorGameStateUpdater : IMessageVisitor
{
public void Visit(MovementMessage m)
{
//Debug.Log(m);
// TODO: Make sure that player can only control their character!

Server.instance.TaskQueue.Enqueue(new Action(() =>
{
/*Debug.Log("Moving entity " + m.GetEntityId() + " to " +
m.GetXCoordinate() + " " +
m.GetYCoordinate() + " " +
m.GetXVelocity() + " " +
m.GetYVelocity());*/

GameState.instance.PlayerMove(m.GetEntityId(),
m.GetSequenceNumber(),
m.SequenceNumber,
m.GetXCoordinate(),
m.GetYCoordinate(),
m.GetXVelocity(),
Expand Down
3 changes: 1 addition & 2 deletions Assets/Server/UDPPacket.cs
Expand Up @@ -185,8 +185,7 @@ public List<Message> Deserialize(byte[] bytes)

while (cursor < length)
{
Message message = Message.Deserialize(bytes, cursor);
message.SequenceNumber = this.SequenceNumber;
Message message = Message.Deserialize(bytes, cursor, this.SequenceNumber);
AddMessage(message);
cursor += message.Size();
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/Server/UDPServer.cs
Expand Up @@ -153,7 +153,10 @@ public void HandleRawPacket(byte[] data, String ip, int port)
#region ackpacket
bool s = this.clients.TryGetValue((ip, port), out Client c);
if (!s) throw new Exception("Unable to find client");
c.AckIncomingPacket(packet);
if(!c.AckIncomingPacket(packet))
{
return;
}
#endregion

List<Message> messages = packet.GetMessages();
Expand Down