Merge branch 'master' into setupBranch
This commit is contained in:
@@ -6,6 +6,7 @@ using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
@@ -45,48 +46,56 @@ namespace Server.Models
|
||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||
return;
|
||||
|
||||
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived > 2048)
|
||||
try
|
||||
{
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
{
|
||||
break;
|
||||
}
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// start reading for a new message
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
// start reading for a new message
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
catch (IOException e)
|
||||
{
|
||||
tcpClient.Close();
|
||||
ServerCommunication.INSTANCE.ServerClientDisconnect(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -122,11 +131,15 @@ namespace Server.Models
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
|
||||
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||
//Takes the data sent from the client, and then sets it in a data packet to be sent.
|
||||
dynamic packet = new
|
||||
{
|
||||
username = textUsername,
|
||||
message = textMsg
|
||||
};
|
||||
|
||||
// todo handle sending to all except this user the username and message to display in chat
|
||||
serverCom.SendToLobby(ServerCommunication.INSTANCE.GetLobbyForUser(User),payload);
|
||||
Debug.WriteLine("Payload has been sent!");
|
||||
//Sends the incomming message to be broadcast to all of the clients inside the current lobby.
|
||||
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet));
|
||||
break;
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
@@ -183,8 +196,15 @@ namespace Server.Models
|
||||
break;
|
||||
case LobbyIdentifier.JOIN:
|
||||
int id = JSONConvert.GetLobbyID(payload);
|
||||
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
|
||||
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
|
||||
bool isHost;
|
||||
ServerCommunication.INSTANCE.JoinLobby(this.User,id, out isHost);
|
||||
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage(isHost));
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
case LobbyIdentifier.LEAVE:
|
||||
id = JSONConvert.GetLobbyID(payload);
|
||||
ServerCommunication.INSTANCE.LeaveLobby(User, id);
|
||||
sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace Server.Models
|
||||
public bool Started = false;
|
||||
public List<Lobby> lobbies;
|
||||
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
||||
internal Action DisconnectClientAction;
|
||||
public Action newClientAction;
|
||||
|
||||
|
||||
@@ -97,6 +98,26 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public void ServerClientDisconnect(ServerClient serverClient)
|
||||
{
|
||||
Debug.WriteLine("[SERVERCOMM] handling disconnect");
|
||||
DisconnectClientAction?.Invoke();
|
||||
int id = -1;
|
||||
foreach (Lobby l in serverClientsInlobbies.Keys)
|
||||
{
|
||||
if (serverClientsInlobbies[l].Contains(serverClient))
|
||||
{
|
||||
id = l.ID;
|
||||
}break;
|
||||
}
|
||||
|
||||
if (id != -1)
|
||||
{
|
||||
LeaveLobby(serverClient.User, id);
|
||||
SendToAllExcept(serverClient, JSONConvert.ConstructLobbyLeaveMessage(id));
|
||||
}
|
||||
}
|
||||
|
||||
public void SendToAllExcept(string username, byte[] message)
|
||||
{
|
||||
foreach (ServerClient sc in serverClients)
|
||||
@@ -105,6 +126,14 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public void SendToAllExcept(ServerClient sc, byte[] message)
|
||||
{
|
||||
foreach (ServerClient s in serverClients)
|
||||
{
|
||||
if (s != sc) s.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendToLobby(Lobby lobby, byte[] message)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
@@ -161,6 +190,7 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int HostForLobby(User user)
|
||||
{
|
||||
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
||||
@@ -171,12 +201,18 @@ namespace Server.Models
|
||||
return lobby.ID;
|
||||
}
|
||||
|
||||
public void JoinLobby(User user, int id)
|
||||
public void JoinLobby(User user, int id, out bool isHost)
|
||||
{
|
||||
isHost = false;
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l.ID == id)
|
||||
{
|
||||
if (l.Users.Count == 0)
|
||||
{
|
||||
user.Host = true;
|
||||
isHost = true;
|
||||
}
|
||||
AddToLobby(l, user);
|
||||
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
||||
break;
|
||||
@@ -184,6 +220,41 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public void LeaveLobby(User user, int id)
|
||||
{
|
||||
Debug.WriteLine("[SERVERCOMM] removing user from lobby");
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l.ID == id)
|
||||
{
|
||||
Debug.WriteLine($"[SERVERCOMM] checking for lobby with id {l.ID}");
|
||||
|
||||
foreach (User u in l.Users)
|
||||
{
|
||||
Debug.WriteLine($"[SERVERCOMM] checking if {u.Username} is {user.Username} ");
|
||||
// contains doesn't work, so we'll do it like this...
|
||||
if (u.Username == user.Username)
|
||||
{
|
||||
Debug.WriteLine("[SERVERCOMM] removed user from lobby!");
|
||||
l.Users.Remove(user);
|
||||
foreach (ServerClient sc in serverClients)
|
||||
{
|
||||
if (sc.User.Username == user.Username)
|
||||
{
|
||||
serverClientsInlobbies[l].Remove(sc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseALobby(int lobbyID)
|
||||
{
|
||||
foreach (Lobby lobby in lobbies)
|
||||
|
||||
Reference in New Issue
Block a user