remove try catch in network
This commit is contained in:
parent
a47f73d523
commit
f72cc03c66
1 changed files with 52 additions and 88 deletions
|
@ -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,8 +41,6 @@ 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);
|
||||||
|
@ -58,12 +48,8 @@ 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);
|
||||||
|
@ -73,11 +59,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 +71,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,23 +86,15 @@ 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);
|
||||||
|
@ -132,13 +102,9 @@ 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;
|
||||||
|
@ -155,7 +121,5 @@ namespace DangerousD.GameCore.Network
|
||||||
GetReceivingMessages(tasks);
|
GetReceivingMessages(tasks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue