private void Heartbeat(CancellationToken cancellationToken) { Client.Client client = new Client.Client(); while (true) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } try { client.Input(new string[] { "-t", "dateTime", (DateTime.Now + TimeSpan.FromMilliseconds(3000)).ToString() }); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif } Thread.Sleep(1000); } }
private static void Loop() { while (true) { Client.Client client = new Client.Client(); #if DEBUG string[] args = Console.ReadLine().Split(new char[] { ' ' }); Console.WriteLine(client.Input(args)); #endif } }
public void Input() { bool whileContinue = true; bool notValidResponse1 = false; int threads; Client.Client client = new Client.Client(); TCPServer.TCPServer tcpServer = new TCPServer.TCPServer(); CancellationToken ct = new CancellationToken(); Task serverTask = new Task(() => tcpServer.Input(ct), TaskCreationOptions.LongRunning); serverTask.Start(); do { Console.WriteLine(); Console.Write("Please enter an initial number of threads: "); string ifString = Console.ReadLine(); if (int.TryParse(ifString.Trim(), out threads)) { while (whileContinue) { Console.WriteLine(); Console.Write("Number of threads: " + threads); Console.WriteLine(); Parallel.For(0, threads, index => client.Input(new string[] { "-t", "username", "location" })); bool notValidResponse2 = false; do { Console.WriteLine(); Console.Write("Would you like to continue Y/N or enter a number of threads: "); ifString = Console.ReadLine().Trim(); int tempThreads; if (int.TryParse(ifString, out tempThreads)) { threads = tempThreads; } else { ifString = ifString.ToUpper(); if (ifString == "Y") { threads++; } else { if (ifString == "N") { whileContinue = false; } else { Console.WriteLine(); Console.Write("Please enter a valid response"); notValidResponse2 = true; } } } } while (notValidResponse2); } } else { Console.WriteLine(); Console.Write("Please enter a valid response"); notValidResponse1 = true; } } while (notValidResponse1); }
private void Client(CancellationToken cancellationToken) { string temporaryIP = string.Empty; Client.Client client = new Client.Client(); while (searchActivity.ip == string.Empty) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } temporaryIP = client.Input(new string[] { "-u" }); int temporaryInt; if (int.TryParse(temporaryIP.Split(new char[] { '.' })[0], out temporaryInt)) { searchActivity.ip = temporaryIP; } if (searchActivity.ip != string.Empty) { try { client.Input(new string[] { "-t", "-i", searchActivity.ip, "test", "123" }); int.Parse(Regex.Split(client.Input(new string[] { "-t", "-i", searchActivity.ip, "test" }), ": ")[1]); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif searchActivity.ip = string.Empty; } } Thread.Sleep(1000); } searchActivity.playerPosition = int.Parse(Regex.Split(client.Input(new string[] { "-t", "-i", searchActivity.ip, "numberOfPlayers" }), ": ")[1]) + 1; searchActivity.numberOfPlayers = searchActivity.playerPosition; client.Input(new string[] { "-t", "-i", searchActivity.ip, "numberOfPlayers", searchActivity.numberOfPlayers.ToString() }); searchActivity.RunOnUiThread(() => searchActivity.searchTextView.Text = context.Resources.GetString(Resource.String.JoinSearchTitleConnected) + " " + context.Resources.GetString(Resource.String.JoinSearchTitleWaiting)); while (true) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } if (Regex.Split(client.Input(new string[] { "-t", "-i", searchActivity.ip, "start" }), ": ")[1] == "true") { searchActivity.GoToGameActivity(searchActivity.searchType); } Thread.Sleep(1000); } }
private void Output(CancellationToken cancellationToken) { bool notStartedBool = true; Client.Client client = new Client.Client(); while (notStartedBool) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } notStartedBool = false; try { client.Input(new string[] { "-t", "test", "123" }); int.Parse(Regex.Split(client.Input(new string[] { "-t", "test" }), ": ")[1]); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif notStartedBool = true; } Thread.Sleep(1000); } Console.WriteLine(DateTime.Now + TimeSpan.FromMilliseconds(10000)); client.Input(new string[] { "-t", "dateTime", (DateTime.Now + TimeSpan.FromMilliseconds(10000)).ToString() }); Console.WriteLine(Regex.Split(client.Input(new string[] { "-t", "dateTime" }), ": ")[1]); DateTime serverDateTime = DateTime.Parse(Regex.Split(client.Input(new string[] { "-t", "dateTime" }), ": ")[1]); //while (serverDateTime >= DateTime.Now) while (true) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { cancelationTokenSource.Cancel(); // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } Thread.Sleep(1000); try { serverDateTime = DateTime.Parse(Regex.Split(client.Input(new string[] { "-t", "dateTime" }), ": ")[1]); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif } } }
private void Client(CancellationToken cancellationToken) { bool notStartedBool = true; Client.Client client = new Client.Client(); while (notStartedBool) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } notStartedBool = false; try { client.Input(new string[] { "-t", "test", "123" }); int.Parse(Regex.Split(client.Input(new string[] { "-t", "test" }), ": ")[1]); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif notStartedBool = true; } Thread.Sleep(1000); } client.Input(new string[] { "-t", "start", "false" }); client.Input(new string[] { "-t", "numberOfPlayers", "1" }); searchActivity.playerPosition = 1; searchActivity.numberOfPlayers = 1; searchActivity.RunOnUiThread(() => searchActivity.searchButton.Text = context.Resources.GetString(Resource.String.HostSearchButtonLabelPlay) + " " + searchActivity.numberOfPlayers + " " + context.Resources.GetString(Resource.String.HostSearchButtonLabelPlayers)); int temporaryNumberOfPlayers; while (true) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } try { temporaryNumberOfPlayers = int.Parse(Regex.Split(client.Input(new string[] { "-t", "numberOfPlayers" }), ": ")[1]); if (temporaryNumberOfPlayers > searchActivity.numberOfPlayers) { searchActivity.numberOfPlayers = temporaryNumberOfPlayers; searchActivity.RunOnUiThread(() => searchActivity.searchButton.Text = context.Resources.GetString(Resource.String.HostSearchButtonLabelPlay) + " " + searchActivity.numberOfPlayers + " " + context.Resources.GetString(Resource.String.HostSearchButtonLabelPlayers)); } } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif } Thread.Sleep(1000); } }
private void JoinClient(CancellationToken cancellationToken) { bool notStartedBool = true; Client.Client client = new Client.Client(); while (notStartedBool) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } notStartedBool = false; try { client.Input(new string[] { "-t", "-i", ip, "test", "123" }); int.Parse(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "test" }), ": ")[1]); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif notStartedBool = true; } Thread.Sleep(1000); } Thread.Sleep(3000); string temporaryString; RunOnUiThread(() => { client.Input(new string[] { "-t", "-i", ip, "player" + playerPosition.ToString(), Static.Serialise.SerialiseLatLng(playerArray[playerPosition].currentPosition) }); gamePlayArea.vertices = Static.Serialise.DeserialiseLatLngLinkedList(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "playArea" }), ": ")[1]); gamePlayArea.polygons.RemoveFirst(); gamePlayArea.playAreaDrawnBool = false; utilities.SetPolygon(gamePlayArea.vertices); playerArray[playerPosition].vertices = new LinkedList <LatLng>(); playerArray[playerPosition].drawingBool = false; playerArray[playerPosition].positionBool = false; playerArray[playerPosition].deathBool = false; circle.Center = Static.Serialise.DeserialiseLatLng(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "circleCentre", }), ": ")[1], out temporaryString); circle.Radius = double.Parse(Static.Serialise.DeserialiseString(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "circleRadius", }), ": ")[1], out temporaryString)); }); while (true) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } try { Parallel.For(0, numberOfPlayers, (i) => { if (i != playerPosition) { // Poll on this property if you have to do // other cleanup before throwing. if (cancellationToken.IsCancellationRequested) { // Clean up here, then... cancellationToken.ThrowIfCancellationRequested(); } try { LatLng temporaryPosition; RunOnUiThread(() => { temporaryPosition = Static.Serialise.DeserialiseLatLng(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "player" + (i).ToString() }), ": ")[1], out temporaryString); if (temporaryPosition.Latitude != playerArray[i].currentPosition.Latitude || temporaryPosition.Longitude != playerArray[i].currentPosition.Longitude) { playerArray[i].currentPosition = temporaryPosition; utilities.UpdateLocationInformation(i, playerArray[i].currentPosition); } }); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif } } else { RunOnUiThread(() => client.Input(new string[] { "-t", "-i", ip, "player" + playerPosition.ToString(), Static.Serialise.SerialiseLatLng(playerArray[playerPosition].currentPosition) })); } }); RunOnUiThread(() => circle.Center = Static.Serialise.DeserialiseLatLng(Regex.Split(client.Input(new string[] { "-t", "-i", ip, "circleCentre", }), ": ")[1], out temporaryString)); } catch (Exception ex) { #if DEBUG //This prints to the screen an error message Console.WriteLine("ERROR: " + ex.ToString()); #endif } Thread.Sleep(1000); } }