全面使用NetType
This commit is contained in:
parent
841ca272fa
commit
1d767a00d7
|
@ -193,7 +193,7 @@ void AP0104::OpenClient()
|
||||||
AddControl(*Host, *tk);
|
AddControl(*Host, *tk);
|
||||||
|
|
||||||
TokenConfig cfg;
|
TokenConfig cfg;
|
||||||
cfg.Protocol = ProtocolType::Udp;
|
cfg.Protocol = NetType::Udp;
|
||||||
cfg.ServerIP = IPAddress::Broadcast().Value;
|
cfg.ServerIP = IPAddress::Broadcast().Value;
|
||||||
cfg.ServerPort = 3355;
|
cfg.ServerPort = 3355;
|
||||||
AddControl(*Host, cfg);
|
AddControl(*Host, cfg);
|
||||||
|
|
|
@ -183,7 +183,7 @@ void AP0801::OpenClient()
|
||||||
AddControl(*Host, *tk);
|
AddControl(*Host, *tk);
|
||||||
|
|
||||||
TokenConfig cfg;
|
TokenConfig cfg;
|
||||||
cfg.Protocol = ProtocolType::Udp;
|
cfg.Protocol = NetType::Udp;
|
||||||
cfg.ServerIP = IPAddress::Broadcast().Value;
|
cfg.ServerIP = IPAddress::Broadcast().Value;
|
||||||
cfg.ServerPort = 3355;
|
cfg.ServerPort = 3355;
|
||||||
AddControl(*Host, cfg);
|
AddControl(*Host, cfg);
|
||||||
|
|
|
@ -117,7 +117,7 @@ void IOK027X::OpenClient()
|
||||||
AddControl(*Host, *tk);
|
AddControl(*Host, *tk);
|
||||||
|
|
||||||
TokenConfig cfg;
|
TokenConfig cfg;
|
||||||
cfg.Protocol = ProtocolType::Udp;
|
cfg.Protocol = NetType::Udp;
|
||||||
cfg.ServerIP = IPAddress::Broadcast().Value;
|
cfg.ServerIP = IPAddress::Broadcast().Value;
|
||||||
cfg.ServerPort = 3355;
|
cfg.ServerPort = 3355;
|
||||||
AddControl(*Host, cfg);
|
AddControl(*Host, cfg);
|
||||||
|
|
|
@ -173,10 +173,10 @@ TokenClient* PA0903::CreateClient()
|
||||||
client->Cfg = tk;
|
client->Cfg = tk;
|
||||||
|
|
||||||
// 如果是TCP,需要再建立一个本地UDP
|
// 如果是TCP,需要再建立一个本地UDP
|
||||||
//if(tk->Protocol == ProtocolType::Tcp)
|
//if(tk->Protocol == NetType::Tcp)
|
||||||
{
|
{
|
||||||
// 建立一个监听内网的UDP Socket
|
// 建立一个监听内网的UDP Socket
|
||||||
socket = Host->CreateSocket(ProtocolType::Udp);
|
socket = Host->CreateSocket(NetType::Udp);
|
||||||
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
|
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
|
||||||
socket->Remote.Address = IPAddress::Broadcast();
|
socket->Remote.Address = IPAddress::Broadcast();
|
||||||
socket->Local.Port = tk->Port;
|
socket->Local.Port = tk->Port;
|
||||||
|
|
|
@ -231,7 +231,7 @@ void Esp8266::Config()
|
||||||
SetAutoConn(AutoConn);
|
SetAutoConn(AutoConn);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISocket* Esp8266::CreateSocket(ProtocolType type)
|
ISocket* Esp8266::CreateSocket(NetType type)
|
||||||
{
|
{
|
||||||
auto es = (EspSocket**)_sockets;
|
auto es = (EspSocket**)_sockets;
|
||||||
|
|
||||||
|
@ -249,10 +249,10 @@ ISocket* Esp8266::CreateSocket(ProtocolType type)
|
||||||
|
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case ProtocolType::Tcp:
|
case NetType::Tcp:
|
||||||
return es[i] = new EspTcp(*this, i);
|
return es[i] = new EspTcp(*this, i);
|
||||||
|
|
||||||
case ProtocolType::Udp:
|
case NetType::Udp:
|
||||||
return es[i] = new EspUdp(*this, i);
|
return es[i] = new EspUdp(*this, i);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
virtual void Config();
|
virtual void Config();
|
||||||
|
|
||||||
//virtual const String ToString() const { return String("Esp8266"); }
|
//virtual const String ToString() const { return String("Esp8266"); }
|
||||||
virtual ISocket* CreateSocket(ProtocolType type);
|
virtual ISocket* CreateSocket(NetType type);
|
||||||
// 启用DNS
|
// 启用DNS
|
||||||
virtual bool EnableDNS();
|
virtual bool EnableDNS();
|
||||||
// 启用DHCP
|
// 启用DHCP
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
/******************************** Socket ********************************/
|
/******************************** Socket ********************************/
|
||||||
|
|
||||||
EspSocket::EspSocket(Esp8266& host, ProtocolType protocol, byte idx)
|
EspSocket::EspSocket(Esp8266& host, NetType protocol, byte idx)
|
||||||
: _Host(host)
|
: _Host(host)
|
||||||
{
|
{
|
||||||
_Index = idx;
|
_Index = idx;
|
||||||
|
@ -43,7 +43,7 @@ bool EspSocket::OnOpen()
|
||||||
_Host.SetMux(true);
|
_Host.SetMux(true);
|
||||||
|
|
||||||
#if NET_DEBUG
|
#if NET_DEBUG
|
||||||
net_printf("%s::Open ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
|
net_printf("%s::Open ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
|
||||||
Local.Show(false);
|
Local.Show(false);
|
||||||
net_printf(" => ");
|
net_printf(" => ");
|
||||||
if(Server)
|
if(Server)
|
||||||
|
@ -58,9 +58,9 @@ bool EspSocket::OnOpen()
|
||||||
String cmd = "AT+CIPSTART=";
|
String cmd = "AT+CIPSTART=";
|
||||||
cmd = cmd + _Index + ",";
|
cmd = cmd + _Index + ",";
|
||||||
|
|
||||||
if(Protocol == ProtocolType::Udp)
|
if(Protocol == NetType::Udp)
|
||||||
cmd += "\"UDP\"";
|
cmd += "\"UDP\"";
|
||||||
else if(Protocol == ProtocolType::Tcp)
|
else if(Protocol == NetType::Tcp)
|
||||||
cmd += "\"TCP\"";
|
cmd += "\"TCP\"";
|
||||||
|
|
||||||
auto rm = Server;
|
auto rm = Server;
|
||||||
|
@ -81,7 +81,7 @@ bool EspSocket::OnOpen()
|
||||||
auto rt = _Host.Send(cmd + "\r\n", "OK", "ERROR", 10000);
|
auto rt = _Host.Send(cmd + "\r\n", "OK", "ERROR", 10000);
|
||||||
if(!rt.Contains("OK") && !rt.Contains("ALREADY CONNECTED"))
|
if(!rt.Contains("OK") && !rt.Contains("ALREADY CONNECTED"))
|
||||||
{
|
{
|
||||||
net_printf("协议 %d, %d 打开失败 \r\n", Protocol, Remote.Port);
|
net_printf("协议 %d, %d 打开失败 \r\n", (byte)Protocol, Remote.Port);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ protected:
|
||||||
int _Error;
|
int _Error;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EspSocket(Esp8266& host, ProtocolType protocol, byte idx);
|
EspSocket(Esp8266& host, NetType protocol, byte idx);
|
||||||
virtual ~EspSocket();
|
virtual ~EspSocket();
|
||||||
|
|
||||||
// 打开Socket
|
// 打开Socket
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/******************************** Tcp ********************************/
|
/******************************** Tcp ********************************/
|
||||||
|
|
||||||
EspTcp::EspTcp(Esp8266& host, byte idx)
|
EspTcp::EspTcp(Esp8266& host, byte idx)
|
||||||
: EspSocket(host, ProtocolType::Tcp, idx)
|
: EspSocket(host, NetType::Tcp, idx)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
/******************************** Udp ********************************/
|
/******************************** Udp ********************************/
|
||||||
|
|
||||||
EspUdp::EspUdp(Esp8266& host, byte idx)
|
EspUdp::EspUdp(Esp8266& host, byte idx)
|
||||||
: EspSocket(host, ProtocolType::Udp, idx)
|
: EspSocket(host, NetType::Udp, idx)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -632,14 +632,14 @@ byte W5500::GetSocket()
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISocket* W5500::CreateSocket(ProtocolType type)
|
ISocket* W5500::CreateSocket(NetType type)
|
||||||
{
|
{
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case ProtocolType::Tcp:
|
case NetType::Tcp:
|
||||||
return new TcpClient(*this);
|
return new TcpClient(*this);
|
||||||
|
|
||||||
case ProtocolType::Udp:
|
case NetType::Udp:
|
||||||
return new UdpClient(*this);
|
return new UdpClient(*this);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -920,7 +920,7 @@ enum S_Status
|
||||||
#define SocRegWrites(P, D) _Host.WriteFrame(offsetof(TSocket, P), D, Index, 0x01)
|
#define SocRegWrites(P, D) _Host.WriteFrame(offsetof(TSocket, P), D, Index, 0x01)
|
||||||
#define SocRegReads(P, bs) _Host.ReadFrame(offsetof(TSocket, P), bs, Index, 0x01)
|
#define SocRegReads(P, bs) _Host.ReadFrame(offsetof(TSocket, P), bs, Index, 0x01)
|
||||||
|
|
||||||
HardSocket::HardSocket(W5500& host, ProtocolType protocol) : _Host(host)
|
HardSocket::HardSocket(W5500& host, NetType protocol) : _Host(host)
|
||||||
{
|
{
|
||||||
MaxSize = 1500;
|
MaxSize = 1500;
|
||||||
|
|
||||||
|
@ -1071,7 +1071,7 @@ bool HardSocket::OnOpen()
|
||||||
Local.Address = _Host.IP;
|
Local.Address = _Host.IP;
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
debug_printf("%s::Open ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
|
debug_printf("%s::Open ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
|
||||||
Local.Show(false);
|
Local.Show(false);
|
||||||
debug_printf(" => ");
|
debug_printf(" => ");
|
||||||
Server.Show(false);
|
Server.Show(false);
|
||||||
|
@ -1081,9 +1081,9 @@ bool HardSocket::OnOpen()
|
||||||
|
|
||||||
// 设置分片长度,参考W5500数据手册,该值可以不修改
|
// 设置分片长度,参考W5500数据手册,该值可以不修改
|
||||||
// 默认值:udp 1472 tcp 1460 其他类型不管他 有默认不设置也没啥
|
// 默认值:udp 1472 tcp 1460 其他类型不管他 有默认不设置也没啥
|
||||||
if(Protocol == ProtocolType::Udp)
|
if(Protocol == NetType::Udp)
|
||||||
SocRegWrite2(MSSR, 1472);
|
SocRegWrite2(MSSR, 1472);
|
||||||
else if(Protocol == ProtocolType::Tcp)
|
else if(Protocol == NetType::Tcp)
|
||||||
SocRegWrite2(MSSR, 1460);
|
SocRegWrite2(MSSR, 1460);
|
||||||
|
|
||||||
// 设置自己的端口号
|
// 设置自己的端口号
|
||||||
|
@ -1097,9 +1097,9 @@ bool HardSocket::OnOpen()
|
||||||
// 设置Socket为UDP模式
|
// 设置Socket为UDP模式
|
||||||
S_Mode mode;
|
S_Mode mode;
|
||||||
mode.Init();
|
mode.Init();
|
||||||
if(Protocol == ProtocolType::Tcp)
|
if(Protocol == NetType::Tcp)
|
||||||
mode.Protocol = 0x01;
|
mode.Protocol = 0x01;
|
||||||
if(Protocol == ProtocolType::Udp)
|
if(Protocol == NetType::Udp)
|
||||||
mode.Protocol = 0x02;
|
mode.Protocol = 0x02;
|
||||||
//if(Protocol == 0x02) mode.MULTI_MFEN = 1;
|
//if(Protocol == 0x02) mode.MULTI_MFEN = 1;
|
||||||
SocRegWrite(MR, mode.ToByte());
|
SocRegWrite(MR, mode.ToByte());
|
||||||
|
@ -1122,8 +1122,8 @@ bool HardSocket::OnOpen()
|
||||||
while(!tw.Expired())
|
while(!tw.Expired())
|
||||||
{
|
{
|
||||||
sr = ReadStatus();
|
sr = ReadStatus();
|
||||||
if((Protocol == ProtocolType::Tcp && sr == SOCK_INIT)
|
if((Protocol == NetType::Tcp && sr == SOCK_INIT)
|
||||||
|| (Protocol == ProtocolType::Udp && sr == SOCK_UDP))
|
|| (Protocol == NetType::Udp && sr == SOCK_UDP))
|
||||||
{
|
{
|
||||||
rs = true;
|
rs = true;
|
||||||
break;
|
break;
|
||||||
|
@ -1131,7 +1131,7 @@ bool HardSocket::OnOpen()
|
||||||
}
|
}
|
||||||
if(!rs)
|
if(!rs)
|
||||||
{
|
{
|
||||||
debug_printf("protocol %d, Socket %d 打开失败 SR : 0x%02X \r\n", Protocol,Index, sr);
|
debug_printf("protocol %d, Socket %d 打开失败 SR : 0x%02X \r\n", (byte)Protocol,Index, sr);
|
||||||
OnClose();
|
OnClose();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -1145,7 +1145,7 @@ void HardSocket::OnClose()
|
||||||
WriteConfig(CLOSE);
|
WriteConfig(CLOSE);
|
||||||
WriteInterrupt(0xFF);
|
WriteInterrupt(0xFF);
|
||||||
|
|
||||||
debug_printf("%s::Close ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
|
debug_printf("%s::Close ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
|
||||||
Local.Show(false);
|
Local.Show(false);
|
||||||
debug_printf(" => ");
|
debug_printf(" => ");
|
||||||
Remote.Show(true);
|
Remote.Show(true);
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
|
|
||||||
cstring ToString() const { return "W5500"; }
|
cstring ToString() const { return "W5500"; }
|
||||||
|
|
||||||
virtual ISocket* CreateSocket(ProtocolType type);
|
virtual ISocket* CreateSocket(NetType type);
|
||||||
|
|
||||||
// DNS解析。默认仅支持字符串IP地址解析
|
// DNS解析。默认仅支持字符串IP地址解析
|
||||||
virtual IPAddress QueryDNS(const String& domain);
|
virtual IPAddress QueryDNS(const String& domain);
|
||||||
|
@ -114,7 +114,7 @@ public:
|
||||||
bool Enable; // 启用
|
bool Enable; // 启用
|
||||||
byte Index; // 使用的硬Socket编号 也是BSB选项的一部分
|
byte Index; // 使用的硬Socket编号 也是BSB选项的一部分
|
||||||
|
|
||||||
HardSocket(W5500& host, ProtocolType protocol);
|
HardSocket(W5500& host, NetType protocol);
|
||||||
virtual ~HardSocket();
|
virtual ~HardSocket();
|
||||||
|
|
||||||
// 网卡状态输出
|
// 网卡状态输出
|
||||||
|
@ -152,7 +152,7 @@ public:
|
||||||
class TcpClient : public HardSocket
|
class TcpClient : public HardSocket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TcpClient(W5500& host): HardSocket(host, ProtocolType::Tcp){ Init(); };
|
TcpClient(W5500& host): HardSocket(host, NetType::Tcp){ Init(); };
|
||||||
void Init();
|
void Init();
|
||||||
virtual ~TcpClient();
|
virtual ~TcpClient();
|
||||||
virtual bool OnOpen();
|
virtual bool OnOpen();
|
||||||
|
@ -179,7 +179,7 @@ private:
|
||||||
class UdpClient : public HardSocket
|
class UdpClient : public HardSocket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UdpClient(W5500& host) : HardSocket(host, ProtocolType::Udp) { }
|
UdpClient(W5500& host) : HardSocket(host, NetType::Udp) { }
|
||||||
|
|
||||||
virtual bool SendTo(const Buffer& bs, const IPEndPoint& remote);
|
virtual bool SendTo(const Buffer& bs, const IPEndPoint& remote);
|
||||||
|
|
||||||
|
|
|
@ -358,7 +358,7 @@ short dns_makequery(short op, const String& name, Buffer& bs)
|
||||||
|
|
||||||
DNS::DNS(ISocketHost& host, const IPAddress& dns) : Host(host)
|
DNS::DNS(ISocketHost& host, const IPAddress& dns) : Host(host)
|
||||||
{
|
{
|
||||||
Socket = host.CreateSocket(ProtocolType::Udp);
|
Socket = host.CreateSocket(NetType::Udp);
|
||||||
|
|
||||||
Socket->Remote.Port = 53;
|
Socket->Remote.Port = 53;
|
||||||
Socket->Remote.Address = !dns.IsAny() ? dns : host.DNSServer;
|
Socket->Remote.Address = !dns.IsAny() ? dns : host.DNSServer;
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
Dhcp::Dhcp(ISocketHost& host) : Host(host)
|
Dhcp::Dhcp(ISocketHost& host) : Host(host)
|
||||||
{
|
{
|
||||||
Socket = host.CreateSocket(ProtocolType::Udp);
|
Socket = host.CreateSocket(NetType::Udp);
|
||||||
|
|
||||||
Socket->Local.Port = 68;
|
Socket->Local.Port = 68;
|
||||||
Socket->Remote.Port = 67;
|
Socket->Remote.Port = 67;
|
||||||
|
|
|
@ -242,7 +242,7 @@ typedef struct _ICMP_HEADER
|
||||||
typedef struct _ARP_HEADER
|
typedef struct _ARP_HEADER
|
||||||
{
|
{
|
||||||
ushort HardType; // 硬件类型
|
ushort HardType; // 硬件类型
|
||||||
ushort ProtocolType; // 协议类型
|
ushort NetType; // 协议类型
|
||||||
byte HardLength; // 硬件地址长度
|
byte HardLength; // 硬件地址长度
|
||||||
byte ProtocolLength; // 协议地址长度
|
byte ProtocolLength; // 协议地址长度
|
||||||
ushort Option; // 选项
|
ushort Option; // 选项
|
||||||
|
@ -255,7 +255,7 @@ typedef struct _ARP_HEADER
|
||||||
void Init(bool recursion = false)
|
void Init(bool recursion = false)
|
||||||
{
|
{
|
||||||
HardType = 0x0100;
|
HardType = 0x0100;
|
||||||
ProtocolType = 0x08;
|
NetType = 0x08;
|
||||||
HardLength = 6;
|
HardLength = 6;
|
||||||
ProtocolLength = 4;
|
ProtocolLength = 4;
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
#include "IPAddress.h"
|
#include "IPAddress.h"
|
||||||
#include "IPEndPoint.h"
|
#include "IPEndPoint.h"
|
||||||
#include "MacAddress.h"
|
|
||||||
#include "Delegate.h"
|
|
||||||
|
|
||||||
// 协议类型
|
// 协议类型
|
||||||
enum class NetType
|
enum class NetType
|
||||||
|
|
|
@ -101,7 +101,7 @@ bool ISocketHost::SaveConfig()
|
||||||
nc.IP = IP.Value;
|
nc.IP = IP.Value;
|
||||||
nc.Mask = Mask.Value;
|
nc.Mask = Mask.Value;
|
||||||
Mac.CopyTo(nc.Mac);
|
Mac.CopyTo(nc.Mac);
|
||||||
nc.Mode = Mode;
|
nc.Mode = (byte)Mode;
|
||||||
|
|
||||||
nc.DHCPServer = DHCPServer.Value;
|
nc.DHCPServer = DHCPServer.Value;
|
||||||
nc.DNSServer = DNSServer.Value;
|
nc.DNSServer = DNSServer.Value;
|
||||||
|
|
|
@ -69,7 +69,7 @@ class ISocket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ISocketHost* Host; // 主机
|
ISocketHost* Host; // 主机
|
||||||
ProtocolType Protocol; // 协议类型
|
NetType Protocol; // 协议类型
|
||||||
|
|
||||||
IPEndPoint Local; // 本地地址。包含本地局域网IP地址,实际监听的端口,从1024开始累加
|
IPEndPoint Local; // 本地地址。包含本地局域网IP地址,实际监听的端口,从1024开始累加
|
||||||
IPEndPoint Remote; // 远程地址
|
IPEndPoint Remote; // 远程地址
|
||||||
|
|
|
@ -86,7 +86,7 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
||||||
#if NET_DEBUG
|
#if NET_DEBUG
|
||||||
// 数据校验
|
// 数据校验
|
||||||
assert_param(arp->HardType == 0x0100);
|
assert_param(arp->HardType == 0x0100);
|
||||||
assert_param(arp->ProtocolType == ETH_IP);
|
assert_param(arp->NetType == ETH_IP);
|
||||||
assert_param(arp->HardLength == 6);
|
assert_param(arp->HardLength == 6);
|
||||||
assert_param(arp->ProtocolLength == 4);
|
assert_param(arp->ProtocolLength == 4);
|
||||||
//assert_param(arp->Option == 0x0100);
|
//assert_param(arp->Option == 0x0100);
|
||||||
|
|
|
@ -11,7 +11,7 @@ TcpSocket::TcpSocket(TinyIP* tip) : TinySocket(tip, IP_TCP)
|
||||||
{
|
{
|
||||||
MaxSize = 1500;
|
MaxSize = 1500;
|
||||||
Host = tip;
|
Host = tip;
|
||||||
Protocol = ProtocolType::Tcp;
|
Protocol = NetType::Tcp;
|
||||||
|
|
||||||
// 累加端口
|
// 累加端口
|
||||||
static ushort g_tcp_port = 1024;
|
static ushort g_tcp_port = 1024;
|
||||||
|
|
|
@ -318,14 +318,14 @@ bool TinyIP::SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint
|
||||||
return SendEthernet(ETH_IP, mac, (byte*)ip, sizeof(IP_HEADER) + len);
|
return SendEthernet(ETH_IP, mac, (byte*)ip, sizeof(IP_HEADER) + len);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISocket* TinyIP::CreateSocket(ProtocolType type)
|
ISocket* TinyIP::CreateSocket(NetType type)
|
||||||
{
|
{
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case ProtocolType::Tcp:
|
case NetType::Tcp:
|
||||||
return new TcpSocket(this);
|
return new TcpSocket(this);
|
||||||
|
|
||||||
case ProtocolType::Udp:
|
case NetType::Udp:
|
||||||
return new UdpSocket(this);
|
return new UdpSocket(this);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -66,7 +66,7 @@ public:
|
||||||
bool SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len);
|
bool SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len);
|
||||||
bool IsBroadcast(const IPAddress& ip); // 是否广播地址
|
bool IsBroadcast(const IPAddress& ip); // 是否广播地址
|
||||||
|
|
||||||
virtual ISocket* CreateSocket(ProtocolType type);
|
virtual ISocket* CreateSocket(NetType type);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -7,7 +7,7 @@ UdpSocket::UdpSocket(TinyIP* tip) : TinySocket(tip, IP_UDP)
|
||||||
{
|
{
|
||||||
MaxSize = 1500;
|
MaxSize = 1500;
|
||||||
Host = tip;
|
Host = tip;
|
||||||
Protocol = ProtocolType::Udp;
|
Protocol = NetType::Udp;
|
||||||
|
|
||||||
// 累加端口
|
// 累加端口
|
||||||
static ushort g_udp_port = 1024;
|
static ushort g_udp_port = 1024;
|
||||||
|
|
|
@ -17,8 +17,9 @@ HelloMessage::HelloMessage() : Cipher(1), Key(0)
|
||||||
LocalTime = DateTime::Now().TotalMs();
|
LocalTime = DateTime::Now().TotalMs();
|
||||||
Cipher[0] = 1;
|
Cipher[0] = 1;
|
||||||
|
|
||||||
Protocol = 17;
|
//Protocol = 17;
|
||||||
Port = 0;
|
//Port = 0;
|
||||||
|
Uri.Type = NetType::Udp;
|
||||||
}
|
}
|
||||||
|
|
||||||
HelloMessage::HelloMessage(const HelloMessage& msg) : MessageBase(msg), Cipher(1), Key(0)
|
HelloMessage::HelloMessage(const HelloMessage& msg) : MessageBase(msg), Cipher(1), Key(0)
|
||||||
|
@ -31,9 +32,10 @@ HelloMessage::HelloMessage(const HelloMessage& msg) : MessageBase(msg), Cipher(1
|
||||||
Cipher.Copy(0, msg.Cipher, 0, msg.Cipher.Length());
|
Cipher.Copy(0, msg.Cipher, 0, msg.Cipher.Length());
|
||||||
Key.Copy(0, msg.Key, 0, msg.Key.Length());
|
Key.Copy(0, msg.Key, 0, msg.Key.Length());
|
||||||
|
|
||||||
Protocol = msg.Protocol;
|
//Protocol = msg.Protocol;
|
||||||
Port = msg.Port;
|
//Port = msg.Port;
|
||||||
Server = msg.Server;
|
//Server = msg.Server;
|
||||||
|
Uri = msg.Uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从数据流中读取消息
|
// 从数据流中读取消息
|
||||||
|
@ -52,12 +54,14 @@ bool HelloMessage::Read(Stream& ms)
|
||||||
Stream urims(uri);
|
Stream urims(uri);
|
||||||
BinaryPair uribp(urims);
|
BinaryPair uribp(urims);
|
||||||
|
|
||||||
uribp.Get("Type", Protocol); // 服务店 ProtocolType 17 为UDP
|
byte type;
|
||||||
if (Protocol == 0x00) Protocol = 0x11; // 避免 unknown 出现
|
uribp.Get("Type", type); // 服务店 NetType 17 为UDP
|
||||||
uribp.Get("Host", Server);
|
Uri.Type = (NetType)type;
|
||||||
|
if (Uri.Type == NetType::Unknown) Uri.Type = NetType::Udp; // 避免 unknown 出现
|
||||||
|
uribp.Get("Host", Uri.Host);
|
||||||
uint uintPort; // 服务端 Port 为 int 类型
|
uint uintPort; // 服务端 Port 为 int 类型
|
||||||
uribp.Get("Port", uintPort);
|
uribp.Get("Port", uintPort);
|
||||||
Port = uintPort ;
|
Uri.Port = uintPort ;
|
||||||
}
|
}
|
||||||
bp.Get("VisitToken", VisitToken);
|
bp.Get("VisitToken", VisitToken);
|
||||||
|
|
||||||
|
@ -118,12 +122,13 @@ String& HelloMessage::ToStr(String& str) const
|
||||||
//str += ' ';
|
//str += ' ';
|
||||||
str += (ErrCode == 0xFD) ? " 临时跳转 " : " 永久跳转 ";
|
str += (ErrCode == 0xFD) ? " 临时跳转 " : " 永久跳转 ";
|
||||||
|
|
||||||
if(Protocol == ProtocolType::Tcp)
|
/*if(Protocol == NetType::Tcp)
|
||||||
str += "Tcp://";
|
str += "Tcp://";
|
||||||
else if(Protocol == ProtocolType::Udp)
|
else if(Protocol == NetType::Udp)
|
||||||
str += "Udp://";
|
str += "Udp://";
|
||||||
|
|
||||||
str = str + Server + ":" + Port;
|
str = str + Server + ":" + Port;*/
|
||||||
|
str += Uri;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#define __HelloMessage_H__
|
#define __HelloMessage_H__
|
||||||
|
|
||||||
#include "Message\MessageBase.h"
|
#include "Message\MessageBase.h"
|
||||||
#include "Net\Socket.h"
|
#include "Net\NetUri.h"
|
||||||
|
|
||||||
// 握手消息
|
// 握手消息
|
||||||
// 请求:2版本 + S类型 + S名称 + 8本地时间 + 6本地IP端口 + S支持加密算法列表
|
// 请求:2版本 + S类型 + S名称 + 8本地时间 + 6本地IP端口 + S支持加密算法列表
|
||||||
|
@ -24,8 +24,9 @@ public:
|
||||||
String ErrMsg; // 错误信息
|
String ErrMsg; // 错误信息
|
||||||
|
|
||||||
byte Protocol; // 协议,17为UDP 6为TCP
|
byte Protocol; // 协议,17为UDP 6为TCP
|
||||||
String Server; // 服务器地址。可能是域名或IP
|
//String Server; // 服务器地址。可能是域名或IP
|
||||||
ushort Port; // 本地端口
|
//ushort Port; // 本地端口
|
||||||
|
NetUri Uri;
|
||||||
String VisitToken; //访问令牌
|
String VisitToken; //访问令牌
|
||||||
|
|
||||||
// 初始化消息,各字段为0
|
// 初始化消息,各字段为0
|
||||||
|
|
|
@ -111,10 +111,10 @@ TokenClient* Token::CreateClient(ISocketHost* host)
|
||||||
client.Controls.Add(&ctrl);
|
client.Controls.Add(&ctrl);
|
||||||
|
|
||||||
// 如果是TCP,需要再建立一个本地UDP
|
// 如果是TCP,需要再建立一个本地UDP
|
||||||
//if(tk->Protocol == ProtocolType::Tcp)
|
//if(tk->Protocol == NetType::Tcp)
|
||||||
{
|
{
|
||||||
// 建立一个监听内网的UDP Socket
|
// 建立一个监听内网的UDP Socket
|
||||||
socket = host->CreateSocket(ProtocolType::Udp);
|
socket = host->CreateSocket(NetType::Udp);
|
||||||
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
|
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
|
||||||
socket->Remote.Address = IPAddress::Broadcast();
|
socket->Remote.Address = IPAddress::Broadcast();
|
||||||
socket->Local.Port = tk->Port;
|
socket->Local.Port = tk->Port;
|
||||||
|
|
|
@ -359,15 +359,15 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
|
||||||
if(!(msg.ErrCode == 0xFE || msg.ErrCode == 0xFD)) return false;
|
if(!(msg.ErrCode == 0xFE || msg.ErrCode == 0xFD)) return false;
|
||||||
|
|
||||||
auto cfg = Cfg;
|
auto cfg = Cfg;
|
||||||
cfg->Protocol = (ProtocolType)msg.Protocol;
|
cfg->Protocol = msg.Uri.Type;
|
||||||
|
|
||||||
cfg->Show();
|
cfg->Show();
|
||||||
|
|
||||||
cfg->Server() = msg.Server;
|
cfg->Server() = msg.Uri.Host;
|
||||||
cfg->ServerPort = msg.Port;
|
cfg->ServerPort = msg.Uri.Port;
|
||||||
cfg->Token() = msg.VisitToken;
|
cfg->Token() = msg.VisitToken;
|
||||||
|
|
||||||
ChangeIPEndPoint(msg.Server, msg.Port);
|
ChangeIPEndPoint(msg.Uri);
|
||||||
|
|
||||||
cfg->Show();
|
cfg->Show();
|
||||||
|
|
||||||
|
@ -381,21 +381,21 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TokenClient::ChangeIPEndPoint(const String& domain, ushort port)
|
bool TokenClient::ChangeIPEndPoint(const NetUri& uri)
|
||||||
{
|
{
|
||||||
TS("TokenClient::ChangeIPEndPoint");
|
TS("TokenClient::ChangeIPEndPoint");
|
||||||
|
|
||||||
debug_printf("ChangeIPEndPoint ");
|
debug_printf("ChangeIPEndPoint ");
|
||||||
|
|
||||||
domain.Show(true);
|
uri.Show(true);
|
||||||
|
|
||||||
auto& ctrl = *(TokenController*)Controls[0];
|
auto& ctrl = *(TokenController*)Controls[0];
|
||||||
auto socket = ctrl.Socket;
|
auto socket = ctrl.Socket;
|
||||||
if(socket == nullptr) return false;
|
if(socket == nullptr) return false;
|
||||||
|
|
||||||
ctrl.Port->Close();
|
ctrl.Port->Close();
|
||||||
socket->Remote.Port = port;
|
socket->Remote.Port = uri.Port;
|
||||||
socket->Server = domain;
|
socket->Server = uri.Host;
|
||||||
|
|
||||||
Cfg->ServerIP = socket->Remote.Address.Value;
|
Cfg->ServerIP = socket->Remote.Address.Value;
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ private:
|
||||||
bool OnLocalLogin(TokenMessage& msg,TokenController* ctrl);
|
bool OnLocalLogin(TokenMessage& msg,TokenController* ctrl);
|
||||||
|
|
||||||
bool OnPing(TokenMessage& msg, TokenController* ctrl);
|
bool OnPing(TokenMessage& msg, TokenController* ctrl);
|
||||||
bool ChangeIPEndPoint(const String& domain, ushort port);
|
bool ChangeIPEndPoint(const NetUri& uri);
|
||||||
|
|
||||||
void OnRead(const TokenMessage& msg, TokenController* ctrl);
|
void OnRead(const TokenMessage& msg, TokenController* ctrl);
|
||||||
void OnWrite(const TokenMessage& msg, TokenController* ctrl);
|
void OnWrite(const TokenMessage& msg, TokenController* ctrl);
|
||||||
|
|
|
@ -24,7 +24,7 @@ void TokenConfig::Init()
|
||||||
//User[16] = '\0';
|
//User[16] = '\0';
|
||||||
//Key[15] = '\0';
|
//Key[15] = '\0';
|
||||||
|
|
||||||
Protocol = ProtocolType::Udp;
|
Protocol = NetType::Udp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TokenConfig::Show() const
|
void TokenConfig::Show() const
|
||||||
|
@ -32,12 +32,12 @@ void TokenConfig::Show() const
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
debug_printf("TokenConfig::令牌配置:\r\n");
|
debug_printf("TokenConfig::令牌配置:\r\n");
|
||||||
|
|
||||||
debug_printf("\t协议: %s ,%d\r\n", Protocol == ProtocolType::Udp ? "UDP" : Protocol == ProtocolType::Tcp ? "TCP" : "", Protocol);
|
//debug_printf("\t协议: %s ,%d\r\n", Protocol == NetType::Udp ? "UDP" : Protocol == NetType::Tcp ? "TCP" : "", Protocol);
|
||||||
debug_printf("\t端口: %d \r\n", Port);
|
debug_printf("\t端口: %d \r\n", Port);
|
||||||
|
|
||||||
debug_printf("\t远程: ");
|
debug_printf("\t远程: ");
|
||||||
IPEndPoint ep2(IPAddress(ServerIP), ServerPort);
|
NetUri uri(Protocol, IPAddress(ServerIP), ServerPort);
|
||||||
ep2.Show(true);
|
uri.Show(true);
|
||||||
debug_printf("\t服务: %s \r\n", _Server);
|
debug_printf("\t服务: %s \r\n", _Server);
|
||||||
debug_printf("\t厂商: %s \r\n", _Vendor);
|
debug_printf("\t厂商: %s \r\n", _Vendor);
|
||||||
debug_printf("\t登录: %s \r\n", _User);
|
debug_printf("\t登录: %s \r\n", _User);
|
||||||
|
@ -45,7 +45,7 @@ void TokenConfig::Show() const
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenConfig* TokenConfig::Create(cstring vendor, ProtocolType protocol, ushort sport, ushort port)
|
TokenConfig* TokenConfig::Create(cstring vendor, NetType protocol, ushort sport, ushort port)
|
||||||
{
|
{
|
||||||
static TokenConfig tc;
|
static TokenConfig tc;
|
||||||
if(!Current)
|
if(!Current)
|
||||||
|
@ -56,7 +56,7 @@ TokenConfig* TokenConfig::Create(cstring vendor, ProtocolType protocol, ushort s
|
||||||
tc.Load();
|
tc.Load();
|
||||||
|
|
||||||
// 默认 UDP 不允许 unknown
|
// 默认 UDP 不允许 unknown
|
||||||
if(tc.Protocol == 0x00) tc.Protocol = ProtocolType::Udp;
|
if(tc.Protocol == NetType::Unknown) tc.Protocol = NetType::Udp;
|
||||||
|
|
||||||
bool rs = tc.New;
|
bool rs = tc.New;
|
||||||
auto vnd = tc.Vendor();
|
auto vnd = tc.Vendor();
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
#ifndef __TokenConfig_H__
|
#ifndef __TokenConfig_H__
|
||||||
#define __TokenConfig_H__
|
#define __TokenConfig_H__
|
||||||
|
|
||||||
#include "Sys.h"
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Net\IPAddress.h"
|
#include "Net\NetUri.h"
|
||||||
|
|
||||||
// 必须设定为1字节对齐,否则offsetof会得到错误的位置
|
// 必须设定为1字节对齐,否则offsetof会得到错误的位置
|
||||||
//#pragma pack(push) // 保存对齐状态
|
//#pragma pack(push) // 保存对齐状态
|
||||||
|
@ -22,7 +21,7 @@ public:
|
||||||
ushort SoftVer; // 软件版本
|
ushort SoftVer; // 软件版本
|
||||||
|
|
||||||
byte PingTime; // 心跳时间。秒
|
byte PingTime; // 心跳时间。秒
|
||||||
ProtocolType Protocol; // 协议,TCP=6/UDP=17
|
NetType Protocol; // 协议,TCP=6/UDP=17
|
||||||
ushort Port; // 本地端口
|
ushort Port; // 本地端口
|
||||||
uint ServerIP; // 服务器IP地址。服务器域名解析成功后覆盖
|
uint ServerIP; // 服务器IP地址。服务器域名解析成功后覆盖
|
||||||
ushort ServerPort; // 服务器端口
|
ushort ServerPort; // 服务器端口
|
||||||
|
@ -43,7 +42,7 @@ public:
|
||||||
String Vendor() { return String(_Vendor, sizeof(_Vendor), false); }
|
String Vendor() { return String(_Vendor, sizeof(_Vendor), false); }
|
||||||
|
|
||||||
static TokenConfig* Current;
|
static TokenConfig* Current;
|
||||||
static TokenConfig* Create(cstring vendor, ProtocolType protocol, ushort sport, ushort port);
|
static TokenConfig* Create(cstring vendor, NetType protocol, ushort sport, ushort port);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue