// 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; } } }
// Processing messages from players private void UpdateLog(GamerList gamerList) { Set printData = new Set(); Set addToDataGrid = new Set(); switch (gamerList.Type) { // Update list gamers case 0: { Data.gamerList.Clear(); Data.GamersList = gamerList; // Get your number Get getMyNumber = new Get(); getMyNumber.GetMyNamber(); // Host sending for (int i = 0; i < Data.GamersList.gamer.Count; i++) { addToDataGrid.AddToGrid(i); } // Exit the method return; } // Changing map case 1: { Data.SettingCh.St.Map = gamerList.Map; Data.LobbyLink.ShowMap(gamerList.Map); // Exit the method return; } // Changing car case 2: { Data.MainWin.Dispatcher.Invoke(new Action(delegate() { Data.GamersList.gamer[gamerList.Number].GCar = gamerList.Map; })); // Exit the method return; } // Changing team case 11: { Data.MainWin.Dispatcher.Invoke(new Action(delegate() { Data.GamersList.gamer[gamerList.Number].GTeam = gamerList.Map; })); // Exit the method return; } // Changing gamers status case 3: { Data.LobbyLink.Dispatcher.Invoke(new Action(delegate() { Data.GamersList.gamer[gamerList.Number].GStatus = gamerList.Map; if (Data.GamersList.gamer[Data.MyNamber].Status == 0) { Data.LobbyLink.UnLock(); } else { Data.LobbyLink.Lock(); } if (Data.GamersList.gamer[0].Status != 0 && Data.GamersList.gamer[Data.MyNamber].Status != 0) { Data.BtnReady.IsEnabled = false; Data.LineBtnReady.IsEnabled = false; } else { Data.BtnReady.IsEnabled = true; Data.LineBtnReady.IsEnabled = true; } })); // Exit the method return; } // Set message from gamer case 4: { Set setMassage = new Set(); setMassage.SetMassage(gamerList.Number, gamerList.Message); // Exit the method return; } // Set message abot already taken car case 5: { Set MassageAboutCar = new Set(); MassageAboutCar.SetMassageCar(gamerList.Number); // Exit the method return; } // Changing score case 6: { Data.SettingCh.St.Score = gamerList.Score; Data.MainWin.Dispatcher.Invoke(new Action(delegate() { Data.LobbyLink.ScoreChange(gamerList.Score); Sound.PlaySound("score.mp3"); })); // Exit the method return; } // Changing UDP ports case 7: { for (int i = 0; i < Data.GamersList.gamer.Count; i++) { Data.GamersList.gamer[i].Udp = gamerList.gamer[i].Udp; } // Exit the method return; } // Start ld.exe case 8: { // Everyone has the same data Data.GamersList = gamerList; Ld startLd = new Ld(); Sound.PlaySound("go.mp3"); startLd.StartLDThread(); return; } // Add or leave lobby case 9: { // Everyone has the same data Set massageAddLeave = new Set(); if (gamerList.Message == "add") { massageAddLeave.SetMassageAddLeave(true, gamerList.Number); } else { massageAddLeave.SetMassageAddLeave(false, gamerList.Number); } return; } // Close connection case 10: { CloseConnection("Connection error", "The host terminated your connection"); // Exit the method return; } case 12: { CloseConnection("Connection error", "The lobby is closed"); // Exit the method return; } // Reboot UDP modul case 13: { // Turn off UDP Data.UdpWorkSend = false; Data.UdpWorkRec = false; if (Udp.receiver_server != null) { Udp.receiver_server.Close(); } // Turn on UDP module Udp UdpOn = new Udp(); UdpOn.StartUDPModul0neSecThread(); // Exit the method return; } default: { return; } } }
// 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(); }