本文整理汇总了C++中Transport::listen方法的典型用法代码示例。如果您正苦于以下问题:C++ Transport::listen方法的具体用法?C++ Transport::listen怎么用?C++ Transport::listen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Transport
的用法示例。
在下文中一共展示了Transport::listen方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: start
void EchoServer::start()
{
_transport.start();
EchoPacketFactory factory;
DefaultPacketStreamer streamer(&factory);
EchoServerAdapter serverAdapter;
IOComponent *ioc = _transport.listen(_spec, &streamer, &serverAdapter);
if (ioc == NULL) {
TBSYS_LOG(ERROR, "listen error.");
return;
}
_transport.wait();
}
示例2: start
void EchoServer::start() {
DefaultPacketFactory factory;
DefaultPacketStreamer streamer(&factory);
IOComponent *ioc = _transport.listen(_spec.c_str(), &streamer, this);
if (ioc == NULL) {
ANET_LOG(ERROR, "listen error.");
return;
}
ANET_LOG(INFO, "EchoServer(%s) started.", _spec.c_str());
_transport.run();
ANET_LOG(INFO, "EchoServer(%s) Quiting...", _spec.c_str());
ANET_GLOBAL_STAT.log();
ioc->close();
ioc->subRef();
}
示例3: testReadMaliciousDataInfinitLoop
void TCPCONNECTIONTF::testReadMaliciousDataInfinitLoop() {
ANET_LOG(DEBUG, "Begin testReadMaliciousDataInfinitLoop");
int64_t now = TimeUtil::getTime();
Transport tranClient;
Transport tranServer;
const char sepc[] = "tcp:localhost:12346";
ConnPacketFactory factory;
DefaultPacketStreamer defaultStreamer(&factory);
DefaultPacketHandler defaultHandler;
TCPServerAdapter adapter;
MaliciousStreamer maliciousStreamer(&factory);
IOComponent *listener =
tranServer.listen(sepc, &defaultStreamer, &adapter);
CPPUNIT_ASSERT(listener);
ANET_LOG(SPAM,"After tranServer->listen()");
tranServer.eventIteration(now);
ANET_LOG(SPAM,"After tranServer->eventIteration(now)");
//create a connection
Connection *conn = tranClient.connect(sepc, &maliciousStreamer);
ANET_LOG(SPAM,"After tranClient->connect(spec, _streamer, false)");
CPPUNIT_ASSERT(conn);
tranClient.eventIteration(now);
ANET_LOG(SPAM,"After tranClient->eventIteration(now)");
//accept the connection
tranServer.eventIteration(now);
ANET_LOG(SPAM,"After tranServer->eventIteration(now)");
conn->postPacket(new ConnPacket(11), &defaultHandler, NULL);
maliciousStreamer._maliciousLen = 0x80000000 + 10;
ANET_LOG(DEBUG,"before conn->writeData()");
conn->writeData();
ANET_LOG(DEBUG,"before connAcc->readData()");
tranServer.eventIteration(now);
ANET_LOG(DEBUG,"after connAcc->readData()");
tranClient.eventIteration(now);
CPPUNIT_ASSERT(conn->isClosed());
ANET_LOG(DEBUG,"after tranClient.eventIteration(now)");
conn->subRef();
listener->subRef();
ANET_LOG(DEBUG, "End testReadMaliciousDataInfinitLoop");
}
示例4: testMemLeak
/**
* test Server and Client memory leak
* test whether packets are deleted when connection closed
*/
void TCPCONNECTIONTF::testMemLeak() {
char spec[] = "tcp:localhost:13147";
int64_t now = TimeUtil::getTime();
Transport *tranServer = new Transport;
Transport *tranClient = new Transport;
ConnServerAdapter *adapter = new ConnServerAdapter;
//add listener to tranServer
IOComponent *listener = tranServer->listen(spec, _streamer, adapter);
CPPUNIT_ASSERT(listener);
tranServer->eventIteration(now);
//create a connection
Connection *conn = tranClient->connect(spec, _streamer, false);
CPPUNIT_ASSERT(conn);
tranClient->eventIteration(now);
//accept the connection
tranServer->eventIteration(now);
// client send two packets
_conn->postPacket(new ConnPacket, _handler, NULL, true);
_conn->postPacket(new ConnPacket, _handler, NULL, true);
tranClient->eventIteration(now);
//server accept two packets
tranServer->eventIteration(now);
IOComponent *ioc = tranServer->_iocListTail;
Connection *tmpConn = ((TCPComponent *)ioc)->_connection;
//client close the connection
_conn->close();
tranClient->eventIteration(now);
tranServer->eventIteration(now);
CPPUNIT_ASSERT_EQUAL((size_t)0, tmpConn->_outputQueue.size());
delete adapter;
delete tranClient;
delete tranServer;
listener->subRef();
conn->subRef();
}
示例5: testClose
void TCPCONNECTIONTF::testClose()
{
TCPServerAdapter adapter;
Transport *tranServer = new Transport;
char spec[] = "tcp:localhost:13345";
tranServer->start();
IOComponent *listener = tranServer->listen(spec, _streamer, &adapter);
CPPUNIT_ASSERT(listener);
//start client
Transport *tranClient = new Transport;
tranClient->start();
Connection *conn = tranClient->connect(spec, _streamer, false);
CPPUNIT_ASSERT(conn);
CPPUNIT_ASSERT(_conn->postPacket(new ConnPacket(31), _handler, NULL));
// the close() has not implement
ANET_LOG(SPAM,"Before Calling _conn->close();");
_conn->close();
ANET_LOG(SPAM,"After Calling _conn->close();");
CPPUNIT_ASSERT(_conn->isClosed());
ANET_LOG(SPAM,"After Assert(_conn->isClosed();");
tranClient->stop();
ANET_LOG(SPAM,"After Calling tranClient->stop();");
tranClient->wait();
ANET_LOG(SPAM,"After Calling tranClient->wait();");
tranServer->stop();
ANET_LOG(SPAM,"After Calling tran_server->stop();");
tranServer->wait();
ANET_LOG(SPAM,"After Calling tranClient->wait();");
delete tranClient;
delete tranServer;
listener->subRef();
conn->subRef();
}
示例6: doProcess
void doProcess(unsigned int port, unsigned int num, CMClient& client)
{
Transport transport;
transport.start(); //using multithreads mode of anet
Thread *threads = new Thread[num];
RequestProcessor *runnables = new RequestProcessor [num];
assert(threads);
assert(runnables);
for (long i=0; i<num; i++) {
threads[i].start(runnables + i, (void*)&client);
}
HTTPPacketFactory factory;
HTTPStreamer streamer(&factory);
HTTPServerAdapter serverAdapter;
stringstream ss;
char hostname[1024];
if (gethostname(hostname, 1024) != 0) {
transport.stop();
transport.wait();
delete [] threads;
delete [] runnables;
exit(-1);
}
// ss << "tcp:" << hostname << ":" << port;
ss << "tcp:" << ":" << port;
string str = ss.str();
const char *spec = str.c_str();
IOComponent *ioc = transport.listen(spec, &streamer, &serverAdapter);
if (ioc == NULL) {
printf("create listen port error\n");
transport.stop();
transport.wait();
delete [] threads;
delete [] runnables;
return;
}
printf("webserver start ok\n");
while (!globalStopFlag) {
usleep(100000);
}
transport.stop();
transport.wait();
globalQueue._condition.lock();
while (globalQueue._queue.size()) {
HTTPRequestEntry entry = globalQueue._queue.front();
entry._packet->free();
entry._connection->subRef();
globalQueue._queue.pop();
}
globalQueue._condition.broadcast();
globalQueue._condition.unlock();
for (long i=0; i<num; i++) {
threads[i].join();
}
delete [] threads;
delete [] runnables;
}
示例7: doProcess
void doProcess(unsigned int port, unsigned int num) {
Thread *threads = new Thread[num];
RequestProcessor *runnables = new RequestProcessor [num];
assert(threads);
assert(runnables);
for (long i=0; i<num; i++) {
ANET_LOG(DEBUG, "Starting thread(%d)", i);
threads[i].start(&runnables[i], (void*)i);
}
Transport transport;
// transport.start(); //using multithreads mode of anet
DefaultPacketFactory factory;
DefaultPacketStreamer streamer(&factory);
MyServerAdapter serverAdapter;
stringstream ss;
char hostname[1024];
if (gethostname(hostname, 1024) != 0) {
ANET_LOG(ERROR, "Failed to get hostname");
exit(-1);
}
ss << "tcp:" << hostname << ":" << port;
string str = ss.str().c_str();
const char *spec = str.c_str();
ANET_LOG(INFO, "listen to %s", spec);
IOComponent *ioc = transport.listen(spec, &streamer, &serverAdapter);
if (ioc == NULL) {
ANET_LOG(ERROR, "listen (%s) ERROR.", spec);
return;
}
while (!globalStopFlag) {
// usleep(100000);
int64_t now;
transport.runIteration(now);
}
// transport.stop();
// transport.wait();
transport.closeComponents();
globalQueue._condition.lock();
while (globalQueue._queue.size()) {
RequestEntry entry = globalQueue._queue.front();
DefaultPacket *packet = (DefaultPacket*)entry._packet;
ANET_LOG(WARN,"Discard request %s", packet->getBody());
entry._packet->free();
entry._connection->subRef();
globalQueue._queue.pop();
}
globalQueue._condition.broadcast();
globalQueue._condition.unlock();
for (long i=0; i<num; i++) {
ANET_LOG(INFO, "Join thread(%d)",i);
threads[i].join();
}
printf("*****************************************************\n");
printf("Worker Summary\n");
printf("THREAD_ID TOT_TIME(ms) LOCK_TIME(ms) TASK_TIME(ms) TASK_WAIT(ms) TASKS_PROCESSED\n");
for (long i=0; i<num; i++) {
printf("%8ld %12.2f %12.2f %12.2f %13.2f %12ld\n",
runnables[i].m_id,
runnables[i].m_totalTime / 1000.0,
runnables[i].m_lockTime / 1000.0,
runnables[i].m_taskProcessTime / 1000.0,
runnables[i].m_taskWaitTime / 1000.0,
runnables[i].m_taskProcesssed);
}
printf("\n");
printf("Queue Summary\n");
printf(" #Enqueue Acc_Enqueue_Len #Signals Push_Time(ms) Push_Wait_Time(ms)\n");
printf("%12ld %14ld %8ld %13.2f %18.2f\n",
globalQueue.mEnqueueCount,
globalQueue.mAccDequeueLength,
globalQueue.mSignalCount,
globalQueue.mAccEnqueueTime / 1000.0,
globalQueue.mAccEnqueueWaitTime / 1000.0);
printf(" #Dequeue Acc_Dequeue_Len #Waits Pop_Time(ms) Pop_Wait_Time(ms)\n");
printf("%12ld %14ld %8ld %13.2f %18.2f\n",
globalQueue.mDequeCount,
globalQueue.mAccDequeueLength,
globalQueue.mWaitCount,
globalQueue.mAccDequeueTime / 1000.0,
globalQueue.mAccDequeueWaitTime / 1000.0);
printf("*****************************************************\n");
delete [] threads;
delete [] runnables;
}