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;
|
||||
|
||||
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,8 +41,6 @@ namespace DangerousD.GameCore.Network
|
|||
{
|
||||
Socket clientSocket = clSocket as Socket;
|
||||
while (clientSocket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytesCount = new byte[4];
|
||||
clientSocket.Receive(bytesCount);
|
||||
|
@ -58,12 +48,8 @@ namespace DangerousD.GameCore.Network
|
|||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
Init(IpAddress);
|
||||
socket.Bind(endPoint);
|
||||
|
@ -73,11 +59,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 +71,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,23 +86,15 @@ 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);
|
||||
|
@ -132,13 +102,9 @@ namespace DangerousD.GameCore.Network
|
|||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
StateObject so = ar.AsyncState as StateObject;
|
||||
Socket clientSocket = so.workSocket;
|
||||
|
@ -155,7 +121,5 @@ namespace DangerousD.GameCore.Network
|
|||
GetReceivingMessages(tasks);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue