代码结构 文件介绍
InetAddress.h
InetAddress类 ip和端口设置
Socket.h
Socket类 设置fd
Epoll.h
epollfd 管理类
Channel.h
Channel类 管理epoll以及对应回调函数实现
EventLoop.h
EventLoop事件循环类
TcpServer.h
服务器类
tcpepoll.cpp 主函数
InetAddress.h
#ifndef _INETADDRESS_H
#define _INETADDRESS_H#pragma on_INETADDRESS_He#include <string>
#include <arpa/inet.h>
#include <netinet/in.h>class InetAddress
{
private:sockaddr_in addr_;
public:InetAddress(const std::string &ip, uint16_t port);InetAddress(const sockaddr_in addr);InetAddress();~InetAddress();const char *ip()const;uint16_t port()const;const sockaddr *addr()const;void setaddr(sockaddr_in clientaddr);
};#endif // _INETADDRESS_H
InetAddress.cpp
#include "InetAddress.h"InetAddress::InetAddress()
{}InetAddress::InetAddress(const std::string &ip, uint16_t port)
{addr_.sin_family = AF_INET;addr_.sin_addr.s_addr = inet_addr(ip.c_str());addr_.sin_port = htons(port);
}InetAddress::InetAddress(const sockaddr_in addr):addr_(addr)
{}InetAddress::~InetAddress()
{}const char* InetAddress::ip()const
{return inet_ntoa(addr_.sin_addr);
}
uint16_t InetAddress::port()const
{return ntohs(addr_.sin_port);
}
const sockaddr* InetAddress::addr()const
{return (sockaddr*)&addr_;
}void InetAddress::setaddr(sockaddr_in clientaddr)
{addr_ = clientaddr;
}
Socket.h
#ifndef SOCKET_H
#define SOCKET_H#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/fcntl.h>
#include <sys/epoll.h>
#include <netinet/tcp.h> // TCP_NODELAY#include "InetAddress.h"int createnonblocking();class Socket
{
public:Socket(int fd);~Socket();int fd() const;void setreuseaddr(bool on);void setreuseport(bool on);void settcpnodelay(bool on);void setkeepalive(bool on);void bind(const InetAddress &servaddr);void listen(int n=128);int accept(InetAddress &clientaddr);private:const int fd_;
};#endif // !SOCKET_H
Socket.cpp
#include "Socket.h"int createnonblocking()
{int listenfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);if(listenfd < 0){perror("socket() failed"); exit(-1);} return listenfd;
}Socket::Socket(int fd):fd_(fd)
{}
Socket::~Socket()
{close(fd_);
}int Socket::fd() const
{return fd_;
}
void Socket::setreuseaddr(bool on)
{int optval = on ? 1 : 0;setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &optval, static_cast<socklen_t>(sizeof(optval)));
}
void Socket::setreuseport(bool on)
{int optval = on ? 1 : 0;setsockopt(fd_, SOL_SOCKET, SO_REUSEPORT, &optval, static_cast<socklen_t>(sizeof(optval)));
}
void Socket::settcpnodelay(bool on)
{int optval = on ? 1 : 0;setsockopt(fd_, SOL_SOCKET, TCP_NODELAY, &optval, static_cast<socklen_t>(sizeof(optval)));
}
void Socket::setkeepalive(bool on)
{int optval = on ? 1 : 0;setsockopt(fd_, SOL_SOCKET, SO_KEEPALIVE, &optval, static_cast<socklen_t>(sizeof(optval)));
}
void Socket::bind(const InetAddress &servaddr)
{if(::bind(fd_, servaddr.addr(), sizeof(sockaddr)) < 0){perror("bind() failed"); close(fd_); exit(-1);}
}
void Socket::listen(int n)
{if(::listen(fd_, n) != 0){perror("listen() failed");close(fd_);exit(-1);}
}
int Socket::accept(InetAddress &clientaddr)
{struct sockaddr_in peeraddr;socklen_t len = sizeof(peeraddr);int clientfd = accept4(fd_, (struct sockaddr*)&clientaddr, &len, SOCK_NONBLOCK);clientaddr.setaddr(peeraddr);return clientfd;
}
Epoll.h
#pragma once
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/fcntl.h>
#include <sys/epoll.h>
#include <netinet/tcp.h> // TCP_NODELAY
#include <vector>
#include "Channel.h"class Channel;class Epoll
{private:static const int MaxEvents = 100;int epollfd_;epoll_event events_[MaxEvents];public:Epoll();~Epoll();//void addfd(int fd, uint32_t op);void updatechannel(Channel *ch);//std::vector<epoll_event> loop(int timeout=-1);std::vector<Channel*> loop(int timeout=-1);
};
Epoll.cpp
#include "Epoll.h"/*
class Epoll
{private:static const int MaxEvents = 100;int epollfd;epoll_event events_[MaxEvents];public:Epoll();~Epoll();void addfd(int fd, uint32_t op);std::vector<epoll_event> loop(int timeout=-1);
}
*/Epoll::Epoll()
{if((epollfd_ = epoll_create(1)) == -1){printf("epoll_create() failed(%d).\n", errno);exit(-1);}
}
Epoll::~Epoll()
{close(epollfd_);
}
/*
void Epoll::addfd(int fd, uint32_t op)
{struct epoll_event ev;ev.data.fd = fd;ev.events = op; //水平if(epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev) == -1){printf("epoll_ctl() failed(%d).\n", errno);exit(-1); }
}
*/void Epoll::updatechannel(Channel *ch)
{epoll_event ev;ev.data.ptr = ch;ev.events = ch->events();if(ch->inpoll()){if(epoll_ctl(epollfd_, EPOLL_CTL_MOD, ch->fd(),&ev) == -1){printf("epoll_ctl() failed(%d).\n", errno);exit(-1); }printf("epoll_ctl() EPOLL_CTL_MOD success. %d\n", ch->fd());}else{if(epoll_ctl(epollfd_, EPOLL_CTL_ADD, ch->fd(),&ev) == -1){printf("epoll_ctl() failed(%d).\n", errno);exit(-1); }ch->setinepoll();printf("epoll_ctl() EPOLL_CTL_ADD success. %d\n", ch->fd());}
}/*
std::vector<epoll_event> Epoll::loop(int timeout)
{std::vector<epoll_event> evs;bzero(events_, sizeof(events_));int infds = epoll_wait(epollfd_, events_, MaxEvents, timeout);if(infds < 0){perror("epoll_wait() failed "); exit(-1);}if(infds == 0){perror("epoll_wait() timeout \n"); return evs;}for(int i = 0; i < infds; i++){evs.push_back(events_[i]);}return evs;
}*/std::vector<Channel*> Epoll::loop(int timeout)
{std::vector<Channel*> channles;bzero(events_, sizeof(events_));int infds = epoll_wait(epollfd_, events_, MaxEvents, timeout);if(infds < 0){perror("epoll_wait() failed "); exit(-1);}if(infds == 0){perror("epoll_wait() timeout \n"); return channles;}for(int i = 0; i < infds; i++){Channel *ch = (Channel*)events_[i].data.ptr; ch->setrevents(events_[i].events);channles.push_back(ch);}return channles;
}
Channel.h
#ifndef CHANNEL_H
#define CHANNEL_H#pragma once
#include <sys/epoll.h>
#include <functional>#include "Epoll.h"
#include "InetAddress.h"
#include "Socket.h"class Epoll;class Channel
{private:int fd_=-1;Epoll *ep_ = nullptr; //channle 对应的红黑树bool inepoll_=false; // epoll_ctl add moduint32_t events_=0; //fd_需要监视的事件uint32_t revents_=0; //fd 已发生的事件std::function<void()> readcallback_;public:Channel(Epoll *ep, int fd);~Channel();int fd();void useet(); //采用边缘触发void enablereading(); //让epoll_wait()监视fd_的读事件void setinepoll();void setrevents(uint32_t ev);bool inpoll();uint32_t events();uint32_t revents(); //返回revents_成员void handleevent();void newconnection(Socket* servsock);void onmessage();void setreadcallback(std::function<void()> fn);
};#endif // ! CHANNEL_H
Channel.cpp
#include "Channel.h"/*
class Channel
{private:int fd_=-1;Epoll *ep_ = nullptr; //channle 对应的红黑树bool inepoll_=false; // epoll_ctl add moduint32_t events_=0; //fd_需要监视的事件uint32_t revents_0; //fd 已发生的事件public:Channel(Epoll *ep, intfd);~Channel();int fd();void useet(); //采用边缘触发void enablereading(); //让epoll_wait()监视fd_的读事件void setinepoll();void setrevents(uint32_t ev);bool inpoll();uint32_t events();uint32_t revents(); //返回revents_成员
};*/Channel::Channel(Epoll *ep, int fd):ep_(ep),fd_(fd)
{}Channel::~Channel()
{
//在析构函数中,不要销毁ep_ 也不能关闭fd_ 不属于channel类
}int Channel::fd()
{return fd_;
}
void Channel::useet()
{events_ = events_ | EPOLLET;
}
void Channel::enablereading()
{events_ |= EPOLLIN;ep_->updatechannel(this);
}
void Channel::setinepoll()
{inepoll_ = true;
}
void Channel::setrevents(uint32_t ev)
{revents_= ev;
}
bool Channel::inpoll()
{return inepoll_;
}
uint32_t Channel::events()
{return events_;
}
uint32_t Channel::revents()
{return revents_;
}//事件处理函数, epoll_wait返回的时候执行它。
void Channel::handleevent()
{if(revents_ & EPOLLRDHUP){printf("cilent fd =%d disconnection\n", fd_);close(fd_);} else if (revents_ & EPOLLIN|EPOLLPRI){readcallback_();}else if (revents_ & EPOLLOUT){}else{printf("cilent fd =%d\n", fd_);close(fd_);}
}void Channel::newconnection(Socket* servsock)
{InetAddress clientaddr;Socket *clientsock = new Socket(servsock->accept(clientaddr));printf("FILE(%s)FUNCTION(%s)LINE(%d) accept client fd=%d, ip=%s,port=%d ok.\n",__FILE__, __func__, __LINE__,clientsock->fd(), clientaddr.ip(), clientaddr.port());Channel *clientchannel = new Channel(ep_, clientsock->fd());clientchannel->setreadcallback(std::bind(&Channel::onmessage, clientchannel));clientchannel->useet();clientchannel->enablereading();
}void Channel::onmessage()
{char buffer[1024];memset(buffer, 0, sizeof(buffer));size_t nread = recv(fd_, buffer, sizeof(buffer), 0);if(nread > 0){printf("tcpepoll Recv:%s\n", buffer);send(fd_, buffer, strlen(buffer), 0);}else if (nread == -1 && errno == EINTR){}else if(nread == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))){}else if (nread == 0){printf("clientfd:%d disconnected\n", fd_);close(fd_);}
}//设置fd的回调函数
void Channel::setreadcallback(std::function<void()> fn)
{readcallback_ = fn;
}
EventLoop.h
#pragma once#include "Epoll.h"class EventLoop
{
private:Epoll *ep_;
public:EventLoop();~EventLoop();void run();Epoll* ep();
};
EventLoop.cpp
#include "EventLoop.h"/*
class EventLoop
{
private:Epoll *ep_;
public:EventLoop();~EventLoop();void run();
};
*/EventLoop::EventLoop():ep_(new Epoll)
{}EventLoop::~EventLoop()
{delete ep_;
}void EventLoop::run()
{while(true){std::vector<Channel*> channles = ep_->loop();for(auto &ch:channles){ch->handleevent();}}
}Epoll* EventLoop::ep()
{return ep_;
}
TcpServer.h
#pragma once#include "EventLoop.h"
#include "Socket.h"
#include "Channel.h"class TcpServer
{
private:EventLoop loop_;
public:TcpServer(const std::string &ip, const uint16_t port);~TcpServer();void start();
};
TcpServer.cpp
#include "TcpServer.h"/*
class TcpServer
{
private:EventLoop loop_;
public:TcpServer(const std::string &ip, const uint16_t port);~TcpServer();
};
*/TcpServer::TcpServer(const std::string &ip, const uint16_t port)
{Socket *servsock = new Socket(createnonblocking());InetAddress servaddr(ip, port);servsock->setreuseaddr(true);servsock->setreuseport(true);servsock->settcpnodelay(true);servsock->setkeepalive(true);servsock->bind(servaddr);servsock->listen();Channel *servchannel = new Channel(loop_.ep(), servsock->fd());servchannel->setreadcallback(std::bind(&Channel::newconnection, servchannel, servsock));servchannel->enablereading();
}TcpServer::~TcpServer()
{}void TcpServer::start()
{loop_.run();
}
tcpepoll.cpp
#include "TcpServer.h"int main(int argc, char *argv[])
{if(argc !=3){printf("usage: ./tcpepoll ip port\n");printf("examples ./tcpepoll 127.0.0.1 6666\n");return -1;}TcpServer tcpserver(argv[1], atoi(argv[2]));tcpserver.start(); //运行事件循环return 0;
}
client.cpp
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/fcntl.h>
#include <sys/epoll.h>
#include <netinet/tcp.h> // TCP_NODELAY
#include <time.h>int main(int argc, char *argv[])
{if(argc !=3){printf("usage: ./client ip port");return -1;}int sockfd;struct sockaddr_in servaddr;char buf[1024];if((sockfd=socket(AF_INET,SOCK_STREAM, 0)) < 0){return -1;}memset(&servaddr, 0, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port = htons(atoi(argv[2]));if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))!=0){return -1;}printf("connect ok\n");for(int i = 0; i < 200000; i++){memset(buf, 0, sizeof(buf));printf("please input:");scanf("%s", buf);if(send(sockfd, buf, strlen(buf), 0) < 0){close(sockfd);return -1;}memset(buf, 0, sizeof(buf));if(recv(sockfd, buf, sizeof(buf), 0) <= 0){return -1;}printf("i:%d recv:%s\n", i, buf);}return 0;
}
makefile
all: client tcpepollclient: client.cppg++ -g -o client client.cpp tcpepoll:tcpepoll.cpp InetAddress.cpp Socket.cpp Epoll.cpp Channel.cpp EventLoop.cpp TcpServer.cppg++ -g -o tcpepoll tcpepoll.cpp InetAddress.cpp Socket.cpp Epoll.cpp Channel.cpp EventLoop.cpp TcpServer.cppclean:rm -f client tcpepoll
运行
服务器
客户端