Skip to content

Commit 3750d4a

Browse files
committed
[修改]1. 修改连接域名获取失败的处理
1 parent 4dc0fdb commit 3750d4a

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

Runtime/Network/Network/NetworkManager.NetworkChannelBase.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ public void SetRPCEndHandler(EventHandler<MessageObject> handler)
542542
PRpcState.SetRPCEndHandler(handler);
543543
}
544544

545+
protected bool IsVerifyAddress = true;
546+
protected IPEndPoint ConnectEndPoint;
547+
545548
/// <summary>
546549
/// 连接到远程主机。
547550
/// </summary>
@@ -556,32 +559,42 @@ public virtual void Connect(Uri address, object userData = null)
556559
PSocket = null;
557560
}
558561

559-
bool isVerify = true;
560-
IPEndPoint endPoint = null;
562+
IsVerifyAddress = true;
563+
ConnectEndPoint = null;
561564
if (IPAddress.TryParse(address.Host, out var ipAddress))
562565
{
563-
endPoint = new IPEndPoint(ipAddress, address.Port);
566+
ConnectEndPoint = new IPEndPoint(ipAddress, address.Port);
564567
}
565568
else
566569
{
567-
var ipHost = Utility.Net.GetHostIPv4(address.Host);
568-
if (IPAddress.TryParse(ipHost, out ipAddress))
570+
try
569571
{
570-
endPoint = new IPEndPoint(ipAddress, address.Port);
572+
var ipHost = Utility.Net.GetHostIPv4(address.Host);
573+
if (IPAddress.TryParse(ipHost, out ipAddress))
574+
{
575+
ConnectEndPoint = new IPEndPoint(ipAddress, address.Port);
576+
}
577+
else
578+
{
579+
// 获取IP失败
580+
Log.Error($"IP address is invalid.{address.Host}");
581+
IsVerifyAddress = false;
582+
Close();
583+
PSocket = null;
584+
}
571585
}
572-
else
586+
catch (Exception e)
573587
{
574-
// 获取IP失败
575-
Log.Error($"IP address is invalid.{address.Host}");
576-
isVerify = false;
588+
Log.Error($"IP address is invalid.{address.Host} {e.Message}");
589+
IsVerifyAddress = false;
577590
Close();
578591
PSocket = null;
579592
}
580593
}
581594

582-
if (isVerify)
595+
if (IsVerifyAddress && ConnectEndPoint != null)
583596
{
584-
switch (endPoint.AddressFamily)
597+
switch (ConnectEndPoint.AddressFamily)
585598
{
586599
case System.Net.Sockets.AddressFamily.InterNetwork:
587600
PAddressFamily = AddressFamily.IPv4;
@@ -592,7 +605,7 @@ public virtual void Connect(Uri address, object userData = null)
592605
break;
593606

594607
default:
595-
string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", endPoint.AddressFamily);
608+
string errorMessage = Utility.Text.Format("Not supported address family '{0}'.", ConnectEndPoint.AddressFamily);
596609
if (NetworkChannelError != null)
597610
{
598611
NetworkChannelError(this, NetworkErrorCode.AddressFamilyError, SocketError.Success, errorMessage);

Runtime/Network/Network/SystemSocket/NetworkManager.SystemTcpNetworkChannel.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public sealed partial class NetworkManager
2121
private sealed class SystemTcpNetworkChannel : NetworkChannelBase
2222
{
2323
private ConnectState m_ConnectState = null;
24-
private IPEndPoint m_ConnectedEndPoint = null;
2524
private SystemNetSocket PSystemNetSocket = null;
2625

2726
/// <summary>
@@ -46,29 +45,13 @@ public override void Connect(Uri address, object userData = null)
4645
return;
4746
}
4847

49-
if (IPAddress.TryParse(address.Host, out var ipAddress))
50-
{
51-
m_ConnectedEndPoint = new IPEndPoint(ipAddress, address.Port);
52-
}
53-
else
48+
base.Connect(address, userData);
49+
if (IsVerifyAddress)
5450
{
55-
var ipHost = Utility.Net.GetHostIPv4(address.Host);
56-
if (IPAddress.TryParse(ipHost, out ipAddress))
57-
{
58-
m_ConnectedEndPoint = new IPEndPoint(ipAddress, address.Port);
59-
}
60-
else
61-
{
62-
// 获取IP失败
63-
Log.Error($"IP address is invalid.{address.Host}");
64-
Close();
65-
return;
66-
}
51+
PSystemNetSocket = new SystemNetSocket(ConnectEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
52+
PSocket = PSystemNetSocket;
6753
}
6854

69-
base.Connect(address, userData);
70-
PSystemNetSocket = new SystemNetSocket(m_ConnectedEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
71-
PSocket = PSystemNetSocket;
7255
if (PSocket == null)
7356
{
7457
string errorMessage = "Initialize network channel failure.";
@@ -337,7 +320,7 @@ private void ConnectAsync(object userData)
337320
{
338321
PIsConnecting = true;
339322
m_ConnectState = new ConnectState(PSystemNetSocket, userData);
340-
((SystemNetSocket)PSocket).BeginConnect(m_ConnectedEndPoint.Address, m_ConnectedEndPoint.Port, ConnectCallback, m_ConnectState);
323+
((SystemNetSocket)PSocket).BeginConnect(ConnectEndPoint.Address, ConnectEndPoint.Port, ConnectCallback, m_ConnectState);
341324
}
342325
catch (Exception exception)
343326
{

0 commit comments

Comments
 (0)