Merge remote-tracking branch 'origin/setupBranch' into setupBranch

This commit is contained in:
lars
2020-10-13 11:49:40 +02:00
10 changed files with 65 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ namespace Server.Models
{ {
public 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; }
@@ -15,5 +16,7 @@ namespace Server.Models
return "Offline"; return "Offline";
} }
} }
public int ClientsConnected{ get { return ServerCommunication.INSTANCE.ClientsConnected; } }
} }
} }

View File

@@ -84,7 +84,30 @@ namespace Server.Models
/// <param name="message">the incoming message</param> /// <param name="message">the incoming message</param>
private void HandleIncomingMessage(byte[] message) private void HandleIncomingMessage(byte[] message)
{ {
Debug.WriteLine($"Got message from client : {message}"); Debug.WriteLine($"Got message from {Username} : {message}");
byte id = message[0];
byte[] payload = new byte[message.Length - 1];
Array.Copy(message,1,payload,0,message.Length-1);
switch(id)
{
case 0x01:
// canvas data
break;
case 0x02:
// json message data
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1;
string textMsg = combo.Item2;
// todo handle sending to all except this user the username and message to display in chat
break;
case 0x03:
// object data
break;
default:
Debug.WriteLine("Received weird identifier: " + id);
break;
}
//TODO implement ways to handle the message //TODO implement ways to handle the message
} }

View File

@@ -14,6 +14,7 @@ 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;
public int ClientsConnected { get { return serverClients.Count; } }
/// <summary> /// <summary>
/// use a padlock object to make sure the singleton is thread-safe /// use a padlock object to make sure the singleton is thread-safe
@@ -85,5 +86,13 @@ namespace Server.Models
sc.sendMessage(message); sc.sendMessage(message);
} }
} }
public void sendToAllExcept(string username, byte[] message)
{
foreach (ServerClient sc in serverClients)
{
if (sc.Username != username) sc.sendMessage(message);
}
}
} }
} }

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" /> <PackageReference Include="AsyncAwaitBestPractices" Version="4.3.0" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" /> <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PropertyChanged.Fody" Version="3.2.9" /> <PackageReference Include="PropertyChanged.Fody" Version="3.2.9" />
</ItemGroup> </ItemGroup>

View File

@@ -9,6 +9,7 @@ using System.Net.Sockets;
using System.Text; using System.Text;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Server.ViewModels namespace Server.ViewModels
{ {
@@ -21,24 +22,21 @@ namespace Server.ViewModels
public MainViewModel(MainWindow mainWindow) public MainViewModel(MainWindow mainWindow)
{ {
serverCommunication = ServerCommunication.INSTANCE;
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
Debug.WriteLine("init mainviewmodel"); Debug.WriteLine("init mainviewmodel");
InformationModel = new Information(); InformationModel = new Information();
InformationModel.CanStartServer = true; InformationModel.CanStartServer = true;
InformationModel.ServerOnline = false; InformationModel.ServerOnline = false;
//BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative));
//BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));
this.ServerStartCommand = new RelayCommand(() => this.ServerStartCommand = new RelayCommand(() =>
{ {
Debug.WriteLine("connect button clicked"); Debug.WriteLine("connect button clicked");
if (serverCommunication == null)
{
Debug.WriteLine("making new server communication");
serverCommunication = ServerCommunication.INSTANCE;
}
if (!serverCommunication.Started) if (!serverCommunication.Started)
{ {
Debug.WriteLine("can start server " + InformationModel.CanStartServer);
serverCommunication.Start(); serverCommunication.Start();
InformationModel.ServerOnline = true; InformationModel.ServerOnline = true;
InformationModel.CanStartServer = false; InformationModel.CanStartServer = false;

View File

@@ -8,8 +8,11 @@
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<Button Content="Start Server" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Command="{Binding ServerStartCommand}" IsEnabled="{Binding InformationModel.CanStartServer}"/> <Button Content="Start Server" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Command="{Binding ServerStartCommand}" IsEnabled="{Binding InformationModel.CanStartServer}"/>
<Label Name="serverStatusLabel" Content="{Binding InformationModel.ServerStatus}" HorizontalAlignment="Left" Margin="169,10,0,0" VerticalAlignment="Top"/> <Label Name="serverStatusLabel" Content="{Binding InformationModel.ServerStatus}" HorizontalAlignment="Left" Margin="174,7,0,0" VerticalAlignment="Top"/>
<Label Content="Server Status:" HorizontalAlignment="Left" Margin="80,7,0,0" VerticalAlignment="Top"/> <Label Content="Server Status:" HorizontalAlignment="Left" Margin="80,7,0,0" VerticalAlignment="Top"/>
<Image Source="https://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/16/Status-user-online-icon.png" HorizontalAlignment="Left" Margin="161,16,0,0" VerticalAlignment="Top" Width="8"/> <Label Content="Clients connected:" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding InformationModel.ClientsConnected}" HorizontalAlignment="Left" Margin="117,35,0,0" VerticalAlignment="Top"/>
</Grid> </Grid>
</Window> </Window>

BIN
Server/img/offline.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 762 B

BIN
Server/img/online.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 B

View File

@@ -0,0 +1,17 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace SharedClientServer
{
class JSONConvert
{
public static (string,string) GetUsernameAndMessage(byte[] json)
{
string msg = Encoding.ASCII.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message);
}
}
}

View File

@@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientServerUtil.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ClientServerUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JSONConvert.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObservableObject.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ObservableObject.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>