本文整理汇总了C++中EventBase类的典型用法代码示例。如果您正苦于以下问题:C++ EventBase类的具体用法?C++ EventBase怎么用?C++ EventBase使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EventBase类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST_F
TEST_F(EventHandlerTest, simple) {
const size_t writes = 4;
size_t readsRemaining = writes;
EventBase eb;
EventHandlerMock eh(&eb, efd);
eh.registerHandler(EventHandler::READ | EventHandler::PERSIST);
EXPECT_CALL(eh, _handlerReady(_))
.Times(writes)
.WillRepeatedly(Invoke([&](uint16_t /* events */) {
efd_read();
if (--readsRemaining == 0) {
eh.unregisterHandler();
}
}));
efd_write(writes);
eb.loop();
EXPECT_EQ(0, readsRemaining);
}
示例2:
const bool operator<= ( const EventBase& rhs ) const
{
if( getTime() <= rhs.getTime() )
{
return true;
}
else
{
return false;
}
}
示例3: TEST
TEST(RequestContext, SimpleTest) {
EventBase base;
// There should always be a default context with get()
EXPECT_TRUE(RequestContext::get() != nullptr);
// but not with saveContext()
EXPECT_EQ(RequestContext::saveContext(), nullptr);
RequestContext::create();
EXPECT_NE(RequestContext::saveContext(), nullptr);
RequestContext::create();
EXPECT_NE(RequestContext::saveContext(), nullptr);
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
RequestContext::get()->setContextData(
"test",
std::unique_ptr<TestData>(new TestData(10)));
base.runInEventBaseThread([&](){
EXPECT_TRUE(RequestContext::get() != nullptr);
auto data = dynamic_cast<TestData*>(
RequestContext::get()->getContextData("test"))->data_;
EXPECT_EQ(10, data);
base.terminateLoopSoon();
});
auto th = std::thread([&](){
base.loopForever();
});
th.join();
EXPECT_TRUE(RequestContext::get() != nullptr);
auto a = dynamic_cast<TestData*>(
RequestContext::get()->getContextData("test"));
auto data = a->data_;
EXPECT_EQ(10, data);
RequestContext::setContext(std::shared_ptr<RequestContext>());
// There should always be a default context
EXPECT_TRUE(nullptr != RequestContext::get());
}
示例4: TEST
TEST(TEventServerTest, ServerShutdownWithOutstandingMessage) {
int workerThreads = 1;
// Initialize thrift service
EventBase eventBase;
auto handler = make_shared<TEventServerServiceHandler>(&eventBase);
auto processor = make_shared<TEventServerTestServiceAsyncProcessor>(handler);
TEventServerCreator serverCreator(processor, 0, workerThreads);
ScopedServerThread serverThread(&serverCreator);
auto address = *serverThread.getAddress();
unique_ptr<TEventServerTestServiceCobClient> cl(
createClient<TEventServerTestServiceCobClient>(
&eventBase, address));
cl->sendResponse(bind(responseReceived, placeholders::_1, false),
1024 * 1024 * 8);
eventBase.loop();
serverThread.stop();
eventBase.loop();
}
示例5: read
bool EventQueue::addFromStream(Tick tick, BitStreamReader& stream) {
EventTypeId typeId;
read(stream, typeId);
if (typeId == 0) return false;
auto type = EventType::getById(typeId);
EventBase* event = type.create();
event->read(stream);
add(tick, event);
{
std::stringstream ss;
ss << *event;
//TRACE(net) << "Add event to queue for tick " << tick << ": "
//<< ss.str();
}
return true;
}
示例6: TEST
/**
* Test cancelling a timeout
*/
TEST(EventBaseTest, CancelTimeout) {
EventBase eb;
vector<uint32_t> timeouts;
timeouts.push_back(10);
timeouts.push_back(30);
timeouts.push_back(25);
ReschedulingTimeout t(&eb, timeouts);
t.start();
eb.tryRunAfterDelay(std::bind(&AsyncTimeout::cancelTimeout, &t), 50);
TimePoint start;
eb.loop();
TimePoint end;
ASSERT_EQ(t.timestamps.size(), 2);
T_CHECK_TIMEOUT(start, t.timestamps[0], milliseconds(10));
T_CHECK_TIMEOUT(start, t.timestamps[1], milliseconds(40));
T_CHECK_TIMEOUT(start, end, milliseconds(50));
}
示例7: SSLCacheClient
void
ClientRunner::run()
{
EventBase eb;
std::list<SSLCacheClient *> clients;
SSL_SESSION* session = nullptr;
for (int i = 0; i < FLAGS_clients; i++) {
SSLCacheClient* c = new SSLCacheClient(&eb, &session, this);
c->start();
clients.push_back(c);
}
eb.loop();
for (auto it = clients.begin(); it != clients.end(); it++) {
delete* it;
}
reqs += hits + miss;
}
示例8: TEST
/**
* Test SSL client socket
*/
TEST(AsyncSSLSocketTest, SSLClientTest) {
// Start listening on a local port
WriteCallbackBase writeCallback;
ReadCallback readCallback(&writeCallback);
HandshakeCallback handshakeCallback(&readCallback);
SSLServerAcceptCallbackDelay acceptCallback(&handshakeCallback);
TestSSLServer server(&acceptCallback);
// Set up SSL client
EventBase eventBase;
auto client = std::make_shared<SSLClient>(&eventBase, server.getAddress(), 1);
client->connect();
EventBaseAborter eba(&eventBase, 3000);
eventBase.loop();
EXPECT_EQ(client->getMiss(), 1);
EXPECT_EQ(client->getHit(), 0);
cerr << "SSLClientTest test completed" << endl;
}
示例9:
EventBase *
State::remove(size_t id)
{
EventBase * eb = NULL;
std::map<size_t,EventBase *>::iterator itr =
_map_by_id.find(id);
if(itr != _map_by_id.end()) {
eb = (*itr).second;
std::multimap<time_t,EventBase *>::iterator mitr =
_map_by_time.lower_bound(eb->expiry());
while(mitr != _map_by_time.end()
&& (*mitr).second != eb
&& (*mitr).second->expiry() <= eb->expiry()) {
++mitr;
}
_map_by_id.erase(itr);
if((*mitr).second == eb) {
_map_by_time.erase(mitr);
}
}
return eb;
}
示例10: timeGetTime
void EventManager::Run(DWORD overTime)
{
if (0 == overTime)
return;
m_Locked = true;
DWORD curTime = timeGetTime();
for (EventList::iterator it = m_EventList.begin(); it != m_EventList.end();)
{
EventBase *e = it->second;
// 如果发现了第一个时间还未到的事件,就表示时间到了的事件已全部处理完
if (curTime < e->m_actTime)
break;
it = m_EventList.erase(it);
// 此处激活事件
e->OnActive();
e->SelfDestory();
// 判断事件处理是否已经超时
if (INFINITE != overTime && ((DWORD)abs((int)(timeGetTime()-curTime)) > overTime))
break;
}
// 如果后续列表中有事件,那么先做一个合并(以后优化)
if (m_BackUpList.size())
{
m_EventList.insert(m_BackUpList.begin(), m_BackUpList.end());
m_BackUpList.clear();
}
m_Locked = false;
return;
}
示例11: GetQueuedCompletionStatus
EventBase* EventPool::pullEvent()
{
// ????????
OVERLAPPED *pOverlapped = NULL;
Target*target = NULL;
DWORD dataLen = 0;
BOOL bReturn = GetQueuedCompletionStatus(
poolHandle,
&dataLen,
(PULONG_PTR)&target,
&pOverlapped,
INFINITE
);
// ?????????????????????????
if ( 0==(DWORD)target)
{
return NULL;
}
// ??�???????????
if( !bReturn )
{
target->inEvt->eventType = ERR_EVT;
return target->inEvt;
}
else
{
// ??????????
EventBase* evt = CONTAINING_RECORD(pOverlapped, EventBase, overlapped);
evt->dataLen = dataLen;
// ??evt ???????
if (dataLen == 0 && (evt->isIn() || evt->isOut()))
{
evt->eventType = ERR_EVT; // ???ب�????
}
return evt;
}
return NULL;
}
示例12: jack_ringbuffer_read_space
void Top::processDsp()
{
// check if there's anything to read
int availableRead = jack_ringbuffer_read_space( guiToDsp );
while ( availableRead >= sizeof(EventBase) )
{
jack_ringbuffer_peek( guiToDsp, (char*)guiToDspMem, sizeof(EventBase) );
EventBase* e = static_cast<EventBase*>( guiToDspMem );
// have to check the size against the event size (as opposed to the EventBase
// size check earlier. Just because the write thread has written the EventBase
// doesn't mean the whole event is written (2 memcpy writes when buffer wraps).
// this check gaurantees that the whole event is available, and can be run
if ( availableRead >= e->size() )
{
switch ( e->type() )
{
case EventBase::EVENT_BUFFER_TRANSFER:
{
if ( availableRead >= sizeof(EventBufferTransfer) )
{
EventBufferTransfer ev( (AudioBuffer*)0);
jack_ringbuffer_read( guiToDsp, (char*)&ev, sizeof(EventBufferTransfer) );
// pass the pointer to DSP
dsp->setBuffer( ev.audioBufferPtr );
}
break;
}
}
}
// update available read, and loop over events
availableRead = jack_ringbuffer_read_space(guiToDsp);
}
}
示例13: sendOne
void QueueTest::sendOne() {
// Create a notification queue and a callback in this thread
EventBase eventBase;
QueueConsumer consumer;
consumer.fn = [&](int) {
// Stop consuming after we receive 1 message
consumer.stopConsuming();
};
consumer.startConsuming(&eventBase, &queue);
// Start a new EventBase thread to put a message on our queue
ScopedEventBaseThread t1;
t1.getEventBase()->runInEventBaseThread([&] {
this->queue.putMessage(5);
});
// Loop until we receive the message
eventBase.loop();
const auto& messages = consumer.messages;
EXPECT_EQ(1, messages.size());
EXPECT_EQ(5, messages.at(0));
}
示例14: TEST
TEST(ThriftServer, SemiFutureClientTest) {
using std::chrono::steady_clock;
auto handler = std::make_shared<TestInterface>();
apache::thrift::ScopedServerInterfaceThread runner(handler);
EventBase base;
ManualExecutor executor;
auto client = runner.newClient<FutureServiceAsyncClient>(base);
auto future = client->semifuture_sendResponse(1).via(&executor);
base.loop();
EXPECT_FALSE(future.isReady());
future.waitVia(&executor);
EXPECT_TRUE(future.isReady());
auto value = future.value();
EXPECT_EQ(value, "test1");
auto len = client->semifuture_sendResponse(4)
.via(&base)
.thenTry([](folly::Try<std::string>&& response) {
EXPECT_TRUE(response.hasValue());
EXPECT_EQ(response.value(), "test4");
return response.value().size();
})
.waitVia(&base);
EXPECT_EQ(len.value(), 5);
RpcOptions options;
options.setTimeout(std::chrono::milliseconds(1));
auto f =
client->semifuture_sendResponse(options, 3000).via(&base).waitVia(&base);
EXPECT_TRUE(f.hasException());
}
示例15:
/*virtual*/void DefaultEventBus::FireEvent( EventBase& evt, u_int64_t channelId)
{
EventSubscription::Subscriptions* subscriptions = nullptr;
if ( channelId>0 )
{
subscriptions = m_Channels[channelId];
}
else
{
subscriptions = m_Channels[evt.getTypeId()];
}
if (subscriptions == nullptr)
{
return;
}
for (auto & sub : *subscriptions)
{
static_cast<EventListener<EventBase>*>(sub->getListener())->dispatch(evt);
}
}