Drastically increased network performance
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"resolution": {
|
"resolution": {
|
||||||
"width": 3840,
|
"width": 1366,
|
||||||
"height": 2160,
|
"height": 768,
|
||||||
"fullscreen": true
|
"fullscreen": false
|
||||||
},
|
},
|
||||||
"server": {
|
"server": {
|
||||||
"hostname": "localhost",
|
"hostname": "localhost",
|
||||||
|
@ -9,11 +9,13 @@ namespace FluckyGame.Library
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public class Packet : Dictionary<string, object>
|
public class Packet : Dictionary<string, object>
|
||||||
{
|
{
|
||||||
private static readonly IFormatter formatter;
|
MemoryStream memoryStream;
|
||||||
|
|
||||||
|
private static readonly Queue<IFormatter> formatters;
|
||||||
|
|
||||||
static Packet()
|
static Packet()
|
||||||
{
|
{
|
||||||
formatter = new BinaryFormatter();
|
formatters = new Queue<IFormatter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Packet() : base() { }
|
public Packet() : base() { }
|
||||||
@ -32,7 +34,27 @@ namespace FluckyGame.Library
|
|||||||
|
|
||||||
public static Packet Receive(Stream stream)
|
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;
|
var packet = obj as Packet;
|
||||||
if (packet == null)
|
if (packet == null)
|
||||||
@ -41,9 +63,41 @@ namespace FluckyGame.Library
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClearCache()
|
||||||
|
{
|
||||||
|
memoryStream = null;
|
||||||
|
}
|
||||||
|
|
||||||
public void Send(Stream stream)
|
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;
|
Position.Y += ySpeed;
|
||||||
|
|
||||||
|
var packet = new Packet() {
|
||||||
|
{"type", "UPDATE" },
|
||||||
|
{"id", Id },
|
||||||
|
{"position", Position }
|
||||||
|
};
|
||||||
|
|
||||||
lock (Program.clients)
|
lock (Program.clients)
|
||||||
foreach (var client in Program.clients)
|
foreach (var client in Program.clients)
|
||||||
client.SendPacket(new Packet() {
|
client.SendPacket(packet);
|
||||||
{"type", "UPDATE" },
|
|
||||||
{"id", Id },
|
|
||||||
{"position", Position }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,19 @@ namespace FluckyGame.Server
|
|||||||
|
|
||||||
Program.entities.Add(playerEntity = new Entity(Guid.NewGuid().ToString(), "dude", Vector3.Zero, Vector3.Zero, new Vector3(1)));
|
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)
|
lock (Program.clients)
|
||||||
foreach (var client in Program.clients)
|
foreach (var client in Program.clients)
|
||||||
if(client != this)
|
if(client != this)
|
||||||
client.SendPacket(new Packet() {
|
client.SendPacket(packet);
|
||||||
{"type", "NEW" },
|
|
||||||
{"id", playerEntity.Id },
|
|
||||||
{"model", playerEntity.Model },
|
|
||||||
{"position", playerEntity.Position },
|
|
||||||
{"rotation", playerEntity.Rotation },
|
|
||||||
{"scalation", playerEntity.Scalation }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thread = new Thread(ReceivePackets);
|
thread = new Thread(ReceivePackets);
|
||||||
@ -77,16 +79,18 @@ namespace FluckyGame.Server
|
|||||||
|
|
||||||
Program.entities.Add(entity);
|
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)
|
lock (Program.clients)
|
||||||
foreach (var client in Program.clients)
|
foreach (var client in Program.clients)
|
||||||
client.SendPacket(new Packet() {
|
client.SendPacket(newPacket);
|
||||||
{"type", "NEW" },
|
|
||||||
{"id", entity.Id },
|
|
||||||
{"model", entity.Model },
|
|
||||||
{"position", entity.Position },
|
|
||||||
{"rotation", entity.Rotation },
|
|
||||||
{"scalation", entity.Scalation }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (type == "PLAYER")
|
else if (type == "PLAYER")
|
||||||
@ -94,15 +98,17 @@ namespace FluckyGame.Server
|
|||||||
playerEntity.Position = (Vector3)packet["position"];
|
playerEntity.Position = (Vector3)packet["position"];
|
||||||
playerEntity.Rotation = (Vector3)packet["rotation"];
|
playerEntity.Rotation = (Vector3)packet["rotation"];
|
||||||
|
|
||||||
|
var newPacket = new Packet() {
|
||||||
|
{"type", "UPDATE" },
|
||||||
|
{"id", playerEntity.Id },
|
||||||
|
{"position", playerEntity.Position },
|
||||||
|
{"rotation", playerEntity.Rotation }
|
||||||
|
};
|
||||||
|
|
||||||
lock (Program.clients)
|
lock (Program.clients)
|
||||||
foreach (var client in Program.clients)
|
foreach (var client in Program.clients)
|
||||||
if(client != this)
|
if(client != this)
|
||||||
client.SendPacket(new Packet() {
|
client.SendPacket(newPacket);
|
||||||
{"type", "UPDATE" },
|
|
||||||
{"id", playerEntity.Id },
|
|
||||||
{"position", playerEntity.Position },
|
|
||||||
{"rotation", playerEntity.Rotation }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new Exception("Unknown packet type!");
|
throw new Exception("Unknown packet type!");
|
||||||
@ -116,12 +122,13 @@ namespace FluckyGame.Server
|
|||||||
Program.clients.Remove(this);
|
Program.clients.Remove(this);
|
||||||
lock (Program.entities)
|
lock (Program.entities)
|
||||||
Program.entities.Remove(playerEntity);
|
Program.entities.Remove(playerEntity);
|
||||||
|
var newPacket = new Packet() {
|
||||||
|
{ "type", "REMOVE" },
|
||||||
|
{ "id", playerEntity.Id }
|
||||||
|
};
|
||||||
lock (Program.clients)
|
lock (Program.clients)
|
||||||
foreach (var client in Program.clients)
|
foreach (var client in Program.clients)
|
||||||
client.SendPacket(new Packet() {
|
client.SendPacket(newPacket);
|
||||||
{ "type", "REMOVE" },
|
|
||||||
{ "id", playerEntity.Id }
|
|
||||||
});
|
|
||||||
networkStream.Dispose();
|
networkStream.Dispose();
|
||||||
tcpClient.Dispose();
|
tcpClient.Dispose();
|
||||||
return;
|
return;
|
||||||
|
Reference in New Issue
Block a user