当前位置: 首页>>代码示例>>C++>>正文


C++ TCPConnection::readData方法代码示例

本文整理汇总了C++中TCPConnection::readData方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPConnection::readData方法的具体用法?C++ TCPConnection::readData怎么用?C++ TCPConnection::readData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPConnection的用法示例。


在下文中一共展示了TCPConnection::readData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: testReadData

  void TCPCONNECTIONTF::testReadData() {
    ANET_LOG(DEBUG, "BEGIN testReadData()");
    Socket *client = new Socket;
    client->setAddress("localhost", 12345);
    client->connect();
    Socket *accept = _server->accept();
    CPPUNIT_ASSERT(accept);

    //    ConnPacketFactory *factory = new ConnPacketFactory;
    //    DefaultPacketStreamer *streamer = new DefaultPacketStreamer(factory);
    
    //tricket 47
    TCPConnection *tmpConn = new TCPConnection(accept, _streamer, NULL);
    DataBuffer buffer;
    buffer.writeInt32(ANET_PACKET_FLAG + 1);
    buffer.writeInt32(111);
    buffer.writeInt32(222);
    const char *data = "just for test";
    buffer.writeInt32(strlen(data) - 1);
    buffer.writeBytes(data, strlen(data));
    ANET_LOG(DEBUG, "buffer(%p) length:(%d)",&buffer, buffer.getDataLen());
    client->write(buffer.getData(), buffer.getDataLen());
    CPPUNIT_ASSERT(!tmpConn->readData());

    buffer.clear();
    buffer.writeInt32(ANET_PACKET_FLAG);
    buffer.writeInt32(111);
    buffer.writeInt32(222);
    buffer.writeInt32(-1);
    buffer.writeBytes(data, strlen(data));
    client->write(buffer.getData(), buffer.getDataLen());
    CPPUNIT_ASSERT(!tmpConn->readData());

    buffer.clear();
    buffer.writeInt32(ANET_PACKET_FLAG);
    buffer.writeInt32(111);
    buffer.writeInt32(222);
    buffer.writeInt32(strlen(data)/2);
    buffer.writeBytes(data, strlen(data));
    client->write(buffer.getData(), buffer.getDataLen());
    CPPUNIT_ASSERT(!tmpConn->readData());
    delete tmpConn;
    delete accept;
    client->close();
    delete client;

    //    TCPConnection *conn = new TCPConnection(_client, NULL, NULL);
    
    _conn->setServer(false);
    _conn->postPacket(new ConnPacket(11), _handler, NULL);
    _conn->postPacket(new ConnPacket(21), _handler, NULL);
    _conn->postPacket(new ConnPacket(31), _handler, NULL);
    _conn->writeData();

    // flag(0x416e457) chid pcode datalen 
    //DataBuffer *input = &_conn->_input;
    TCPServerAdapter *adapter = new TCPServerAdapter;
    TCPConnection *connAcc = 
      new TCPConnection(_accept, _streamer, adapter);
    connAcc->setServer(true);
    connAcc->_streamer = _streamer;
    connAcc->_iocomponent = 
      new TCPComponent(_transport, _accept);
    //connAcc->_streamer, NULL);
    CPPUNIT_ASSERT(connAcc->readData());
    CPPUNIT_ASSERT_EQUAL(3, adapter->handNum);

    //test error packet
    adapter->reset(); //set the hand packet num to zero
    _conn->postPacket(new ConnPacket(20, 11), _handler, NULL);
    _conn->postPacket(new ConnPacket(20, 30), _handler, NULL);
    CPPUNIT_ASSERT(_conn->writeData());
    CPPUNIT_ASSERT(connAcc->readData());
    CPPUNIT_ASSERT_EQUAL(2, adapter->handNum);

    ANET_LOG(SPAM, "connAcc(%p), ioc (%p)", connAcc, connAcc->_iocomponent);
    delete connAcc->_iocomponent;
    delete connAcc;
    _accept = NULL;
    delete adapter;
    ANET_LOG(DEBUG,"END testReadData()");
  }
开发者ID:AllanXiang,项目名称:Source,代码行数:82,代码来源:tcpconnectiontf.cpp


注:本文中的TCPConnection::readData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。