diff --git a/Client/Client.cs b/Client/Client.cs index a4f9916..6d5e80b 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -2,8 +2,10 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Net.Sockets; using System.Text; +using System.Windows; using static SharedClientServer.JSONConvert; namespace Client @@ -23,6 +25,7 @@ namespace Client public Callback OnLobbiesListReceived; public Callback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; + public Callback OnServerDisconnect; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; private ClientData data = ClientData.Instance; @@ -39,45 +42,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) { if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; - - int amountReceived = stream.EndRead(ar); - - if (totalBufferReceived + amountReceived > 1024) + try { - throw new OutOfMemoryException("buffer too small"); - } + int amountReceived = stream.EndRead(ar); - Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); - totalBufferReceived += amountReceived; + if (totalBufferReceived + amountReceived > 1024) + { + throw new OutOfMemoryException("buffer too small"); + } - int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived); + totalBufferReceived += amountReceived; - while (totalBufferReceived >= expectedMessageLength) + 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; - expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + Debug.WriteLine("[CLIENT] server not responding! got error: " + e.Message); + OnServerDisconnect?.Invoke(); } - - ar.AsyncWaitHandle.WaitOne(); - stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); + } private void handleData(byte[] message) diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index c1c91df..55c909b 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -35,6 +35,10 @@ namespace Client client = ClientData.Instance.Client; client.OnLobbiesListReceived = updateLobbies; client.OnLobbyLeave = leaveLobby; + client.OnServerDisconnect = () => + { + Environment.Exit(0); + }; OnHostButtonClick = new RelayCommand(hostGame);