fix network
This commit is contained in:
parent
12e3101988
commit
fa399951a8
5 changed files with 64 additions and 94 deletions
|
@ -135,6 +135,7 @@ namespace DangerousD.GameCore.GUI
|
|||
};
|
||||
Elements.Add(joinByIpButton);
|
||||
joinByIpButton.LeftButtonPressed += () => {
|
||||
AppManager.Instance.ChangeGameState(GameState.Game);
|
||||
AppManager.Instance.NetworkManager.ClientInit(searchBarTextBox.text);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
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);
|
||||
|
|
|
@ -20,28 +20,20 @@ namespace DangerousD.GameCore.Network
|
|||
string state;
|
||||
|
||||
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 { }
|
||||
}
|
||||
private void AcceptSockets()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
Socket clientSocket = socket.Accept();
|
||||
clientSockets.Add(clientSocket);
|
||||
Thread receiveThread = new Thread(BeginHostReceive);
|
||||
receiveThread.Start(clientSocket);
|
||||
}
|
||||
catch { }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -49,21 +41,22 @@ namespace DangerousD.GameCore.Network
|
|||
{
|
||||
Socket clientSocket = clSocket as Socket;
|
||||
while (clientSocket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytesCount = new byte[4];
|
||||
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);
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
Init(IpAddress);
|
||||
socket.Bind(endPoint);
|
||||
|
@ -73,11 +66,7 @@ namespace DangerousD.GameCore.Network
|
|||
state = "Host";
|
||||
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Host);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
public void ClientInit(string IpAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
Init(IpAddress);
|
||||
socket.Connect(endPoint);
|
||||
|
@ -89,15 +78,11 @@ namespace DangerousD.GameCore.Network
|
|||
AppManager.Instance.NetworkTasks.Add(connectionTask);
|
||||
AppManager.Instance.SetMultiplayerState(MultiPlayerStatus.Client);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
public void SendMsg(List<NetworkTask> networkTask, Socket ignoreSocket = null)
|
||||
{
|
||||
byte[] Data = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(networkTask));
|
||||
int count = Data.Length;
|
||||
if (state == "Host")
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (Socket socket in clientSockets)
|
||||
{
|
||||
|
@ -108,54 +93,29 @@ namespace DangerousD.GameCore.Network
|
|||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
socket.Send(BitConverter.GetBytes(count));
|
||||
socket.Send(Data);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
private void ReceiveMsgFromHost()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytesCount = new byte[4];
|
||||
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);
|
||||
IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
private void AsyncReceiveCallback(IAsyncResult ar)
|
||||
while (so.UploadedBytesCount < length)
|
||||
{
|
||||
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);
|
||||
int count = socket.Receive(so.buffer, so.UploadedBytesCount, length-so.UploadedBytesCount, SocketFlags.None);
|
||||
so.UploadedBytesCount += count;
|
||||
}
|
||||
else
|
||||
{
|
||||
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(so.sb.ToString());
|
||||
List<NetworkTask> tasks = JsonConvert.DeserializeObject<List<NetworkTask>>(Encoding.Unicode.GetString(so.buffer, 0, length));
|
||||
GetReceivingMessages(tasks);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue