From a644d6c02ab7d70ea7ee49e56cba5956de9ed39c Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 01:18:46 +0300 Subject: [PATCH 1/3] WorkServerWithGUI --- ZoFo/GameCore/Client.cs | 6 ++-- ZoFo/GameCore/GUI/SelectModeMenu.cs | 13 +-------- ZoFo/GameCore/GUI/SelectingServerGUI.cs | 9 ++++-- ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs | 11 ++++---- .../NetworkManager/ClientNetworkManager.cs | 6 ++-- .../NetworkManager/ServerNetworkManager.cs | 28 ++++++++++++++----- ZoFo/GameCore/Server.cs | 5 ++-- 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 94e27a0..3a3216e 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -43,9 +43,9 @@ namespace ZoFo.GameCore } public void GameEndedUnexpectedly() { } - public void JoinRoom(string ip) + public void JoinRoom(string ip,int port) { - networkManager.JoinRoom(ip); + networkManager.JoinRoom(ip,port); } public void JoinYourself() { networkManager.JoinYourself(); } @@ -66,7 +66,7 @@ namespace ZoFo.GameCore } } - internal void GotData(IUpdateData update) + internal void GotData(UpdateData update) { if (update is UpdateTileCreated) { diff --git a/ZoFo/GameCore/GUI/SelectModeMenu.cs b/ZoFo/GameCore/GUI/SelectModeMenu.cs index 2ddf7f4..6ae2edc 100644 --- a/ZoFo/GameCore/GUI/SelectModeMenu.cs +++ b/ZoFo/GameCore/GUI/SelectModeMenu.cs @@ -41,12 +41,8 @@ public class SelectModeMenu : AbstractGUI // single Server server = new Server(); Client client = new Client(); -<<<<<<< HEAD server.CreateRoom(1); client.JoinYourself(); - -======= ->>>>>>> e9698a8669e52b8738987a7a6ccb79c6726f91ac AppManager.Instance.SetServer(server); AppManager.Instance.SetClient(client); AppManager.Instance.ChangeState(GameState.HostPlaying); @@ -74,14 +70,7 @@ public class SelectModeMenu : AbstractGUI { AppManager.Instance.SetGUI(new SelectingServerGUI()); // multi - Server server = new Server(); //Server Logic MultiPlayer - Client client = new Client(); - server.CreateRoom(5); - client.JoinRoom("127.0.0.1"); //указать айпишник - AppManager.Instance.SetServer(server); - AppManager.Instance.SetClient(client); - string key = client.IsConnected.ToString(); - AppManager.Instance.debugHud.Set(key, "MultiPlayer"); + // ваш код здесь }; Elements.Add(optionButton); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index a108a29..ad36b4f 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -79,10 +79,15 @@ public class SelectingServerGUI : AbstractGUI }; hostButton.LeftButtonPressed += () => { - AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); - // host + // host + Server server = new Server(); //Server Logic MultiPlayer + server.CreateRoom(5); + AppManager.Instance.SetServer(server); + string key = server.MyIp; + AppManager.Instance.debugHud.Set(key, "MultiPlayer"); // ваш код здесь + AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); }; Elements.Add(hostButton); diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 5c9250d..4c5c6b5 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -17,6 +17,7 @@ public class WaitingForPlayersGUI : AbstractGUI { private DrawableUIElement menuBackground; private bool isHost; + private Label ip; public WaitingForPlayersGUI(bool isHost) { @@ -26,13 +27,13 @@ public class WaitingForPlayersGUI : AbstractGUI { int width = AppManager.Instance.CurentScreenResolution.X; int height = AppManager.Instance.CurentScreenResolution.Y; - + menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures\\GUI\\background\\Waiting" }; Elements.Add(menuBackground); menuBackground.LoadTexture(AppManager.Instance.Content); - - Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Waiting", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font"}); - + // string pcIp = + ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp, fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; + Elements.Add(ip); if (isHost) { Button startButton = new Button(Manager) @@ -47,7 +48,7 @@ public class WaitingForPlayersGUI : AbstractGUI startButton.LeftButtonPressed += () => { // start - + // ваш код здесь }; Elements.Add(startButton); diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index c178154..12ad84d 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -15,7 +15,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { public class ClientNetworkManager { - private int port = 7632; + private int port = 0; private EndPoint endPoint; private Socket socket; List updates = new List(); @@ -60,13 +60,14 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// /// - public void JoinRoom(string ip) // multyplayer + public void JoinRoom(string ip, int port) // multyplayer { endPoint = new IPEndPoint(IPAddress.Parse(ip), port); socket.Connect(endPoint); SendData(); Thread listen = new Thread(StartListening); + listen.IsBackground = true; listen.Start(); } @@ -79,6 +80,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager socket.Connect(endPoint); SendData(); Thread listen = new Thread(StartListening); + listen.IsBackground = true; listen.Start(); } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 0686800..dbe149d 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -16,16 +16,17 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { public class ServerNetworkManager { - private IPAddress ip =IPAddress.Parse("127.0.0.1"); //IPAddress.Any - private int port = 7632; + private IPAddress ip = IPAddress.Parse("127.0.0.1"); + private const int port = 0; private IPEndPoint endPoint; private Socket socket; private List clients; - public List updates; + public List updates; public delegate void OnDataSend(string data); public event OnDataSend GetDataSend; // event Dictionary managerThread; Thread serverTheread; + public IPEndPoint InfoConnect => endPoint; public ServerNetworkManager() { Init(); } @@ -34,6 +35,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// private void Init() { + ip = GetIp(); endPoint = new IPEndPoint(ip, port); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); managerThread = new Dictionary(); @@ -43,10 +45,21 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager socket.Bind(endPoint); } + /// + /// Получает IP устройства + /// + /// + public static IPAddress GetIp() + { + string hostName = Dns.GetHostName(); // Retrive the Name of HOST + string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP + return IPAddress.Parse(myIP); + } + /// /// отправляет клиенту Data /// - public void SendData() + public void SendData() { for (int i = 0; i < updates.Count; i++) { @@ -76,7 +89,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// По сути конец игры и отключение игроков /// - public void CloseConnection() + public void CloseConnection() { foreach (var item in clients) { @@ -104,11 +117,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void Start(object players) { serverTheread = new Thread(StartWaitingForPlayers); + serverTheread.IsBackground = true; serverTheread.Start(players); } //Потоки Клиентов - /// /// Слушает игроков, которые хотят подключиться /// @@ -116,12 +129,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void StartWaitingForPlayers(object players) { int playNumber = (int)players; - + socket.Listen(playNumber); for (int i = 0; i < playNumber; i++) { Socket client = socket.Accept(); Thread thread = new Thread(StartListening); + thread.IsBackground = true; thread.Start(client); managerThread.Add(client, thread); clients.Add(client); //добавляем клиентов в лист diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 72329d9..edc1022 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -19,6 +19,7 @@ namespace ZoFo.GameCore { private ServerNetworkManager networkManager; private int ticks = 0; + public string MyIp { get { return networkManager.InfoConnect.ToString(); } } public Server() { networkManager = new ServerNetworkManager(); @@ -29,7 +30,7 @@ namespace ZoFo.GameCore //TODO Comment pls public void OnDataSend(string data) { - List updateDatas = JsonSerializer.Deserialize>(data); + List updateDatas = JsonSerializer.Deserialize>(data); for (int i = 0; i < updateDatas.Count; i++) { ProcessIUpdateData(updateDatas[i]); @@ -39,7 +40,7 @@ namespace ZoFo.GameCore /// Обработка апдейтсов, которые нам прислал клиент /// /// - public void ProcessIUpdateData(IUpdateData updateData) + public void ProcessIUpdateData(UpdateData updateData) { //ТУТ Switch case будет честное слово From b1f24ce81bd99875cb60e3e7b27e784160632453 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 01:43:50 +0300 Subject: [PATCH 2/3] WorkServerWithGUI2 --- ZoFo/GameCore/Client.cs | 2 +- ZoFo/GameCore/GUI/SelectModeMenu.cs | 2 +- ZoFo/GameCore/GUI/SelectingServerGUI.cs | 2 +- ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs | 2 +- .../NetworkManager/ClientNetworkManager.cs | 14 +++++++++++--- .../NetworkManager/ServerNetworkManager.cs | 2 +- ZoFo/GameCore/Server.cs | 3 ++- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 3a3216e..ed77e89 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -47,7 +47,7 @@ namespace ZoFo.GameCore { networkManager.JoinRoom(ip,port); } - public void JoinYourself() { networkManager.JoinYourself(); } + public void JoinYourself(int port) { networkManager.JoinYourself(port); } List mapObjects = new List(); diff --git a/ZoFo/GameCore/GUI/SelectModeMenu.cs b/ZoFo/GameCore/GUI/SelectModeMenu.cs index 6ae2edc..94a9500 100644 --- a/ZoFo/GameCore/GUI/SelectModeMenu.cs +++ b/ZoFo/GameCore/GUI/SelectModeMenu.cs @@ -42,7 +42,7 @@ public class SelectModeMenu : AbstractGUI Server server = new Server(); Client client = new Client(); server.CreateRoom(1); - client.JoinYourself(); + client.JoinYourself(server.MyIp.Port); AppManager.Instance.SetServer(server); AppManager.Instance.SetClient(client); AppManager.Instance.ChangeState(GameState.HostPlaying); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index ad36b4f..1827f03 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -84,7 +84,7 @@ public class SelectingServerGUI : AbstractGUI Server server = new Server(); //Server Logic MultiPlayer server.CreateRoom(5); AppManager.Instance.SetServer(server); - string key = server.MyIp; + string key = server.MyIp.ToString(); AppManager.Instance.debugHud.Set(key, "MultiPlayer"); // ваш код здесь AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 4c5c6b5..b0a808b 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -32,7 +32,7 @@ public class WaitingForPlayersGUI : AbstractGUI Elements.Add(menuBackground); menuBackground.LoadTexture(AppManager.Instance.Content); // string pcIp = - ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp, fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; + ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; Elements.Add(ip); if (isHost) { diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index 12ad84d..b55d8cb 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -16,12 +16,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public class ClientNetworkManager { private int port = 0; - private EndPoint endPoint; + private IPEndPoint endPoint; private Socket socket; List updates = new List(); public delegate void OnDataSent(string Data); public event OnDataSent GetDataSent; // event public bool IsConnected { get { return socket.Connected; } } + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ClientNetworkManager() { @@ -74,9 +75,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// создается одиночная комната к которой ты подключаешься /// - public void JoinYourself() // single player + public void JoinYourself(int port) // single player { - endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); + endPoint = new IPEndPoint(GetIp(), port); socket.Connect(endPoint); SendData(); Thread listen = new Thread(StartListening); @@ -84,6 +85,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager listen.Start(); } + public static IPAddress GetIp() + { + string hostName = Dns.GetHostName(); // Retrive the Name of HOST + string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP + return IPAddress.Parse(myIP); + } + //поток 2 public void StartListening() { diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index dbe149d..9b6259e 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -26,7 +26,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public event OnDataSend GetDataSend; // event Dictionary managerThread; Thread serverTheread; - public IPEndPoint InfoConnect => endPoint; + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ServerNetworkManager() { Init(); } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index edc1022..4b05b1d 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Text.Json; using System.Threading.Tasks; @@ -19,7 +20,7 @@ namespace ZoFo.GameCore { private ServerNetworkManager networkManager; private int ticks = 0; - public string MyIp { get { return networkManager.InfoConnect.ToString(); } } + public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } public Server() { networkManager = new ServerNetworkManager(); From f6663bdd3c90065aaa38b7e8461587f419b9b4b4 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 10:50:05 +0300 Subject: [PATCH 3/3] StartCreateLogicMultiPlayer --- ZoFo/GameCore/GUI/SelectingServerGUI.cs | 15 ++++++++++++--- .../NetworkManager/ClientNetworkManager.cs | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index 1827f03..531f9db 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Net; using System.Xml; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; @@ -62,10 +63,18 @@ public class SelectingServerGUI : AbstractGUI }; joinButton.LeftButtonPressed += () => { - AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + // join - - // ваш код здесь + Client client = new Client(); + var endpoint = ipBox.text.Split(':'); + int port; + if (int.TryParse(endpoint[1], out port)) + { + client.JoinRoom(endpoint[0], port); + AppManager.Instance.SetClient(client); + AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + } + // ваш код здесь }; Elements.Add(joinButton); Button hostButton = new Button(Manager) diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index b55d8cb..2a93f71 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -71,6 +71,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager listen.IsBackground = true; listen.Start(); } + public void JoinRoom(IPEndPoint endPoint) // multyplayer + { + + this.endPoint = endPoint; + socket.Connect(endPoint); + SendData(); + Thread listen = new Thread(StartListening); + listen.IsBackground = true; + listen.Start(); + } /// /// создается одиночная комната к которой ты подключаешься