Implement LobbyServer

This commit is contained in:
2019-05-03 20:24:02 +02:00
parent e3a7e37411
commit ed1771664b
10 changed files with 306 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
using System;
using System.Net.Sockets;
using System.Text;
namespace LobbyServer
{
class CommandStream
{
private NetworkStream _stream;
public CommandStream(NetworkStream stream)
{
_stream = stream;
}
public CommandPacket Receive()
{
// Length
byte[] buffer = new byte[4];
_stream.Read(buffer, 0, buffer.Length);
uint length = BitConverter.ToUInt32(buffer, 0);
// Data
buffer = new byte[length];
_stream.Read(buffer, 0, buffer.Length);
string[] packet = Encoding.UTF8.GetString(buffer).Split('\n');
if (!Enum.TryParse(packet[0], true, out Command command))
{
command = Command.Invalid;
}
return new CommandPacket(command, packet[1]);
}
public void Send(CommandPacket command)
{
string packet = command.ToString();
byte[] data = Encoding.UTF8.GetBytes(packet);
_stream.Write(BitConverter.GetBytes((uint)data.Length), 0, 4);
_stream.Write(data, 0, data.Length);
}
}
}