diff --git a/Client/Client.csproj b/Client/Client.csproj index 6c68d0e..0bcea97 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -6,4 +6,8 @@ true + + + + \ No newline at end of file diff --git a/Eindproject/Eindproject.sln b/Eindproject/Eindproject.sln index fc7fc0d..b96f3b4 100644 --- a/Eindproject/Eindproject.sln +++ b/Eindproject/Eindproject.sln @@ -11,6 +11,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SharedClientServer", "..\Sh EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution + ..\SharedClientServer\SharedClientServer.projitems*{67a9bf1a-d317-47ca-9f07-c3480d1360ff}*SharedItemsImports = 5 ..\SharedClientServer\SharedClientServer.projitems*{6d26f969-9cb1-414f-ac3e-7253d449ac5a}*SharedItemsImports = 13 EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Server/FodyWeavers.xml b/Server/FodyWeavers.xml new file mode 100644 index 0000000..d5abfed --- /dev/null +++ b/Server/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Server/FodyWeavers.xsd b/Server/FodyWeavers.xsd new file mode 100644 index 0000000..69dbe48 --- /dev/null +++ b/Server/FodyWeavers.xsd @@ -0,0 +1,74 @@ + + + + + + + + + + + Used to control if the On_PropertyName_Changed feature is enabled. + + + + + Used to control if the Dependent properties feature is enabled. + + + + + Used to control if the IsChanged property feature is enabled. + + + + + Used to change the name of the method that fires the notify event. This is a string that accepts multiple values in a comma separated form. + + + + + Used to control if equality checks should be inserted. If false, equality checking will be disabled for the project. + + + + + Used to control if equality checks should use the Equals method resolved from the base class. + + + + + Used to control if equality checks should use the static Equals method resolved from the base class. + + + + + Used to turn off build warnings from this weaver. + + + + + Used to turn off build warnings about mismatched On_PropertyName_Changed methods. + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/Server/Models/Information.cs b/Server/Models/Information.cs new file mode 100644 index 0000000..8de3a41 --- /dev/null +++ b/Server/Models/Information.cs @@ -0,0 +1,14 @@ + + +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Server.Models +{ + class Information : ObservableObject + { + public bool CanStartServer { get; set; } + } +} diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs new file mode 100644 index 0000000..28d4f53 --- /dev/null +++ b/Server/Models/ServerClient.cs @@ -0,0 +1,99 @@ + +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; + +namespace Server.Models +{ + class ServerClient : ObservableObject + { + public string Username { get; set; } + private TcpClient tcpClient; + private NetworkStream stream; + private byte[] buffer = new byte[1024]; + private byte[] totalBuffer = new byte[1024]; + private int totalBufferReceived = 0; + + public ServerClient(TcpClient client) + { + tcpClient = client; + stream = tcpClient.GetStream(); + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + } + + /// + /// callback method that gets called when the stream has finished reading a message from the stream. + /// + /// the async result status + private void OnRead(IAsyncResult ar) + { + int bytesReceived = this.stream.EndRead(ar); + + if (totalBufferReceived + bytesReceived > 1024) + { + throw new OutOfMemoryException("buffer is too small!"); + } + + // copy the received bytes into the buffer + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived); + // add the bytes we received to the total amount + totalBufferReceived += bytesReceived; + + // calculate the expected length of the message + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + + while (totalBufferReceived >= expectedMessageLength) + { + // we have received the full packet + byte[] message = new byte[expectedMessageLength]; + // copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method + Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength); + HandleIncomingMessage(message); + + // move the contents of the totalbuffer to the start of the array + Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); + + totalBufferReceived -= expectedMessageLength; + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + if (expectedMessageLength == 0) + { + break; + } + + + } + // start reading for a new message + stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null); + + } + + /// + /// Method to handle incoming message data + /// + /// the incoming message + private void HandleIncomingMessage(byte[] message) + { + //TODO implement ways to handle the message + } + + /// + /// sends a message to the tcp client + /// + /// message to send + public void sendMessage(byte[] message) + { + stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); + } + + /// + /// callback method that gets called when the stream has finished writing the message + /// + /// the async result status + private void OnWrite(IAsyncResult ar) + { + stream.EndWrite(ar); + } + } +} diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs new file mode 100644 index 0000000..2a2c16b --- /dev/null +++ b/Server/Models/ServerCommunication.cs @@ -0,0 +1,41 @@ + +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net.Sockets; +using System.Text; + +namespace Server.Models +{ + class ServerCommunication : ObservableObject + { + private TcpListener listener; + private List serverClients; + public bool Started = false; + + public ServerCommunication(TcpListener listener) + { + this.listener = listener; + serverClients = new List(); + } + + public void Start() + { + listener.Start(); + Debug.WriteLine($"================================================\nStarted Accepting clients at {DateTime.Now}\n================================================"); + Started = true; + listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), null); + } + + private void OnClientConnected(IAsyncResult ar) + { + TcpClient tcpClient = listener.EndAcceptTcpClient(ar); + Console.WriteLine($"Got connection from {tcpClient.Client.RemoteEndPoint}"); + ServerClient sc = new ServerClient(tcpClient); + + serverClients.Add(new ServerClient(tcpClient)); + listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), null); + } + } +} diff --git a/Server/Server.csproj b/Server/Server.csproj index 6c68d0e..afc85b6 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -6,4 +6,12 @@ true + + + + + + + + \ No newline at end of file diff --git a/Server/ViewModels/MainViewModel.cs b/Server/ViewModels/MainViewModel.cs new file mode 100644 index 0000000..576f376 --- /dev/null +++ b/Server/ViewModels/MainViewModel.cs @@ -0,0 +1,45 @@ +using GalaSoft.MvvmLight.Command; +using Server.Models; +using SharedClientServer; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Windows.Input; + +namespace Server.ViewModels +{ + class MainViewModel : ObservableObject + { + private ServerCommunication serverCommunication; + public ICommand ServerStartCommand { get; set; } + public Information InformationModel { get; set; } + + public MainViewModel() + { + Debug.WriteLine("init mainviewmodel"); + InformationModel = new Information(); + InformationModel.CanStartServer = true; + this.ServerStartCommand = new RelayCommand(() => + { + Debug.WriteLine("connect button clicked"); + if (serverCommunication == null) + { + Debug.WriteLine("making new server communication"); + serverCommunication = new ServerCommunication(new TcpListener(IPAddress.Any,5555)); + } + if (!serverCommunication.Started) + { + + Debug.WriteLine("can start server " + InformationModel.CanStartServer); + serverCommunication.Start(); + + InformationModel.CanStartServer = false; + } + }); + } + } +} + diff --git a/Server/Views/MainWindow.xaml b/Server/Views/MainWindow.xaml index 45eec7f..b42da60 100644 --- a/Server/Views/MainWindow.xaml +++ b/Server/Views/MainWindow.xaml @@ -7,9 +7,7 @@ mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> - -