[ADDED] canvas data sending to the server works, but not fully yet to all the clients
This commit is contained in:
@@ -8,13 +8,18 @@ using static SharedClientServer.JSONConvert;
|
|||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
public delegate void OnLobbyCreated(int id);
|
public delegate void OnLobbyCreated(int id);
|
||||||
|
|
||||||
|
public delegate void CanvasDataReceived(double[] coordinates);
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private ClientData clientData = ClientData.Instance;
|
||||||
|
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
private byte[] buffer = new byte[1024];
|
private byte[] buffer = new byte[2048];
|
||||||
private byte[] totalBuffer = new byte[1024];
|
private byte[] totalBuffer = new byte[2048];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public int Port = 5555;
|
public int Port = 5555;
|
||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
@@ -24,6 +29,8 @@ namespace Client
|
|||||||
public Callback OnLobbyJoinSuccess;
|
public Callback OnLobbyJoinSuccess;
|
||||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||||
public OnLobbyCreated OnLobbyCreated;
|
public OnLobbyCreated OnLobbyCreated;
|
||||||
|
|
||||||
|
public CanvasDataReceived CanvasDataReceived;
|
||||||
public Lobby[] Lobbies { get; set; }
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
@@ -48,7 +55,7 @@ namespace Client
|
|||||||
{
|
{
|
||||||
int amountReceived = stream.EndRead(ar);
|
int amountReceived = stream.EndRead(ar);
|
||||||
|
|
||||||
if (totalBufferReceived + amountReceived > 1024)
|
if (totalBufferReceived > 2048)
|
||||||
{
|
{
|
||||||
throw new OutOfMemoryException("buffer too small");
|
throw new OutOfMemoryException("buffer too small");
|
||||||
}
|
}
|
||||||
@@ -68,6 +75,7 @@ namespace Client
|
|||||||
handleData(message);
|
handleData(message);
|
||||||
|
|
||||||
totalBufferReceived -= expectedMessageLength;
|
totalBufferReceived -= expectedMessageLength;
|
||||||
|
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
||||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +132,8 @@ namespace Client
|
|||||||
|
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
// canvas data
|
// canvas data
|
||||||
|
//clientData.CanvasData = JSONConvert.getCoordinates(payload);
|
||||||
|
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ namespace Client
|
|||||||
private Client _client;
|
private Client _client;
|
||||||
private Lobby _lobby;
|
private Lobby _lobby;
|
||||||
private string _message;
|
private string _message;
|
||||||
|
private double[] _canvasData = new double[4];
|
||||||
|
|
||||||
private ClientData()
|
private ClientData()
|
||||||
{
|
{
|
||||||
@@ -68,5 +69,11 @@ namespace Client
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double[] CanvasData
|
||||||
|
{
|
||||||
|
get { return _canvasData; }
|
||||||
|
set { _canvasData = value; }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace Client.ViewModels
|
|||||||
class ViewModelGame : INotifyPropertyChanged
|
class ViewModelGame : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
private ClientData data = ClientData.Instance;
|
private ClientData data = ClientData.Instance;
|
||||||
|
private GameWindow window;
|
||||||
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ namespace Client.ViewModels
|
|||||||
currentPoint = e.GetPosition(window.CanvasForPaint);
|
currentPoint = e.GetPosition(window.CanvasForPaint);
|
||||||
|
|
||||||
window.CanvasForPaint.Children.Add(line);
|
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)
|
if (_payload == null)
|
||||||
{
|
{
|
||||||
_message = "";
|
_message = "";
|
||||||
@@ -97,6 +99,24 @@ namespace Client.ViewModels
|
|||||||
//Messages.Add($"{data.User.Username}: {Message}");
|
//Messages.Add($"{data.User.Username}: {Message}");
|
||||||
}
|
}
|
||||||
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
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()
|
private void ChatBox_KeyDown()
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace Client.Views
|
|||||||
private ViewModelGame viewModel;
|
private ViewModelGame viewModel;
|
||||||
public GameWindow()
|
public GameWindow()
|
||||||
{
|
{
|
||||||
this.viewModel = new ViewModelGame();
|
this.viewModel = new ViewModelGame(this);
|
||||||
DataContext = this.viewModel;
|
DataContext = this.viewModel;
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
private byte[] buffer = new byte[1024];
|
private byte[] buffer = new byte[2048];
|
||||||
private byte[] totalBuffer = new byte[1024];
|
private byte[] totalBuffer = new byte[2048];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public User User { get; set; }
|
public User User { get; set; }
|
||||||
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
||||||
@@ -48,7 +48,7 @@ namespace Server.Models
|
|||||||
|
|
||||||
int bytesReceived = this.stream.EndRead(ar);
|
int bytesReceived = this.stream.EndRead(ar);
|
||||||
|
|
||||||
if (totalBufferReceived + bytesReceived > 1024)
|
if (totalBufferReceived > 2048)
|
||||||
{
|
{
|
||||||
throw new OutOfMemoryException("buffer is too small!");
|
throw new OutOfMemoryException("buffer is too small!");
|
||||||
}
|
}
|
||||||
@@ -136,6 +136,11 @@ namespace Server.Models
|
|||||||
break;
|
break;
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE 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
|
// canvas data
|
||||||
// todo send canvas data to all other serverclients in lobby
|
// todo send canvas data to all other serverclients in lobby
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ using System.Text;
|
|||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
{
|
{
|
||||||
public delegate void Callback();
|
public delegate void Callback();
|
||||||
|
|
||||||
|
|
||||||
class ClientServerUtil
|
class ClientServerUtil
|
||||||
{
|
{
|
||||||
// creates a message array to send to the server or to clients
|
// creates a message array to send to the server or to clients
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ namespace SharedClientServer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
public static byte[] ConstructLobbyJoinMessage(int lobbyID)
|
||||||
{
|
{
|
||||||
return GetMessageToSend(LOBBY, new
|
return GetMessageToSend(LOBBY, new
|
||||||
@@ -140,6 +141,24 @@ namespace SharedClientServer
|
|||||||
|
|
||||||
#endregion
|
#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<double[]>();
|
||||||
|
|
||||||
|
return coordinates;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// constructs a message that can be sent to the clients or server
|
/// constructs a message that can be sent to the clients or server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user