如果用thrift,没多少需要处理的,最大缺点boost挺讨厌的。
如果httpserver,libevent简单列子比下面的thrift代码还少,还简结,话说,既然httpserver,为啥用C++
#include <thrift/concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <thrift/protocol/TCompactProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <server/TNonblockingServer.h>
#include "MessageService.h"
class MessageServiceHandler : virtual public MessageServiceIf {
public:
MessageServiceHandler() {
printf("start\n");
}
void processRequest(MessageServiceReturn &_return, const MessageServiceRequest &requst) {
printf("todo:\n");
}
};
using boost::shared_ptr;
int main(int argc, char **argv) {
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using namespace apache::thrift::protocol;
using namespace apache::thrift::concurrency;
int port = 9090;
int threadNum = 8;
shared_ptr<MessageServiceHandler> handler(new MessageServiceHandler());
shared_ptr<TProcessor> processor(new MessageServiceProcessor(handler));
TServerSocket *socket = new TServerSocket(port);
shared_ptr<TServerTransport> serverTransport(socket);// timeout no set
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
socket->setAcceptTimeout(20 * 1000);
socket->setSendTimeout(240 * 1000);
socket->setRecvTimeout(120 * 1000);
boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(
threadNum);
boost::shared_ptr<ThreadFactory> threadFactory(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TNonblockingServer server(processor, protocolFactory, port, threadManager);
int nIOThreads = 32;
server.setNumIOThreads(nIOThreads);
server.serve();
return 0;
}
--
修改:lushan5436 FROM 114.247.175.*
FROM 114.247.175.*