[FIX] fixed server client message structure
This commit is contained in:
@@ -16,11 +16,11 @@ namespace Client
|
|||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
public int Port = 5555;
|
public int Port = 5555;
|
||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
//TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username)
|
private string username;
|
||||||
public string Username { get; }
|
|
||||||
|
|
||||||
public Client()
|
public Client(string username)
|
||||||
{
|
{
|
||||||
|
this.username = username;
|
||||||
this.tcpClient = new TcpClient();
|
this.tcpClient = new TcpClient();
|
||||||
Debug.WriteLine("Starting connect to server");
|
Debug.WriteLine("Starting connect to server");
|
||||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||||
@@ -31,6 +31,7 @@ namespace Client
|
|||||||
Debug.Write("finished connecting to server");
|
Debug.Write("finished connecting to server");
|
||||||
this.tcpClient.EndConnect(ar);
|
this.tcpClient.EndConnect(ar);
|
||||||
this.stream = tcpClient.GetStream();
|
this.stream = tcpClient.GetStream();
|
||||||
|
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,11 +101,13 @@ namespace Client
|
|||||||
|
|
||||||
public void SendMessage(byte[] message)
|
public void SendMessage(byte[] message)
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnWriteComplete(IAsyncResult ar)
|
private void OnWriteComplete(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("[CLIENT] finished writing");
|
||||||
stream.EndWrite(ar);
|
stream.EndWrite(ar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace Client
|
|||||||
_model = new Model();
|
_model = new Model();
|
||||||
ButtonCommand = new RelayCommand(() =>
|
ButtonCommand = new RelayCommand(() =>
|
||||||
{
|
{
|
||||||
Client client = new Client();
|
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
_lobbies = new List<Lobby>();
|
_lobbies = new List<Lobby>();
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ namespace Client.Views
|
|||||||
|
|
||||||
private void Button_EnterUsername(object sender, RoutedEventArgs e)
|
private void Button_EnterUsername(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
User user = new User(usernameTextbox.Text, 0, false);
|
User user = new User(usernameTextbox.Text);
|
||||||
Client client = new Client();
|
Client client = new Client(user.Username);
|
||||||
|
|
||||||
data.User = user;
|
data.User = user;
|
||||||
data.Client = client;
|
data.Client = client;
|
||||||
|
|||||||
@@ -47,7 +47,7 @@
|
|||||||
</ComboBox>
|
</ComboBox>
|
||||||
|
|
||||||
<Label Grid.Row="3" Name="testLabel" FontSize="15" VerticalAlignment="Center"/>
|
<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>
|
</Grid>
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Client
|
|||||||
this.DataContext = new ViewModel();
|
this.DataContext = new ViewModel();
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
usernameTextbox.Text = data.User.Username;
|
usernameLabel.Content = data.User.Username;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Button_Click(object sender, RoutedEventArgs e)
|
private void Button_Click(object sender, RoutedEventArgs e)
|
||||||
|
|||||||
@@ -24,8 +24,10 @@ namespace Server.Models
|
|||||||
/// <param name="client">the TcpClient object to use</param>
|
/// <param name="client">the TcpClient object to use</param>
|
||||||
public ServerClient(TcpClient client)
|
public ServerClient(TcpClient client)
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
|
||||||
tcpClient = client;
|
tcpClient = client;
|
||||||
stream = tcpClient.GetStream();
|
stream = tcpClient.GetStream();
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] starting read");
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
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>
|
/// <param name="ar">the async result status</param>
|
||||||
private void OnRead(IAsyncResult ar)
|
private void OnRead(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
|
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
|
||||||
int bytesReceived = this.stream.EndRead(ar);
|
int bytesReceived = this.stream.EndRead(ar);
|
||||||
|
|
||||||
if (totalBufferReceived + bytesReceived > 1024)
|
if (totalBufferReceived + bytesReceived > 1024)
|
||||||
@@ -84,16 +90,18 @@ namespace Server.Models
|
|||||||
/// <param name="message">the incoming message</param>
|
/// <param name="message">the incoming message</param>
|
||||||
private void HandleIncomingMessage(byte[] message)
|
private void HandleIncomingMessage(byte[] message)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Got message from {User?.Username} : {message}");
|
Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}");
|
||||||
byte id = message[0];
|
byte id = message[4];
|
||||||
byte[] payload = new byte[message.Length - 1];
|
byte[] payload = new byte[message.Length - 5];
|
||||||
Array.Copy(message,1,payload,0,message.Length-1);
|
Array.Copy(message,5,payload,0,message.Length-5);
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
|
||||||
switch(id)
|
switch(id)
|
||||||
{
|
{
|
||||||
|
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
// json log in username data
|
// json log in username data
|
||||||
string uName = JSONConvert.GetUsernameLogin(payload);
|
string uName = JSONConvert.GetUsernameLogin(payload);
|
||||||
|
|
||||||
if (uName != null)
|
if (uName != null)
|
||||||
{
|
{
|
||||||
User = new User(uName);
|
User = new User(uName);
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
foreach(ServerClient sc in serverClients)
|
foreach(ServerClient sc in serverClients)
|
||||||
{
|
{
|
||||||
if (sc.Username == user.Username)
|
if (sc.User.Username == user.Username)
|
||||||
{
|
{
|
||||||
serverClientsInlobbies[l].Add(sc);
|
serverClientsInlobbies[l].Add(sc);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using Newtonsoft.Json;
|
using Client;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
@@ -25,7 +27,7 @@ namespace SharedClientServer
|
|||||||
return payload.username;
|
return payload.username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] GetUsernameMessage(string uName)
|
public static byte[] ConstructUsernameMessage(string uName)
|
||||||
{
|
{
|
||||||
return GetMessageToSend(LOGIN, new
|
return GetMessageToSend(LOGIN, new
|
||||||
{
|
{
|
||||||
@@ -33,6 +35,13 @@ namespace SharedClientServer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] ConstructLobbyDataMessage(Lobby lobby)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <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>
|
||||||
@@ -44,9 +53,13 @@ namespace SharedClientServer
|
|||||||
// convert the dynamic to bytes
|
// convert the dynamic to bytes
|
||||||
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
|
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
|
// 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];
|
byte[] res = new byte[payloadBytes.Length + 5];
|
||||||
Array.Copy(payloadBytes, 0, res, 1, payloadBytes.Length);
|
// put the payload in the res array
|
||||||
res[0] = identifier;
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user