public static void Initialize(string ip, int port)
        {
            TcpClient = new TcpClient(ip, port);
            Player    = new Player(new Vector2(0));
            Id        = -1;

            NetHelper.SendMessageTo(TcpClient, "0");

            while (true)
            {
                string status;
                status = NetHelper.ReceiveMessageFrom(TcpClient);

                if (status == "OK")
                {
                    break;
                }
            }

            NetHelper.SendMessageTo(TcpClient,
                                    NetHelper.BuildRequestString((int)Headers.DataType.RequestData,
                                                                 (int)Headers.RequestDataType.GetFreeSlot));

            while (!Stream.DataAvailable)
            {
                Console.WriteLine("Waiting for ID from server...");

                Thread.Sleep(1000 / 2);
            }

            // Possibly have id.
            string[] message = NetHelper.ReceiveMessageFrom(TcpClient).Split(Headers.SplitChar);

            switch (Int32.Parse(message[0]))
            {
            case (int)Headers.DataType.RequestJoin:
            {
                Id        = Int32.Parse(message[1]);
                Player.Id = Id;
                Console.WriteLine("Player id set to: " + Id);

                if (Id == -1)
                {
                    Console.WriteLine("Server is full.");
                    Environment.Exit(0);
                }
                break;
            }

            default:
            {
                Console.WriteLine("Received message was invalid.");
                break;
            }
            }

            ServerSetDrawChar(DrawChar);
            ServerSetColor(DrawColor);
            ServerSetPlayerName(PlayerName);

            World.CharTiles = ServerGetTiles();

            Console.ReadKey(true);
        }
Ejemplo n.º 2
0
        public static void Update()
        {
            World.Update();

            int clientCount = ConnectedClients.Count(c => c != null);

            //Console.WriteLine("Server client count: " + clientCount);

            foreach (Client c in ConnectedClients)
            {
                if (c == null)
                {
                    continue;
                }

                TcpClient tcpClient = c.TcpClient;

                if (!tcpClient.GetStream().DataAvailable)
                {
                    continue;
                }

                string message = NetHelper.ReceiveMessageFrom(tcpClient);

                string[] messages = message.Split(Headers.SplitChar);
                int      dataType = int.Parse(messages[0]);

                switch (dataType)
                {
                case (int)Headers.DataType.RequestData:
                {
                    int requestType = int.Parse(messages[1]);

                    switch (requestType)
                    {
                    case (int)Headers.RequestDataType.GetPlayerPosition:
                    {
                        int    id         = int.Parse(messages[2]);
                        string posMessage = NetHelper.BuildRequestString(ConnectedClients[id].Player.Position.X, ConnectedClients[id].Player.Position.Y);

                        NetHelper.SendMessageTo(tcpClient, posMessage);
                        break;
                    }

                    case (int)Headers.RequestDataType.GetPlayersInfo:                                             // Send players position and draw character in the form of: id:x:y:drawChar:drawColor:name.
                    {
                        string msg = string.Empty;
                        foreach (Client _c in ConnectedClients)
                        {
                            if (_c == null)
                            {
                                continue;
                            }

                            msg += _c.Id + ":" + _c.Player.Position.X + ":" + _c.Player.Position.Y + ":" + (int)_c.Player.DrawChar + ":" + (int)_c.Player.Drawable.Color + ":" + _c.Player.Name + Headers.SplitChar;
                        }

                        NetHelper.SendMessageTo(tcpClient, msg);
                        break;
                    }

                    case (int)Headers.RequestDataType.GetFreeSlot:
                    {
                        bool sendFull = true;
                        for (int i = 0; i < ConnectedClients.Length; i++)
                        {
                            Client _c = ConnectedClients[i];
                            if (_c == null)
                            {
                                // Send i to tcpClient.
                                NetHelper.SendMessageTo(tcpClient,
                                                        (int)Headers.DataType.RequestJoin + Headers.SplitChar.ToString() + (int)MathHelper.Clamp(i - 1, 0, int.MaxValue));
                                sendFull = false;
                                break;
                            }
                        }

                        if (sendFull)
                        {
                            NetHelper.SendMessageTo(tcpClient, (int)Headers.DataType.RequestJoin + ";-1");
                        }

                        break;
                    }

                    case (int)Headers.RequestDataType.GetWorldTiles:
                    {
                        string msg = "";
                        foreach (Drawable d in World.CharTiles)
                        {
                            msg += d.Position.X + ":" + d.Position.Y + ":" + (int)d.DrawChar + ":" + (int)d.Color + Headers.SplitChar;
                        }

                        NetHelper.SendMessageTo(tcpClient, msg);

                        break;
                    }
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerPosition:
                {
                    int id      = int.Parse(messages[1]);
                    int x       = int.Parse(messages[2]);
                    int y       = int.Parse(messages[3]);
                    int success = 1;

                    // Collision check with console buffer size.
                    if (x >= 0 && y >= 0 && x < Console.BufferWidth && y < Console.BufferHeight)
                    {
                        foreach (Client client in ConnectedClients)
                        {
                            if (client == null)
                            {
                                continue;
                            }

                            // Collision check with other players.
                            if (client.Player.Position == new Vector2(x, y))
                            {
                                success = 0;
                            }
                        }

                        // Collision check with world.
                        if (!(x >= World.Width || y >= World.Height))
                        {
                            if (World.CharTiles[x + y * World.Width].DrawChar != ' ')
                            {
                                success = 0;
                            }
                        }
                        else
                        {
                            success = 0;
                        }
                    }
                    else
                    {
                        success = 0;
                    }

                    if (success == 1)
                    {
                        ConnectedClients[id].Player.Position = new Vector2(x, y);
                        NetHelper.SendMessageTo(tcpClient, "1");
                    }
                    else
                    {
                        NetHelper.SendMessageTo(tcpClient, "0");
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerChar:
                {
                    int  id      = int.Parse(messages[1]);
                    char charSet = (char)int.Parse(messages[2]);

                    if (charSet != Headers.SplitChar)
                    {
                        ConnectedClients[id].Player.DrawChar = charSet;

                        NetHelper.SendMessageTo(tcpClient, "1");
                    }
                    else
                    {
                        NetHelper.SendMessageTo(tcpClient, "0");
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerColor:
                {
                    int          id    = int.Parse(messages[1]);
                    ConsoleColor color = (ConsoleColor)int.Parse(messages[2]);

                    ConnectedClients[id].Player.Drawable.Color = color;

                    NetHelper.SendMessageTo(tcpClient, "1");

                    break;
                }

                case (int)Headers.DataType.SetPlayerName:
                {
                    int    id   = int.Parse(messages[1]);
                    string name = messages[2];

                    ConnectedClients[id].Player.Name = name;

                    NetHelper.SendMessageTo(tcpClient, "1");

                    Console.WriteLine("Set name to: " + name);

                    break;
                }

                case (int)Headers.DataType.SendChatMessage:
                {
                    int          id    = int.Parse(messages[1]);
                    string       msg   = messages[2];
                    ConsoleColor color = (ConsoleColor)int.Parse(messages[3]);

                    if (msg.Contains(Headers.SplitChar))
                    {
                        continue;
                    }

                    foreach (Client _c in ConnectedClients)
                    {
                        if (_c == null)
                        {
                            continue;
                        }

                        NetHelper.SendMessageTo(_c.TcpClient,
                                                NetHelper.BuildRequestString((int)Headers.DataType.SendChatMessage, _c.Id) + msg +
                                                Headers.SplitChar + (int)color);
                    }

                    break;
                }

                default:
                {
                    Console.WriteLine("Unknown data type recieved: " + dataType + ".");
                    break;
                }
                }
            }
        }
Ejemplo n.º 3
0
        private static void StartListening()
        {
            _listenThread = new Thread(delegate()
            {
                while (Listening)
                {
                    TcpClient connection = _listener.AcceptTcpClient();

                    string message;
                    while (true)
                    {
                        message = NetHelper.ReceiveMessageFrom(connection);

                        if (message != string.Empty)
                        {
                            break;
                        }
                    }

                    if (message.Contains("User-Agent"))                                                                             // Is a browser, send back a message.
                    {
                        string send = "<body bgcolor=\"#808080\"><div style=\"margin-left:auto; margin-right:auto;\"><h1 style=\"text-align:center;\">Nope.</h1></div></body>";

                        connection.GetStream().Write(Encoding.Unicode.GetBytes(send), 0, Encoding.ASCII.GetBytes(send).Length);

                        connection.GetStream().Close();
                        connection.Close();
                        continue;
                    }

                    if (message == "1")                                                                             // Is server, don't add to client list.
                    {
                        continue;
                    }
                    else
                    {
                        NetHelper.SendMessageTo(connection, "OK");
                    }

                    if (Server.ConnectedClients == null)
                    {
                        continue;
                    }

                    int slot = 0;
                    for (int i = 0; i < Server.ConnectedClients.Length; i++)
                    {
                        Client c = Server.ConnectedClients[i];
                        if (c == null)
                        {
                            slot = i;
                            break;
                        }
                    }

                    Server.ConnectedClients[slot] = new Client(connection, slot, slot == 0);

                    Console.WriteLine("Client connected from " + connection.Client.RemoteEndPoint + ".");
                    Console.WriteLine("Total clients connected: " + Server.GetConnectedClientsCount() + "/" +
                                      Server.ConnectedClients.Length);
                }
            });

            _listenThread.Start();
        }