速度决定网络接口优先级
This commit is contained in:
parent
3092595946
commit
c29903d8df
|
@ -113,7 +113,7 @@ void Dhcp::Request()
|
||||||
auto opt = (DHCP_OPT*)p;
|
auto opt = (DHCP_OPT*)p;
|
||||||
opt->SetType(DHCP_TYPE_Request);
|
opt->SetType(DHCP_TYPE_Request);
|
||||||
|
|
||||||
opt = opt->Next()->SetData(DHCP_OPT_DHCPServer, Host.DHCPServer.Value);
|
opt = opt->Next()->SetData(DHCP_OPT_DHCPServer, Server.Value);
|
||||||
|
|
||||||
// 发往DHCP服务器
|
// 发往DHCP服务器
|
||||||
SendDhcp(buf, (byte*)opt->Next() - p);
|
SendDhcp(buf, (byte*)opt->Next() - p);
|
||||||
|
@ -259,7 +259,7 @@ void Dhcp::PareOption(Stream& ms)
|
||||||
{
|
{
|
||||||
case DHCP_OPT_Mask: Host.Mask = ms.ReadUInt32(); len -= 4; break;
|
case DHCP_OPT_Mask: Host.Mask = ms.ReadUInt32(); len -= 4; break;
|
||||||
case DHCP_OPT_Router: Host.Gateway = ms.ReadUInt32(); len -= 4; break;
|
case DHCP_OPT_Router: Host.Gateway = ms.ReadUInt32(); len -= 4; break;
|
||||||
case DHCP_OPT_DHCPServer: Host.DHCPServer= ms.ReadUInt32(); len -= 4; break;
|
case DHCP_OPT_DHCPServer: Server = ms.ReadUInt32(); len -= 4; break;
|
||||||
case DHCP_OPT_DNSServer:
|
case DHCP_OPT_DNSServer:
|
||||||
{
|
{
|
||||||
// 有可能有多个DNS,只要第一个
|
// 有可能有多个DNS,只要第一个
|
||||||
|
|
|
@ -20,6 +20,7 @@ private:
|
||||||
public:
|
public:
|
||||||
NetworkInterface& Host; // 主机
|
NetworkInterface& Host; // 主机
|
||||||
IPAddress IP; // 获取的IP地址
|
IPAddress IP; // 获取的IP地址
|
||||||
|
IPAddress Server;
|
||||||
|
|
||||||
uint ExpiredTime; // 过期时间,默认5000毫秒
|
uint ExpiredTime; // 过期时间,默认5000毫秒
|
||||||
bool Running; // 正在运行
|
bool Running; // 正在运行
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
#define net_printf(format, ...)
|
#define net_printf(format, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
List<NetworkInterface*> NetworkInterface::All;
|
||||||
|
|
||||||
/******************************** NetworkInterface ********************************/
|
/******************************** NetworkInterface ********************************/
|
||||||
|
|
||||||
struct NetConfig
|
struct NetConfig
|
||||||
|
@ -19,7 +21,6 @@ struct NetConfig
|
||||||
byte Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
byte Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
||||||
byte Reserved;
|
byte Reserved;
|
||||||
|
|
||||||
uint DHCPServer;
|
|
||||||
uint DNSServer;
|
uint DNSServer;
|
||||||
uint DNSServer2;
|
uint DNSServer2;
|
||||||
uint Gateway;
|
uint Gateway;
|
||||||
|
@ -49,7 +50,7 @@ void NetworkInterface::InitConfig()
|
||||||
IP[3] = first;
|
IP[3] = first;
|
||||||
|
|
||||||
Mask = IPAddress(255, 255, 255, 0);
|
Mask = IPAddress(255, 255, 255, 0);
|
||||||
DHCPServer = Gateway = defip;
|
Gateway = defip;
|
||||||
|
|
||||||
// 修改为双DNS方案,避免单点故障。默认使用阿里和百度公共DNS。
|
// 修改为双DNS方案,避免单点故障。默认使用阿里和百度公共DNS。
|
||||||
DNSServer = IPAddress(223, 5, 5, 5);
|
DNSServer = IPAddress(223, 5, 5, 5);
|
||||||
|
@ -82,7 +83,6 @@ bool NetworkInterface::LoadConfig()
|
||||||
Mac = nc.Mac;
|
Mac = nc.Mac;
|
||||||
Mode = (NetworkType)nc.Mode;
|
Mode = (NetworkType)nc.Mode;
|
||||||
|
|
||||||
DHCPServer = nc.DHCPServer;
|
|
||||||
DNSServer = nc.DNSServer;
|
DNSServer = nc.DNSServer;
|
||||||
DNSServer2 = nc.DNSServer2;
|
DNSServer2 = nc.DNSServer2;
|
||||||
Gateway = nc.Gateway;
|
Gateway = nc.Gateway;
|
||||||
|
@ -103,7 +103,6 @@ bool NetworkInterface::SaveConfig()
|
||||||
Mac.CopyTo(nc.Mac);
|
Mac.CopyTo(nc.Mac);
|
||||||
nc.Mode = (byte)Mode;
|
nc.Mode = (byte)Mode;
|
||||||
|
|
||||||
nc.DHCPServer = DHCPServer.Value;
|
|
||||||
nc.DNSServer = DNSServer.Value;
|
nc.DNSServer = DNSServer.Value;
|
||||||
nc.DNSServer2 = DNSServer2.Value;
|
nc.DNSServer2 = DNSServer2.Value;
|
||||||
nc.Gateway = Gateway.Value;
|
nc.Gateway = Gateway.Value;
|
||||||
|
@ -130,8 +129,8 @@ void NetworkInterface::ShowConfig()
|
||||||
Mask.Show();
|
Mask.Show();
|
||||||
net_printf("\r\n Gate:\t");
|
net_printf("\r\n Gate:\t");
|
||||||
Gateway.Show();
|
Gateway.Show();
|
||||||
net_printf("\r\n DHCP:\t");
|
//net_printf("\r\n DHCP:\t");
|
||||||
DHCPServer.Show();
|
//DHCPServer.Show();
|
||||||
net_printf("\r\n DNS:\t");
|
net_printf("\r\n DNS:\t");
|
||||||
DNSServer.Show();
|
DNSServer.Show();
|
||||||
if(!DNSServer2.IsAny())
|
if(!DNSServer2.IsAny())
|
||||||
|
@ -164,25 +163,6 @@ void NetworkInterface::ShowConfig()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket* NetworkInterface::CreateClient(const NetUri& uri)
|
|
||||||
{
|
|
||||||
auto socket = CreateSocket(uri.Type);
|
|
||||||
socket->Local.Address = uri.Address;
|
|
||||||
socket->Local.Port = uri.Port;
|
|
||||||
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket* NetworkInterface::CreateRemote(const NetUri& uri)
|
|
||||||
{
|
|
||||||
auto socket = CreateSocket(uri.Type);
|
|
||||||
socket->Remote.Address = uri.Address;
|
|
||||||
socket->Remote.Port = uri.Port;
|
|
||||||
socket->Server = uri.Host;
|
|
||||||
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
// DNS解析。默认仅支持字符串IP地址解析
|
// DNS解析。默认仅支持字符串IP地址解析
|
||||||
IPAddress NetworkInterface::QueryDNS(const String& domain)
|
IPAddress NetworkInterface::QueryDNS(const String& domain)
|
||||||
{
|
{
|
||||||
|
@ -198,3 +178,40 @@ bool NetworkInterface::IsAP() const
|
||||||
{
|
{
|
||||||
return Mode == NetworkType::AP || Mode == NetworkType::STA_AP;
|
return Mode == NetworkType::AP || Mode == NetworkType::STA_AP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Socket* NetworkInterface::CreateClient(const NetUri& uri)
|
||||||
|
{
|
||||||
|
for(int i=0; i < All.Count(); i++)
|
||||||
|
{
|
||||||
|
auto ni = All[i];
|
||||||
|
if(ni->Active)
|
||||||
|
{
|
||||||
|
auto socket = ni->CreateSocket(uri.Type);
|
||||||
|
socket->Local.Address = uri.Address;
|
||||||
|
socket->Local.Port = uri.Port;
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket* NetworkInterface::CreateRemote(const NetUri& uri)
|
||||||
|
{
|
||||||
|
for(int i=0; i < All.Count(); i++)
|
||||||
|
{
|
||||||
|
auto ni = All[i];
|
||||||
|
if(ni->Active)
|
||||||
|
{
|
||||||
|
auto socket = ni->CreateSocket(uri.Type);
|
||||||
|
socket->Remote.Address = uri.Address;
|
||||||
|
socket->Remote.Port = uri.Port;
|
||||||
|
socket->Server = uri.Host;
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "NetUri.h"
|
#include "NetUri.h"
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
|
|
||||||
|
#include "Core\List.h"
|
||||||
#include "Core\Delegate.h"
|
#include "Core\Delegate.h"
|
||||||
|
|
||||||
class Socket;
|
class Socket;
|
||||||
|
@ -24,19 +25,24 @@ enum class NetworkType
|
||||||
class NetworkInterface
|
class NetworkInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
byte Index; // 索引
|
||||||
|
bool Active; // 可用
|
||||||
|
ushort Speed; // 速度Mbps。决定优先级
|
||||||
|
cstring Name; // 名称
|
||||||
|
|
||||||
IPAddress IP; // 本地IP地址
|
IPAddress IP; // 本地IP地址
|
||||||
IPAddress Mask; // 子网掩码
|
IPAddress Mask; // 子网掩码
|
||||||
MacAddress Mac; // 本地Mac地址
|
MacAddress Mac; // 本地Mac地址
|
||||||
NetworkType Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
NetworkType Mode; // 无线模式。0不是无线,1是STA,2是AP,3是混合
|
||||||
|
|
||||||
IPAddress DHCPServer;
|
IPAddress Gateway;
|
||||||
IPAddress DNSServer;
|
IPAddress DNSServer;
|
||||||
IPAddress DNSServer2;
|
IPAddress DNSServer2;
|
||||||
IPAddress Gateway;
|
|
||||||
|
|
||||||
String* SSID; // 无线SSID
|
String* SSID; // 无线SSID
|
||||||
String* Pass; // 无线密码
|
String* Pass; // 无线密码
|
||||||
|
|
||||||
|
Delegate<NetworkInterface&> Changed; // 网络改变
|
||||||
Delegate<NetworkInterface&> NetReady; // 网络准备就绪。带this参数
|
Delegate<NetworkInterface&> NetReady; // 网络准备就绪。带this参数
|
||||||
|
|
||||||
NetworkInterface();
|
NetworkInterface();
|
||||||
|
@ -53,8 +59,6 @@ public:
|
||||||
void ShowConfig();
|
void ShowConfig();
|
||||||
|
|
||||||
virtual Socket* CreateSocket(NetType type) = 0;
|
virtual Socket* CreateSocket(NetType type) = 0;
|
||||||
virtual Socket* CreateClient(const NetUri& uri);
|
|
||||||
virtual Socket* CreateRemote(const NetUri& uri);
|
|
||||||
|
|
||||||
// DNS解析。默认仅支持字符串IP地址解析
|
// DNS解析。默认仅支持字符串IP地址解析
|
||||||
virtual IPAddress QueryDNS(const String& domain);
|
virtual IPAddress QueryDNS(const String& domain);
|
||||||
|
@ -65,6 +69,11 @@ public:
|
||||||
|
|
||||||
bool IsStation() const;
|
bool IsStation() const;
|
||||||
bool IsAP() const;
|
bool IsAP() const;
|
||||||
|
|
||||||
|
// 全局静态
|
||||||
|
static List<NetworkInterface*> All;
|
||||||
|
static Socket* CreateClient(const NetUri& uri);
|
||||||
|
static Socket* CreateRemote(const NetUri& uri);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -35,7 +35,7 @@ void TinyIP::Init()
|
||||||
_Arr.SetLength(1500);
|
_Arr.SetLength(1500);
|
||||||
|
|
||||||
Mask = 0x00FFFFFF;
|
Mask = 0x00FFFFFF;
|
||||||
DHCPServer = Gateway = DNSServer = IP = 0;
|
Gateway = DNSServer = IP = 0;
|
||||||
|
|
||||||
Arp = nullptr;
|
Arp = nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue