本文整理汇总了C++中DataBuffer::getData方法的典型用法代码示例。如果您正苦于以下问题:C++ DataBuffer::getData方法的具体用法?C++ DataBuffer::getData怎么用?C++ DataBuffer::getData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataBuffer
的用法示例。
在下文中一共展示了DataBuffer::getData方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendDatagram
int UDPManager::sendDatagram(const char * data, int length, unsigned long ipAddress, unsigned long port, unsigned long frameCount)
{
if (initErrors || data == NULL || length == 0 || port == 0) return -1;
sockaddr_in otherAddress;
otherAddress.sin_family = AF_INET;
otherAddress.sin_addr.s_addr = ipAddress;
otherAddress.sin_port = port;
DataBuffer * sendBuffer = new DataBuffer(length + 4);
//sendBuffer->copy(0, &length, sizeof(int));
sendBuffer->copy(0, &frameCount, 4);
sendBuffer->copy(4, data, length);
if (sendto(sock, sendBuffer->getData(), sendBuffer->getSize(), 0, (struct sockaddr *)&otherAddress, sizeof(otherAddress)) < 0) {
perror("UDP sendto() failed\n");
delete sendBuffer;
return -1;
}
//printf("UDP Send:");
//Utilities::printfNetworkPacket(sendBuffer->getData(), sendBuffer->getSize());
delete sendBuffer;
return 0;
}
示例2: synchClient
int Server::synchClient(int clientID)
{
//aquireClientsMutex(clientID);
//Clear Objects
DataBuffer * temp = NetworkFunctions::createClearObjectsBuffer();
clients[clientID]->pushSendData(temp->getData(), temp->getSize());
delete temp;
//Add Existing Objects
for (list<pair<unsigned long, GameObject*>>::iterator iter = gameObjects->begin(); iter != gameObjects->end(); iter++) {
temp = NetworkFunctions::createCreateObjectBuffer(iter->second);
clients[clientID]->pushSendData(temp->getData(), temp->getSize());
delete temp;
}
/*for (int i = 0; i < gameObjects->size(); i++) {
temp = NetworkFunctions::createCreateObjectBuffer(gameObjects->at(i));
clients[clientID]->pushSendData(temp->getData(), temp->getSize());
delete temp;
}*/
for (int i = 0; i < playerObjects->size(); i++) {
if (playerObjects->at(i) != NULL) {
//Add All Existing Spells to players
/*for (int elementIndex = 0; elementIndex < 5; elementIndex++) {
for (int subIndex = 0; subIndex < playerObjects->at(i)->getSpells()[elementIndex].size(); subIndex++) {
Spell * tempSpell = playerObjects->at(i)->getSpell(elementIndex, subIndex);
if (tempSpell != NULL) {
temp = NetworkFunctions::createAddSpellBuffer(clientID, elementIndex, tempSpell->getSpellID());
clients[clientID]->pushSendData(temp->getData(), temp->getSize());
}
}
}*/
//Change Spell to active one
int currentElementIndex, currentSubIndex;
playerObjects->at(i)->getActiveSpellIndices(¤tElementIndex, ¤tSubIndex);
temp = NetworkFunctions::createChangeSpellBuffer(i, currentElementIndex, currentSubIndex);
clients[clientID]->pushSendData(temp->getData(), temp->getSize());
}
}
//send all the items to create
//releaseClientsMutex(clientID);
return 0;
}
示例3: testSocket
// ---------------------------------------------
void testSocket(string ip, unsigned short port) {
Socket* socket = new Socket("null", 0);
Socket::InetAddressPort remoteAddrAndPort;
remoteAddrAndPort.port = port;
Socket::getSockaddrByIpAndPort(&remoteAddrAndPort.addr, ip, port);
socket->makeNonBlocking();
while (true) {
bool result = socket->connect(remoteAddrAndPort);
if (result) {
cout << "connect success!" << endl;
cm::Thread::sleep(100);
char sendData[] = "TCP client request";
int numberOfBytesSent;
if (SKT_SUCC == socket->send(sendData, strlen(sendData), numberOfBytesSent)) {
assert(numberOfBytesSent == strlen(sendData));
cout << "send " << numberOfBytesSent << " bytes data complete" << endl;
DataBuffer* response = new DataBuffer();
int numOfBytesRecved = 0;
while (true) {
result = socket->recv(response->getEndOfDataPointer(), response->getSize() - response->getLength(), numOfBytesRecved);
assert(result != SKT_ERR);
if (result == SKT_SUCC) {
assert(numOfBytesRecved != 0);
response->increaseDataLength(numOfBytesRecved);
cout << "recv " << numOfBytesRecved << " bytes data: " << response->getData() << endl;
response->reset();
break;
} else {
cm::Thread::sleep(1);
}
}
}
socket->close();
break;
} else {
cm::Thread::sleep(1);
}
}
}
示例4: if
DataBuffer * UDPManager::recvDatagram(sockaddr_in ** originAddress)
{
if (initErrors) return NULL;
struct timeval timeout;
struct fd_set fds;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(sock, &fds);
int selectValue = select(0, &fds, 0, 0, &timeout);
if (selectValue > 0) {
DataBuffer * receiveBuffer = new DataBuffer(MAX_PACKET_SIZE);
sockaddr_in * recvAddress = new sockaddr_in();
int recvBufferSize = MAX_PACKET_SIZE;
int returnValue = recvfrom(sock, receiveBuffer->getData(), MAX_PACKET_SIZE, 0, (struct sockaddr *)recvAddress, &recvBufferSize);
if (returnValue == SOCKET_ERROR) {
printf("recvfrom() failed: %d\n", WSAGetLastError());
delete receiveBuffer;
return NULL;
}
receiveBuffer->setSize(returnValue);
if (originAddress == NULL) delete recvAddress;
else *originAddress = recvAddress;
//printf("UDP Rec:");
//Utilities::printfNetworkPacket(receiveBuffer->getData(), receiveBuffer->getSize());
//check that data was sent from client?
return receiveBuffer;
}
else if (selectValue < 0) printf("Error polling for UDP Data\n");
return NULL;
}
示例5: run
int Server::run(const char * ini)
{
readServerData(ini);
//clients = new Client[maxPlayers];
clients = vector<Client*>();
clients.reserve(maxPlayers);
for (int i = 0; i < maxPlayers; i++) {
clients.push_back(new Client());
clients[i]->setID(i);
}
double elapsedTime = 0.0;
double timePerFrame = 1000.0 / updateRate;
double timePerRenderFrame = 1000.0 / renderRefreshRate;
static double timeSinceLastRender = 0.0;
long lastTime = 0;
double targetSleep = 0.0;
double sleepRemainder = 0.0;
double startSleep = 0.0;
double sleepTime = 0.0;
loadConfig();
initSDL();
initGlew();
initBullet();
initGLContexts();
initObjects();
running = true;
HANDLE listenThreadHandle = HostListener::start();
loadResources();
loadLevel("asdf");
while (running) {
frameCount++;
if (GetAsyncKeyState(VK_ESCAPE) && (GetForegroundWindow() == consoleWindow) &&
MessageBox(0, "Shutdown the Server?", "Rune Server Shutdown", MB_YESNO) == IDYES) break;
lastTime = lastTime = SDL_GetTicks();
serverEventsTCP->clearSendBuffer();
serverEventsTCP->clearReceiveBuffer();
serverEventsUDP->clearSendBuffer();
serverEventsUDP->clearReceiveBuffer();
//Get all commands from clients
for (int i = 0; i < maxPlayers; i++) {
DataBuffer * temp;
aquireClientsMutex(i);
temp = clients[i]->popReceiveData();
if (!clients[i]->isConnected() && playerObjects->at(i) != NULL) {
int index = -1;
int x = 0;
for (list<pair<unsigned long, GameObject*>>::iterator iter = gameObjects->begin(); iter != gameObjects->end(); iter++) {
if (playerObjects->at(i) == iter->second) {
index = x;
break;
}
x++;
}
/*for (int x = 0; x < gameObjects->size(); x++) {
if (playerObjects->at(i) == gameObjects->at(x))
index = x;
}*/
gameObjects->remove(playerObjects->at(i)->getNetID());
delete playerObjects->at(i);
playerObjects->at(i) = NULL;
//gameObjects->erase(gameObjects->begin() + index);
serverEventsTCP->appendToSendBuffer(NetworkFunctions::createDeleteObjectBuffer(index));
}
releaseClientsMutex(i);
serverEventsTCP->appendToReceiveBuffer(temp);
}
//Receive All UDP Events
sockaddr_in * originAddress = NULL;
DataBuffer * udpBuffer = UDPManager::recvDatagram(&originAddress);
while (udpBuffer != NULL) {
//origin Address matches client address?
unsigned long packetFrameCount = 0;
memcpy(&packetFrameCount, udpBuffer->getData(), 4);
int clientID = getClientIDByAddress(originAddress->sin_addr.s_addr, originAddress->sin_port);
if (clientID == -1) {
int asdfadsfasdf = 123123;
}
else {
//compare clientFrameCount to client->frameCount
unsigned long clientFrameCount;
aquireClientsMutex(clientID);
clientFrameCount = clients[clientID]->getFrameCount();
releaseClientsMutex(clientID);
if (packetFrameCount < clientFrameCount) {
//discard
printf("Packet from Client %d discarded\n", clientID);
}
//.........这里部分代码省略.........
示例6: 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"
//.........这里部分代码省略.........
示例7: 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;
}
示例8: 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()");
}