diff --git a/Server/Models/Information.cs b/Server/Models/Information.cs
index 7e1ae99..8f41ba9 100644
--- a/Server/Models/Information.cs
+++ b/Server/Models/Information.cs
@@ -1,20 +1,19 @@
-
-
-using SharedClientServer;
-using System;
-using System.Collections.Generic;
-using System.Text;
+using SharedClientServer;
namespace Server.Models
{
- class Information : ObservableObject
+ public class Information : ObservableObject
{
public bool CanStartServer { get; set; }
public bool ServerOnline { get; set; }
- public string ServerStatus { get {
- if (ServerOnline) return "Online";
- return "Offline";
- }
+
+ public string ServerStatus
+ {
+ get
+ {
+ if (ServerOnline) return "Online";
+ return "Offline";
+ }
}
}
-}
+}
\ No newline at end of file
diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs
index e1e4cb8..4db4128 100644
--- a/Server/Models/ServerClient.cs
+++ b/Server/Models/ServerClient.cs
@@ -2,6 +2,7 @@
using SharedClientServer;
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
@@ -17,6 +18,10 @@ namespace Server.Models
private int totalBufferReceived = 0;
+ ///
+ /// Constructor that creates a new serverclient object with the given tcp client.
+ ///
+ /// the TcpClient object to use
public ServerClient(TcpClient client)
{
tcpClient = client;
@@ -56,8 +61,11 @@ namespace Server.Models
// move the contents of the totalbuffer to the start of the array
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
+ // remove the length of the expected message from the total buffer
totalBufferReceived -= expectedMessageLength;
+ // and set the new expected length to the rest that is still in the buffer
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
+
if (expectedMessageLength == 0)
{
break;
@@ -86,6 +94,7 @@ namespace Server.Models
/// message to send
public void sendMessage(byte[] message)
{
+ // start writing the message from the start and until it is empty. When we are done we want to execute the OnWrite method.
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
@@ -95,6 +104,7 @@ namespace Server.Models
/// the async result status
private void OnWrite(IAsyncResult ar)
{
+ // end writing
stream.EndWrite(ar);
}
}
diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs
index cfd4c4a..6e899f7 100644
--- a/Server/Models/ServerCommunication.cs
+++ b/Server/Models/ServerCommunication.cs
@@ -14,15 +14,24 @@ namespace Server.Models
private TcpListener listener;
private List serverClients;
public bool Started = false;
+
+ ///
+ /// use a padlock object to make sure the singleton is thread-safe
+ ///
private static readonly object padlock = new object();
+
private static ServerCommunication instance = null;
+ public int port = 5555;
private ServerCommunication()
{
- listener = new TcpListener(IPAddress.Any, 5555);
+ listener = new TcpListener(IPAddress.Any, port);
serverClients = new List();
}
+ ///
+ /// returns the singleton serverCommunication instance
+ ///
public static ServerCommunication INSTANCE
{
get
@@ -38,24 +47,37 @@ namespace Server.Models
}
}
+ ///
+ /// start the server and start listening on port 5555 and begin acceptinc tcp clients
+ ///
public void Start()
{
listener.Start();
Debug.WriteLine($"================================================\nStarted Accepting clients at {DateTime.Now}\n================================================");
Started = true;
+ // when we have accepted a tcp client, call the onclientconnected callback method
listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), null);
}
+ ///
+ /// callback method that gets called when a client is accepted
+ ///
+ /// the result of the asynchronous connect call
private void OnClientConnected(IAsyncResult ar)
{
+ // stop the acceptation
TcpClient tcpClient = listener.EndAcceptTcpClient(ar);
Console.WriteLine($"Got connection from {tcpClient.Client.RemoteEndPoint}");
- ServerClient sc = new ServerClient(tcpClient);
-
+ // create a new serverclient object and add it to the list
serverClients.Add(new ServerClient(tcpClient));
+ //start listening for new tcp clients
listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), null);
}
+ ///
+ /// send a message to all tcp clients in the list
+ ///
+ /// the message to send
public void sendToAll(byte[] message)
{
foreach (ServerClient sc in serverClients)