/// Messages from clients will be structure as 2 bytes (e.g. 11, 12, 21, etc) /// First = players ID (Each player will be assigned an ID by the server) /// Second = player action/state public void checkMessages() { NetIncomingMessage message; while ((message = server.ReadMessage()) != null) { switch (message.MessageType) { case NetIncomingMessageType.Data: // handle client messages string msg = message.ReadString(); if (!msg.ToString().Equals("0")) { string[] mess = msg.Split('/'); string id = mess[0]; string action = mess[1]; DateTime time = Convert.ToDateTime(mess[2]); ServerPlayer p = getPlayer(id); if (p == null) { Console.WriteLine("ERROR OCCURED: Unable to find player via ID (" + id + ")"); } else { applyLogic(p, action); // Update player accordingly string send = id + "/" + p.getX() + "/" + p.getY(); sendMessages(send, null); // Send updated pos to all players } } break; case NetIncomingMessageType.StatusChanged: // handle connection status messages if (message.SenderConnection.Status == NetConnectionStatus.Connected) { Tuple <int, int> startPos = world.getPlayerPos(); ServerPlayer player = new ServerPlayer(startPos.Item1, startPos.Item2, message.SenderConnection); player.setID(currID.ToString()); currID++; players.Add(player); sendMessages(player.getID(), message.SenderConnection); } else if (message.SenderConnection.Status == NetConnectionStatus.Disconnected) { for (int pos = 0; pos < players.Count; pos++) { if (message.SenderConnection == players[pos].getRecipiant()) { players.RemoveAt(pos); } } } break; case NetIncomingMessageType.WarningMessage: Console.WriteLine(message.ReadString()); break; default: Console.WriteLine("Unhandled message with type: " + message.MessageType); Console.WriteLine(message.ReadString()); break; } } }