本文整理汇总了C++中DataBuffer::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ DataBuffer::clear方法的具体用法?C++ DataBuffer::clear怎么用?C++ DataBuffer::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataBuffer
的用法示例。
在下文中一共展示了DataBuffer::clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: translateReceivedEvents
int NetworkFunctions::translateReceivedEvents(Packet * serverEvents)
{
while (serverEvents->getReceiveBufferSize() > 0) {
DataBuffer * tempBuffer = new DataBuffer(MAX_PACKET_SIZE);
int charCount = 0;
int functionIndex = -1;
int bufferLength = -1;
for (int i = 0; i < serverEvents->getReceiveBufferSize(); i++) {
tempBuffer->copy(tempBuffer->getSize(), serverEvents->getReceiveBuffer() + i, 1);
if (charCount == 3) {
memcpy(&functionIndex, tempBuffer->getData(), 4);
if (functionIndex < 0 || functionIndex >= FUNCTION_COUNT || functionIndex == 10) {
while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
tempBuffer->clear();
continue;
}
else {
bufferLength = bufferSizes[functionIndex];
}
}
if (charCount == 7 && functionIndex == EVENT_UPDATE_GAME_OBJECT) {
unsigned long netID = 0;
memcpy(&netID, tempBuffer->getData() + 4, 4);
GameObject * tempObject = gameObjects->getValue(netID);
if (tempObject == NULL) {
while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
tempBuffer->clear();
continue;
}
/*int objectIndex = -1;
memcpy(&objectIndex, tempBuffer->getData() + 4, 4);
if (objectIndex < 0 || objectIndex >= gameObjects->size()) {
while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
tempBuffer->clear();
continue;
}*/
else {
bufferLength = tempObject->getSerializedSize();
}
}
if (charCount == bufferLength-1) {
if (tempBuffer->getSize() == 0) {
tempBuffer->clear();
continue;
}
executeIndexedFunction(tempBuffer);
tempBuffer->clear();
charCount = 0;
continue;
}
/*if (serverEvents->getReceiveBuffer()[i] == '\n') {
if (tempBuffer->getSize() == 0) {
tempBuffer->clear();
continue;
}
executeIndexedFunction(tempBuffer);
tempBuffer->clear();
}*/
charCount++;
}
delete tempBuffer;
serverEvents->refillReceiveBuffer();
}
return 0;
}
示例2: testDecodeAndEncode
void HttpRequestAndResponsePacketTF::testDecodeAndEncode() {
HttpRequestPacket *request = new HttpRequestPacket;
HttpResponsePacket *response;
DataBuffer output;
DataBuffer input;
PacketHeader header;
// http_load -r 1 -f 2 url
char s1[] = "GET / HTTP/1.0\r\n"
"Host: localhost\r\n"
"User-Agent: http_load 12mar2006\r\n\r\n";
header._dataLen = sizeof(s1)-1;
input.writeBytes(s1, sizeof(s1) - 1);
CPPUNIT_ASSERT(request->decode(&input, &header));
CPPUNIT_ASSERT_EQUAL(1, request->_method);
CPPUNIT_ASSERT_EQUAL(1, request->_version);
CPPUNIT_ASSERT_EQUAL(false, request->isKeepAlive());
CPPUNIT_ASSERT(strcmp("localhost", request->findHeader("Host")) == 0);
CPPUNIT_ASSERT(request->findHeader("User-Agent"));
CPPUNIT_ASSERT(strcmp("http_load 12mar2006", request->findHeader("User-Agent")) == 0);
response = doReply(request);
response->encode(&output);
char r1[] = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 1\r\n\r\n/";
CPPUNIT_ASSERT(strncmp(r1, output.getData(), sizeof(r1) - 1) == 0);
output.clear();
delete response;
request->free();
request = new HttpRequestPacket;
//fbench -q url -c 0 -n 15 -k -s 10 localhost 12345
request->_isKeepAlive = true;
char s2[] = "GET http://localhost:12345?name=huang&jj=yy HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: fbench/0.9\r\n\r\n";
header._dataLen = sizeof(s2)-1;
input.writeBytes(s2, sizeof(s2) - 1);
CPPUNIT_ASSERT(request->decode(&input, &header));
CPPUNIT_ASSERT_EQUAL(1, request->_method);
CPPUNIT_ASSERT_EQUAL(2, request->_version);
CPPUNIT_ASSERT_EQUAL(true, request->isKeepAlive());
CPPUNIT_ASSERT(strcmp("localhost", request->findHeader("Host")) == 0);
CPPUNIT_ASSERT(strcmp("fbench/0.9", request->findHeader("User-Agent")) == 0);
CPPUNIT_ASSERT(strcmp(request->getQuery(), "http://localhost:12345?name=huang&jj=yy") == 0);
response = doReply(request);
response->encode(&output);
char r2[] = "HTTP/1.1 200 OK\r\n"
"Connection: Keep-Alive\r\n"
"Keep-Alive: timeout=10, max=10\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 39\r\n\r\n"
"http://localhost:12345?name=huang&jj=yy";
CPPUNIT_ASSERT(strncmp(r2, output.getData(), sizeof(r2) - 1) == 0);
output.clear();
delete response;
request->free();
request = new HttpRequestPacket;
//fbench -q url -c 0 -n 15 -s 10 localhost 12345
char s3[] = "GET http://localhost:12345?name=huang&jj=yy HTTP/1.1\r\n"
"Host: localhost\r\n"
"Connection: close\r\n"
"User-Agent: fbench/0.9\r\n\r\n";
header._dataLen = sizeof(s3)-1;
input.writeBytes(s3, sizeof(s3) - 1);
CPPUNIT_ASSERT(request->decode(&input, &header));
CPPUNIT_ASSERT_EQUAL(1, request->_method);
CPPUNIT_ASSERT_EQUAL(2, request->_version);
CPPUNIT_ASSERT_EQUAL(false, request->isKeepAlive());
CPPUNIT_ASSERT(strcmp("localhost", request->findHeader("Host")) == 0);
CPPUNIT_ASSERT(strcmp("fbench/0.9", request->findHeader("User-Agent")) == 0);
CPPUNIT_ASSERT(strcmp(request->getQuery(), "http://localhost:12345?name=huang&jj=yy") == 0);
response = doReply(request);
response->encode(&output);
char r3[] = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 39\r\n\r\n"
"http://localhost:12345?name=huang&jj=yytp://localhost:12345?name=huang&jj=yy";
CPPUNIT_ASSERT(strncmp(r3, output.getData(), sizeof(r3) - 1) == 0);
output.clear();
delete response;
request->free();
request = new HttpRequestPacket;
//firefox
request->_isKeepAlive = true;
char s4[] ="GET /?name=huang&jj=yy HTTP/1.0\r\n"
"Host: localhost:12345\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9b5) Gecko/2008042803 Red Hat/3.0b5-0.beta5.6.el5 Firefox/3.0b5\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
//.........这里部分代码省略.........
示例3: 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()");
}