//first connect to the server private void FirstContact(SocketState state) { state.callMe = ReceiveStartup; NController.Send(socket, name + "\n"); NController.GetData(state); }
/// <summary> /// update method use to send info to the client /// </summary> public static void update() { // wait until enough time while (watch.ElapsedMilliseconds < MSperFrame) { } watch.Restart(); // use to append message StringBuilder sb = new StringBuilder(); lock (theworld) { theworld.update(); foreach (star s in theworld.getStar().Values) { sb.Append(s.ToString() + '\n'); } foreach (Ship s in theworld.getShip().Values) { sb.Append(s.ToString() + "\n"); } foreach (projectile s in theworld.getProj().Values) { sb.Append(s.ToString() + "\n"); } // clean up the unconnected ship and died stuffs theworld.cleanup(); } //check all client statuses lock (list) { foreach (SocketState ss in list.ToArray()) { if (ss.theSocket.Connected == true) { NController.Send(ss.theSocket, sb.ToString()); } // deal with the unconnected clients else { lock (theworld) { theworld.addLostID(ss.uid); theworld.getShip()[ss.uid].setLost(); } list.Remove(ss); } } } }
/// <summary> /// call back, if the client connects get the user name then generate a new ship, finally send back startup info,and add the client to the list /// </summary> public static void ReceiveName(SocketState ss) { String[] parts = ss.sb.ToString().Split('\n'); String name = parts[0]; Ship ship; lock (theworld) { ship = theworld.generateShip(name); ss.uid = ship.getID(); } lock (list) { list.AddLast(ss); Console.WriteLine("welcome player: " + ship.getName()); } NController.Send(ss.theSocket, ship.getID() + "\n" + UniverSize + "\n"); ss.sb.Clear(); ss.callMe = userInput; NController.GetData(ss); }
/// <summary> /// /// </summary> /// <param name="data"></param> public void sendData(String data) { NController.Send(socket, data + "\n"); }