diff --git a/Server/Client.cs b/Server/Client.cs index 88be22b..4a92aac 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -1,10 +1,65 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; using System.Text; +using Newtonsoft; +using Newtonsoft.Json; namespace Server { class Client { + private Communication communication; + private TcpClient tcpClient; + private NetworkStream stream; + private byte[] buffer = new byte[1024]; + private byte[] totalBuffer = new byte[1024]; + + public string Username { get; set; } + + public Client(Communication communication, TcpClient tcpClient) + { + this.communication = communication; + this.tcpClient = tcpClient; + this.stream = this.tcpClient.GetStream(); + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } + + private void OnRead(IAsyncResult ar) + { + try + { + int receivedBytes = stream.EndRead(ar); + + } + catch (IOException) + { + communication.Disconnect(this); + return; + } + + int counter = 0; + + while (buffer.Length > counter) + { + byte[] lenghtBytes = new byte[4]; + Array.Copy(buffer, counter, lenghtBytes, 0, 4); + int length = Convert.ToInt32(lenghtBytes); + byte[] packet = new byte[length]; + Array.Copy(buffer, counter + 4, packet, 0, length-4); + HandleData(Encoding.Default.GetString(packet)); + counter += length; + } + + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } + + private void HandleData(string packet) + { + Console.WriteLine(packet); + JsonConvert.DeserializeObject(packet); + } } } diff --git a/Server/Comms.cs b/Server/Comms.cs deleted file mode 100644 index af2a9a1..0000000 --- a/Server/Comms.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Server -{ - class Comms - { - } -} diff --git a/Server/Communication.cs b/Server/Communication.cs new file mode 100644 index 0000000..1eed0b0 --- /dev/null +++ b/Server/Communication.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO.Pipes; +using System.Net.Sockets; +using System.Text; + +namespace Server +{ + class Communication + { + private TcpListener listener; + private List clients; + + public Communication(TcpListener listener) + { + this.listener = listener; + this.clients = new List(); + } + + public void Start() + { + listener.Start(); + listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); + } + + private void OnConnect(IAsyncResult ar) + { + var tcpClient = listener.EndAcceptTcpClient(ar); + Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); + clients.Add(new Client(this, tcpClient)); + listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); + } + + internal void Disconnect(Client client) + { + clients.Remove(client); + } + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 72c96d1..bf66448 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Net; +using System.Net.Sockets; namespace Server { @@ -6,7 +8,13 @@ namespace Server { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Communication communication = new Communication(new TcpListener(IPAddress.Any, 5555)); + communication.Start(); + + while (true) + { + + } } } } diff --git a/Server/Server.csproj b/Server/Server.csproj index c73e0d1..0eaee1b 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -5,4 +5,8 @@ netcoreapp3.1 + + + +