From 8190724f770a62c2f6e4ef955f73db2896569750 Mon Sep 17 00:00:00 2001 From: Lars Date: Wed, 21 Oct 2020 23:18:37 +0200 Subject: [PATCH] [ADDED] canvas data sending to the server works, but not fully yet to all the clients --- Client/Client.cs | 20 +++++++++++++++----- Client/ClientData.cs | 7 +++++++ Client/ViewModels/ViewModelGame.cs | 24 ++++++++++++++++++++++-- Client/Views/GameWindow.xaml.cs | 2 +- Server/Models/ServerClient.cs | 11 ++++++++--- SharedClientServer/ClientServerUtil.cs | 2 ++ SharedClientServer/JSONConvert.cs | 19 +++++++++++++++++++ 7 files changed, 74 insertions(+), 11 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 5346d92..1540944 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -8,13 +8,18 @@ using static SharedClientServer.JSONConvert; namespace Client { - public delegate void OnLobbyCreated(int id); + public delegate void OnLobbyCreated(int id); + + public delegate void CanvasDataReceived(double[] coordinates); class Client : ObservableObject - { + { + + private ClientData clientData = ClientData.Instance; + private TcpClient tcpClient; private NetworkStream stream; - private byte[] buffer = new byte[1024]; - private byte[] totalBuffer = new byte[1024]; + private byte[] buffer = new byte[2048]; + private byte[] totalBuffer = new byte[2048]; private int totalBufferReceived = 0; public int Port = 5555; public bool Connected = false; @@ -24,6 +29,8 @@ namespace Client public Callback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public OnLobbyCreated OnLobbyCreated; + + public CanvasDataReceived CanvasDataReceived; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -48,7 +55,7 @@ namespace Client { int amountReceived = stream.EndRead(ar); - if (totalBufferReceived + amountReceived > 1024) + if (totalBufferReceived > 2048) { throw new OutOfMemoryException("buffer too small"); } @@ -68,6 +75,7 @@ namespace Client handleData(message); totalBufferReceived -= expectedMessageLength; + Debug.WriteLine($"reduced buffer: {expectedMessageLength}"); expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); } @@ -124,6 +132,8 @@ namespace Client case JSONConvert.CANVAS: // canvas data + //clientData.CanvasData = JSONConvert.getCoordinates(payload); + CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload)); break; default: diff --git a/Client/ClientData.cs b/Client/ClientData.cs index 8a217a0..507dbca 100644 --- a/Client/ClientData.cs +++ b/Client/ClientData.cs @@ -31,6 +31,7 @@ namespace Client private Client _client; private Lobby _lobby; private string _message; + private double[] _canvasData = new double[4]; private ClientData() { @@ -68,5 +69,11 @@ namespace Client } } + public double[] CanvasData + { + get { return _canvasData; } + set { _canvasData = value; } + } + } } diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index 65fd353..f4522eb 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -14,6 +14,7 @@ namespace Client.ViewModels class ViewModelGame : INotifyPropertyChanged { private ClientData data = ClientData.Instance; + private GameWindow window; public event PropertyChangedEventHandler PropertyChanged; @@ -68,7 +69,7 @@ namespace Client.ViewModels currentPoint = e.GetPosition(window.CanvasForPaint); window.CanvasForPaint.Children.Add(line); - data.Client.SendMessage(JSONConvert.GetMessageToSend(0x04, coordinates)); + data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(coordinates)); } } @@ -83,8 +84,9 @@ namespace Client.ViewModels } - public ViewModelGame() + public ViewModelGame(GameWindow window) { + this.window = window; if (_payload == null) { _message = ""; @@ -97,6 +99,24 @@ namespace Client.ViewModels //Messages.Add($"{data.User.Username}: {Message}"); } OnKeyDown = new RelayCommand(ChatBox_KeyDown); + data.Client.CanvasDataReceived = UpdateCanvasWithNewData; + } + + + private void UpdateCanvasWithNewData(double[] coordinates) + { + Application.Current.Dispatcher.Invoke(delegate + { + Line line = new Line(); + line.Stroke = new SolidColorBrush(Colors.Black); + line.X1 = coordinates[0]; + line.Y1 = coordinates[1]; + line.X2 = coordinates[2]; + line.Y2 = coordinates[3]; + + this.window.CanvasForPaint.Children.Add(line); + }); + } private void ChatBox_KeyDown() diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index 9d90635..d59926f 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -22,7 +22,7 @@ namespace Client.Views private ViewModelGame viewModel; public GameWindow() { - this.viewModel = new ViewModelGame(); + this.viewModel = new ViewModelGame(this); DataContext = this.viewModel; InitializeComponent(); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 90eab73..0e5f9b8 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -16,8 +16,8 @@ namespace Server.Models { private TcpClient tcpClient; private NetworkStream stream; - private byte[] buffer = new byte[1024]; - private byte[] totalBuffer = new byte[1024]; + private byte[] buffer = new byte[2048]; + private byte[] totalBuffer = new byte[2048]; private int totalBufferReceived = 0; public User User { get; set; } private ServerCommunication serverCom = ServerCommunication.INSTANCE; @@ -48,7 +48,7 @@ namespace Server.Models int bytesReceived = this.stream.EndRead(ar); - if (totalBufferReceived + bytesReceived > 1024) + if (totalBufferReceived > 2048) { throw new OutOfMemoryException("buffer is too small!"); } @@ -136,6 +136,11 @@ namespace Server.Models break; case JSONConvert.CANVAS: Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!"); + dynamic canvasData = new { + coordinatesLine = JSONConvert.getCoordinates(payload) + }; + serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData)); + // canvas data // todo send canvas data to all other serverclients in lobby break; diff --git a/SharedClientServer/ClientServerUtil.cs b/SharedClientServer/ClientServerUtil.cs index d9d30fe..1832973 100644 --- a/SharedClientServer/ClientServerUtil.cs +++ b/SharedClientServer/ClientServerUtil.cs @@ -7,6 +7,8 @@ using System.Text; namespace SharedClientServer { public delegate void Callback(); + + class ClientServerUtil { // creates a message array to send to the server or to clients diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index dd967e8..209057d 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -83,6 +83,7 @@ namespace SharedClientServer }); } + public static byte[] ConstructLobbyJoinMessage(int lobbyID) { return GetMessageToSend(LOBBY, new @@ -140,6 +141,24 @@ namespace SharedClientServer #endregion + public static byte[] ConstructCanvasDataSend(double[] coordinates) + { + return GetMessageToSend(CANVAS, new + { + coordinatesLine = coordinates + }); ; + } + + public static double[] getCoordinates(byte[] payload) + { + dynamic payloadD = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); + JArray coordinatesArray = payloadD.coordinatesLine; + + double[] coordinates = coordinatesArray.ToObject(); + + return coordinates; + } + /// /// constructs a message that can be sent to the clients or server ///