WorkServerWithGUI
This commit is contained in:
parent
ceb10ce7c4
commit
a644d6c02a
7 changed files with 45 additions and 33 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ public class WaitingForPlayersGUI : AbstractGUI
|
|||
{
|
||||
private DrawableUIElement menuBackground;
|
||||
private bool isHost;
|
||||
private Label ip;
|
||||
|
||||
public WaitingForPlayersGUI(bool isHost)
|
||||
{
|
||||
|
@ -30,9 +31,9 @@ public class WaitingForPlayersGUI : AbstractGUI
|
|||
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)
|
||||
|
|
|
@ -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<UpdateData> updates = new List<UpdateData>();
|
||||
|
@ -60,13 +60,14 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="port"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Socket> clients;
|
||||
public List<IUpdateData> updates;
|
||||
public List<UpdateData> updates;
|
||||
public delegate void OnDataSend(string data);
|
||||
public event OnDataSend GetDataSend; // event
|
||||
Dictionary<Socket, Thread> managerThread;
|
||||
Thread serverTheread;
|
||||
public IPEndPoint InfoConnect => endPoint;
|
||||
|
||||
public ServerNetworkManager() { Init(); }
|
||||
|
||||
|
@ -34,6 +35,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
/// </summary>
|
||||
private void Init()
|
||||
{
|
||||
ip = GetIp();
|
||||
endPoint = new IPEndPoint(ip, port);
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
managerThread = new Dictionary<Socket, Thread>();
|
||||
|
@ -43,6 +45,17 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
socket.Bind(endPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает IP устройства
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// отправляет клиенту Data
|
||||
/// </summary>
|
||||
|
@ -104,11 +117,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
public void Start(object players)
|
||||
{
|
||||
serverTheread = new Thread(StartWaitingForPlayers);
|
||||
serverTheread.IsBackground = true;
|
||||
serverTheread.Start(players);
|
||||
}
|
||||
|
||||
//Потоки Клиентов
|
||||
|
||||
/// <summary>
|
||||
/// Слушает игроков, которые хотят подключиться
|
||||
/// </summary>
|
||||
|
@ -122,6 +135,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
{
|
||||
Socket client = socket.Accept();
|
||||
Thread thread = new Thread(StartListening);
|
||||
thread.IsBackground = true;
|
||||
thread.Start(client);
|
||||
managerThread.Add(client, thread);
|
||||
clients.Add(client); //добавляем клиентов в лист
|
||||
|
|
|
@ -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<IUpdateData> updateDatas = JsonSerializer.Deserialize<List<IUpdateData>>(data);
|
||||
List<UpdateData> updateDatas = JsonSerializer.Deserialize<List<UpdateData>>(data);
|
||||
for (int i = 0; i < updateDatas.Count; i++)
|
||||
{
|
||||
ProcessIUpdateData(updateDatas[i]);
|
||||
|
@ -39,7 +40,7 @@ namespace ZoFo.GameCore
|
|||
/// Обработка апдейтсов, которые нам прислал клиент
|
||||
/// </summary>
|
||||
/// <param name="updateData"></param>
|
||||
public void ProcessIUpdateData(IUpdateData updateData)
|
||||
public void ProcessIUpdateData(UpdateData updateData)
|
||||
{
|
||||
|
||||
//ТУТ Switch case будет честное слово
|
||||
|
|
Loading…
Add table
Reference in a new issue