增加Tcp和Udp,尝试扩展

This commit is contained in:
Stone 2014-08-12 15:56:52 +00:00
parent ebc113ab9b
commit 6aa57b3006
7 changed files with 94 additions and 7 deletions

View File

@ -12,11 +12,13 @@ private:
void* _param; void* _param;
public: public:
virtual void Send(byte* buf, uint len) = 0; // 获取负载数据指针。外部可以直接填充数据
void Register(DataHandler handler, void* param = NULL); virtual byte* GetPayload() = 0;
virtual void Send(IP_TYPE type, uint len) = 0;
void Register(IP_TYPE type, DataHandler handler, void* param = NULL);
protected: protected:
virtual void OnReceive(byte* buf, uint len); virtual void OnReceive(byte* buf, uint len) = 0;
}; };
// 以太网 // 以太网

1
Net/Tcp.cpp Normal file
View File

@ -0,0 +1 @@
#include "Tcp.h"

18
Net/Tcp.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __Tcp_H__
#define __Tcp_H__
#include "Sys.h"
#include "Net.h"
#include "Ethernet.h"
// Tcp协议
class Tcp
{
private:
IEthernetAdapter* _adapter;
public:
Tcp(IEthernetAdapter* adapter);
};
#endif

28
Net/Udp.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "Udp.h"
Udp::Udp(IEthernetAdapter* adapter, ushort bufferSize)
{
_adapter = adapter;
_Buffer = new byte[bufferSize];
_header = (UDP_HEADER*)_Buffer;
}
Udp::Udp()
{
_adapter = NULL;
Register(NULL);
if(_Buffer) delete _Buffer;
_Buffer = NULL
}
// 向目标地址发送数据
void Udp::Send(byte* buf, uint len, byte[4] remoteip, ushort remoteport)
{
_adapter->Send(IP_UDP, buf, len);
}
void Udp::Listen()
{
}

35
Net/Udp.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __Udp_H__
#define __Udp_H__
#include "Sys.h"
#include "Net.h"
#include "Ethernet.h"
// UDP协议
class Udp
{
private:
IEthernetAdapter* adapter;
byte* _Buffer;
UDP_HEADER* _header;
public:
Udp(IEthernetAdapter* adapter, ushort bufferSize = 1400);
virtual ~Udp();
ushort Port; // 本地端口。0表示监听所有端口
void Listen();
// 向目标地址发送数据,数据必须提前填充到负载里面
void Send(uint len, byte[4] remoteip, ushort remoteport);
// 获取负载数据指针。外部可以直接填充数据
byte* GetPayload() { return _Buffer + sizeof(UDP_HEADER); }
// 注册回调函数
void Register(DatHandler* handler, void* param = NULL);
private:
DataHandler _handler;
void* _Param;
};
#endif

View File

@ -45,7 +45,7 @@ TinyIP::TinyIP(Enc28j60* enc, byte ip[4], byte mac[6])
seqnum = 0xa; seqnum = 0xa;
// 分配缓冲区。比较大,小心栈空间不够 // 分配缓冲区。比较大,小心栈空间不够
if(!Buffer) Buffer = new byte[BufferSize + 1]; if(!Buffer) Buffer = new byte[BufferSize];
assert_param(Buffer); assert_param(Buffer);
assert_param(Sys.CheckMemory()); assert_param(Sys.CheckMemory());
@ -816,6 +816,6 @@ void TinyIP::DHCPConfig(byte* buf)
} }
} }
void TinyIP::Send(byte* buf, uint len) void TinyIP::Send(IP_TYPE type, uint len)
{ {
} }

View File

@ -5,7 +5,7 @@
#include "Net/Ethernet.h" #include "Net/Ethernet.h"
// 精简IP类 // 精简IP类
class TinyIP //: protected IEthernetAdapter class TinyIP : protected IEthernetAdapter
{ {
private: private:
Enc28j60* _enc; Enc28j60* _enc;
@ -41,7 +41,10 @@ private:
void PareOption(byte* buf, int len); void PareOption(byte* buf, int len);
void DHCPConfig(byte* buf); void DHCPConfig(byte* buf);
virtual void Send(byte* buf, uint len); virtual void Send(IP_TYPE type, uint len);
// 获取负载数据指针。外部可以直接填充数据
virtual byte* GetPayload();
virtual void OnReceive(byte* buf, uint len);
public: public:
byte IP[4]; byte IP[4];
byte Mask[4]; byte Mask[4];