This commit is contained in:
Lev 2023-08-17 13:20:08 +03:00
commit ba4466d966
3 changed files with 91 additions and 53 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));
} }
@ -45,7 +45,7 @@ namespace DangerousD.GameCore
sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance; sound.SoundEffect.Volume = (float)sound.GetDistance(playerPos) / MaxSoundDistance;
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(soundPos, soundName)); AppManager.Instance.NetworkManager.SendMsg(new Network.NetworkTask(soundPos, soundName));
} }

View file

@ -21,20 +21,27 @@ namespace DangerousD.GameCore.Network
private void Init(string IpAddress) private void Init(string IpAddress)
{ {
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try
IPAddress address = IPAddress.Parse(IpAddress); {
int port = 8000; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
endPoint = new IPEndPoint(address, port); IPAddress address = IPAddress.Parse(IpAddress);
int port = 8000;
endPoint = new IPEndPoint(address, port);
}
catch { }
} }
private void AcceptSockets() private void AcceptSockets()
{ {
while (true) while (true)
{ {
Socket clientSocket = socket.Accept(); try
clientSockets.Add(clientSocket); {
Thread receiveThread = new Thread(BeginHostReceive); Socket clientSocket = socket.Accept();
receiveThread.Start(clientSocket); clientSockets.Add(clientSocket);
Console.WriteLine("Connected"); Thread receiveThread = new Thread(BeginHostReceive);
receiveThread.Start(clientSocket);
}
catch { }
} }
} }
@ -43,77 +50,106 @@ namespace DangerousD.GameCore.Network
Socket clientSocket = clSocket as Socket; Socket clientSocket = clSocket as Socket;
while (clientSocket != null) while (clientSocket != null)
{ {
byte[] bytesCount = new byte[4]; try
clientSocket.Receive(bytesCount); {
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)]; byte[] bytesCount = new byte[4];
StateObject so = new StateObject(clientSocket, Data); clientSocket.Receive(bytesCount);
IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so); 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);
}
catch { }
} }
} }
public void HostInit(string IpAddress) public void HostInit(string IpAddress)
{ {
Init(IpAddress); try
socket.Bind(endPoint); {
socket.Listen(4); Init(IpAddress);
Thread acceptThread = new Thread(AcceptSockets); socket.Bind(endPoint);
acceptThread.Start(); socket.Listen(4);
state = "Host"; Thread acceptThread = new Thread(AcceptSockets);
Console.WriteLine("Start Accept"); acceptThread.Start();
state = "Host";
}
catch { }
} }
public void ClientInit(string IpAddress) public void ClientInit(string IpAddress)
{ {
Init(IpAddress); try
socket.Connect(endPoint); {
state = "Client"; Init(IpAddress);
Thread.Sleep(10); socket.Connect(endPoint);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost); state = "Client";
ReceivingThread.Start(); Thread.Sleep(10);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
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")
{ {
foreach (Socket socket in clientSockets) try
{
foreach (Socket socket in clientSockets)
{
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
}
}
catch { }
}
else
{
try
{ {
socket.Send(BitConverter.GetBytes(count)); socket.Send(BitConverter.GetBytes(count));
socket.Send(Data); socket.Send(Data);
} }
} catch { }
else
{
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data);
} }
} }
private void ReceiveMsgFromHost() private void ReceiveMsgFromHost()
{ {
while (true) while (true)
{ {
byte[] bytesCount = new byte[4]; try
socket.Receive(bytesCount); {
byte[] Data = new byte[BitConverter.ToInt32(bytesCount)]; byte[] bytesCount = new byte[4];
StateObject so = new StateObject(socket, Data); socket.Receive(bytesCount);
IAsyncResult count = socket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so); 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);
}
catch { }
} }
} }
private void AsyncReceiveCallback(IAsyncResult ar) private void AsyncReceiveCallback(IAsyncResult ar)
{ {
StateObject so = ar.AsyncState as StateObject; try
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); StateObject so = ar.AsyncState as StateObject;
} Socket clientSocket = so.workSocket;
else int readCount = clientSocket.EndReceive(ar);
{ so.UploadedBytesCount += readCount;
GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString())); 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
{
GetReceivingMessages(JsonConvert.DeserializeObject<NetworkTask>(so.sb.ToString()));
}
} }
catch { }
} }
} }
} }