Drastically increased network performance
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
{
|
||||
"resolution": {
|
||||
"width": 3840,
|
||||
"height": 2160,
|
||||
"fullscreen": true
|
||||
"width": 1366,
|
||||
"height": 768,
|
||||
"fullscreen": false
|
||||
},
|
||||
"server": {
|
||||
"hostname": "localhost",
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,13 +28,15 @@ namespace FluckyGame.Server
|
||||
|
||||
Position.Y += ySpeed;
|
||||
|
||||
var packet = new Packet() {
|
||||
{"type", "UPDATE" },
|
||||
{"id", Id },
|
||||
{"position", Position }
|
||||
};
|
||||
|
||||
lock (Program.clients)
|
||||
foreach (var client in Program.clients)
|
||||
client.SendPacket(new Packet() {
|
||||
{"type", "UPDATE" },
|
||||
{"id", Id },
|
||||
{"position", Position }
|
||||
});
|
||||
client.SendPacket(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,17 +35,19 @@ namespace FluckyGame.Server
|
||||
|
||||
Program.entities.Add(playerEntity = new Entity(Guid.NewGuid().ToString(), "dude", Vector3.Zero, Vector3.Zero, new Vector3(1)));
|
||||
|
||||
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(new Packet() {
|
||||
{"type", "NEW" },
|
||||
{"id", playerEntity.Id },
|
||||
{"model", playerEntity.Model },
|
||||
{"position", playerEntity.Position },
|
||||
{"rotation", playerEntity.Rotation },
|
||||
{"scalation", playerEntity.Scalation }
|
||||
});
|
||||
client.SendPacket(packet);
|
||||
}
|
||||
|
||||
thread = new Thread(ReceivePackets);
|
||||
@ -77,16 +79,18 @@ namespace FluckyGame.Server
|
||||
|
||||
Program.entities.Add(entity);
|
||||
|
||||
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(new Packet() {
|
||||
{"type", "NEW" },
|
||||
{"id", entity.Id },
|
||||
{"model", entity.Model },
|
||||
{"position", entity.Position },
|
||||
{"rotation", entity.Rotation },
|
||||
{"scalation", entity.Scalation }
|
||||
});
|
||||
client.SendPacket(newPacket);
|
||||
}
|
||||
}
|
||||
else if (type == "PLAYER")
|
||||
@ -94,15 +98,17 @@ namespace FluckyGame.Server
|
||||
playerEntity.Position = (Vector3)packet["position"];
|
||||
playerEntity.Rotation = (Vector3)packet["rotation"];
|
||||
|
||||
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(new Packet() {
|
||||
{"type", "UPDATE" },
|
||||
{"id", playerEntity.Id },
|
||||
{"position", playerEntity.Position },
|
||||
{"rotation", playerEntity.Rotation }
|
||||
});
|
||||
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);
|
||||
var newPacket = new Packet() {
|
||||
{ "type", "REMOVE" },
|
||||
{ "id", playerEntity.Id }
|
||||
};
|
||||
lock (Program.clients)
|
||||
foreach (var client in Program.clients)
|
||||
client.SendPacket(new Packet() {
|
||||
{ "type", "REMOVE" },
|
||||
{ "id", playerEntity.Id }
|
||||
});
|
||||
client.SendPacket(newPacket);
|
||||
networkStream.Dispose();
|
||||
tcpClient.Dispose();
|
||||
return;
|
||||
|
Reference in New Issue
Block a user