本文整理汇总了C++中DataBuffer::getDataLen方法的典型用法代码示例。如果您正苦于以下问题:C++ DataBuffer::getDataLen方法的具体用法?C++ DataBuffer::getDataLen怎么用?C++ DataBuffer::getDataLen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataBuffer
的用法示例。
在下文中一共展示了DataBuffer::getDataLen方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testEncode
void DEFAULTPACKETSTREAMERTF::testEncode() {
MyPacketFactory *factory = new MyPacketFactory();
DefaultPacketStreamer *streamer
= new DefaultPacketStreamer(factory);
MyPacket packet(16);
DataBuffer buffer;
PacketHeader *header = new PacketHeader;
packet.setPacketHeader(header);
delete header;
header = packet.getPacketHeader();
header->_chid = 1111;
header->_pcode = 2222;
CPPUNIT_ASSERT(streamer->encode(&packet, &buffer));
CPPUNIT_ASSERT_EQUAL(32, (int)buffer.getDataLen());
buffer.drainData(4);
CPPUNIT_ASSERT_EQUAL(1111, (int)buffer.readInt32());
CPPUNIT_ASSERT_EQUAL(2222, (int)buffer.readInt32());
CPPUNIT_ASSERT_EQUAL(16, (int)buffer.readInt32());
MyPacket packet1(100, false);
CPPUNIT_ASSERT(!streamer->encode(&packet1, &buffer));
CPPUNIT_ASSERT_EQUAL(16, (int)buffer.getDataLen());
delete streamer;
delete factory;
}
示例2: testDrainData
void DataBufferTF::testDrainData()
{
DataBuffer buffer;
char str[] = "just for test";
buffer.writeBytes(str, sizeof(str)-1);
int dataLen = buffer.getDataLen();
buffer.drainData(0);
CPPUNIT_ASSERT_EQUAL(dataLen, buffer.getDataLen());
buffer.drainData(sizeof(str)*2);
CPPUNIT_ASSERT_EQUAL(0, buffer.getDataLen());
}
示例3: testExpand
void DataBufferTF::testExpand()
{
DataBuffer buffer;
buffer.expand(0);
CPPUNIT_ASSERT(buffer._pstart);
int64_t content = 0x12345678;
buffer.writeInt64(content);
int oldDataLen = buffer.getDataLen();
buffer.expand(MAX_BUFFER_SIZE);
CPPUNIT_ASSERT_EQUAL(oldDataLen, buffer.getDataLen());
CPPUNIT_ASSERT(buffer.getFreeLen() >= MAX_BUFFER_SIZE);
CPPUNIT_ASSERT_EQUAL(content, buffer.readInt64());
}
示例4: switch
size_t
GenericHeader::Tag::write(DataBuffer &buf) const
{
int pos = buf.getDataLen();
buf.writeBytes(_name.c_str(), _name.size() + 1);
buf.writeInt8(_type);
switch (_type) {
case TYPE_FLOAT:
buf.writeDouble(_fVal);
break;
case TYPE_INTEGER:
buf.writeInt64(_iVal);
break;
case TYPE_STRING:
buf.writeBytes(_sVal.c_str(), _sVal.size() + 1);
break;
default:
LOG_ASSERT(false);
}
return buf.getDataLen() - pos;
}
示例5: testGetPacketInfo
void DEFAULTPACKETSTREAMERTF::testGetPacketInfo() {
DataBuffer buffer;
PacketHeader header;
DefaultPacketStreamer streamer(NULL);
bool broken;
buffer.writeInt32(ANET_PACKET_FLAG);
buffer.writeInt32(1111);
buffer.writeInt32(2222);
buffer.writeInt32(-1);
buffer.writeBytes("data for test", 16);
CPPUNIT_ASSERT(!streamer.getPacketInfo(&buffer, &header, &broken));
CPPUNIT_ASSERT_EQUAL(1111, (int)header._chid);
CPPUNIT_ASSERT_EQUAL(2222, (int)header._pcode);
CPPUNIT_ASSERT_EQUAL(-1, (int)header._dataLen);
CPPUNIT_ASSERT(broken);
buffer.drainData(buffer.getDataLen());
streamer._existPacketHeader = false;
CPPUNIT_ASSERT(!streamer.getPacketInfo(&buffer, &header, &broken));
}
示例6: 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()");
}