全面使用NetType

This commit is contained in:
Stone 2016-06-28 12:47:12 +00:00
parent 841ca272fa
commit 1d767a00d7
30 changed files with 88 additions and 85 deletions

View File

@ -193,7 +193,7 @@ void AP0104::OpenClient()
AddControl(*Host, *tk);
TokenConfig cfg;
cfg.Protocol = ProtocolType::Udp;
cfg.Protocol = NetType::Udp;
cfg.ServerIP = IPAddress::Broadcast().Value;
cfg.ServerPort = 3355;
AddControl(*Host, cfg);

View File

@ -183,7 +183,7 @@ void AP0801::OpenClient()
AddControl(*Host, *tk);
TokenConfig cfg;
cfg.Protocol = ProtocolType::Udp;
cfg.Protocol = NetType::Udp;
cfg.ServerIP = IPAddress::Broadcast().Value;
cfg.ServerPort = 3355;
AddControl(*Host, cfg);

View File

@ -117,7 +117,7 @@ void IOK027X::OpenClient()
AddControl(*Host, *tk);
TokenConfig cfg;
cfg.Protocol = ProtocolType::Udp;
cfg.Protocol = NetType::Udp;
cfg.ServerIP = IPAddress::Broadcast().Value;
cfg.ServerPort = 3355;
AddControl(*Host, cfg);

View File

@ -173,10 +173,10 @@ TokenClient* PA0903::CreateClient()
client->Cfg = tk;
// 如果是TCP需要再建立一个本地UDP
//if(tk->Protocol == ProtocolType::Tcp)
//if(tk->Protocol == NetType::Tcp)
{
// 建立一个监听内网的UDP Socket
socket = Host->CreateSocket(ProtocolType::Udp);
socket = Host->CreateSocket(NetType::Udp);
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
socket->Remote.Address = IPAddress::Broadcast();
socket->Local.Port = tk->Port;

View File

@ -231,7 +231,7 @@ void Esp8266::Config()
SetAutoConn(AutoConn);
}
ISocket* Esp8266::CreateSocket(ProtocolType type)
ISocket* Esp8266::CreateSocket(NetType type)
{
auto es = (EspSocket**)_sockets;
@ -249,10 +249,10 @@ ISocket* Esp8266::CreateSocket(ProtocolType type)
switch(type)
{
case ProtocolType::Tcp:
case NetType::Tcp:
return es[i] = new EspTcp(*this, i);
case ProtocolType::Udp:
case NetType::Udp:
return es[i] = new EspUdp(*this, i);
default:

View File

@ -24,7 +24,7 @@ public:
virtual void Config();
//virtual const String ToString() const { return String("Esp8266"); }
virtual ISocket* CreateSocket(ProtocolType type);
virtual ISocket* CreateSocket(NetType type);
// 启用DNS
virtual bool EnableDNS();
// 启用DHCP

View File

@ -10,7 +10,7 @@
/******************************** Socket ********************************/
EspSocket::EspSocket(Esp8266& host, ProtocolType protocol, byte idx)
EspSocket::EspSocket(Esp8266& host, NetType protocol, byte idx)
: _Host(host)
{
_Index = idx;
@ -43,7 +43,7 @@ bool EspSocket::OnOpen()
_Host.SetMux(true);
#if NET_DEBUG
net_printf("%s::Open ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
net_printf("%s::Open ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
Local.Show(false);
net_printf(" => ");
if(Server)
@ -58,9 +58,9 @@ bool EspSocket::OnOpen()
String cmd = "AT+CIPSTART=";
cmd = cmd + _Index + ",";
if(Protocol == ProtocolType::Udp)
if(Protocol == NetType::Udp)
cmd += "\"UDP\"";
else if(Protocol == ProtocolType::Tcp)
else if(Protocol == NetType::Tcp)
cmd += "\"TCP\"";
auto rm = Server;
@ -81,7 +81,7 @@ bool EspSocket::OnOpen()
auto rt = _Host.Send(cmd + "\r\n", "OK", "ERROR", 10000);
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;
}

View File

@ -11,7 +11,7 @@ protected:
int _Error;
public:
EspSocket(Esp8266& host, ProtocolType protocol, byte idx);
EspSocket(Esp8266& host, NetType protocol, byte idx);
virtual ~EspSocket();
// 打开Socket

View File

@ -3,7 +3,7 @@
/******************************** Tcp ********************************/
EspTcp::EspTcp(Esp8266& host, byte idx)
: EspSocket(host, ProtocolType::Tcp, idx)
: EspSocket(host, NetType::Tcp, idx)
{
}

View File

@ -3,7 +3,7 @@
/******************************** Udp ********************************/
EspUdp::EspUdp(Esp8266& host, byte idx)
: EspSocket(host, ProtocolType::Udp, idx)
: EspSocket(host, NetType::Udp, idx)
{
}

View File

@ -632,14 +632,14 @@ byte W5500::GetSocket()
return 0xFF;
}
ISocket* W5500::CreateSocket(ProtocolType type)
ISocket* W5500::CreateSocket(NetType type)
{
switch(type)
{
case ProtocolType::Tcp:
case NetType::Tcp:
return new TcpClient(*this);
case ProtocolType::Udp:
case NetType::Udp:
return new UdpClient(*this);
default:
@ -920,7 +920,7 @@ enum S_Status
#define SocRegWrites(P, D) _Host.WriteFrame(offsetof(TSocket, P), D, 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;
@ -1071,7 +1071,7 @@ bool HardSocket::OnOpen()
Local.Address = _Host.IP;
#if DEBUG
debug_printf("%s::Open ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
debug_printf("%s::Open ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
Local.Show(false);
debug_printf(" => ");
Server.Show(false);
@ -1081,9 +1081,9 @@ bool HardSocket::OnOpen()
// 设置分片长度参考W5500数据手册该值可以不修改
// 默认值udp 1472 tcp 1460 其他类型不管他 有默认不设置也没啥
if(Protocol == ProtocolType::Udp)
if(Protocol == NetType::Udp)
SocRegWrite2(MSSR, 1472);
else if(Protocol == ProtocolType::Tcp)
else if(Protocol == NetType::Tcp)
SocRegWrite2(MSSR, 1460);
// 设置自己的端口号
@ -1097,9 +1097,9 @@ bool HardSocket::OnOpen()
// 设置Socket为UDP模式
S_Mode mode;
mode.Init();
if(Protocol == ProtocolType::Tcp)
if(Protocol == NetType::Tcp)
mode.Protocol = 0x01;
if(Protocol == ProtocolType::Udp)
if(Protocol == NetType::Udp)
mode.Protocol = 0x02;
//if(Protocol == 0x02) mode.MULTI_MFEN = 1;
SocRegWrite(MR, mode.ToByte());
@ -1122,8 +1122,8 @@ bool HardSocket::OnOpen()
while(!tw.Expired())
{
sr = ReadStatus();
if((Protocol == ProtocolType::Tcp && sr == SOCK_INIT)
|| (Protocol == ProtocolType::Udp && sr == SOCK_UDP))
if((Protocol == NetType::Tcp && sr == SOCK_INIT)
|| (Protocol == NetType::Udp && sr == SOCK_UDP))
{
rs = true;
break;
@ -1131,7 +1131,7 @@ bool HardSocket::OnOpen()
}
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();
return false;
@ -1145,7 +1145,7 @@ void HardSocket::OnClose()
WriteConfig(CLOSE);
WriteInterrupt(0xFF);
debug_printf("%s::Close ", Protocol == ProtocolType::Tcp ? "Tcp" : "Udp");
debug_printf("%s::Close ", Protocol == NetType::Tcp ? "Tcp" : "Udp");
Local.Show(false);
debug_printf(" => ");
Remote.Show(true);

View File

@ -52,7 +52,7 @@ public:
cstring ToString() const { return "W5500"; }
virtual ISocket* CreateSocket(ProtocolType type);
virtual ISocket* CreateSocket(NetType type);
// DNS解析。默认仅支持字符串IP地址解析
virtual IPAddress QueryDNS(const String& domain);
@ -114,7 +114,7 @@ public:
bool Enable; // 启用
byte Index; // 使用的硬Socket编号 也是BSB选项的一部分
HardSocket(W5500& host, ProtocolType protocol);
HardSocket(W5500& host, NetType protocol);
virtual ~HardSocket();
// 网卡状态输出
@ -152,7 +152,7 @@ public:
class TcpClient : public HardSocket
{
public:
TcpClient(W5500& host): HardSocket(host, ProtocolType::Tcp){ Init(); };
TcpClient(W5500& host): HardSocket(host, NetType::Tcp){ Init(); };
void Init();
virtual ~TcpClient();
virtual bool OnOpen();
@ -179,7 +179,7 @@ private:
class UdpClient : public HardSocket
{
public:
UdpClient(W5500& host) : HardSocket(host, ProtocolType::Udp) { }
UdpClient(W5500& host) : HardSocket(host, NetType::Udp) { }
virtual bool SendTo(const Buffer& bs, const IPEndPoint& remote);

View File

@ -358,7 +358,7 @@ short dns_makequery(short op, const String& name, Buffer& bs)
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.Address = !dns.IsAny() ? dns : host.DNSServer;

View File

@ -16,7 +16,7 @@
Dhcp::Dhcp(ISocketHost& host) : Host(host)
{
Socket = host.CreateSocket(ProtocolType::Udp);
Socket = host.CreateSocket(NetType::Udp);
Socket->Local.Port = 68;
Socket->Remote.Port = 67;

View File

@ -242,7 +242,7 @@ typedef struct _ICMP_HEADER
typedef struct _ARP_HEADER
{
ushort HardType; // 硬件类型
ushort ProtocolType; // 协议类型
ushort NetType; // 协议类型
byte HardLength; // 硬件地址长度
byte ProtocolLength; // 协议地址长度
ushort Option; // 选项
@ -255,7 +255,7 @@ typedef struct _ARP_HEADER
void Init(bool recursion = false)
{
HardType = 0x0100;
ProtocolType = 0x08;
NetType = 0x08;
HardLength = 6;
ProtocolLength = 4;

View File

@ -3,8 +3,6 @@
#include "IPAddress.h"
#include "IPEndPoint.h"
#include "MacAddress.h"
#include "Delegate.h"
// 协议类型
enum class NetType

View File

@ -101,7 +101,7 @@ bool ISocketHost::SaveConfig()
nc.IP = IP.Value;
nc.Mask = Mask.Value;
Mac.CopyTo(nc.Mac);
nc.Mode = Mode;
nc.Mode = (byte)Mode;
nc.DHCPServer = DHCPServer.Value;
nc.DNSServer = DNSServer.Value;

View File

@ -69,7 +69,7 @@ class ISocket
{
public:
ISocketHost* Host; // 主机
ProtocolType Protocol; // 协议类型
NetType Protocol; // 协议类型
IPEndPoint Local; // 本地地址。包含本地局域网IP地址实际监听的端口从1024开始累加
IPEndPoint Remote; // 远程地址

View File

@ -86,7 +86,7 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
#if NET_DEBUG
// 数据校验
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->ProtocolLength == 4);
//assert_param(arp->Option == 0x0100);

View File

@ -11,7 +11,7 @@ TcpSocket::TcpSocket(TinyIP* tip) : TinySocket(tip, IP_TCP)
{
MaxSize = 1500;
Host = tip;
Protocol = ProtocolType::Tcp;
Protocol = NetType::Tcp;
// 累加端口
static ushort g_tcp_port = 1024;

View File

@ -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);
}
ISocket* TinyIP::CreateSocket(ProtocolType type)
ISocket* TinyIP::CreateSocket(NetType type)
{
switch(type)
{
case ProtocolType::Tcp:
case NetType::Tcp:
return new TcpSocket(this);
case ProtocolType::Udp:
case NetType::Udp:
return new UdpSocket(this);
default:

View File

@ -66,7 +66,7 @@ public:
bool SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len);
bool IsBroadcast(const IPAddress& ip); // 是否广播地址
virtual ISocket* CreateSocket(ProtocolType type);
virtual ISocket* CreateSocket(NetType type);
};
/*

View File

@ -7,7 +7,7 @@ UdpSocket::UdpSocket(TinyIP* tip) : TinySocket(tip, IP_UDP)
{
MaxSize = 1500;
Host = tip;
Protocol = ProtocolType::Udp;
Protocol = NetType::Udp;
// 累加端口
static ushort g_udp_port = 1024;

View File

@ -17,8 +17,9 @@ HelloMessage::HelloMessage() : Cipher(1), Key(0)
LocalTime = DateTime::Now().TotalMs();
Cipher[0] = 1;
Protocol = 17;
Port = 0;
//Protocol = 17;
//Port = 0;
Uri.Type = NetType::Udp;
}
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());
Key.Copy(0, msg.Key, 0, msg.Key.Length());
Protocol = msg.Protocol;
Port = msg.Port;
Server = msg.Server;
//Protocol = msg.Protocol;
//Port = msg.Port;
//Server = msg.Server;
Uri = msg.Uri;
}
// 从数据流中读取消息
@ -52,12 +54,14 @@ bool HelloMessage::Read(Stream& ms)
Stream urims(uri);
BinaryPair uribp(urims);
uribp.Get("Type", Protocol); // 服务店 ProtocolType 17 为UDP
if (Protocol == 0x00) Protocol = 0x11; // 避免 unknown 出现
uribp.Get("Host", Server);
byte type;
uribp.Get("Type", type); // 服务店 NetType 17 为UDP
Uri.Type = (NetType)type;
if (Uri.Type == NetType::Unknown) Uri.Type = NetType::Udp; // 避免 unknown 出现
uribp.Get("Host", Uri.Host);
uint uintPort; // 服务端 Port 为 int 类型
uribp.Get("Port", uintPort);
Port = uintPort ;
Uri.Port = uintPort ;
}
bp.Get("VisitToken", VisitToken);
@ -118,12 +122,13 @@ String& HelloMessage::ToStr(String& str) const
//str += ' ';
str += (ErrCode == 0xFD) ? " 临时跳转 " : " 永久跳转 ";
if(Protocol == ProtocolType::Tcp)
/*if(Protocol == NetType::Tcp)
str += "Tcp://";
else if(Protocol == ProtocolType::Udp)
else if(Protocol == NetType::Udp)
str += "Udp://";
str = str + Server + ":" + Port;
str = str + Server + ":" + Port;*/
str += Uri;
}
else
{

View File

@ -2,7 +2,7 @@
#define __HelloMessage_H__
#include "Message\MessageBase.h"
#include "Net\Socket.h"
#include "Net\NetUri.h"
// 握手消息
// 请求2版本 + S类型 + S名称 + 8本地时间 + 6本地IP端口 + S支持加密算法列表
@ -24,8 +24,9 @@ public:
String ErrMsg; // 错误信息
byte Protocol; // 协议,17为UDP 6为TCP
String Server; // 服务器地址。可能是域名或IP
ushort Port; // 本地端口
//String Server; // 服务器地址。可能是域名或IP
//ushort Port; // 本地端口
NetUri Uri;
String VisitToken; //访问令牌
// 初始化消息各字段为0

View File

@ -111,10 +111,10 @@ TokenClient* Token::CreateClient(ISocketHost* host)
client.Controls.Add(&ctrl);
// 如果是TCP需要再建立一个本地UDP
//if(tk->Protocol == ProtocolType::Tcp)
//if(tk->Protocol == NetType::Tcp)
{
// 建立一个监听内网的UDP Socket
socket = host->CreateSocket(ProtocolType::Udp);
socket = host->CreateSocket(NetType::Udp);
socket->Remote.Port = 3355; // 广播端口。其实用哪一个都不重要,因为不会主动广播
socket->Remote.Address = IPAddress::Broadcast();
socket->Local.Port = tk->Port;

View File

@ -359,15 +359,15 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
if(!(msg.ErrCode == 0xFE || msg.ErrCode == 0xFD)) return false;
auto cfg = Cfg;
cfg->Protocol = (ProtocolType)msg.Protocol;
cfg->Protocol = msg.Uri.Type;
cfg->Show();
cfg->Server() = msg.Server;
cfg->ServerPort = msg.Port;
cfg->Server() = msg.Uri.Host;
cfg->ServerPort = msg.Uri.Port;
cfg->Token() = msg.VisitToken;
ChangeIPEndPoint(msg.Server, msg.Port);
ChangeIPEndPoint(msg.Uri);
cfg->Show();
@ -381,21 +381,21 @@ bool TokenClient::OnRedirect(HelloMessage& msg)
return true;
}
bool TokenClient::ChangeIPEndPoint(const String& domain, ushort port)
bool TokenClient::ChangeIPEndPoint(const NetUri& uri)
{
TS("TokenClient::ChangeIPEndPoint");
debug_printf("ChangeIPEndPoint ");
domain.Show(true);
uri.Show(true);
auto& ctrl = *(TokenController*)Controls[0];
auto socket = ctrl.Socket;
if(socket == nullptr) return false;
ctrl.Port->Close();
socket->Remote.Port = port;
socket->Server = domain;
socket->Remote.Port = uri.Port;
socket->Server = uri.Host;
Cfg->ServerIP = socket->Remote.Address.Value;

View File

@ -89,7 +89,7 @@ private:
bool OnLocalLogin(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 OnWrite(const TokenMessage& msg, TokenController* ctrl);

View File

@ -24,7 +24,7 @@ void TokenConfig::Init()
//User[16] = '\0';
//Key[15] = '\0';
Protocol = ProtocolType::Udp;
Protocol = NetType::Udp;
}
void TokenConfig::Show() const
@ -32,12 +32,12 @@ void TokenConfig::Show() const
#if DEBUG
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远程: ");
IPEndPoint ep2(IPAddress(ServerIP), ServerPort);
ep2.Show(true);
NetUri uri(Protocol, IPAddress(ServerIP), ServerPort);
uri.Show(true);
debug_printf("\t服务: %s \r\n", _Server);
debug_printf("\t厂商: %s \r\n", _Vendor);
debug_printf("\t登录: %s \r\n", _User);
@ -45,7 +45,7 @@ void TokenConfig::Show() const
#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;
if(!Current)
@ -56,7 +56,7 @@ TokenConfig* TokenConfig::Create(cstring vendor, ProtocolType protocol, ushort s
tc.Load();
// 默认 UDP 不允许 unknown
if(tc.Protocol == 0x00) tc.Protocol = ProtocolType::Udp;
if(tc.Protocol == NetType::Unknown) tc.Protocol = NetType::Udp;
bool rs = tc.New;
auto vnd = tc.Vendor();

View File

@ -1,9 +1,8 @@
#ifndef __TokenConfig_H__
#define __TokenConfig_H__
#include "Sys.h"
#include "Config.h"
#include "Net\IPAddress.h"
#include "Net\NetUri.h"
// 必须设定为1字节对齐否则offsetof会得到错误的位置
//#pragma pack(push) // 保存对齐状态
@ -22,7 +21,7 @@ public:
ushort SoftVer; // 软件版本
byte PingTime; // 心跳时间。秒
ProtocolType Protocol; // 协议TCP=6/UDP=17
NetType Protocol; // 协议TCP=6/UDP=17
ushort Port; // 本地端口
uint ServerIP; // 服务器IP地址。服务器域名解析成功后覆盖
ushort ServerPort; // 服务器端口
@ -43,7 +42,7 @@ public:
String Vendor() { return String(_Vendor, sizeof(_Vendor), false); }
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:
};