private static void update() { StringBuilder sb = new StringBuilder(); Stopwatch watch = new Stopwatch(); watch.Start(); while (watch.ElapsedMilliseconds < MSPerframe) { /*do nothing */ } watch.Reset(); //update stuff lock (Servers.world) { world.Update(); foreach (KeyValuePair <int, Star> entrys in world.GetStar().ToList()) { string message = JsonConvert.SerializeObject(entrys.Value); sb.Append(message + "\n"); } foreach (KeyValuePair <int, Ship> entrys in world.GetShips().ToList()) { string message = JsonConvert.SerializeObject(entrys.Value); sb.Append(message + "\n"); } foreach (KeyValuePair <int, Proj> entrys in world.GetProj().ToList()) { string message = JsonConvert.SerializeObject(entrys.Value); sb.Append(message + "\n"); } world.CleanUp(); } lock (Clients) { foreach (SocketState j in Clients) { Networking.SendData(j.theSocket, sb.ToString()); } } }
private static void ReceiveName(SocketState state) { StringBuilder sb = new StringBuilder(); string playerName = state.sb.ToString();// Get playername string playerName2 = Regex.Replace(playerName.Trim(), @"\t|\n|\r", ""); //the if else statments are just to show that the controls are working GetInputs(state); state.sb.Clear(); if (playerName2.Length > 1 && !playerName.Contains("(")) { lock (Clients) // Adding clients to list { Clients.Add(state); } //Calls for Assign ID and allows asssignID to increment to give a unique ID AssignID(); //create a random ship for player with the ID and name Ship s = RandomShipGen(ID, playerName2); //add it to the world dictionry for ships lock (world.GetShips()) { world.GetShips().Add(s.GetID(), s); } sb.Append(ID + "\n" + UniverseSize); //seeing tokens string final = sb.ToString(); //sends the intial startup Networking.SendData(state.theSocket, final); } //upon recieving the name, send the startup data //the server must not send any wold data to a client before sending the startup data. }
/// <summary> /// Callback used to parse initial information from the client. Sends /// start-up information to client, adds this client to the list of connected /// clients, begins listening for input. /// </summary> /// <param name="State"></param> private void RecieveName(SocketState State) { //Assigns a uniuqe ID that will be used for this client and its snake. State.ID = clients.GetNextID(); //Generates start-up info for the newly connected client string startUpInfo = "" + State.ID + '\n' + world.Width + '\n' + world.Height + '\n'; Networking.SendData(State, startUpInfo); //PARSE NAME INFO & CREATE SNAKE WITH ID = State.ID // Grab the data from he sb String data = State.sb.ToString(); // split the data into the individul pieces String[] DataList = data.Split(); String name; // Set the name if (DataList.Length == 0 || DataList[0].Equals("")) { name = "Boaty McBoatface"; } else { name = DataList[0]; } //Creates a snake for this client and places it in the world world.createSnake(State.ID, name); //Adds this client to list of connected clients clients.Add(State); //Changes the callback to listen for user input State.CallMe = GetInput; //Starts an event loop listening for info from client. Networking.GetData(State); }
/// <summary> /// Tells everything to update for the next frame and then send the info to the clients /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UpdateFrame(object sender, ElapsedEventArgs e) { //Update the world world.UpdateWorld(); //TODO: Put the following serialization into world and lock it with snake lock and food lock // Compile the date to be sent // Create a stringbuilder to hold data to be sent and append new items on StringBuilder data = new StringBuilder(); // Append all the snakes onto the StringBuilder foreach (Snake snake in world.GetSnakes()) { data.Append(JsonConvert.SerializeObject(snake) + "\n"); } // Append all the food onto the StringBuider foreach (Food food in world.GetFood()) { data.Append(JsonConvert.SerializeObject(food) + "\n"); } String message = data.ToString(); //Send data to Clients foreach (SocketState client in clients.GetAllClients()) { try { Networking.SendData(client, message); } catch { Console.WriteLine("Tried to send data to client that has already disconnected."); } } }