少用指针

Tcp/Udp独立管理自己的端口和远程地址
This commit is contained in:
nnhy 2015-06-22 08:23:33 +00:00
parent b5e53811b3
commit b03a44bec5
8 changed files with 27 additions and 22 deletions

View File

@ -47,7 +47,9 @@ void Dhcp::SendDhcp(DHCP_HEADER* dhcp, uint len)
len = (byte*)opt + 1 - p;
}
memcpy(dhcp->ClientMac, (byte*)&Tip->Mac.Value, 6);
//memcpy(dhcp->ClientMac, (byte*)&Tip->Mac.Value, 6);
for(int i=0; i<6; i++)
dhcp->ClientMac[i] = Tip->Mac[i];
RemoteIP = IPAddress::Broadcast;

View File

@ -78,7 +78,7 @@ bool PingCallback(TinyIP* tip, void* param, Stream& ms)
}
// Ping目的地址附带a~z重复的负载数据
bool IcmpSocket::Ping(IPAddress ip, uint payloadLength)
bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
{
byte buf[sizeof(ETH_HEADER) + sizeof(IP_HEADER) + sizeof(ICMP_HEADER) + 64];
uint bufSize = ArrayLength(buf);

View File

@ -20,7 +20,7 @@ public:
PingHandler OnPing;
// Ping目的地址附带a~z重复的负载数据
bool Ping(IPAddress ip, uint payloadLength = 32);
bool Ping(IPAddress& ip, uint payloadLength = 32);
};
#endif

View File

@ -8,7 +8,7 @@ bool Callback(TinyIP* tip, void* param, Stream& ms);
TcpSocket::TcpSocket(TinyIP* tip) : Socket(tip)
{
Type = IP_TCP;
Type = IP_TCP;
Port = 0;
RemoteIP = 0;
@ -398,7 +398,7 @@ void TcpSocket::Send(const byte* buf, uint len)
}
// 连接远程服务器记录远程服务器IP和端口后续发送数据和关闭连接需要
bool TcpSocket::Connect(IPAddress ip, ushort port)
bool TcpSocket::Connect(IPAddress& ip, ushort port)
{
if(ip.IsAny() || port == 0) return false;

View File

@ -7,6 +7,7 @@
class TcpSocket : public Socket, public ITransport
{
private:
//byte seqnum;
uint Seq; // 序列号,本地发出数据包
uint Ack; // 确认号,对方发送数据包的序列号+1
@ -22,6 +23,12 @@ public:
Established = 3,
}TCP_STATUS;
ushort Port; // 本地端口接收该端口数据包。0表示接收所有端口的数据包
ushort BindPort; // 绑定端口用于发出数据包的源端口。默认为Port若Port为0则从1024算起累加
IPAddress RemoteIP; // 远程地址
ushort RemotePort; // 远程端口
IPAddress LocalIP; // 本地IP地址
ushort LocalPort; // 本地端口,收到数据包的目的端口
TCP_STATUS Status; // 状态
TCP_HEADER* Header;
@ -31,7 +38,7 @@ public:
// 处理数据包
virtual bool Process(IP_HEADER& ip, Stream& ms);
bool Connect(IPAddress ip, ushort port); // 连接远程服务器记录远程服务器IP和端口后续发送数据和关闭连接需要
bool Connect(IPAddress& ip, ushort port); // 连接远程服务器记录远程服务器IP和端口后续发送数据和关闭连接需要
void Send(const byte* buf, uint len); // 向Socket发送数据可能是外部数据包
void Disconnect(); // 关闭Socket

View File

@ -15,19 +15,9 @@ class TinyIP;
class Socket
{
public:
TinyIP* Tip; // TinyIP控制器
IP_TYPE Type; // 类型
bool Enable; // 启用
IP_HEADER* IPHeader;
ushort Port; // 本地端口接收该端口数据包。0表示接收所有端口的数据包
ushort BindPort; // 绑定端口用于发出数据包的源端口。默认为Port若Port为0则从1024算起累加
IPAddress RemoteIP; // 远程地址。当前数据包
ushort RemotePort; // 远程端口。当前数据包
IPAddress LocalIP; // 本地IP地址。当前数据包
ushort LocalPort; // 本地端口,收到数据包的目的端口
TinyIP* Tip; // TinyIP控制器
IP_TYPE Type; // 类型
bool Enable; // 启用
Socket(TinyIP* tip);
virtual ~Socket();

View File

@ -4,7 +4,7 @@
UdpSocket::UdpSocket(TinyIP* tip) : Socket(tip)
{
Type = IP_UDP;
Type = IP_UDP;
Port = 0;
RemoteIP = 0;
RemotePort = 0;
@ -151,7 +151,7 @@ UDP_HEADER* UdpSocket::Create()
}
// 发送UDP数据到目标地址
void UdpSocket::Send(const byte* buf, uint len, IPAddress ip, ushort port)
void UdpSocket::Send(const byte* buf, uint len, IPAddress& ip, ushort port)
{
if(!ip.IsAny())
{

View File

@ -10,6 +10,12 @@ private:
UDP_HEADER* Create();
public:
ushort Port; // 本地端口接收该端口数据包。0表示接收所有端口的数据包
ushort BindPort; // 绑定端口用于发出数据包的源端口。默认为Port若Port为0则从1024算起累加
IPAddress RemoteIP; // 远程地址
ushort RemotePort; // 远程端口
IPAddress LocalIP; // 本地IP地址
ushort LocalPort; // 本地端口,收到数据包的目的端口
UdpSocket(TinyIP* tip);
@ -21,7 +27,7 @@ public:
UdpHandler OnReceived;
// 发送UDP数据到目标地址
void Send(const byte* buf, uint len, IPAddress ip = 0, ushort port = 0);
void Send(const byte* buf, uint len, IPAddress& ip = IPAddress::Any, ushort port = 0);
virtual string ToString();