Exemplo n.º 1
0
        public static void ListenTcp()
        {
            Socket     socketListener;
            IPEndPoint endPoint = new IPEndPoint(NetworkInfo.GetCurrentIP(), Port);

            socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketListener.Bind(endPoint);
            socketListener.Listen(MaxUsersAmount);
            Console.WriteLine("The server is avaible (TCP)");
            int playerCounter = 0;

            string map = "";

            map += "##############";
            map += "#............#";
            map += "#....#..#....#";
            map += "#.####..####.#";
            map += "#............#";
            map += "#.####..####.#";
            map += "#....#..#....#";
            map += "#............#";
            map += "##############";
            Vector2D[] spawnPoints = new Vector2D[] { new Vector2D(2, 2),
                                                      new Vector2D(2, 7), new Vector2D(10, 7), new Vector2D(10, 2) };
            gameLogic.Map = new TileMap(14, 9, map, spawnPoints);

            Thread threadUpdateGame = new Thread(gameLoop);

            //    threadUpdateGame.IsBackground = true;
            threadUpdateGame.Start();

            while (true)
            {
                Socket            socketClientHandler = socketListener.Accept();
                ConnectionHandler connection          = new ConnectionHandler(socketClientHandler, playerCounter);
                connection.EventOnMessageReceive += AddCheckedMessage;
                Player newPlayer = new Player(new Vector2D(2, 2), 100, 0.5 / 1000, playerCounter);
                newPlayer.Size = 0.5;
                gameLogic.AddPlayer(playerCounter, newPlayer);
                playersSockets.Add(playerCounter, socketClientHandler);

                SendMessage(new MessagePersonalAddPlayer()
                {
                    PlayerID = playerCounter, Map = gameLogic.Map
                }, socketClientHandler);
                SendAddPlayersInfo(socketClientHandler);
                SendToAll(new MessageAddPlayer()
                {
                    PlayerID = playerCounter, Player = newPlayer
                });

                playerCounter++;
            }
        }
Exemplo n.º 2
0
        public void UdpBroadcastRequest()
        {
            MessageSearchRequest message          = new MessageSearchRequest();
            IPAddress            currentIPAddress = NetworkInfo.GetCurrentIP();

            message.Port     = ((IPEndPoint)socketUdpHandler.LocalEndPoint).Port;
            message.IPAdress = currentIPAddress.ToString();
            IPAddress  mask            = NetworkInfo.GetIPv4Mask(currentIPAddress);
            IPAddress  strongBroadcast = NetworkInfo.GetBroadcastAddress(currentIPAddress, mask);
            IPEndPoint IPendPoint      = new IPEndPoint(strongBroadcast, ServerPort);
            Socket     sendRequest     = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            sendRequest.SendTo(messageSerializer.Serialize(message, message.GetType()), IPendPoint);
            Thread threadReceiveUdp = new Thread(ReceiveMessagesUdp);

            threadReceiveUdp.IsBackground = true;
            threadReceiveUdp.Start();
        }
Exemplo n.º 3
0
        public static void HandleSearchMessage(MessageSearchRequest message)
        {
            while (gameLogic == null)
            {
                ;
            }
            MessageServerInfo messageResponse = new MessageServerInfo()
            {
                IPAdress             = NetworkInfo.GetCurrentIP().ToString(),
                Port                 = Port,
                MaxPlayersAmount     = MaxUsersAmount,
                CurrentPlayersAmount = gameLogic.Players.Count,
                MapName              = "Level 1",
                MapWidth             = gameLogic.Map.Width,
                MapHeight            = gameLogic.Map.Height
            };
            Socket     socketSetAdress = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint endPoint        = new IPEndPoint(IPAddress.Parse(message.IPAdress), message.Port);

            socketSetAdress.SendTo(messageSerializer.Serialize(messageResponse, messageResponse.GetType()), endPoint);
        }