Drastically increased network performance

This commit is contained in:
Daniel Brunner
2016-11-03 18:35:50 +01:00
parent af1fdf5a25
commit c541b0ee03
4 changed files with 101 additions and 38 deletions

View File

@ -1,8 +1,8 @@
{
"resolution": {
"width": 3840,
"height": 2160,
"fullscreen": true
"width": 1366,
"height": 768,
"fullscreen": false
},
"server": {
"hostname": "localhost",

View File

@ -9,11 +9,13 @@ namespace FluckyGame.Library
[Serializable]
public class Packet : Dictionary<string, object>
{
private static readonly IFormatter formatter;
MemoryStream memoryStream;
private static readonly Queue<IFormatter> formatters;
static Packet()
{
formatter = new BinaryFormatter();
formatters = new Queue<IFormatter>();
}
public Packet() : base() { }
@ -32,7 +34,27 @@ namespace FluckyGame.Library
public static Packet Receive(Stream stream)
{
var obj = formatter.Deserialize(stream);
IFormatter formatter;
lock(formatters)
{
if (formatters.Count == 0)
formatter = new BinaryFormatter();
else
formatter = formatters.Dequeue();
}
object obj;
try
{
obj = formatter.Deserialize(stream);
}
finally
{
lock (formatters)
formatters.Enqueue(formatter);
}
var packet = obj as Packet;
if (packet == null)
@ -41,9 +63,41 @@ namespace FluckyGame.Library
return packet;
}
public void ClearCache()
{
memoryStream = null;
}
public void Send(Stream stream)
{
formatter.Serialize(stream, this);
IFormatter formatter;
lock (formatters)
{
if (formatters.Count == 0)
formatter = new BinaryFormatter();
else
formatter = formatters.Dequeue();
}
if (memoryStream == null)
{
memoryStream = new MemoryStream();
try
{
formatter.Serialize(memoryStream, this);
}
finally
{
lock (formatters)
formatters.Enqueue(formatter);
}
}
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(stream);
}
}
}

View File

@ -28,13 +28,15 @@ namespace FluckyGame.Server
Position.Y += ySpeed;
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(new Packet() {
var packet = new Packet() {
{"type", "UPDATE" },
{"id", Id },
{"position", Position }
});
};
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(packet);
}
}
}

View File

@ -35,17 +35,19 @@ namespace FluckyGame.Server
Program.entities.Add(playerEntity = new Entity(Guid.NewGuid().ToString(), "dude", Vector3.Zero, Vector3.Zero, new Vector3(1)));
lock (Program.clients)
foreach (var client in Program.clients)
if(client != this)
client.SendPacket(new Packet() {
var packet = new Packet() {
{"type", "NEW" },
{"id", playerEntity.Id },
{"model", playerEntity.Model },
{"position", playerEntity.Position },
{"rotation", playerEntity.Rotation },
{"scalation", playerEntity.Scalation }
});
};
lock (Program.clients)
foreach (var client in Program.clients)
if(client != this)
client.SendPacket(packet);
}
thread = new Thread(ReceivePackets);
@ -77,16 +79,18 @@ namespace FluckyGame.Server
Program.entities.Add(entity);
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(new Packet() {
var newPacket = new Packet() {
{"type", "NEW" },
{"id", entity.Id },
{"model", entity.Model },
{"position", entity.Position },
{"rotation", entity.Rotation },
{"scalation", entity.Scalation }
});
};
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(newPacket);
}
}
else if (type == "PLAYER")
@ -94,15 +98,17 @@ namespace FluckyGame.Server
playerEntity.Position = (Vector3)packet["position"];
playerEntity.Rotation = (Vector3)packet["rotation"];
lock (Program.clients)
foreach (var client in Program.clients)
if(client != this)
client.SendPacket(new Packet() {
var newPacket = new Packet() {
{"type", "UPDATE" },
{"id", playerEntity.Id },
{"position", playerEntity.Position },
{"rotation", playerEntity.Rotation }
});
};
lock (Program.clients)
foreach (var client in Program.clients)
if(client != this)
client.SendPacket(newPacket);
}
else
throw new Exception("Unknown packet type!");
@ -116,12 +122,13 @@ namespace FluckyGame.Server
Program.clients.Remove(this);
lock (Program.entities)
Program.entities.Remove(playerEntity);
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(new Packet() {
var newPacket = new Packet() {
{ "type", "REMOVE" },
{ "id", playerEntity.Id }
});
};
lock (Program.clients)
foreach (var client in Program.clients)
client.SendPacket(newPacket);
networkStream.Dispose();
tcpClient.Dispose();
return;