public void TestReceiveHugeMessage(bool clientSide) { Init(); SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState); testLocalSocketState.OnNetworkAction = (x) => { if (x.ErrorOccured) { return; } Networking.GetData(x); }; Networking.GetData(testLocalSocketState); StringBuilder message = new StringBuilder(); message.Append('a', (int)(SocketState.BufferSize * 7.5)); Networking.Send(testRemoteSocketState.TheSocket, message.ToString()); NetworkTestHelper.WaitForOrTimeout(() => { return(testLocalSocketState.GetData().Length == message.Length); }, NetworkTestHelper.timeout); Assert.AreEqual(message.ToString(), testLocalSocketState.GetData()); StopTestServer(testListener, testLocalSocketState, testRemoteSocketState); testLocalSocketState.ClearData(); testRemoteSocketState.ClearData(); }
/// <summary> /// Sends selected spreadsheet name to server and moves into the message loop /// </summary> /// <param name="spreadsheet">Name of Spreadsheet</param> public void SendSpreadsheetRequest(string spreadsheet) { _server.ClearData(); Networking.Send(_server.TheSocket, $"{spreadsheet}\n"); _server.OnNetworkAction = ReceiveUpdatesLoop; Networking.GetData(_server); }
private static void ServeHttpRequest(SocketState state) { StringBuilder sb = new StringBuilder(); string data = state.GetData(); state.ClearData(); if (String.IsNullOrEmpty(data)) { Networking.GetData(state); return; } if (data.Contains("GET")) { Networking.SendAndClose(state.TheSocket, DatabaseModel.GetHomePage(0) + DatabaseModel.GetAllGames(DatabaseModel.GetGamesFromDB())); } }
/// <summary> /// when data arrives from clients update the world /// </summary> /// <param name="state"></param> public void UpdateWorld(SocketState state) { string data = state.GetData(); string[] parts = Regex.Split(data, @"(?<=[\n])"); state.ClearData(); foreach (string p in parts) { if (p.Length == 0) { continue; } if (p[p.Length - 1] != '\n') { break; } UpdateArrived(p, state); } }
/// <summary> /// Callback method for after the server receives the name /// </summary> /// <param name="sock"></param> static void ReceiveName(SocketState sock) { //Create the players tank and assign its name and ID Tank mainPlayer = new Tank(); mainPlayer.name = sock.GetData().Trim(); mainPlayer.ID = (int)sock.ID; //Add the tank to both tank classes lock (tanks) { allTanksEver.Add(mainPlayer.ID, mainPlayer); tanks.Add(mainPlayer.ID, mainPlayer); } //Assign the health of the tank and spawn the tank mainPlayer.hitPoints = StartingHitPoints; tankRespawn(mainPlayer); //Change the callback to CommandRequestHandler and clear the sock's data sock.OnNetworkAction = CommandRequestHandler; sock.ClearData(); //Get the startup data, serialize it, and send it to the client string startupData = mainPlayer.ID + "\n" + serverWorld.worldSize + "\n"; foreach (Wall w in walls.Values) { startupData += JsonConvert.SerializeObject(w) + "\n"; } startupData += GetWorldData(); Networking.Send(sock.TheSocket, startupData); //Add the client to the connectedClients list and ask for more data lock (connectedClients) { connectedClients.Add(sock.ID, sock); } Console.WriteLine(mainPlayer.name + " has Joined the Game"); mainPlayer.joined = true; Networking.GetData(sock); }
/// <summary> /// Detects the incoming messages of new players when they connect then calls Get Data in order to create an event loop to continue listening for /// new incoming players /// </summary> /// <param name="state">Connection of incoming player.</param> private void ProcessMessage(SocketState state) { string data; long ID = state.ID; string[] movingCommands; if (state.ErrorOccured) { PlayerDisconnect(state); Networking.SendAndClose(state.TheSocket, "error"); //Networking.GetData(state); return; } data = state.GetData(); state.ClearData(); if (String.IsNullOrEmpty(data)) { Networking.GetData(state); return; } //checks to see if user is sending a user command or name if (data.StartsWith("{\"moving\"")) { movingCommands = data.Split(','); foreach (string s in movingCommands) { string[] command = s.Split(':'); foreach (string t in command) { string tempString = t.Replace("\"", String.Empty); string tempString2 = tempString.Replace("{", String.Empty); string stringToCompare = tempString2.Replace("}", String.Empty); switch (stringToCompare) { case "none": break; case "up": MoveTank(new Vector2D(0, playerSpeed * -1), new Vector2D(0, -1), players[ID]); break; case "down": MoveTank(new Vector2D(0, playerSpeed), new Vector2D(0, 1), players[ID]); break; case "left": MoveTank(new Vector2D(playerSpeed * -1, 0), new Vector2D(-1, 0), players[ID]); break; case "right": MoveTank(new Vector2D(playerSpeed, 0), new Vector2D(1, 0), players[ID]); break; case "main": FireProjectile(players[ID]); break; case "alt": FireBeam(players[ID]); break; case "x": if (double.TryParse(command[2], out double anglex)) { turretAimx = anglex; } break; case "y": if (command[1].Length > 4) { if (double.TryParse(command[1].Substring(0, command[1].Length - 5), out double angley)) { turretAimy = angley; } players[ID].SetAim(new Vector2D(turretAimx, turretAimy)); } break; } } } } else if (!players.ContainsKey(ID)) { Console.WriteLine("Player " + state.ID.ToString() + " Connected."); CreateTank(data, ID); Sendwalls(state); Networking.GetData(state); return; } Networking.GetData(state); }