NetworkManagerPackageDivivsionRealization

This commit is contained in:
AnloGames 2023-08-14 17:05:07 +03:00
parent 2b08ccf0f9
commit e34f8f6b1a
2 changed files with 66 additions and 14 deletions

View file

@ -1,14 +1,13 @@
using Microsoft.Xna.Framework; using System.Net.Sockets;
using System.Net.Sockets;
using System.Net; using System.Net;
using System.Collections.Generic;
using System.Text; using System.Text;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System; using System;
namespace DangerousD.GameCore namespace DangerousD.GameCore.Network
{ {
class NetworkManager public class NetworkManagerTest
{ {
public delegate void ReceivingHandler(string msg); public delegate void ReceivingHandler(string msg);
@ -17,7 +16,6 @@ namespace DangerousD.GameCore
Socket socket; Socket socket;
IPEndPoint endPoint; IPEndPoint endPoint;
List<Socket> clientSockets = new List<Socket>(); List<Socket> clientSockets = new List<Socket>();
Socket HostSocket;
string state; string state;
private void Init(string IpAddress) private void Init(string IpAddress)
@ -44,10 +42,11 @@ namespace DangerousD.GameCore
Socket clientSocket = clSocket as Socket; Socket clientSocket = clSocket as Socket;
while (clientSocket != null) while (clientSocket != null)
{ {
byte[] Data = new byte[256]; byte[] bytesCount = new byte[4];
int count = clientSocket.Receive(Data); clientSocket.Receive(bytesCount);
string msg = Encoding.Unicode.GetString(Data, 0, count); byte[] Data = new byte[BitConverter.ToInt32(bytesCount)];
GetMsg(msg); StateObject so = new StateObject(clientSocket, Data);
IAsyncResult count = clientSocket.BeginReceive(so.buffer, 0, so.bufferSize, SocketFlags.None, AsyncReceiveCallback, so);
} }
} }
public void HostInit(string IpAddress) public void HostInit(string IpAddress)
@ -66,27 +65,54 @@ namespace DangerousD.GameCore
socket.Connect(endPoint); socket.Connect(endPoint);
state = "Client"; state = "Client";
Thread.Sleep(10); Thread.Sleep(10);
Thread ReceivingThread = new Thread(ReceiveMsgFromHost);
ReceivingThread.Start();
} }
public void SendMsg(string msg) public void SendMsg(string msg)
{ {
byte[] Data = Encoding.Unicode.GetBytes(msg); byte[] Data = Encoding.Unicode.GetBytes(msg);
int count = Data.Length;
if (state == "Host") if (state == "Host")
{ {
foreach (Socket socket in clientSockets) foreach (Socket socket in clientSockets)
{ {
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data); socket.Send(Data);
} }
} }
else else
{ {
socket.Send(BitConverter.GetBytes(count));
socket.Send(Data); socket.Send(Data);
} }
} }
public string ReceiveMsgFromHost() public void ReceiveMsgFromHost()
{ {
byte[] Data = new byte[256]; while (true)
int count = socket.Receive(Data); {
return Encoding.Unicode.GetString(Data, 0, count); 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);
}
}
public void AsyncReceiveCallback(IAsyncResult ar)
{
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
{
GetMsg(so.sb.ToString());
}
} }
} }
} }

View file

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace DangerousD.GameCore.Network
{
public class StateObject
{
public Socket workSocket;
public int bufferSize;
public byte[] buffer;
public StringBuilder sb = new StringBuilder();
public int UploadedBytesCount;
public StateObject(Socket socket, byte[] buffer)
{
workSocket = socket;
this.buffer = buffer;
bufferSize = buffer.Length;
UploadedBytesCount = 0;
}
}
}