分为UdpServer类和UdpConn类。

struct UdpServer : public std::enable_shared_from_this<UdpServer>, private noncopyable {
UdpServer(EventBases* bases);
int bind(const std::string& host, unsigned short port, bool reusePort = false);
static UpdServerPtr startServer(EventBases* bases, const std::string& short, unsigned short port, bool reusePort = false);
~UdpServer() {
delete channel_;
}
Ip4Addr getAddr() {
return addr_;
}
EventBase* getBase() {
return base_;
}
void sendTo(Buffer msg, Ip4Addr addr) {
sendTo(msg.data(), msg.size(), addr);
msg.clear();
}
void sendTo(const char* buf, size_t len, Ip4Addr addr);
void sendTo(const std::string& s, Ip4Addr addr) {
sendTo(s.data(), s.size(), addr);
}
void sendTo(const char* s, Ip4Addr addr) {
sendTo(s, strlen(s), addr);
} void onMsg(const UdpSvrCallBack& cb) {
msgcb_ = cb;
} private:
EventBase* base_;
EventBases* bases_;
Ip4Addr addr_;
Channel* channel_;
UdpSvrCallBack msgcb_;
};

其中:

typedef std::shared_ptr<UdpConn> UdpConnPtr;
typedef std::shared_ptr<UdpServer> UdpServerPtr;
typedef std::function<void(const UdpConnPtr&, Buffer)> UdpCallBack;
typedef std::function<void(const UdpServerPtr&, Buffer, Ip4Addr)> UdpSvrCallBack;
struct UdpConn : public std::enable_shared_from_this<UdpConn>, private noncopyable {
UdpConn() {}
virtual ~UdpConn() { close(); }
static UdpConnPtr createConnection(EventBase* base, const std::string& host, unsigned short port); template <class T>
T& context() {
return ctx_.context<T>();
} EventBase* getBase() {
return base_;
} Channel* getChannel() {
return channel_;
} void send(Buffer msg) {
send(msg.data(), msg.size());
msg.clear();
}
void send(const char* buf, size_t len);
void send(const std::string& s) {
send(s.data(), s.size());
}
void send(const char* s) {
send(s, strlen(s));
}
void onMsg(const UdpCallBack& cb) {
cb_ = cb;
}
void close(); std::string str() {
return peer_.toString();
} public:
void handleRead(const UdpConnPtr&);
EventBase* base_;
Channel* channel_;
Ip4Addr local_, peer_;
AutoContext ctx_;
std::string destHost_;
int destPort_;
UdpCallback cb_;
};

下面看一下具体的使用方法,从中可以得知udp相关类的设计及实现的想法。

udp服务器部分:

int main(int argc, const char* argv[]) {
EventBase base;
Signal::signal(SIGINT, [&] { base.exit(); });
UdpServerPtr svr = UdpServer::startServer(&base, "", );
svr->onMsg([](const UdpServerPtr& p, Buffer buf, Ip4Addr peer) {
p->sendTo(buf, peer);
});
base.loop();
return ;
}

可以看出,在指定端口启动服务器后,回调函数被加入到事件循环中去,意为当收到客户端的消息时,调用onMsg设置的回调函数,相应的会调用sendto,向客户端发送一条消息。base.loop()的作用即为启动该事件循环,具体的为调用loop_once进而调用Poller的loop函数,进而会调用epoll_wait去监听套接字上的可读事件。

udp客户端部分:

int main(int argc, const char* argv[]) {
EventBase base;
Signal::signal(SIGINT, [&] { base.exit(); });
UdpConnPtr con = UdpConn::createConnection(&base, "127.0.0.1", );
con->onMsg([](const UdpConnPtr& p, Buffer buf) { info("udp recved %lu bytes", buf.size()); });
base.runAfter(, [=]() { con->send("hello"); }, );
base.loop();
return ;
}

在客户端部分,首先连接到服务器端口,然后设置回调函数,此处的base.loop()不光有上述的调用epoll函数的功能,还有定时调用runAfter中函数对象的功能。

总的过程为:启动服务端和客户端后,base.loop()进行事件循环,客户端的runAfter会定时向服务端发送消息,服务器端在收到该消息后,会调用onMsg设定的回调函数,该回调函数是向客户端发送消息,客户端在收到消息后,将其打印出来。

下面说一下如何将回调函数加入时间循环。

其实可以看一下Channel类的实现,Channel类内置了一个文件描述符,每创建一个新的Channel对象,都会将该文件描述符及其监听的事件类型加入到epoll事件循环中,调用对应事件类型的函数,只要在此函数中加入会回调函数的调用,就可以实现上述所说的功能。

下面是服务器和客户端的相应部分的实现(简化):

int UdpServer::bind(const std::string& host, unsigned short port, bool reusePort) {
addr_ = Ip4Addr(host, port);
::bind(fd, (struct sockaddr*)&addr_.getAddr(), sizeof(struct sockaddr));
channel_ = new Channel(base_, fd, kReadEvent); //创建Channel对象,将服务器端的套接字传入。
channel_->onRead([this] { //在epoll_wait读到读事件时,会调用onRead设置的函数。
Buffer buf;
struct sockaddr_in raddr;
socklen_t rsz = sizeof(raddr);
int fd = channel_->fd;
ssize_t rn = recvfrom(fd, buf.makeRoom(kUdpPacketSize), kUdpPacketSize, 0, (sockaddr*)&raddr, &rsz); //接收消息。
buf.addSize(rn);
this->msgcb_(shared_from_this(), buf, raddr); //此为回调函数的调用,在文件中可以看到msgcb_由onMsg设置,就可以实现由自己调用函数onMsg设置回调函数加入到事件循环进行调用。
});
}

客户端的部分实现(简化):

UdpConnPtr udpConn::createConnection(EventBase* base, const string& host, unsigned short port) {
Ip4Addr addr(host, port);
int fd = socket(AF_INET, SOCK_DGRAM, 0);
::connect(fd, (sockaddr*)&addr.getAddr(), sizeof(sockaddr_in));
UdpConnPtr con(new UdpConn);
Channel* ch = new Channel(base, fd, kReadEvent); //创建Channel对象,将客户端套接字传入。
conn->channel_ = ch;
ch->onRead([con] { //在epoll_wait读到读事件时,会调用onRead设置的函数。
Buffer input;
int fd = con->channel_->fd();
int rn = ::read(fd, input.makeRoom(kUdpPacketSize), kUdpPackeSize);
input.addSize(rn);
con->cb_(con, input); //回调函数的调用,头文件中可以看到cb_由onMsg设置。
});
}

handy源码阅读(六):udp类的更多相关文章

  1. handy源码阅读(六):tcp类

    首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...

  2. Spark常用函数(源码阅读六)

    源码层面整理下我们常用的操作RDD数据处理与分析的函数,从而能更好的应用于工作中. 连接Hbase,读取hbase的过程,首先代码如下: def tableInitByTime(sc : SparkC ...

  3. handy源码阅读(四):Channel类

    通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...

  4. handy源码阅读(三):SafeQueue类

    SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...

  5. handy源码阅读(二):EventsImp类

    EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...

  6. handy源码阅读(一):EventBase类

    类EventBase继承于类EventBases,继承于noncopyable.  其中noncopyable是一个去除了拷贝构造和赋值构造的类. noncopyable: class noncopy ...

  7. handy源码阅读(五):PollerBase类

    使用poll内核函数等待事件发生: struct PollerBase: private noncopyable { int64_t id_; int lastActive_; PollerBase( ...

  8. JDK源码阅读:Object类阅读笔记

    Object 1. @HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate public final native Class<?> g ...

  9. klee源码阅读笔记1--STPBuilder类

    初始化过程中四个数据成员中的两个数据成员被初始化: 一.vc被初始化为STP提供的C调用接口函数vc_createValidityChecker(): 二.optimizeDivides被初始化为fa ...

随机推荐

  1. python学习之文件读写操作

    open函数 在使用文件之前,需要先打开,即使用open函数 如: files=open("文件路径","操作方式") 解释如下: (1.files:为文件对象 ...

  2. 实体类的[Serializable]标签造成WebAPI Post接收不到值

    WebAPI: [HttpPost] public HttpResponseMessage test([FromBody]List<Class1> list) { return Commo ...

  3. 【MM系列】SAP 簇表 A017 物料信息记录 (指定工厂) 包含的透明表

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 簇表 A017 物料信息记录 ...

  4. 【MM系列】SAP MB5B中FI凭证摘要是激活的/结果可能不正确 的错误

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MB5B中FI凭证摘要是激活 ...

  5. 6.文件所有权和权限----免费设置匿名----Windows键盘记录器----简介和python模块

    文件所有权和权限 touch --help cd Desktop mkdir Folder cd Folder clear touch Test1 Test2 Test3 Test4 ls ls -l ...

  6. Git push “fatal: Authentication failed ”

    Git push "fatal: Authentication failed " 问题原因 之前设置了两步验证 If you enabled two-factor authenti ...

  7. 深入理解java:1.1.1. 反射机制

    反射 到底什么是反射(Reflection)呢? 反射有时候也被称为内省(Introspection),事实上,反射,就是一种内省的方式, Java不允许在运行时改变程序结构或类型变量的结构,但它允许 ...

  8. python 并发编程 多进程 队列

    队列介绍 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的 创建队列的类(底层就是以管道和锁定的方式实现) 制 ...

  9. JavaScript Return Object.Type

    var getType = function(obj) { if (obj == null) { return String(obj); } return typeof obj === 'object ...

  10. CentOS 8 下 nginx 服务器安装及配置笔记

    参考文档 nginx官方文档 安装 在CentOS下,nginx官方提供了安装包可以安装 首先先安装前置软件 sudo yum install yum-utils 然后将nginx官方源加入到yum源 ...