[FIX] fixed server client message structure

This commit is contained in:
Sem van der Hoeven
2020-10-20 17:43:28 +02:00
parent 1a539efdd1
commit 33574a7b23
8 changed files with 42 additions and 18 deletions

View File

@@ -16,11 +16,11 @@ namespace Client
private int totalBufferReceived = 0;
public int Port = 5555;
public bool Connected = false;
//TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username)
public string Username { get; }
private string username;
public Client()
public Client(string username)
{
this.username = username;
this.tcpClient = new TcpClient();
Debug.WriteLine("Starting connect to server");
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
@@ -31,6 +31,7 @@ namespace Client
Debug.Write("finished connecting to server");
this.tcpClient.EndConnect(ar);
this.stream = tcpClient.GetStream();
SendMessage(JSONConvert.ConstructUsernameMessage(username));
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
}
@@ -100,11 +101,13 @@ namespace Client
public void SendMessage(byte[] message)
{
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
}
private void OnWriteComplete(IAsyncResult ar)
{
Debug.WriteLine("[CLIENT] finished writing");
stream.EndWrite(ar);
}
}

View File

@@ -18,7 +18,7 @@ namespace Client
_model = new Model();
ButtonCommand = new RelayCommand(() =>
{
Client client = new Client();
}, true);
_lobbies = new List<Lobby>();

View File

@@ -26,8 +26,8 @@ namespace Client.Views
private void Button_EnterUsername(object sender, RoutedEventArgs e)
{
User user = new User(usernameTextbox.Text, 0, false);
Client client = new Client();
User user = new User(usernameTextbox.Text);
Client client = new Client(user.Username);
data.User = user;
data.Client = client;

View File

@@ -47,7 +47,7 @@
</ComboBox>
<Label Grid.Row="3" Name="testLabel" FontSize="15" VerticalAlignment="Center"/>
<Label Content="place username here" Grid.Column="1" HorizontalAlignment="Center" Margin="0,12,0,0" VerticalAlignment="Top" Grid.Row="1"/>
<Label Name="usernameLabel" Content="place username here" Grid.Column="1" HorizontalAlignment="Center" Margin="0,12,0,0" VerticalAlignment="Top" Grid.Row="1"/>
</Grid>

View File

@@ -27,7 +27,7 @@ namespace Client
this.DataContext = new ViewModel();
InitializeComponent();
usernameTextbox.Text = data.User.Username;
usernameLabel.Content = data.User.Username;
}
private void Button_Click(object sender, RoutedEventArgs e)

View File

@@ -24,8 +24,10 @@ namespace Server.Models
/// <param name="client">the TcpClient object to use</param>
public ServerClient(TcpClient client)
{
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
tcpClient = client;
stream = tcpClient.GetStream();
Debug.WriteLine("[SERVERCLIENT] starting read");
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
}
@@ -35,6 +37,10 @@ namespace Server.Models
/// <param name="ar">the async result status</param>
private void OnRead(IAsyncResult ar)
{
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
return;
int bytesReceived = this.stream.EndRead(ar);
if (totalBufferReceived + bytesReceived > 1024)
@@ -84,16 +90,18 @@ namespace Server.Models
/// <param name="message">the incoming message</param>
private void HandleIncomingMessage(byte[] message)
{
Debug.WriteLine($"Got message from {User?.Username} : {message}");
byte id = message[0];
byte[] payload = new byte[message.Length - 1];
Array.Copy(message,1,payload,0,message.Length-1);
Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}");
byte id = message[4];
byte[] payload = new byte[message.Length - 5];
Array.Copy(message,5,payload,0,message.Length-5);
Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
switch(id)
{
case JSONConvert.LOGIN:
// json log in username data
string uName = JSONConvert.GetUsernameLogin(payload);
if (uName != null)
{
User = new User(uName);

View File

@@ -133,7 +133,7 @@ namespace Server.Models
{
foreach(ServerClient sc in serverClients)
{
if (sc.Username == user.Username)
if (sc.User.Username == user.Username)
{
serverClientsInlobbies[l].Add(sc);
break;

View File

@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace SharedClientServer
@@ -25,7 +27,7 @@ namespace SharedClientServer
return payload.username;
}
public static byte[] GetUsernameMessage(string uName)
public static byte[] ConstructUsernameMessage(string uName)
{
return GetMessageToSend(LOGIN, new
{
@@ -33,6 +35,13 @@ namespace SharedClientServer
});
}
public static byte[] ConstructLobbyDataMessage(Lobby lobby)
{
return null;
}
/// <summary>
/// constructs a message that can be sent to the clients or server
/// </summary>
@@ -44,9 +53,13 @@ namespace SharedClientServer
// convert the dynamic to bytes
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
// 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 + 1];
Array.Copy(payloadBytes, 0, res, 1, payloadBytes.Length);
res[0] = identifier;
byte[] res = new byte[payloadBytes.Length + 5];
// put the payload in the res array
Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length);
// put the identifier at the start of the payload part
res[4] = identifier;
// put the length of the payload at the start of the res array
res[0] = BitConverter.GetBytes(payloadBytes.Length+5);
return res;
}
}