From 245130a6488572a4442b081ff5fdfb918767aea1 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 13 Oct 2020 14:15:38 +0200 Subject: [PATCH] wpf sucks v2 --- Client/Client.cs | 2 ++ Client/ViewModel.cs | 2 +- Server/Models/Information.cs | 2 +- Server/Models/ServerCommunication.cs | 11 ++++++++--- Server/ViewModels/MainViewModel.cs | 7 ++++++- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e0c88f0..87ac03d 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -22,11 +22,13 @@ namespace Client public Client() { this.tcpClient = new TcpClient(); + Debug.WriteLine("Starting connect to server"); tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null); } private void OnConnect(IAsyncResult ar) { + Debug.Write("finished connecting to server"); this.tcpClient.EndConnect(ar); this.stream = tcpClient.GetStream(); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); diff --git a/Client/ViewModel.cs b/Client/ViewModel.cs index acd41e8..67bc890 100644 --- a/Client/ViewModel.cs +++ b/Client/ViewModel.cs @@ -19,7 +19,7 @@ namespace Client _model = new Model(); ButtonCommand = new RelayCommand(() => { - ClickCheck(); + Client client = new Client(); }); _lobbies = new List(); diff --git a/Server/Models/Information.cs b/Server/Models/Information.cs index 1764312..eb2b690 100644 --- a/Server/Models/Information.cs +++ b/Server/Models/Information.cs @@ -17,6 +17,6 @@ namespace Server.Models } } - public int ClientsConnected{ get { return ServerCommunication.INSTANCE.ClientsConnected; } } + public int ClientsConnected{ get; set; } } } \ No newline at end of file diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 44315f4..f197c0d 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -15,9 +15,10 @@ namespace Server.Models private TcpListener listener; private List serverClients; public bool Started = false; - public int ClientsConnected { get { return serverClients.Count; } } public List lobbies; private Dictionary> serverClientsInlobbies; + public int ClientsConnected { get; set; } + public Action newClientAction; /// /// use a padlock object to make sure the singleton is thread-safe @@ -33,6 +34,7 @@ namespace Server.Models serverClients = new List(); lobbies = new List(); serverClientsInlobbies = new Dictionary>(); + ClientsConnected = 0; } /// @@ -62,6 +64,7 @@ namespace Server.Models 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); } @@ -72,10 +75,12 @@ namespace Server.Models private void OnClientConnected(IAsyncResult ar) { // stop the acceptation - TcpClient tcpClient = listener.EndAcceptTcpClient(ar); - Console.WriteLine($"Got connection from {tcpClient.Client.RemoteEndPoint}"); + var tcpClient = listener.EndAcceptTcpClient(ar); + Debug.WriteLine($"Got connection from {tcpClient.Client.RemoteEndPoint}"); + newClientAction.Invoke(); // create a new serverclient object and add it to the list serverClients.Add(new ServerClient(tcpClient)); + ClientsConnected++; //start listening for new tcp clients listener.BeginAcceptTcpClient(new AsyncCallback(OnClientConnected), null); } diff --git a/Server/ViewModels/MainViewModel.cs b/Server/ViewModels/MainViewModel.cs index 0ff5359..ad06638 100644 --- a/Server/ViewModels/MainViewModel.cs +++ b/Server/ViewModels/MainViewModel.cs @@ -15,7 +15,7 @@ namespace Server.ViewModels { class MainViewModel : ObservableObject { - private ServerCommunication serverCommunication; + public ServerCommunication serverCommunication { get; set; } public ICommand ServerStartCommand { get; set; } public Information InformationModel { get; set; } private MainWindow mainWindow; @@ -28,6 +28,11 @@ namespace Server.ViewModels InformationModel = new Information(); InformationModel.CanStartServer = true; InformationModel.ServerOnline = false; + InformationModel.ClientsConnected = 0; + serverCommunication.newClientAction = () => + { + InformationModel.ClientsConnected++; + }; //BitmapImage onlineImg = new BitmapImage(new Uri(@"/img/online.png",UriKind.Relative)); //BitmapImage offlineImg = new BitmapImage(new Uri(@"/img/offline.png", UriKind.Relative));