1.TCP网络编程本质
TCP网络编程最本质是的处理三个半事件
1.连接建立:服务器accept(被动)接受连接,客户端connect(主动)发起连接。
2.连接断开:主动断开(close、shutdown),被动断开(read返回0)。
3.消息到达:文件描述符可读。
4.消息发送完毕:这算半个。对于低流量的服务,可不必关心这个事件;这里的发送完毕是指数据写入内核缓冲区,将由TCP协议栈负责数据的发送与重传,不代表对端已经接收到数据。
2.EchoServer类图
显然,这是基于对象的编程思想,在EchoServer的构造函数中会对三个回调函数进行注册,相应的源码后续章节会进行分析。
3.什么都不做的EventLoop
1.one loop perthread意思是说每个线程最多只能有一个EventLoop对象,创建了EventLoop对象的线程就是一个IO线程。
2.EventLoop对象构造的时候,会检查当前线程是否已经创建了其他EventLoop对象,如果已创建,终止程序(LOG_FATAL)。
3.EventLoop构造函数会记住本对象所属线程(threadId_)。
4.创建了EventLoop对象的线程称为IO线程,其功能是运行事件循环(EventLoop::loop()
函数)。
什么都不做的EventLoop源码
EventLoop.h
#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H
#include <boost/noncopyable.hpp>
#include <muduo/base/CurrentThread.h>
#include <muduo/base/Thread.h>
namespace muduo
{
namespace net
{
///
/// Reactor, at most one per thread.(一个线程最多只能有一个reactor)
///
/// This is an interface class, so don't expose too much details.
//EventLoop类其实就是Reactor模式的封装
class EventLoop : boost::noncopyable
{
public:
EventLoop();
~EventLoop(); // force out-line dtor, for scoped_ptr members.
///
/// Loops forever.
///
/// Must be called in the same thread as creation of the object.
///
void loop();
void assertInLoopThread()
{
if (!isInLoopThread())
{
abortNotInLoopThread();
}
}
bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
static EventLoop* getEventLoopOfCurrentThread();
private:
void abortNotInLoopThread();
bool looping_; /* atomic */
const pid_t threadId_; // 当前对象所属线程ID
};
}
}
#endif // MUDUO_NET_EVENTLOOP_H
EventLoop.cc
#include <muduo/net/EventLoop.h>
#include <muduo/base/Logging.h>
#include <poll.h>
using namespace muduo;
using namespace muduo::net;
namespace
{
// 当前线程EventLoop对象指针,在构造函数的函数体中用来检查当前线程是否已经创建了其他EventLoop对象(每个线程最多只能有一个EventLoop对象)
// 线程局部存储,每个线程都有,相互独立
__thread EventLoop* t_loopInThisThread = 0;
}
EventLoop* EventLoop::getEventLoopOfCurrentThread()
{
return t_loopInThisThread;
}
EventLoop::EventLoop()
: looping_(false),
threadId_(CurrentThread::tid())
{
LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
// 如果当前线程已经创建了EventLoop对象,终止(LOG_FATAL)
if (t_loopInThisThread)
{
LOG_FATAL << "Another EventLoop " << t_loopInThisThread
<< " exists in this thread " << threadId_;
}
else
{
t_loopInThisThread = this;
}
}
EventLoop::~EventLoop()
{
t_loopInThisThread = NULL;
}
// 事件循环,该函数不能跨线程调用
// 只能在创建该对象的线程中调用
void EventLoop::loop()
{
assert(!looping_);
// 断言当前处于创建该对象的线程中
assertInLoopThread();
looping_ = true;
LOG_TRACE << "EventLoop " << this << " start looping";
//假设当前没有关注的事件
::poll(NULL, 0, 5*1000);
LOG_TRACE << "EventLoop " << this << " stop looping";
looping_ = false;
}
void EventLoop::abortNotInLoopThread()
{
LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
<< " was created in threadId_ = " << threadId_
<< ", current thread id = " << CurrentThread::tid();
}
测试
1.普通测试程序:主线程和子线程各自创建了一个EventLoop对象,然后开始各自进行事件循环。
Reactor_test01.cc
#include <muduo/net/EventLoop.h>
#include <stdio.h>
using namespace muduo;
using namespace muduo::net;
void threadFunc()
{
printf("threadFunc(): pid = %d, tid = %d\n",
getpid(), CurrentThread::tid());
//子线程中创建了一个EventLoop对象
EventLoop loop;
//子线程开始事件循环
loop.loop();
}
int main(void)
{
printf("main(): pid = %d, tid = %d\n",
getpid(), CurrentThread::tid());
//主线程创建了一个EventLoop对象
EventLoop loop;
//创建了一个子线程
Thread t(threadFunc);
t.start();
//主线程开始事件循环
loop.loop();
t.join();
return 0;
}
2.测试目的:为了强调事件循环函数EventLoop::loop()
不能跨线程调用,只能在创建该对象的线程中调用。
Reactor_test02.cc
#include <muduo/net/EventLoop.h>
#include <stdio.h>
using namespace muduo;
using namespace muduo::net;
EventLoop* g_loop;
void threadFunc()
{
//跨线程调用事件循环函数,显然这样是错误的,
g_loop->loop();
}
int main(void)
{
EventLoop loop;
g_loop = &loop;
Thread t(threadFunc);
t.start();
t.join();
return 0;
}
显然会触发FATAL级别的日志,终止程序。