Merge branch 'main' into livingEntitiesVlad

This commit is contained in:
N4K 2023-08-17 13:15:59 +03:00
commit d7312c4c62
3 changed files with 90 additions and 52 deletions

View file

@ -22,7 +22,7 @@ namespace DangerousD.GameCore
private GraphicsDeviceManager _graphics; private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch; private SpriteBatch _spriteBatch;
public GameState gameState { get; private set; } public GameState gameState { get; private set; }
public MultiPlayerStatus multiPlayerStatus { get; private set; } public MultiPlayerStatus multiPlayerStatus { get; private set; } = MultiPlayerStatus.SinglePlayer;
IDrawableObject MenuGUI; IDrawableObject MenuGUI;
IDrawableObject OptionsGUI; IDrawableObject OptionsGUI;
IDrawableObject LoginGUI; IDrawableObject LoginGUI;
@ -176,6 +176,8 @@ namespace DangerousD.GameCore
public void NetworkSync(NetworkTask networkTask) public void NetworkSync(NetworkTask networkTask)
{ {
//TODO
return;
switch (networkTask.operation) switch (networkTask.operation)
{ {
case NetworkTaskOperationEnum.TakeDamage: case NetworkTaskOperationEnum.TakeDamage:

View file

@ -33,7 +33,7 @@ namespace DangerousD.GameCore
sound.SoundEffect.IsLooped = false; sound.SoundEffect.IsLooped = false;
sound.SoundEffect.Play(); sound.SoundEffect.Play();
PlayingSounds.Add(sound); PlayingSounds.Add(sound);
if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.Host) if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Host)
{ {
AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName)); AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(Vector2.Zero, soundName));
} }

View file

@ -20,21 +20,28 @@ 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 = 8000; int port = 8000;
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);
Console.WriteLine("Connected"); }
catch { }
} }
} }
@ -42,6 +49,8 @@ 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);
@ -49,8 +58,12 @@ namespace DangerousD.GameCore.Network
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); IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
} }
catch { }
}
} }
public void HostInit(string IpAddress) public void HostInit(string IpAddress)
{
try
{ {
Init(IpAddress); Init(IpAddress);
socket.Bind(endPoint); socket.Bind(endPoint);
@ -58,9 +71,12 @@ namespace DangerousD.GameCore.Network
Thread acceptThread = new Thread(AcceptSockets); Thread acceptThread = new Thread(AcceptSockets);
acceptThread.Start(); acceptThread.Start();
state = "Host"; state = "Host";
Console.WriteLine("Start Accept"); }
catch { }
} }
public void ClientInit(string IpAddress) public void ClientInit(string IpAddress)
{
try
{ {
Init(IpAddress); Init(IpAddress);
socket.Connect(endPoint); socket.Connect(endPoint);
@ -69,11 +85,17 @@ namespace DangerousD.GameCore.Network
Thread ReceivingThread = new Thread(ReceiveMsgFromHost); Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start(); ReceivingThread.Start();
} }
catch { }
}
public void SendMsg(NetworkTask networkTask) public void SendMsg(NetworkTask networkTask)
{ {
//TODO
return;
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)
{ {
@ -81,15 +103,23 @@ namespace DangerousD.GameCore.Network
socket.Send(Data); socket.Send(Data);
} }
} }
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);
@ -97,9 +127,13 @@ namespace DangerousD.GameCore.Network
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); IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
} }
catch { }
}
} }
private void AsyncReceiveCallback(IAsyncResult ar) private void AsyncReceiveCallback(IAsyncResult ar)
{
try
{ {
StateObject so = ar.AsyncState as StateObject; StateObject so = ar.AsyncState as StateObject;
Socket clientSocket = so.workSocket; Socket clientSocket = so.workSocket;
@ -115,5 +149,7 @@ namespace DangerousD.GameCore.Network
GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString())); GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString()));
} }
} }
catch { }
}
} }
} }