Merge branch 'master' into setupBranch
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
using System.Windows;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
@@ -30,6 +32,7 @@ namespace Client
|
||||
public Callback OnLobbiesListReceived;
|
||||
public LobbyJoinCallback OnLobbyJoinSuccess;
|
||||
public Callback OnLobbiesReceivedAndWaitingForHost;
|
||||
public Callback OnServerDisconnect;
|
||||
public LobbyCallback OnLobbyCreated;
|
||||
public LobbyCallback OnLobbyLeave;
|
||||
private ClientData data = ClientData.Instance;
|
||||
@@ -48,43 +51,60 @@ namespace Client
|
||||
private void OnConnect(IAsyncResult ar)
|
||||
{
|
||||
Debug.Write("finished connecting to server");
|
||||
this.tcpClient.EndConnect(ar);
|
||||
this.stream = tcpClient.GetStream();
|
||||
OnSuccessfullConnect?.Invoke();
|
||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||
try
|
||||
{
|
||||
this.tcpClient.EndConnect(ar);
|
||||
this.stream = tcpClient.GetStream();
|
||||
OnSuccessfullConnect?.Invoke();
|
||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||
|
||||
} catch (Exception e)
|
||||
{
|
||||
Debug.WriteLine("Can't connect, retrying...");
|
||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnReadComplete(IAsyncResult ar)
|
||||
{
|
||||
int amountReceived = stream.EndRead(ar);
|
||||
if (totalBufferReceived + amountReceived > 1024)
|
||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||
return;
|
||||
try
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += amountReceived;
|
||||
int amountReceived = stream.EndRead(ar);
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
if (totalBufferReceived + amountReceived > 1024)
|
||||
{
|
||||
throw new OutOfMemoryException("buffer too small");
|
||||
}
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
|
||||
totalBufferReceived += amountReceived;
|
||||
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the complete packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// put the message received into the message array
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
|
||||
handleData(message);
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
}
|
||||
|
||||
ar.AsyncWaitHandle.WaitOne();
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
} catch (IOException e)
|
||||
{
|
||||
// we have received the complete packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// put the message received into the message array
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
|
||||
handleData(message);
|
||||
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
Debug.WriteLine($"reduced buffer: {expectedMessageLength}");
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
Debug.WriteLine("[CLIENT] server not responding! got error: " + e.Message);
|
||||
OnServerDisconnect?.Invoke();
|
||||
}
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
|
||||
}
|
||||
|
||||
private void handleData(byte[] message)
|
||||
@@ -162,10 +182,18 @@ namespace Client
|
||||
}
|
||||
break;
|
||||
|
||||
case JSONConvert.RANDOMWORD:
|
||||
//Flag byte for receiving the random word.
|
||||
int lobbyId = JSONConvert.GetLobbyID(payload);
|
||||
|
||||
if(data.Lobby?.ID == lobbyId)
|
||||
ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload));
|
||||
break;
|
||||
default:
|
||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||
break;
|
||||
}
|
||||
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED,null));
|
||||
|
||||
}
|
||||
|
||||
@@ -173,12 +201,14 @@ namespace Client
|
||||
{
|
||||
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);
|
||||
stream.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ namespace Client
|
||||
client = ClientData.Instance.Client;
|
||||
client.OnLobbiesListReceived = updateLobbies;
|
||||
client.OnLobbyLeave = leaveLobby;
|
||||
client.OnServerDisconnect = () =>
|
||||
{
|
||||
Environment.Exit(0);
|
||||
};
|
||||
|
||||
|
||||
OnHostButtonClick = new RelayCommand(hostGame);
|
||||
@@ -61,12 +65,10 @@ namespace Client
|
||||
|
||||
private void becomeHostForLobby(int id)
|
||||
{
|
||||
|
||||
Debug.WriteLine($"got host succes with data {id} ");
|
||||
wantToBeHost = true;
|
||||
wantToBeHostId = id;
|
||||
client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
|
||||
|
||||
}
|
||||
|
||||
private void hostLobbiesReceived()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
using Client.Views;
|
||||
using Client.Views;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
@@ -7,6 +7,7 @@ using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
@@ -25,6 +26,12 @@ namespace Client.ViewModels
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
public static string Word
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public string _username;
|
||||
|
||||
public string _message;
|
||||
@@ -165,6 +172,10 @@ namespace Client.ViewModels
|
||||
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
||||
}
|
||||
|
||||
/*
|
||||
* MISC make this a callback
|
||||
* Handles the incoming chat message from another client.
|
||||
*/
|
||||
public static void HandleIncomingMsg(string username, string message)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
@@ -172,13 +183,21 @@ namespace Client.ViewModels
|
||||
Messages.Add($"{username}: {message}");
|
||||
});
|
||||
}
|
||||
public void LeaveGame(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
public void LeaveGame(object sender, CancelEventArgs e)
|
||||
{
|
||||
Debug.WriteLine("Leaving...");
|
||||
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* MISC make this a callback
|
||||
* Handles the random word that has been received from the server.
|
||||
*/
|
||||
public static void HandleRandomWord(string randomWord)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] Reached the handle random word method!");
|
||||
Word = "NegerPik";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user