一、如何获取client的实例
#pragma once
#include "f:\thirdparty\thrift\thrift-0.9.0\lib\cpp\src\thrift\server\tserver.h"
using namespace apache::thrift::server;
class ServerEventHandler :
public TServerEventHandler
{
public:
ServerEventHandler(void);
~ServerEventHandler(void);
virtual void* createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output);
virtual void deleteContext(void* serverContext, boost::shared_ptr<TProtocol>input, boost::shared_ptr<TProtocol>output);
};
void* ServerEventHandler::createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output)
{
theApp.AddClient(input->getTransport());
return NULL;
}
void ServerEventHandler::deleteContext(void* serverContext, boost::shared_ptr<TProtocol>input, boost::shared_ptr<TProtocol>output)
{
theApp.RemoveClient(input->getTransport());
}
m_thriftServer->setServerEventHandler(boost::shared_ptr<ServerEventHandler>(new ServerEventHandler()));
二、
如何设置接收和发送缓存的大小
#pragma once
#include "f:\thirdparty\thrift\thrift-0.9.0\lib\cpp\src\thrift\transport\tbuffertransports.h"
using namespace apache::thrift::transport;
class BufferedTransportFactory :
public TBufferedTransportFactory
{
public:
BufferedTransportFactory(int rwSize);
~BufferedTransportFactory(void);
public:
virtual boost::shared_ptr<TTransport> getTransport(boost::shared_ptr<TTransport> trans)
{
return boost::shared_ptr<TTransport>(new TBufferedTransport(trans, (uint32_t)m_rwSize));
}
private:
int m_rwSize;
};
shared_ptr<TTransportFactory> transportFactory(new BufferedTransportFactory(1024));
本文介绍如何使用Thrift实现客户端实例的创建与销毁,并通过示例代码展示了如何自定义ServerEventHandler来管理客户端上下文,同时提供了设置接收和发送缓存大小的方法。
1万+

被折叠的 条评论
为什么被折叠?



