#ifndef MUDUO_CONTRIB_THRIFT_THRIFTSERVER_H #define MUDUO_CONTRIB_THRIFT_THRIFTSERVER_H #include #include #include #include "muduo/base/ThreadPool.h" #include "muduo/net/TcpServer.h" #include #include "contrib/thrift/ThriftConnection.h" using apache::thrift::TProcessor; using apache::thrift::TProcessorFactory; using apache::thrift::protocol::TProtocolFactory; using apache::thrift::server::TServer; using apache::thrift::transport::TTransportFactory; class ThriftServer : boost::noncopyable, public TServer { public: template ThriftServer(const boost::shared_ptr& processorFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : TServer(processorFactory), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); } template ThriftServer(const boost::shared_ptr& processor, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); } template ThriftServer(const boost::shared_ptr& processorFactory, const boost::shared_ptr& protocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : TServer(processorFactory), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputProtocolFactory(protocolFactory); setOutputProtocolFactory(protocolFactory); } template ThriftServer(const boost::shared_ptr& processor, const boost::shared_ptr& protocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputProtocolFactory(protocolFactory); setOutputProtocolFactory(protocolFactory); } template ThriftServer(const boost::shared_ptr& processorFactory, const boost::shared_ptr& transportFactory, const boost::shared_ptr& protocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : TServer(processorFactory), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputTransportFactory(transportFactory); setOutputTransportFactory(transportFactory); setInputProtocolFactory(protocolFactory); setOutputProtocolFactory(protocolFactory); } template ThriftServer(const boost::shared_ptr& processor, const boost::shared_ptr& transportFactory, const boost::shared_ptr& protocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputTransportFactory(transportFactory); setOutputTransportFactory(transportFactory); setInputProtocolFactory(protocolFactory); setOutputProtocolFactory(protocolFactory); } template ThriftServer(const boost::shared_ptr& processorFactory, const boost::shared_ptr& inputTransportFactory, const boost::shared_ptr& outputTransportFactory, const boost::shared_ptr& inputProtocolFactory, const boost::shared_ptr& outputProtocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(ProcessorFactory, TProcessorFactory)) : TServer(processorFactory), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputTransportFactory(inputTransportFactory); setOutputTransportFactory(outputTransportFactory); setInputProtocolFactory(inputProtocolFactory); setOutputProtocolFactory(outputProtocolFactory); } template ThriftServer(const boost::shared_ptr& processor, const boost::shared_ptr& inputTransportFactory, const boost::shared_ptr& outputTransportFactory, const boost::shared_ptr& inputProtocolFactory, const boost::shared_ptr& outputProtocolFactory, muduo::net::EventLoop* eventloop, const muduo::net::InetAddress& addr, const muduo::string& name, THRIFT_OVERLOAD_IF(Processor, TProcessor)) : TServer(processor), server_(eventloop, addr, name), numWorkerThreads_(0), workerThreadPool_(name + muduo::string("WorkerThreadPool")) { server_.setConnectionCallback(boost::bind(&ThriftServer::onConnection, this, _1)); setInputTransportFactory(inputTransportFactory); setOutputTransportFactory(outputTransportFactory); setInputProtocolFactory(inputProtocolFactory); setOutputProtocolFactory(outputProtocolFactory); } virtual ~ThriftServer(); void serve(); void start(); void stop(); muduo::ThreadPool& workerThreadPool() { return workerThreadPool_; } bool isWorkerThreadPoolProcessing() const { return numWorkerThreads_ != 0; } void setThreadNum(int numThreads) { server_.setThreadNum(numThreads); } void setWorkerThreadNum(int numWorkerThreads) { assert(numWorkerThreads > 0); numWorkerThreads_ = numWorkerThreads; } private: friend class ThriftConnection; void onConnection(const muduo::net::TcpConnectionPtr& conn); private: muduo::net::TcpServer server_; int numWorkerThreads_; muduo::ThreadPool workerThreadPool_; muduo::MutexLock mutex_; std::map conns_; }; #endif // MUDUO_CONTRIB_THRIFT_THRIFTSERVER_H