当前位置: 首页 > news >正文

团队氛围建设 网站seo发外链的网站

团队氛围建设 网站,seo发外链的网站,工地用木模板尺寸,wordpress页面添加照片简介 TcpServer拥有Acceptor类#xff0c;新连接到达时new TcpConnection后续客户端和TcpConnection类交互。TcpServer管理连接和启动线程池#xff0c;用Acceptor接受连接。 服务端封装 - muduo的server端维护了多个tcpconnection 注意TcpServer本身不带Channel#xff0…简介 TcpServer拥有Acceptor类新连接到达时new TcpConnection后续客户端和TcpConnection类交互。TcpServer管理连接和启动线程池用Acceptor接受连接。 服务端封装 - muduo的server端维护了多个tcpconnection 注意TcpServer本身不带Channel而是使用Acceptor的Channel 成员及属性解析 主要接口 回调setters 这些回调函数会在新连接建立时传递给TcpConnction对象 start 启动threadPool_线程池 在runInLoop中执行acceptor的listen 这里专门设置了一个started_标记防止多次运行start 核心实现newConnection 从accept回调中获得的fd动态创建新的TcpConnection对象 为连接对象注册各类回调函数 将连接对象存入connectionMap_映射表里 主要成员 loop 这个loop也是acceptor的loop acceptor threadPool 一个EventLoopThreadPool用来存放io线程的EventLoopThread socket 服务端用来监听的socket ConnectionMap 一个连接名和实例TcpConnectionPtr的映射容器 TcpServer中回调的传递示意简图 源码剖析 源码已经编写注释 TcpServer.h // Copyright 2010, Shuo Chen. All rights reserved. // http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com) // // This is a public header file, it must only include public header files.#ifndef MUDUO_NET_TCPSERVER_H #define MUDUO_NET_TCPSERVER_H#include muduo/base/Atomic.h #include muduo/base/Types.h #include muduo/net/TcpConnection.h#include mapnamespace muduo { namespace net {class Acceptor; class EventLoop; class EventLoopThreadPool;/// /// TCP server, supports single-threaded and thread-pool models. /// /// This is an interface class, so dont expose too much details. class TcpServer : noncopyable {public:typedef std::functionvoid(EventLoop*) ThreadInitCallback;enum Option{kNoReusePort,//不设置端口复用kReusePort,//设置端口复用};//TcpServer(EventLoop* loop, const InetAddress listenAddr);TcpServer(EventLoop* loop,//mainloopconst InetAddress listenAddr,const string nameArg,Option option kNoReusePort);~TcpServer(); // force out-line dtor, for std::unique_ptr members.const string ipPort() const { return ipPort_; }const string name() const { return name_; }EventLoop* getLoop() const { return loop_; }/// Set the number of threads for handling input.////// Always accepts new connection in loops thread./// Must be called before c start/// param numThreads/// - 0 means all I/O in loops thread, no thread will created./// this is the default value./// - 1 means all I/O in another thread./// - N means a thread pool with N threads, new connections/// are assigned on a round-robin basis.void setThreadNum(int numThreads);//设置subloop数量void setThreadInitCallback(const ThreadInitCallback cb)//设置subloop线程初始化时调用的函数{ threadInitCallback_ cb; }/// valid after calling start()std::shared_ptrEventLoopThreadPool threadPool()//返回EventLoopThread线程池{ return threadPool_; }/// Starts the server if its not listening.////// Its harmless to call it multiple times./// Thread safe.void start();/// Set connection callback./// Not thread safe.void setConnectionCallback(const ConnectionCallback cb){ connectionCallback_ cb; }/// Set message callback./// Not thread safe.void setMessageCallback(const MessageCallback cb){ messageCallback_ cb; }/// Set write complete callback./// Not thread safe.void setWriteCompleteCallback(const WriteCompleteCallback cb){ writeCompleteCallback_ cb; }private:/// Not thread safe, but in loopvoid newConnection(int sockfd, const InetAddress peerAddr);/// Thread safe.void removeConnection(const TcpConnectionPtr conn);/// Not thread safe, but in loopvoid removeConnectionInLoop(const TcpConnectionPtr conn);typedef std::mapstring, TcpConnectionPtr ConnectionMap;EventLoop* loop_; // the acceptor loopconst string ipPort_;const string name_;std::unique_ptrAcceptor acceptor_; // avoid revealing Acceptorstd::shared_ptrEventLoopThreadPool threadPool_;ConnectionCallback connectionCallback_;MessageCallback messageCallback_;WriteCompleteCallback writeCompleteCallback_;ThreadInitCallback threadInitCallback_;AtomicInt32 started_;// always in loop threadint nextConnId_;ConnectionMap connections_; };} // namespace net } // namespace muduo#endif // MUDUO_NET_TCPSERVER_H TcpServer.cc // Copyright 2010, Shuo Chen. All rights reserved. // http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)#include muduo/net/TcpServer.h#include muduo/base/Logging.h #include muduo/net/Acceptor.h #include muduo/net/EventLoop.h #include muduo/net/EventLoopThreadPool.h #include muduo/net/SocketsOps.h#include stdio.h // snprintfusing namespace muduo; using namespace muduo::net;TcpServer::TcpServer(EventLoop* loop,const InetAddress listenAddr,const string nameArg,Option option): loop_(CHECK_NOTNULL(loop)), //TcpServer所在的主线程下运行的事件驱动循环ipPort_(listenAddr.toIpPort()),/* 服务器负责监听的本地ip和端口 */name_(nameArg),/* 服务器名字创建时传入 */acceptor_(new Acceptor(loop, listenAddr, option kReusePort)),/* Acceptor对象负责监听客户端连接请求运行在主线程的EventLoop中 */threadPool_(new EventLoopThreadPool(loop, name_)),/* 事件驱动线程池池中每个线程运行一个EventLoop */connectionCallback_(defaultConnectionCallback),/* 用户传入有tcp连接到达或tcp连接关闭时调用传给TcpConnection */messageCallback_(defaultMessageCallback),/* 用户传入对端发来消息时调用传给TcpConnection */nextConnId_(1) /* TcpConnection特有id每增加一个TcpConnectionnextConnId_加一 */ { /* * 设置回调函数当有客户端请求时Acceptor接收客户端请求然后调用这里设置的回调函数* 回调函数用于创建TcpConnection连接*/acceptor_-setNewConnectionCallback(std::bind(TcpServer::newConnection, this, _1, _2)); }TcpServer::~TcpServer() {loop_-assertInLoopThread();LOG_TRACE TcpServer::~TcpServer [ name_ ] destructing;for (auto item : connections_){TcpConnectionPtr conn(item.second);item.second.reset();conn-getLoop()-runInLoop(std::bind(TcpConnection::connectDestroyed, conn));} }void TcpServer::setThreadNum(int numThreads) {assert(0 numThreads);threadPool_-setThreadNum(numThreads); }void TcpServer::start() {if (started_.getAndSet(1) 0){threadPool_-start(threadInitCallback_);//启动线程池,threadInitCallback_创建好所有线程后调用的回调函数assert(!acceptor_-listenning());loop_-runInLoop( //直接调用linsten函数std::bind(Acceptor::listen, get_pointer(acceptor_)));} }/* * Acceptor接收客户端请求后调用的回调函数* param sockfd: 已经接收完成三次握手完成后的客户端套接字* param peerAddr: 客户端地址* * Acceptor只负责接收客户端请求* TcpServer需要生成一个TcpConnection用于管理tcp连接* * 1.TcpServer内有一个EventLoopThreadPool即事件循环线程池池子中每个线程都是一个EventLoop* 2.每个EventLoop包含一个Poller用于监听注册到这个EventLoop上的所有Channel* 3.当建立起一个新的TcpConnection时这个连接会放到线程池中的某个EventLoop中* 4.TcpServer中的baseLoop只用来检测客户端的连接* * 从libevent的角度看就是* 1.EventLoopThreadPool是一个struct event_base的池子池子中全是struct event_base* 2.TcpServer独占一个event_base这个event_base不在池子中* 3.TcpConnection会扔到这个池子中的某个event_base中*/ void TcpServer::newConnection(int sockfd, const InetAddress peerAddr) {loop_-assertInLoopThread();EventLoop* ioLoop threadPool_-getNextLoop();//从事件驱动线程池中取出一个线程给TcpConnection /* 为TcpConnection生成独一无二的名字 */char buf[64];snprintf(buf, sizeof buf, -%s#%d, ipPort_.c_str(), nextConnId_);nextConnId_;string connName name_ buf;LOG_INFO TcpServer::newConnection [ name_ ] - new connection [ connName ] from peerAddr.toIpPort();/* * 根据sockfd获取tcp连接在本地的地址端口* getsockname(int fd, struct sockaddr*, int *size);*/InetAddress localAddr(sockets::getLocalAddr(sockfd));// FIXME poll with zero timeout to double confirm the new connection// FIXME use make_shared if necessary/* 创建一个新的TcpConnection代表一个Tcp连接 */TcpConnectionPtr conn(new TcpConnection(ioLoop,connName,sockfd,localAddr,peerAddr));/* 添加到所有tcp 连接的map中键是tcp连接独特的名字服务器名客户端地址端口 */connections_[connName] conn;/* 为tcp连接设置回调函数由用户提供 */conn-setConnectionCallback(connectionCallback_);conn-setMessageCallback(messageCallback_);conn-setWriteCompleteCallback(writeCompleteCallback_);/* * 关闭回调函数由TcpServer设置作用是将这个关闭的TcpConnection从map中删除* 当poll返回后发现被激活的原因是EPOLLHUP此时需要关闭tcp连接* 调用Channel的CloseCallback进而调用TcpConnection的handleClose进而调用removeConnection*/conn-setCloseCallback(std::bind(TcpServer::removeConnection, this, _1)); // FIXME: unsafe/* * 连接建立后调用TcpConnection连接建立成功的函数* 1.新建的TcpConnection所在事件循环是在事件循环线程池中的某个线程* 2.所以TcpConnection也就属于它所在的事件驱动循环所在的那个线程* 3.调用TcpConnection的函数时也就应该在自己所在线程调用* 4.所以需要调用runInLoop在自己的那个事件驱动循环所在线程调用这个函数* 5.当前线程是TcpServer的主线程不是TcpConnection的线程如果在这个线程直接调用会阻塞监听客户端请求* 6.其实这里不是因为线程不安全即使在这个线程调用也不会出现线程不安全因为TcpConnection本就是由这个线程创建的*/ioLoop-runInLoop(std::bind(TcpConnection::connectEstablished, conn)); }void TcpServer::removeConnection(const TcpConnectionPtr conn) {// FIXME: unsafeloop_-runInLoop(std::bind(TcpServer::removeConnectionInLoop, this, conn)); }void TcpServer::removeConnectionInLoop(const TcpConnectionPtr conn) {//关闭连接,把fd从epoll中del掉,要释放connector(包括描述符)和channelloop_-assertInLoopThread();LOG_INFO TcpServer::removeConnectionInLoop [ name_ ] - connection conn-name();size_t n connections_.erase(conn-name());(void)n;assert(n 1);EventLoop* ioLoop conn-getLoop();ioLoop-queueInLoop(std::bind(TcpConnection::connectDestroyed, conn)); }
http://www.zqtcl.cn/news/651042/

相关文章:

  • 网站建设算什么专业企业建设网站需要注意什么
  • 太原cms建站模板建设部网站监理注销查询
  • 流量对网站排名的影响因素网站内容的作用
  • 彩钢做网站能赚钱吗合肥市住房和城乡建设厅
  • 顺德网站建设itshunde罗村建网站
  • 网站开发语言开发十大免费货源网址
  • 网站建设要那些收费项如何做自己的淘客网站
  • 郴州文明网网站网站设计策划书3000字
  • 免费学习资源网站网站维护得多久
  • 电子商务网站建设考试重点长沙网站推广平台
  • 商业性质网站建设步骤佛山企业网站优化
  • 做网站投入网站设计与开发未来发展方向
  • 网站seo优化外包顾问网站ip解析
  • 贵阳建网站公司兼职网站推广如何做
  • 建设企业网站公司价格page做网站
  • 直播网站建设模板跨境电商选品
  • 购物网站有哪些shop++是什么
  • 自动化优化系统网站建设网站建设类文章
  • 网站建设以及推广提案书支付通道网站怎么做
  • 上海兼职做网站凤凰军事新闻
  • 青田建设局网站ui培训哪好
  • 佛山网站seo哪家好全返网站建设
  • 快速建站哪个平台好常见网页设计
  • 织梦网站地图模板网站服务费
  • 织梦建设两个网站 视频互联网公司排名1000
  • 广州企业网站设计西昌手机网
  • 一个工厂做网站有用吗wordpress重写登录页面
  • 网站服务器如何搭建网站分页设计
  • 可以直接进入网站的正能量连接温州注册网络公司
  • 清丰网站建设价格福州绿光网站建设工作室