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