handy源码阅读(六):udp类
分为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类的更多相关文章
- handy源码阅读(六):tcp类
首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...
- Spark常用函数(源码阅读六)
源码层面整理下我们常用的操作RDD数据处理与分析的函数,从而能更好的应用于工作中. 连接Hbase,读取hbase的过程,首先代码如下: def tableInitByTime(sc : SparkC ...
- handy源码阅读(四):Channel类
通道,封装了可以进行epoll的一个fd. struct Channel: private noncopyable { Channel(EventBase* base, int fd, int eve ...
- handy源码阅读(三):SafeQueue类
SafeQueue类继承与信号量mutex(用于加锁),nonocopyable 定义如下: template <typename T> struct SafeQueue : privat ...
- handy源码阅读(二):EventsImp类
EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...
- handy源码阅读(一):EventBase类
类EventBase继承于类EventBases,继承于noncopyable. 其中noncopyable是一个去除了拷贝构造和赋值构造的类. noncopyable: class noncopy ...
- handy源码阅读(五):PollerBase类
使用poll内核函数等待事件发生: struct PollerBase: private noncopyable { int64_t id_; int lastActive_; PollerBase( ...
- JDK源码阅读:Object类阅读笔记
Object 1. @HotSpotIntrinsicCandidate @HotSpotIntrinsicCandidate public final native Class<?> g ...
- klee源码阅读笔记1--STPBuilder类
初始化过程中四个数据成员中的两个数据成员被初始化: 一.vc被初始化为STP提供的C调用接口函数vc_createValidityChecker(): 二.optimizeDivides被初始化为fa ...
随机推荐
- 测开之路一百五十一:ajax的作用和基本实现原理
有些情况需要请求和刷新部分资源,但是又不希望整个页面都刷新,这个时候就需要用ajax来处理,即页面的某一部分触发请求和刷新内容 准备两个视图和html from flask import Flask, ...
- python字符串的学习计划
python字符串有14小节内容, 计划7天学完吧(不知道能完成不) 今天依然是在禅道上写用例的一天 禅道上的用例,编写的时候比较方便 修改维护的时候,有点小麻烦(没有在Excel表中容易修改) D5 ...
- note.js 引用jQuery,
1.安装jQuery npm install jquery 在自己的项目目录里面打开cmd进行安装 2.下面直接在js文件里面引用jQuery //调用jQuery来操作页面 const jsdom ...
- 使用Dockerfile制作镜像
组成部分 基础镜像信息 FROM 维护者信息 MAINTAINER.LABEL 镜像操作指令 RUN.COPY.ADD.EXPOSE.WORKDIR.ONBUILD.US ...
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...
- hive数据去重
Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能 hive的元数据存储:通常是存储在关系数据库如 mysql(推荐) , derby(内嵌 ...
- C++中的深拷贝和浅拷贝构造函数
1,对象的构造在实际工程开发当中是相当重要的,C++ 中使用类就要创建对象,这 就涉及了对象的构造,本节课讲解对象的构造和内存操作方面的问题: 2,实际工程开发中,bug 产生的根源,必然的会有内存操 ...
- set(详解)
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
- 权限控制(vue)
权限控制(vue) 经常会遇到,角色权限控制问题,若是页面控制,倒好说,可如果是当前页面部分可见不可见,这就有些麻烦,如果加上条件就更加苛刻.之前只是简单的v-if进行控制,如今想试试指令(网上一直有 ...
- BloomFilter&python支持
BloomFilter&python支持 BloomFilter 布隆过滤器是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时, ...