[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
|
||||
{
|
||||
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:
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user