Implement LobbyServer
This commit is contained in:
47
LobbyServer/CommandStream.cs
Normal file
47
LobbyServer/CommandStream.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user