Beispiel #1
0
        private void UdpListener()
        {
            string data = "";

            while (true)
            {
                try
                {
                    receiveBytes = udpServer.Receive(ref ipEndPoint);
                    data         = Encoding.ASCII.GetString(receiveBytes);

                    Packet packet = Packet.Populate(data);

                    switch (packet.PacketType)
                    {
                    case PacketType.Coordinates:
                        string[] contentParts = packet.Content.Split('|');

                        var foundPlayers = from p in Participants
                                           where p.Nickname == contentParts[0]
                                           select p;

                        if (foundPlayers != null && foundPlayers.Count() == 1)
                        {
                            if (!Settings.FreezeAllPlayersEnabled)
                            {
                                ServerParticipant foundPlayer = foundPlayers.First();

                                foundPlayer.X        = float.Parse(contentParts[1]);
                                foundPlayer.Y        = float.Parse(contentParts[2]);
                                foundPlayer.Z        = float.Parse(contentParts[3]);
                                foundPlayer.SpeedX   = float.Parse(contentParts[4]);
                                foundPlayer.SpeedY   = float.Parse(contentParts[5]);
                                foundPlayer.SpeedZ   = float.Parse(contentParts[6]);
                                foundPlayer.VectorX1 = float.Parse(contentParts[7]);
                                foundPlayer.VectorY1 = float.Parse(contentParts[8]);
                                foundPlayer.VectorZ1 = float.Parse(contentParts[9]);
                                foundPlayer.VectorX2 = float.Parse(contentParts[10]);
                                foundPlayer.VectorY2 = float.Parse(contentParts[11]);
                                foundPlayer.VectorZ2 = float.Parse(contentParts[12]);

                                foundPlayer.LastActivity = DateTime.Now;
                            }

                            string stringToSend = PacketType.Coordinates + "|" + Settings.Serialize();

                            foreach (ServerParticipant player in participants)
                            {
                                stringToSend += "|" + player.Nickname + ";" + player.X + ";" + player.Y + ";" + player.Z + ";" + player.SpeedX + ";" + player.SpeedY + ";" + player.SpeedZ + ";" + player.VectorX1 + ";" + player.VectorY1 + ";" + player.VectorZ1 + ";" + player.VectorX2 + ";" + player.VectorY2 + ";" + player.VectorZ2 + ";" + player.PowerUpType + ";" + player.PowerUpWhiteBricks;
                            }

                            sendBytes = Encoding.ASCII.GetBytes(stringToSend);
                            udpServer.Send(sendBytes, sendBytes.GetLength(0), ipEndPoint);
                        }
                        break;
                    }
                }
                catch (SocketException exc)
                {
                    if (exc.ErrorCode == 10054)
                    {
                        // Participant closed the Client - no action is required as the Server will clean it up automatically
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }

                //Console.WriteLine("Handling client at " + ipEndPoint + " - ");
                //Console.WriteLine("Message Received " + data.TrimEnd());

                //server.Send(receivedBytes, receivedBytes.Length, remoteIPEndPoint);
                //Console.WriteLine("Message Echoed to" + remoteIPEndPoint + data);
            }
        }