Merge remote-tracking branch 'origin/setupBranch' into setupBranch

This commit is contained in:
Dogukan
2020-10-20 19:53:35 +02:00
16 changed files with 298 additions and 52 deletions

View File

@@ -10,12 +10,12 @@ namespace Server.Models
{
class ServerClient : ObservableObject
{
public string Username { get; set; }
private TcpClient tcpClient;
private NetworkStream stream;
private byte[] buffer = new byte[1024];
private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0;
public User User { get; set; }
/// <summary>
@@ -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,24 +90,27 @@ namespace Server.Models
/// <param name="message">the incoming message</param>
private void HandleIncomingMessage(byte[] message)
{
Debug.WriteLine($"Got message from {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 0x01:
case JSONConvert.LOGIN:
// json log in username data
string uName = JSONConvert.GetUsernameLogin(message);
string uName = JSONConvert.GetUsernameLogin(payload);
if (uName != null)
{
Username = uName;
Debug.WriteLine("[SERVERCLIENT] set username to " + Username);
User = new User(uName);
User.Username = uName;
Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
}
break;
case 0x02:
case JSONConvert.MESSAGE:
// json message data
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1;
@@ -110,10 +119,10 @@ namespace Server.Models
// todo handle sending to all except this user the username and message to display in chat
break;
case 0x03:
case JSONConvert.LOBBY:
// lobby data
break;
case 0x04:
case JSONConvert.CANVAS:
// canvas data
// todo send canvas data to all other serverclients in lobby
break;

View File

@@ -17,6 +17,7 @@ namespace Server.Models
public List<Lobby> lobbies;
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
public Action newClientAction;
/// <summary>
/// use a padlock object to make sure the singleton is thread-safe
@@ -97,7 +98,7 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClients)
{
if (sc.Username != username) sc.sendMessage(message);
if (sc.User.Username != username) sc.sendMessage(message);
}
}
@@ -116,14 +117,14 @@ namespace Server.Models
}
}
public void AddToLobby(Lobby lobby, string username)
public void AddToLobby(Lobby lobby, User user)
{
foreach (Lobby l in lobbies)
{
if (l == lobby)
{
bool succ;
l.AddUser(username, out succ);
l.AddUser(user, out succ);
if (!succ)
{
// TODO send lobby full message
@@ -131,7 +132,7 @@ namespace Server.Models
{
foreach(ServerClient sc in serverClients)
{
if (sc.Username == username)
if (sc.User.Username == user.Username)
{
serverClientsInlobbies[l].Add(sc);
break;