本文整理汇总了C++中Transport::connect方法的典型用法代码示例。如果您正苦于以下问题:C++ Transport::connect方法的具体用法?C++ Transport::connect怎么用?C++ Transport::connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Transport
的用法示例。
在下文中一共展示了Transport::connect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: UDTsend
int CGMP::UDTsend(const char* ip, const int& port, CGMPMessage* msg)
{
Transport t;
if (t.open(m_iUDTReusePort, false, true) < 0)
return -1;
if (t.connect(ip, port) < 0)
{
t.close();
return -1;
}
if ((t.send((char*)(&m_iPort), 4) < 0) || (t.send((char*)(msg->m_piHeader), 16) < 0) || (t.send((char*)&(msg->m_iLength), 4) < 0))
{
t.close();
return -1;
}
if (t.send(msg->m_pcData, msg->m_iLength) < 0)
{
t.close();
return -1;
}
t.close();
return 16 + msg->m_iLength;
}
示例2: 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");
}
示例3: 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();
}
示例4: start
void start(string addr){
cout<<"fake client start"<<endl;
trans.start();
CalPacketFactory packetFactory;
ClientPacketHandler handler(trans);
DefaultPacketStreamer ps(&packetFactory);
Connection* conns= trans.connect(addr.c_str(), &ps, true);
conns->setDefaultPacketHandler(&handler);
conns->setQueueLimit(500);
CalPacket *packet = new CalPacket(REQ);
uint32_t op1=1;
uint32_t op2=2;
packet->setOp(op1,op2);
cout<<"befor send:"<<"\n"
<<packet->toString()<<endl;
if(conns->postPacket(packet)){
cout<<"post ok"<<endl;
cout<<packet->toString()<<endl;
}else{
cout<<"post faile"<<endl;
}
trans.wait();
}
示例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: connect
int DataChn::connect(const string& ip, int port)
{
// no need to connect to self
if ((ip == m_strIP) && (port == m_iPort))
return 1;
Address addr;
addr.m_strIP = ip;
addr.m_iPort = port;
pthread_mutex_lock(&m_ChnLock);
map<Address, ChnInfo*, AddrComp>::iterator i = m_mChannel.find(addr);
if (i != m_mChannel.end())
{
if ((NULL != i->second->m_pTrans) && i->second->m_pTrans->isConnected())
{
pthread_mutex_unlock(&m_ChnLock);
return 0;
}
delete i->second->m_pTrans;
i->second->m_pTrans = NULL;
}
ChnInfo* c = NULL;
if (i == m_mChannel.end())
{
c = new ChnInfo;
c->m_pTrans = NULL;
pthread_mutex_init(&c->m_SndLock, NULL);
pthread_mutex_init(&c->m_RcvLock, NULL);
pthread_mutex_init(&c->m_QueueLock, NULL);
m_mChannel[addr] = c;
}
else
{
c = i->second;
}
pthread_mutex_unlock(&m_ChnLock);
pthread_mutex_lock(&c->m_SndLock);
if ((NULL != c->m_pTrans) && c->m_pTrans->isConnected())
{
c->m_iCount ++;
pthread_mutex_unlock(&c->m_SndLock);
return 0;
}
Transport* t = new Transport;
t->open(m_iPort, true, true);
int r = t->connect(ip.c_str(), port);
if (NULL == c->m_pTrans)
c->m_pTrans = t;
else
{
Transport* tmp = c->m_pTrans;
c->m_pTrans = t;
delete tmp;
}
pthread_mutex_unlock(&c->m_SndLock);
return r;
}
示例7: main
int main(int argc, char *argv[]) {
if(2 > argc) {
printf("Less address!\n");
return 1;
}
Logger::logSetup();
Logger::setLogLevel(0);
char *address = argv[1];
char host[100] = {0};
char path[1024*1024] = {0};
char spec[200] = {0};
int port = -1;
sscanf(address, "http://%[-a-zA-Z0-9.]:%d%s", host, &port, path);
if(0 == host[0] || -1 == port) {
sscanf(address, "%[-a-zA-Z0-9.]:%d%s", host, &port, path);
}
if(0 == host[0] || -1 == port) {
port = 80;
sscanf(address, "http://%[-a-zA-Z0-9.]%s", host, path);
}
if(0 == host[0] || -1 == port) {
sscanf(address, "%[-a-zA-Z0-9.]%s", host, path);
}
if(0 == host[0] || -1 == port) {
printf("Wrong address!\n");
return 1;
}
if(0 == path[0]) {
path[0] = '/';
path[1] = 0;
}
sprintf(spec, "tcp:%s:%d", host, port);
Transport transport;
transport.start();
Connection *connection = NULL;
HTTPPacketFactory factory;
HTTPStreamer streamer(&factory);
connection = transport.connect(spec, &streamer);
if (NULL == connection) {
printf("Failed to connect server %s\n", spec);
exit(1);
}
HTTPPacket *requst = new HTTPPacket;
requst->setMethod(HTTPPacket::HM_GET);
requst->setURI(path);
requst->addHeader("Accept", "*/*");
requst->addHeader("Connection", "Keep-Alive");
requst->addHeader("Host", (const char*)(spec+4));
Packet *ret = connection->sendPacket(requst);
HTTPPacket *reply = NULL;
if (NULL != ret && ret->isRegularPacket()
&& (reply = dynamic_cast<HTTPPacket*>(ret)))
{
printf("------------reply from '%s' ----------\r\n", address);
printf("%s %d %s\r\n", reply->getVersion() ? "HTTP/1.1" : "HTTP/1.0",
reply->getStatusCode(), reply->getReasonPhrase());
for (HTTPPacket::ConstHeaderIterator it = reply->headerBegin();
it != reply->headerEnd(); it ++)
{
printf("%s: %s\r\n", it->first, it->second);
}
printf("\r\n");
if (reply->getBody()) {
fwrite(reply->getBody(), 1, reply->getBodyLen(), stdout);
}
printf("\n-----------end of reply-------------------\n");
} else {
printf("Fail to get reply from '%s' ----------\r\n", address);
ControlPacket *cmd = dynamic_cast<ControlPacket*>(ret);
if (cmd) {
printf("%s", cmd->what());
}
}
connection->subRef();
transport.stop();
transport.wait();
return 0;
}