UnbindSocketFix

This commit is contained in:
AnloGames 2024-08-27 20:28:15 +03:00
parent cb6b1292ff
commit cdbbb26243
5 changed files with 55 additions and 26 deletions

View file

@ -325,7 +325,8 @@ namespace ZoFo.GameCore
}
public void GameEnd()
{
networkManager.CloseConnection();
AppManager.Instance.ChangeState(GameState.NotPlaying);
AppManager.Instance.SetGUI(new FinishingGUI());
}

View file

@ -264,15 +264,27 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
{
byte[] buffer = new byte[65535];
string data;
while (socket != null)
{
EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0);
int size = socket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref senderRemote);
byte[] correctedBuffer = new byte[size];
Array.Copy(buffer, correctedBuffer, size);
data = Encoding.UTF8.GetString(correctedBuffer);
GetDataSent(data);
int size;
try
{
while (socket != null)
{
EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0);
size = socket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref senderRemote);
byte[] correctedBuffer = new byte[size];
Array.Copy(buffer, correctedBuffer, size);
data = Encoding.UTF8.GetString(correctedBuffer);
GetDataSent(data);
}
}
catch (Exception)
{
return;
}
}
public void CloseConnection()
{
socket.Close();
}
}
}

View file

@ -204,6 +204,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
AppManager.Instance.ChangeState(GameState.HostPlaying);
AppManager.Instance.SetGUI(new HUD());////
}
public void CloseConnection()
{
//socket.Shutdown(SocketShutdown.Both);
clientsEP.Clear();
socket.Close();
}
//Потоки Клиентов
/// <summary>
@ -214,24 +220,32 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
{
byte[] buffer = new byte[65535];
string data;
while (socket != null)
int size;
try
{
EndPoint senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
int size = socket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref senderRemote);
if (AppManager.Instance.gamestate != GameState.HostPlaying && !clientsEP.Contains(senderRemote) &&
senderRemote != new IPEndPoint(IPAddress.Any, 0))
while (socket != null)
{
clientsEP.Add((IPEndPoint)senderRemote);
AppManager.Instance.debugHud.Log($"Connect {senderRemote.ToString()}");
if (!isMultiplayer) AppManager.Instance.ChangeState(GameState.HostPlaying);
// Отправлять Init апдейт с информацией об ID игрока и ID датаграмма на сервере
//Можно добавить bool isInit для Датаграммов
}
byte[] correctedBuffer = new byte[size];
Array.Copy(buffer, correctedBuffer, size);
data = Encoding.UTF8.GetString(correctedBuffer);
GetDataSend(data);
EndPoint senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
size = socket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref senderRemote);
if (AppManager.Instance.gamestate != GameState.HostPlaying && !clientsEP.Contains(senderRemote) &&
senderRemote != new IPEndPoint(IPAddress.Any, 0))
{
clientsEP.Add((IPEndPoint)senderRemote);
AppManager.Instance.debugHud.Log($"Connect {senderRemote.ToString()}");
if (!isMultiplayer) AppManager.Instance.ChangeState(GameState.HostPlaying);
// Отправлять Init апдейт с информацией об ID игрока и ID датаграмма на сервере
//Можно добавить bool isInit для Датаграммов
}
byte[] correctedBuffer = new byte[size];
Array.Copy(buffer, correctedBuffer, size);
data = Encoding.UTF8.GetString(correctedBuffer);
GetDataSend(data);
}
}
catch (Exception)
{
return;
}
}
public void AnalyzeData(string data)

View file

@ -36,6 +36,7 @@ namespace ZoFo.GameCore.GameObjects
sender.position = new Vector2(0f, 0f);
AppManager.Instance.server.EndGame();
AppManager.Instance.debugHud.Set("Exit", sender.position.ToString());
AppManager.Instance.ChangeState(GameState.NotPlaying);
}
}
}

View file

@ -157,8 +157,9 @@ namespace ZoFo.GameCore
public void EndGame()
{
UpdateGameEnded gameEnded = new UpdateGameEnded();
networkManager.AddData(gameEnded);
// networkManager.CloseConnection();
networkManager.AddData(gameEnded);
networkManager.SendData();
networkManager.CloseConnection();
}
public List<GameObject> gameObjects = new List<GameObject>();