fix network

This commit is contained in:
Ivan Filipenkov 2023-08-18 17:45:17 +03:00
parent 12e3101988
commit fa399951a8
5 changed files with 64 additions and 94 deletions

View file

@ -135,6 +135,7 @@ namespace DangerousD.GameCore.GUI
};
Elements.Add(joinByIpButton);
joinByIpButton.LeftButtonPressed += () => {
AppManager.Instance.ChangeGameState(GameState.Game);
AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text);
};
}

View file

@ -23,7 +23,7 @@ namespace DangerousD.GameCore
public class AppManager : Game
{
public static AppManager Instance { get; private set; }
public string IpAddress { get; private set; } = "127.0.0.1";
public string IpAddress { get; private set; } = "0.0.0.0";
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public GameState gameState { get; private set; }
@ -220,6 +220,7 @@ namespace DangerousD.GameCore
public void NetworkSync(List<NetworkTask> networkTasks)
{
DebugHUD.Log("networksync");
foreach (NetworkTask networkTask in networkTasks)
{
switch (networkTask.operation)

View file

@ -30,6 +30,9 @@ namespace DangerousD.GameCore
public List<GameObject> otherObjects = new();
public Vector4 CameraBorder;
public Player GetPlayer1 { get; private set; }
private int _lastUpdate = 0;
private int _currTime = 0;
public GameManager()
{
others = new List<GameObject>();
@ -130,10 +133,16 @@ namespace DangerousD.GameCore
public void Update(GameTime gameTime)
{
_currTime += gameTime.ElapsedGameTime.Milliseconds;
if (AppManager.Instance.NetworkTasks.Count > 0)
{
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
AppManager.Instance.NetworkTasks.Clear();
if (_currTime - _lastUpdate > 50)
{
AppManager.Instance.DebugHUD.Log("sending");
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
AppManager.Instance.NetworkTasks.Clear();
_lastUpdate = _currTime;
}
}
foreach (var item in BackgroundObjects)
item.Update(gameTime);

View file

@ -21,27 +21,19 @@ namespace DangerousD.GameCore.Network
private void Init(string IpAddress)
{
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(IpAddress);
int port = 51873;
endPoint = new IPEndPoint(address, port);
}
catch { }
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(IpAddress);
int port = 51873;
endPoint = new IPEndPoint(address, port);
}
private void AcceptSockets()
{
while (true)
{
try
{
Socket clientSocket = socket.Accept();
clientSockets.Add(clientSocket);
Thread receiveThread = new Thread(BeginHostReceive);
receiveThread.Start(clientSocket);
}
catch { }
Socket clientSocket = socket.Accept();
clientSockets.Add(clientSocket);
Thread receiveThread = new Thread(BeginHostReceive);
receiveThread.Start(clientSocket);
}
}
@ -50,46 +42,41 @@ namespace DangerousD.GameCore.Network
Socket clientSocket = clSocket as Socket;
while (clientSocket != null)
{
try
byte[] bytesCount = new byte[4];
clientSocket.Receive(bytesCount);
int length = BitConverter.ToInt32(bytesCount);
byte[] Data = new byte[length];
StateObject so = new StateObject(clientSocket, Data);
while (so.UploadedBytesCount < length)
{
byte[] bytesCount = new byte[4];
clientSocket.Receive(bytesCount);
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)];
StateObject so = new StateObject(clientSocket, Data);
IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
int count = clientSocket.Receive(so.buffer, so.UploadedBytesCount, length - so.UploadedBytesCount, SocketFlags.None);
so.UploadedBytesCount += count;
}
catch { }
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(Encoding.Unicode.GetString(so.buffer, 0, length));
GetReceivingMessages(tasks);
}
}
public void HostInit(string IpAddress)
{
try
{
Init(IpAddress);
socket.Bind(endPoint);
socket.Listen(4);
Thread acceptThread = new Thread(AcceptSockets);
acceptThread.Start();
state = "Host";
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
}
catch { }
Init(IpAddress);
socket.Bind(endPoint);
socket.Listen(4);
Thread acceptThread = new Thread(AcceptSockets);
acceptThread.Start();
state = "Host";
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
}
public void ClientInit(string IpAddress)
{
try
{
Init(IpAddress);
socket.Connect(endPoint);
state = "Client";
Thread.Sleep(10);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start();
NetworkTask connectionTask = new NetworkTask("Player");
AppManager.Instance.NetworkTasks.Add(connectionTask);
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
}
catch { }
Init(IpAddress);
socket.Connect(endPoint);
state = "Client";
Thread.Sleep(10);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start();
NetworkTask connectionTask = new NetworkTask("Player");
AppManager.Instance.NetworkTasks.Add(connectionTask);
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
}
public void SendMsg(List<NetworkTask> networkTask, Socket ignoreSocket = null)
{
@ -97,65 +84,38 @@ namespace DangerousD.GameCore.Network
int count = Data.Length;
if (state == "Host")
{
try
foreach (Socket socket in clientSockets)
{
foreach (Socket socket in clientSockets)
if (!(socket == ignoreSocket))
{
if (!(socket == ignoreSocket))
{
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
}
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
}
}
catch { }
}
else
{
try
{
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
}
catch { }
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
}
}
private void ReceiveMsgFromHost()
{
while (true)
{
try
byte[] bytesCount = new byte[4];
socket.Receive(bytesCount);
int length = BitConverter.ToInt32(bytesCount);
byte[] Data = new byte[length];
StateObject so = new StateObject(socket, Data);
while (so.UploadedBytesCount < length)
{
byte[] bytesCount = new byte[4];
socket.Receive(bytesCount);
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)];
StateObject so = new StateObject(socket, Data);
IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
int count = socket.Receive(so.buffer, so.UploadedBytesCount, length-so.UploadedBytesCount, SocketFlags.None);
so.UploadedBytesCount += count;
}
catch { }
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(Encoding.Unicode.GetString(so.buffer, 0, length));
GetReceivingMessages(tasks);
}
}
private void AsyncReceiveCallback(IAsyncResult ar)
{
try
{
StateObject so = ar.AsyncState as StateObject;
Socket clientSocket = so.workSocket;
int readCount = clientSocket.EndReceive(ar);
so.UploadedBytesCount += readCount;
so.sb.Append(Encoding.Unicode.GetString(so.buffer, 0, readCount));
if (so.UploadedBytesCount < so.bufferSize)
{
clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, new AsyncCallback(AsyncReceiveCallback), so);
}
else
{
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(so.sb.ToString());
GetReceivingMessages(tasks);
}
}
catch { }
}
}
}

View file

@ -12,7 +12,6 @@ namespace DangerousD.GameCore.Network
public Socket workSocket;
public int bufferSize;
public byte[] buffer;
public StringBuilder sb = new StringBuilder();
public int UploadedBytesCount;
public StateObject(Socket socket, byte[] buffer)