the fuck?????

This commit is contained in:
Sem van der Hoeven
2020-10-22 22:04:20 +02:00
parent eae05d17df
commit 5e408091cb
4 changed files with 314 additions and 257 deletions

View File

@@ -10,7 +10,7 @@ using static SharedClientServer.JSONConvert;
namespace Client namespace Client
{ {
public delegate void LobbyJoinCallback(bool isHost); public delegate void LobbyJoinCallback(bool isHost);
public delegate void CanvasDataReceived(double[] coordinates, Color color); public delegate void CanvasDataReceived(double[][] coordinates, Color color);
public delegate void CanvasReset(); public delegate void CanvasReset();
public delegate void LobbyCallback(int id); public delegate void LobbyCallback(int id);
class Client : ObservableObject class Client : ObservableObject
@@ -58,7 +58,7 @@ namespace Client
private void OnReadComplete(IAsyncResult ar) private void OnReadComplete(IAsyncResult ar)
{ {
int amountReceived = stream.EndRead(ar); int amountReceived = stream.EndRead(ar);
if (totalBufferReceived + amountReceived > 1024) if (totalBufferReceived + amountReceived > 2048)
{ {
throw new OutOfMemoryException("buffer too small"); throw new OutOfMemoryException("buffer too small");
} }
@@ -83,7 +83,7 @@ namespace Client
Debug.WriteLine($"reduced buffer: {expectedMessageLength}"); Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
} }
ar.AsyncWaitHandle.WaitOne();
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
} }
@@ -149,18 +149,21 @@ namespace Client
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
// canvas data // canvas data
//clientData.CanvasData = JSONConvert.getCoordinates(payload); //clientData.CanvasData = JSONConvert.getCoordinates(payload);
CanvasInfo type = JSONConvert.GetCanvasMessageType(payload); int type = JSONConvert.GetCanvasMessageType(payload);
switch (type) switch (type)
{ {
case CanvasInfo.RESET: case JSONConvert.CANVAS_RESET:
CReset?.Invoke(); CReset?.Invoke();
break; break;
case CanvasInfo.DRAWING: case JSONConvert.CANVAS_WRITING:
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload)); CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
// we hebben gedrawed, dus stuur dat we weer kunnen drawen
break; break;
} }
break; break;
default: default:
Debug.WriteLine("[CLIENT] Received weird identifier: " + id); Debug.WriteLine("[CLIENT] Received weird identifier: " + id);

View File

@@ -3,9 +3,11 @@
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using SharedClientServer; using SharedClientServer;
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Timers;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
@@ -19,7 +21,12 @@ namespace Client.ViewModels
private ClientData data = ClientData.Instance; private ClientData data = ClientData.Instance;
private GameWindow window; private GameWindow window;
private Point currentPoint = new Point(); private Point currentPoint = new Point();
private Color color; public Color color;
public double[][] buffer;
public int pos = 0;
public int maxLines = 1;
public Queue<double[][]> linesQueue;
private Timer queueTimer;
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>(); public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
@@ -59,6 +66,9 @@ namespace Client.ViewModels
//_username = data.User.Username; //_username = data.User.Username;
//Messages.Add($"{data.User.Username}: {Message}"); //Messages.Add($"{data.User.Username}: {Message}");
} }
buffer = new double[maxLines][];
linesQueue = new Queue<double[][]>();
OnKeyDown = new RelayCommand(ChatBox_KeyDown); OnKeyDown = new RelayCommand(ChatBox_KeyDown);
ButtonStartGame = new RelayCommand(BeginGame); ButtonStartGame = new RelayCommand(BeginGame);
ButtonResetCanvas = new RelayCommand(CanvasResetLocal); ButtonResetCanvas = new RelayCommand(CanvasResetLocal);
@@ -72,6 +82,10 @@ namespace Client.ViewModels
public void BeginGame() public void BeginGame()
{ {
queueTimer = new Timer(500);
queueTimer.Start();
queueTimer.Elapsed += sendArrayFromQueue;
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID)); data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
} }
@@ -79,7 +93,7 @@ namespace Client.ViewModels
private void CanvasResetLocal() private void CanvasResetLocal()
{ {
this.window.CanvasForPaint.Children.Clear(); this.window.CanvasForPaint.Children.Clear();
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CanvasInfo.RESET)); data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.CANVAS_RESET));
} }
@@ -109,9 +123,33 @@ namespace Client.ViewModels
coordinates[2] = line.X2; coordinates[2] = line.X2;
coordinates[3] = line.Y2; coordinates[3] = line.Y2;
currentPoint = e.GetPosition(window.CanvasForPaint); currentPoint = e.GetPosition(window.CanvasForPaint);
buffer[pos] = coordinates;
pos++;
window.CanvasForPaint.Children.Add(line); window.CanvasForPaint.Children.Add(line);
data.Client.SendMessage(JSONConvert.ConstructCanvasDataSend(JSONConvert.CanvasInfo.DRAWING,coordinates, color)); if (pos == maxLines)
{
double[][] temp = new double[maxLines][];
for (int i = 0; i < maxLines; i++)
{
temp[i] = buffer[i];
}
linesQueue.Enqueue(temp);
Array.Clear(buffer, 0, buffer.Length);
pos = 0;
}
}
}
private void sendArrayFromQueue(object sender, ElapsedEventArgs e)
{
if (linesQueue.Count != 0)
{
Debug.WriteLine("[GAME] sending canvas data...");
double[][] temp = linesQueue.Dequeue();
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.CANVAS, JSONConvert.ConstructDrawingCanvasData(temp,color)));
} }
} }
@@ -125,17 +163,20 @@ namespace Client.ViewModels
color = colorSelected; color = colorSelected;
} }
private void UpdateCanvasWithNewData(double[] coordinates, Color color) private void UpdateCanvasWithNewData(double[][] buffer, Color color)
{ {
Application.Current.Dispatcher.Invoke(delegate Application.Current.Dispatcher.Invoke(delegate
{ {
Line line = new Line(); foreach (double[] arr in buffer)
line.Stroke = new SolidColorBrush(color); {
line.X1 = coordinates[0]; Line line = new Line();
line.Y1 = coordinates[1]; line.Stroke = new SolidColorBrush(color);
line.X2 = coordinates[2]; line.X1 = arr[0];
line.Y2 = coordinates[3]; line.Y1 = arr[1];
this.window.CanvasForPaint.Children.Add(line); line.X2 = arr[2];
line.Y2 = arr[3];
this.window.CanvasForPaint.Children.Add(line);
}
}); });
} }

View File

@@ -50,7 +50,7 @@ namespace Server.Models
{ {
int bytesReceived = this.stream.EndRead(ar); int bytesReceived = this.stream.EndRead(ar);
if (totalBufferReceived + bytesReceived > 1024) if (totalBufferReceived + bytesReceived > 2048)
{ {
throw new OutOfMemoryException("buffer is too small!"); throw new OutOfMemoryException("buffer is too small!");
} }
@@ -149,21 +149,21 @@ namespace Server.Models
break; break;
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
CanvasInfo typeToCheck = JSONConvert.GetCanvasMessageType(payload); int typeToCheck = JSONConvert.GetCanvasMessageType(payload);
switch (typeToCheck) switch (typeToCheck)
{ {
case CanvasInfo.DRAWING: case JSONConvert.CANVAS_WRITING:
dynamic canvasData = new dynamic canvasData = new
{ {
type = JSONConvert.GetCanvasMessageType(payload), canvasType = typeToCheck,
coordinatesLine = JSONConvert.getCoordinates(payload), coords = JSONConvert.getCoordinates(payload),
color = JSONConvert.getCanvasDrawingColor(payload) color = JSONConvert.getCanvasDrawingColor(payload)
}; };
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(CANVAS, canvasData)); serverCom.SendToLobby(serverCom.GetLobbyForUser(User),JSONConvert.GetMessageToSend(JSONConvert.CANVAS,canvasData));
break; break;
case CanvasInfo.RESET: case JSONConvert.CANVAS_RESET:
dynamic canvasDataForReset = new dynamic canvasDataForReset = new
{ {
type = JSONConvert.GetCanvasMessageType(payload) type = JSONConvert.GetCanvasMessageType(payload)

View File

@@ -18,6 +18,10 @@ namespace SharedClientServer
public const byte CANVAS = 0x04; public const byte CANVAS = 0x04;
public const byte GAME = 0x05; public const byte GAME = 0x05;
public const int CANVAS_WRITING = 0;
public const int CANVAS_RESET = 1;
public enum LobbyIdentifier public enum LobbyIdentifier
{ {
HOST, HOST,
@@ -28,15 +32,9 @@ namespace SharedClientServer
REQUEST REQUEST
} }
public enum CanvasInfo
{
DRAWING,
RESET
}
public static (string,string) GetUsernameAndMessage(byte[] json) public static (string,string) GetUsernameAndMessage(byte[] json)
{ {
string msg = Encoding.ASCII.GetString(json); string msg = Encoding.UTF8.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg); dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message); return (payload.username, payload.message);
@@ -44,7 +42,7 @@ namespace SharedClientServer
public static string GetUsernameLogin(byte[] json) public static string GetUsernameLogin(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
return payload.username; return payload.username;
} }
@@ -112,13 +110,13 @@ namespace SharedClientServer
} }
public static LobbyIdentifier GetLobbyIdentifier(byte[] json) public static LobbyIdentifier GetLobbyIdentifier(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
return payload.identifier; return payload.identifier;
} }
public static Lobby[] GetLobbiesFromMessage(byte[] json) public static Lobby[] GetLobbiesFromMessage(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
JArray lobbiesArray = payload.lobbies; JArray lobbiesArray = payload.lobbies;
Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString()); Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString());
Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>(); Lobby[] lobbiesTemp = lobbiesArray.ToObject<Lobby[]>();
@@ -132,13 +130,13 @@ namespace SharedClientServer
public static int GetLobbyID(byte[] json) public static int GetLobbyID(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
return payload.id; return payload.id;
} }
public static Lobby GetLobby(byte[] json) public static Lobby GetLobby(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
JObject dynamicAsObject = payload.lobby; JObject dynamicAsObject = payload.lobby;
return dynamicAsObject.ToObject<Lobby>(); return dynamicAsObject.ToObject<Lobby>();
} }
@@ -151,42 +149,56 @@ namespace SharedClientServer
public static bool GetLobbyJoinIsHost(byte[] json) public static bool GetLobbyJoinIsHost(byte[] json)
{ {
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); dynamic payload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(json));
return payload.host; return payload.host;
} }
#endregion #endregion
public static byte[] ConstructCanvasDataSend(CanvasInfo typeToSend, double[] coordinates, Color colorToSend) public static byte[] ConstructCanvasDataSend(int typeToSend, double[][] buffer, Color colorToSend)
{ {
return GetMessageToSend(CANVAS, new return GetMessageToSend(CANVAS, new
{ {
type = typeToSend, canvasType = typeToSend,
coordinatesLine = coordinates, coords = buffer,
color = colorToSend color = colorToSend
}); ; }); ;
} }
public static CanvasInfo GetCanvasMessageType(byte[] payload) public static byte[] ConstructDrawingCanvasData(double[][] buffer, Color colorToSend)
{ {
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); return GetMessageToSend(CANVAS, new
CanvasInfo type = json.type; {
return type; canvasType = CANVAS_WRITING,
coords = buffer,
color = colorToSend
}); ;
} }
public static double[] getCoordinates(byte[] payload) public static int GetCanvasMessageType(byte[] payload)
{ {
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
JArray coordinatesArray = json.coordinatesLine; var temp = Convert.FromBase64String(json);
string fdasf = System.Text.Encoding.UTF8.GetString(temp);
int cType = temp.canvasType;
return cType;
}
double[] coordinates = coordinatesArray.ToObject<double[]>(); public static double[][] getCoordinates(byte[] payload)
{
Debug.WriteLine("got coords " + Encoding.UTF8.GetString(payload));
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
JArray coordinatesArray = json.coords;
double[][] coordinates = coordinatesArray.ToObject<double[][]>();
return coordinates; return coordinates;
} }
public static Color getCanvasDrawingColor(byte[] payload) public static Color getCanvasDrawingColor(byte[] payload)
{ {
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
Color color = json.color; Color color = json.color;
return color; return color;
} }
@@ -203,13 +215,13 @@ namespace SharedClientServer
public static string GetGameCommand(byte[] payload) public static string GetGameCommand(byte[] payload)
{ {
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
return json.command; return json.command;
} }
public static int GetStartGameLobbyID(byte[] payload) public static int GetStartGameLobbyID(byte[] payload)
{ {
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload)); dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
return json.lobbyToStart; return json.lobbyToStart;
} }
@@ -222,7 +234,8 @@ namespace SharedClientServer
public static byte[] GetMessageToSend(byte identifier, dynamic payload) public static byte[] GetMessageToSend(byte identifier, dynamic payload)
{ {
// convert the dynamic to bytes // convert the dynamic to bytes
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload)); string json = JsonConvert.SerializeObject(payload);
byte[] payloadBytes = Encoding.UTF8.GetBytes(json);
// make the array that holds the message and copy the payload into it with the first spot containing the identifier // make the array that holds the message and copy the payload into it with the first spot containing the identifier
byte[] res = new byte[payloadBytes.Length + 5]; byte[] res = new byte[payloadBytes.Length + 5];
// put the payload in the res array // put the payload in the res array