Esempio n. 1
0
        // For receiving mssages
        private void ReceiveMessages()

        {
            int bufferSize = 8000;
            Set printData  = new Set();
            Set setGamer   = new Set();

            // Create a buffer for receiving
            byte[] bytes = new byte[bufferSize];

            _tcpStream = _tcpServer.GetStream();

            try
            {
                int bytesRead = _tcpStream.Read(bytes, 0, bytes.Length);
            }
            catch { }

            GamerList gamerTempList = new GamerList();

            gamerTempList = Json.ReadHost(bytes);

            if (gamerTempList == null)
            {
                MainWin.Message("Connection error", "Connection error");
                // Exit
                return;
            }

            switch (gamerTempList.Type)
            {
            // Error in nikename
            case -4:
            {
                CloseConnection("Connection error", "Invalid characters in name");
                return;
            }

            // Closing room
            case -3:
            {
                CloseConnection("Connection error", "Lobby closed");
                return;
            }

            // Wrong password
            case -2:
            {
                CloseConnection("Connection error", "Wrong password");
                return;
            }

            // Name is already used
            case -1:
            {
                CloseConnection("Connection error", "Your name is already used");
                return;
            }

            // If there are no errors and the request is accepted
            default:
            {
                Data.MainWin.Dispatcher.Invoke(new Action(delegate()
                    {
                        // Create lobby window
                        Data.MainWin.CreateLobby();
                    }));

                // Accept the list of all players
                Data.GamersList = gamerTempList;

                printData.SetMassageInfo("You are logged in as " + Data.SettingCh.St.Name + "\r\n");

                // Display the list in the table
                for (int i = 0; i < Data.GamersList.gamer.Count; i++)
                {
                    setGamer.AddToGrid(i);
                }

                // Get your number
                Get getMyNumber = new Get();
                getMyNumber.GetMyNamber();

                Data.LobbyLink.Dispatcher.Invoke(new Action(delegate()
                    {
                        Get getMap = new Get();

                        // Set map
                        Data.LobbyLink.ShowMap(Data.GamersList.Map);

                        // Set score
                        Data.LobbyLink.ScoreChange(Data.GamersList.Score);

                        // Set header
                        if (Data.JoinToServer)
                        {
                            string name;
                            if (Data.webList[Data.LobbyNumber].Name.Length > 16)
                            {
                                name = Data.webList[Data.LobbyNumber].Name.Substring(0, 16) + "...";
                            }
                            else
                            {
                                name = Data.webList[Data.LobbyNumber].Name;
                            }

                            Data.LobbyLink.Title = name + " [ " + Data.GamersList.ModOrig + " | " + Data.GamersList.LobbyType + " ]";
                        }
                        else
                        {
                            Data.LobbyLink.Title = Data.GamersList.gamer[0].Name + "'s Lobby [ " + Data.GamersList.ModOrig + " | " + Data.GamersList.LobbyType + " ]";
                        }
                    }));

                // Turn on UDP module
                Udp UdpOn = new Udp();
                UdpOn.StartUDPModul();

                srReceiver = new System.IO.StreamReader(_tcpStream);

                // While Connected == true accept messages
                while (Connected)
                {
                    try
                    {
                        int    byteRead;
                        byte[] bytesHost = new byte[bufferSize];

                        byteRead = _tcpStream.Read(bytesHost, 0, bufferSize);

                        if (byteRead != 0)
                        {
                            GamerList gamerList = new GamerList();
                            gamerList = Json.ReadHost(bytesHost);

                            Data.LobbyLink.Dispatcher.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { gamerList });
                        }

                        else
                        {
                            CloseConnection("Connection error", "The lobby is closed by host");
                            return;
                        }
                    }

                    catch { return; }
                }

                return;
            }
            }
        }
Esempio n. 2
0
        // Starts the server and starts receiving clients
        public void StartListening(string port)
        {
            // Before determining its IP, this variable is false
            getMyIp = false;
            Set message   = new Set();
            int portInt32 = 0;

            Int32.TryParse(port, out portInt32);

            // Configure sockets over TCP, use IP and open TCP port
            try
            {
                tlsClient = new TcpListener(portInt32);
            }
            catch
            {
                MainWin.Message("Connection error", "Wrong Tcp port");
            }

            // Add the first term equal to zero instead of ourselves
            tcpClients.Add(null);

            // Start to listen
            try
            {
                tlsClient.Start();

                // Create lobby window

                Data.MainWin.CreateLobby();
                Data.MyNamber = 0;

                message.SetMassageInfo("Waiting other players...\r\n");

                // Set a unique session number and will transmit it
                Data.GamersList.IdSession = DateTime.Now.ToString("dd hh:mm:ss").Replace(" ", "").Replace(".", "").Replace(":", "");

                //_____________ Connect the UDP module ___________
                Udp UdpOn = new Udp();
                UdpOn.StartUDPModul();
            }
            catch
            {
                MainWin.Message("Connection error", "Wrong Tcp port [2]");
                return;
            }

            // UDP port FROM FILE for listening from all IPs
            int portUdp = 0;

            Int32.TryParse(Data.SettingCh.St.UdpPort, out portUdp);

            IPEndPoint RemoteEndPoint = null;

            RemoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portUdp);

            Data.GamersList.gamer.Add(new Gamers()
            {
                Type = 0, Ping = "", Name = Data.SettingCh.St.Name, Number = 0, Status = 0, Car = Data.SettingCh.St.Car, Team = Data.SettingCh.St.Team, Udp = null, Message = null, Password = null,
            });
            Data.GamersList.Map   = Data.SettingCh.St.Map;
            Data.GamersList.Score = Data.SettingCh.St.Score;

            // if created on the server, then enable updates to the server
            if (Data.CreateToServer)
            {
                // Create lobby
                Web createLobby = new Web();
                // This variable is responsible for updating
                Data.FollowToLobby = true;
                // Start creating and updating the lobby in a new thread
                createLobby.CreateLobbyInThread();
            }

            Data.gamerList.Add(Data.GamersList.gamer[0]);

            // Create a new thread for the connection wait loop
            thrListener = new Thread(KeepListening);
            thrListener.Start();
        }