Merge branch 'networkFixes'
This commit is contained in:
commit
af5251ad4d
5 changed files with 64 additions and 94 deletions
|
@ -135,6 +135,7 @@ namespace DangerousD.GameCore.GUI
|
||||||
};
|
};
|
||||||
Elements.Add(joinByIpButton);
|
Elements.Add(joinByIpButton);
|
||||||
joinByIpButton.LeftButtonPressed += () => {
|
joinByIpButton.LeftButtonPressed += () => {
|
||||||
|
AppManager.Instance.ChangeGameState(GameState.Game);
|
||||||
AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text);
|
AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ namespace DangerousD.GameCore
|
||||||
public class AppManager : Game
|
public class AppManager : Game
|
||||||
{
|
{
|
||||||
public static AppManager Instance { get; private set; }
|
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 GraphicsDeviceManager _graphics;
|
||||||
private SpriteBatch _spriteBatch;
|
private SpriteBatch _spriteBatch;
|
||||||
public GameState gameState { get; private set; }
|
public GameState gameState { get; private set; }
|
||||||
|
@ -220,6 +220,7 @@ namespace DangerousD.GameCore
|
||||||
|
|
||||||
public void NetworkSync(List<NetworkTask> networkTasks)
|
public void NetworkSync(List<NetworkTask> networkTasks)
|
||||||
{
|
{
|
||||||
|
DebugHUD.Log("networksync");
|
||||||
foreach (NetworkTask networkTask in networkTasks)
|
foreach (NetworkTask networkTask in networkTasks)
|
||||||
{
|
{
|
||||||
switch (networkTask.operation)
|
switch (networkTask.operation)
|
||||||
|
|
|
@ -30,6 +30,9 @@ namespace DangerousD.GameCore
|
||||||
public List<GameObject> otherObjects = new();
|
public List<GameObject> otherObjects = new();
|
||||||
public Vector4 CameraBorder;
|
public Vector4 CameraBorder;
|
||||||
public Player GetPlayer1 { get; private set; }
|
public Player GetPlayer1 { get; private set; }
|
||||||
|
private int _lastUpdate = 0;
|
||||||
|
private int _currTime = 0;
|
||||||
|
|
||||||
public GameManager()
|
public GameManager()
|
||||||
{
|
{
|
||||||
others = new List<GameObject>();
|
others = new List<GameObject>();
|
||||||
|
@ -131,10 +134,16 @@ namespace DangerousD.GameCore
|
||||||
|
|
||||||
public void Update(GameTime gameTime)
|
public void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
|
_currTime += gameTime.ElapsedGameTime.Milliseconds;
|
||||||
if (AppManager.Instance.NetworkTasks.Count > 0)
|
if (AppManager.Instance.NetworkTasks.Count > 0)
|
||||||
{
|
{
|
||||||
|
if (_currTime - _lastUpdate > 50)
|
||||||
|
{
|
||||||
|
AppManager.Instance.DebugHUD.Log("sending");
|
||||||
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
|
AppManager.Instance.NetworkManager.SendMsg(AppManager.Instance.NetworkTasks.ToList());
|
||||||
AppManager.Instance.NetworkTasks.Clear();
|
AppManager.Instance.NetworkTasks.Clear();
|
||||||
|
_lastUpdate = _currTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
foreach (var item in BackgroundObjects)
|
foreach (var item in BackgroundObjects)
|
||||||
item.Update(gameTime);
|
item.Update(gameTime);
|
||||||
|
|
|
@ -20,28 +20,20 @@ namespace DangerousD.GameCore.Network
|
||||||
string state;
|
string state;
|
||||||
|
|
||||||
private void Init(string IpAddress)
|
private void Init(string IpAddress)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
IPAddress address = IPAddress.Parse(IpAddress);
|
IPAddress address = IPAddress.Parse(IpAddress);
|
||||||
int port = 51873;
|
int port = 51873;
|
||||||
endPoint = new IPEndPoint(address, port);
|
endPoint = new IPEndPoint(address, port);
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
private void AcceptSockets()
|
private void AcceptSockets()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Socket clientSocket = socket.Accept();
|
Socket clientSocket = socket.Accept();
|
||||||
clientSockets.Add(clientSocket);
|
clientSockets.Add(clientSocket);
|
||||||
Thread receiveThread = new Thread(BeginHostReceive);
|
Thread receiveThread = new Thread(BeginHostReceive);
|
||||||
receiveThread.Start(clientSocket);
|
receiveThread.Start(clientSocket);
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,21 +41,22 @@ namespace DangerousD.GameCore.Network
|
||||||
{
|
{
|
||||||
Socket clientSocket = clSocket as Socket;
|
Socket clientSocket = clSocket as Socket;
|
||||||
while (clientSocket != null)
|
while (clientSocket != null)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
byte[] bytesCount = new byte[4];
|
byte[] bytesCount = new byte[4];
|
||||||
clientSocket.Receive(bytesCount);
|
clientSocket.Receive(bytesCount);
|
||||||
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)];
|
int length = BitConverter.ToInt32(bytesCount);
|
||||||
|
byte[] Data = new byte[length];
|
||||||
StateObject so = new StateObject(clientSocket, Data);
|
StateObject so = new StateObject(clientSocket, Data);
|
||||||
IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
|
while (so.UploadedBytesCount < length)
|
||||||
|
{
|
||||||
|
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)
|
public void HostInit(string IpAddress)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Init(IpAddress);
|
Init(IpAddress);
|
||||||
socket.Bind(endPoint);
|
socket.Bind(endPoint);
|
||||||
|
@ -73,11 +66,7 @@ namespace DangerousD.GameCore.Network
|
||||||
state = "Host";
|
state = "Host";
|
||||||
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
|
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
public void ClientInit(string IpAddress)
|
public void ClientInit(string IpAddress)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Init(IpAddress);
|
Init(IpAddress);
|
||||||
socket.Connect(endPoint);
|
socket.Connect(endPoint);
|
||||||
|
@ -89,15 +78,11 @@ namespace DangerousD.GameCore.Network
|
||||||
AppManager.Instance.NetworkTasks.Add(connectionTask);
|
AppManager.Instance.NetworkTasks.Add(connectionTask);
|
||||||
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
|
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
public void SendMsg(List<NetworkTask> networkTask, Socket ignoreSocket = null)
|
public void SendMsg(List<NetworkTask> networkTask, Socket ignoreSocket = null)
|
||||||
{
|
{
|
||||||
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
|
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
|
||||||
int count = Data.Length;
|
int count = Data.Length;
|
||||||
if (state == "Host")
|
if (state == "Host")
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
foreach (Socket socket in clientSockets)
|
foreach (Socket socket in clientSockets)
|
||||||
{
|
{
|
||||||
|
@ -108,54 +93,29 @@ namespace DangerousD.GameCore.Network
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
socket.Send(BitConverter.GetBytes(count));
|
socket.Send(BitConverter.GetBytes(count));
|
||||||
socket.Send(Data);
|
socket.Send(Data);
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private void ReceiveMsgFromHost()
|
private void ReceiveMsgFromHost()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
byte[] bytesCount = new byte[4];
|
byte[] bytesCount = new byte[4];
|
||||||
socket.Receive(bytesCount);
|
socket.Receive(bytesCount);
|
||||||
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)];
|
int length = BitConverter.ToInt32(bytesCount);
|
||||||
|
byte[] Data = new byte[length];
|
||||||
StateObject so = new StateObject(socket, Data);
|
StateObject so = new StateObject(socket, Data);
|
||||||
IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
|
while (so.UploadedBytesCount < length)
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AsyncReceiveCallback(IAsyncResult ar)
|
|
||||||
{
|
{
|
||||||
try
|
int count = socket.Receive(so.buffer, so.UploadedBytesCount, length-so.UploadedBytesCount, SocketFlags.None);
|
||||||
{
|
so.UploadedBytesCount += count;
|
||||||
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>>(Encoding.Unicode.GetString(so.buffer, 0, length));
|
||||||
{
|
|
||||||
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(so.sb.ToString());
|
|
||||||
GetReceivingMessages(tasks);
|
GetReceivingMessages(tasks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ namespace DangerousD.GameCore.Network
|
||||||
public Socket workSocket;
|
public Socket workSocket;
|
||||||
public int bufferSize;
|
public int bufferSize;
|
||||||
public byte[] buffer;
|
public byte[] buffer;
|
||||||
public StringBuilder sb = new StringBuilder();
|
|
||||||
public int UploadedBytesCount;
|
public int UploadedBytesCount;
|
||||||
|
|
||||||
public StateObject(Socket socket, byte[] buffer)
|
public StateObject(Socket socket, byte[] buffer)
|
||||||
|
|
Loading…
Add table
Reference in a new issue