每次Tcp链接都递增本地端口
This commit is contained in:
parent
05592e75da
commit
947cedec8c
|
@ -1,6 +1,6 @@
|
|||
#include "Arp.h"
|
||||
|
||||
#define NET_DEBUG 0
|
||||
#define NET_DEBUG 1
|
||||
|
||||
class ArpSession
|
||||
{
|
||||
|
|
|
@ -17,11 +17,6 @@ TcpSocket::TcpSocket(TinyIP* tip) : Socket(tip)
|
|||
//LocalIP = 0;
|
||||
//LocalPort = 0;
|
||||
|
||||
// 累加端口
|
||||
static ushort g_tcp_port = 1024;
|
||||
if(g_tcp_port < 1024) g_tcp_port = 1024;
|
||||
BindPort = g_tcp_port++;
|
||||
|
||||
// 我们仅仅递增第二个字节,这将允许我们以256或者512字节来发包
|
||||
static uint seqnum = 0xa;
|
||||
Seq = seqnum << 8;
|
||||
|
@ -222,7 +217,7 @@ void TcpSocket::OnDataReceive(TCP_HEADER& tcp, uint len)
|
|||
else
|
||||
{
|
||||
#if NET_DEBUG
|
||||
debug_printf("Tcp Receive(%d) From ", len);
|
||||
debug_printf("Tcp:Receive(%d) From ", len);
|
||||
Remote.Show();
|
||||
debug_printf(" ");
|
||||
Sys.ShowString(tcp.Next(), len);
|
||||
|
@ -263,7 +258,7 @@ void TcpSocket::OnDisconnect(TCP_HEADER& tcp, uint len)
|
|||
Status = Closed;
|
||||
}
|
||||
|
||||
void TcpSocket::Send(TCP_HEADER& tcp, uint len, byte flags)
|
||||
bool TcpSocket::Send(TCP_HEADER& tcp, uint len, byte flags)
|
||||
{
|
||||
tcp.SrcPort = __REV16(Port > 0 ? Port : Local.Port);
|
||||
tcp.DestPort = __REV16(Remote.Port);
|
||||
|
@ -284,7 +279,7 @@ void TcpSocket::Send(TCP_HEADER& tcp, uint len, byte flags)
|
|||
#endif
|
||||
|
||||
// 注意tcp->Size()包括头部的扩展数据
|
||||
Tip->SendIP(IP_TCP, Remote.Address, (byte*)&tcp, tcp.Size() + len);
|
||||
return Tip->SendIP(IP_TCP, Remote.Address, (byte*)&tcp, tcp.Size() + len);
|
||||
}
|
||||
|
||||
void TcpSocket::SetSeqAck(TCP_HEADER& tcp, uint ackNum, bool opSeq)
|
||||
|
@ -339,7 +334,7 @@ void TcpSocket::SendAck(uint len)
|
|||
Send(*tcp, len, TCP_FLAGS_ACK | TCP_FLAGS_PUSH);
|
||||
}
|
||||
|
||||
void TcpSocket::Disconnect()
|
||||
bool TcpSocket::Disconnect()
|
||||
{
|
||||
debug_printf("Tcp::Disconnect ");
|
||||
Remote.Show();
|
||||
|
@ -350,20 +345,20 @@ void TcpSocket::Disconnect()
|
|||
|
||||
TCP_HEADER* tcp = ms.Retrieve<TCP_HEADER>();
|
||||
tcp->Init(true);
|
||||
Send(*tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN);
|
||||
return Send(*tcp, 0, TCP_FLAGS_ACK | TCP_FLAGS_PUSH | TCP_FLAGS_FIN);
|
||||
}
|
||||
|
||||
void TcpSocket::Send(ByteArray& bs)
|
||||
bool TcpSocket::Send(ByteArray& bs)
|
||||
{
|
||||
if(!Enable)
|
||||
{
|
||||
if(!Open()) return;
|
||||
if(!Open()) return false;
|
||||
}
|
||||
|
||||
// 如果连接已关闭,必须重新连接
|
||||
if(Status == Closed)
|
||||
{
|
||||
if(!Connect(Remote.Address, Remote.Port)) return;
|
||||
if(!Connect(Remote.Address, Remote.Port)) return false;
|
||||
}
|
||||
|
||||
debug_printf("Tcp::Send ");
|
||||
|
@ -387,7 +382,7 @@ void TcpSocket::Send(ByteArray& bs)
|
|||
tcp->Ack = __REV(Ack);
|
||||
// 发送数据的时候,需要同时带PUSH和ACK
|
||||
//debug_printf("Seq=0x%04x Ack=0x%04x \r\n", Seq, Ack);
|
||||
Send(*tcp, bs.Length(), TCP_FLAGS_PUSH | TCP_FLAGS_ACK);
|
||||
if(!Send(*tcp, bs.Length(), TCP_FLAGS_PUSH | TCP_FLAGS_ACK)) return false;
|
||||
|
||||
//Tip->LoopWait(Callback, this, 3000);
|
||||
|
||||
|
@ -407,6 +402,8 @@ void TcpSocket::Send(ByteArray& bs)
|
|||
debug_printf("发送成功!\r\n");
|
||||
else
|
||||
debug_printf("发送失败!\r\n");
|
||||
|
||||
return wait;
|
||||
}
|
||||
|
||||
// 连接远程服务器,记录远程服务器IP和端口,后续发送数据和关闭连接需要
|
||||
|
@ -419,7 +416,14 @@ bool TcpSocket::Connect(IPAddress& ip, ushort port)
|
|||
if(!Open()) return false;
|
||||
}
|
||||
|
||||
// 累加端口
|
||||
static ushort g_tcp_port = 1024;
|
||||
if(g_tcp_port < 1024) g_tcp_port = 1024;
|
||||
BindPort = g_tcp_port++;
|
||||
|
||||
debug_printf("Tcp::Connect ");
|
||||
Tip->IP.Show();
|
||||
debug_printf(":%d => ", BindPort);
|
||||
ip.Show();
|
||||
debug_printf(":%d ...... \r\n", port);
|
||||
|
||||
|
@ -438,7 +442,7 @@ bool TcpSocket::Connect(IPAddress& ip, ushort port)
|
|||
SetMss(*tcp);
|
||||
|
||||
Status = SynSent;
|
||||
Send(*tcp, 0, TCP_FLAGS_SYN);
|
||||
if(!Send(*tcp, 0, TCP_FLAGS_SYN)) return false;
|
||||
|
||||
//if(Tip->LoopWait(Callback, this, 3000))
|
||||
|
||||
|
@ -522,3 +526,10 @@ uint TcpSocket::OnRead(byte* buf, uint len)
|
|||
// 暂时不支持
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
三次握手过程:
|
||||
A=>B SYN
|
||||
B=>A SYN+ACK
|
||||
A=>B ACK
|
||||
*/
|
||||
|
|
|
@ -35,8 +35,8 @@ public:
|
|||
virtual bool Process(IP_HEADER& ip, Stream& ms);
|
||||
|
||||
bool Connect(IPAddress& ip, ushort port); // 连接远程服务器,记录远程服务器IP和端口,后续发送数据和关闭连接需要
|
||||
void Send(ByteArray& bs); // 向Socket发送数据,可能是外部数据包
|
||||
void Disconnect(); // 关闭Socket
|
||||
bool Send(ByteArray& bs); // 向Socket发送数据,可能是外部数据包
|
||||
bool Disconnect(); // 关闭Socket
|
||||
|
||||
// 收到Tcp数据时触发,传递结构体和负载数据长度。返回值指示是否向对方发送数据包
|
||||
typedef bool (*TcpHandler)(TcpSocket& socket, TCP_HEADER& tcp, byte* buf, uint len);
|
||||
|
@ -51,7 +51,7 @@ protected:
|
|||
|
||||
void SetSeqAck(TCP_HEADER& tcp, uint ackNum, bool cp_seq);
|
||||
void SetMss(TCP_HEADER& tcp);
|
||||
void Send(TCP_HEADER& tcp, uint len, byte flags);
|
||||
bool Send(TCP_HEADER& tcp, uint len, byte flags);
|
||||
|
||||
virtual void OnProcess(TCP_HEADER& tcp, Stream& ms);
|
||||
virtual void OnAccept(TCP_HEADER& tcp, uint len);
|
||||
|
|
|
@ -255,7 +255,7 @@ void TinyIP::ShowInfo()
|
|||
#endif
|
||||
}
|
||||
|
||||
void TinyIP::SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* buf, uint len)
|
||||
bool TinyIP::SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* buf, uint len)
|
||||
{
|
||||
ETH_HEADER* eth = (ETH_HEADER*)(buf - sizeof(ETH_HEADER));
|
||||
assert_param2(IS_ETH_TYPE(type), "这个不是以太网类型");
|
||||
|
@ -282,10 +282,10 @@ void TinyIP::SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* b
|
|||
/*Sys.ShowHex((byte*)eth->Next(), len, '-');
|
||||
debug_printf("\r\n");*/
|
||||
|
||||
_port->Write((byte*)eth, len);
|
||||
return _port->Write((byte*)eth, len);
|
||||
}
|
||||
|
||||
void TinyIP::SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len)
|
||||
bool TinyIP::SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len)
|
||||
{
|
||||
IP_HEADER* ip = (IP_HEADER*)(buf - sizeof(IP_HEADER));
|
||||
assert_param2(IS_IP_TYPE(type), "这个不是IP消息类型");
|
||||
|
@ -320,7 +320,7 @@ void TinyIP::SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint
|
|||
remote.Show();
|
||||
debug_printf("\r\n");
|
||||
#endif
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*string name = "Unkown";
|
||||
|
@ -337,7 +337,7 @@ void TinyIP::SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint
|
|||
remote.Show();
|
||||
debug_printf("\r\n");*/
|
||||
|
||||
SendEthernet(ETH_IP, mac, (byte*)ip, sizeof(IP_HEADER) + len);
|
||||
return SendEthernet(ETH_IP, mac, (byte*)ip, sizeof(IP_HEADER) + len);
|
||||
}
|
||||
|
||||
#define TinyIP_HELP
|
||||
|
|
|
@ -83,8 +83,8 @@ public:
|
|||
void ShowInfo();
|
||||
ushort CheckSum(IPAddress* remote, const byte* buf, uint len, byte type);
|
||||
|
||||
void SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* buf, uint len);
|
||||
void SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len);
|
||||
bool SendEthernet(ETH_TYPE type, const MacAddress& remote, const byte* buf, uint len);
|
||||
bool SendIP(IP_TYPE type, const IPAddress& remote, const byte* buf, uint len);
|
||||
bool IsBroadcast(const IPAddress& ip); // 是否广播地址
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue