TEST(ByteArraySuite, copy) { const shared_int SIZE = 100; char buffer[SIZE]; // Copy ByteArray copyFrom; ByteArray copyTo; EXPECT_TRUE(copyFrom.init(&buffer[0], SIZE)); EXPECT_TRUE(copyTo.load(copyFrom)); EXPECT_EQ((shared_int)copyTo.getBufferSize(), SIZE); EXPECT_TRUE(copyTo.load(copyFrom)); EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE); // Copy too large ByteArray tooBig; if (tooBig.getMaxBufferSize()-1 <= std::numeric_limits<shared_int>::max()) { shared_int TOO_BIG = tooBig.getMaxBufferSize()-1; char bigBuffer[TOO_BIG]; EXPECT_TRUE(tooBig.init(&bigBuffer[0], TOO_BIG)); EXPECT_FALSE(copyTo.load(tooBig)); // A failed load should not change the buffer. EXPECT_EQ((shared_int)copyTo.getBufferSize(), 2*SIZE); } else std::cout << std::string(15, ' ') << "ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl; }
TEST(ByteArraySuite, loading) { const shared_int SIZE = 100; char buffer[SIZE]; ByteArray bytes; ByteArray empty; ASSERT_TRUE(bytes.init(&buffer[0], SIZE)); shared_bool bIN = true, bOUT = false; shared_int iIN = 999, iOUT = 0; shared_real rIN = 9999.9999, rOUT = 0; // Boolean loading EXPECT_TRUE(bytes.load(bIN)); EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_bool)); EXPECT_TRUE(bytes.unload(bOUT)); EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE); EXPECT_EQ(bOUT, bIN); // Integer loading EXPECT_TRUE(bytes.load(iIN)); EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_int)); EXPECT_TRUE(bytes.unload(iOUT)); EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE); EXPECT_EQ(iOUT, iIN); // Real loading EXPECT_TRUE(bytes.load(rIN)); EXPECT_EQ(bytes.getBufferSize(), SIZE+sizeof(shared_real)); EXPECT_TRUE(bytes.unload(rOUT)); EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE); EXPECT_EQ(rOUT, rIN); // Unloading a single member (down to an empty buffer size) EXPECT_TRUE(empty.load(bIN)); EXPECT_EQ(empty.getBufferSize(), sizeof(shared_bool)); EXPECT_TRUE(empty.unload(bOUT)); EXPECT_EQ((int)empty.getBufferSize(), 0); EXPECT_EQ(bOUT, bIN); // Loading two members (unloading the first) and then checking the value of the second rOUT = 0.0; iOUT = 0; EXPECT_TRUE(empty.load(rIN)); EXPECT_EQ(empty.getBufferSize(), sizeof(shared_real)); EXPECT_TRUE(empty.load(iIN)); EXPECT_EQ(empty.getBufferSize(), sizeof(shared_real)+sizeof(shared_int)); EXPECT_TRUE(empty.unloadFront(rOUT)); EXPECT_EQ(rOUT, rIN); EXPECT_TRUE(empty.unload(iOUT)); EXPECT_EQ((int)empty.getBufferSize(), 0); EXPECT_EQ(iOUT, iIN); }
bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes) { int rc = this->SOCKET_FAIL; bool rtn = false; // Reset the buffer (this is not required since the buffer length should // ensure that we don't read any of the garbage that may be left over from // a previous read), but it is good practice. memset(&this->buffer_, 0, sizeof(this->buffer_)); // Doing a sanity check to determine if the byte array buffer is larger than // what can be sent in the socket. This should not happen and might be indicative // of some code synchronization issues between the client and server base. if (this->MAX_BUFFER_SIZE < (int)buffer.getMaxBufferSize()) { LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u", this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize()); } if (this->isConnected()) { rc = rawReceiveBytes(this->buffer_, num_bytes); if (this->SOCKET_FAIL != rc) { if (rc > 0) { LOG_COMM("Byte array receive, bytes read: %u", rc); buffer.init(&this->buffer_[0], rc); rtn = true; } else { LOG_WARN("Recieved zero bytes: %u", rc); rtn = false; } } else { this->logSocketError("Socket received failed", rc); rtn = false; } } else { rtn = false; LOG_WARN("Not connected, bytes not sent"); } if (!rtn) { this->setConnected(false); } return rtn; }
TEST(ByteArraySuite, byteSwapping) { if(ByteArray::isByteSwapEnabled()) { ASSERT_TRUE(ByteArray::isByteSwapEnabled()); ByteArray swapped; unsigned char buffer[] = { 0x00, 0x00, 0x00, 0x38, // be: 56 0x00, 0x00, 0x00, 0x0a, // be: 10 0x00, 0x00, 0x00, 0x01, // be: 1 0x3e, 0x81, 0x32, 0x64, // be: 0.25233757495880127 0x3f, 0x30, 0x4b, 0x75, // be: 0.68865138292312622 0x3f, 0xa8, 0x9d, 0xd2, // be: 1.3173162937164307 0x3f, 0x85, 0x93, 0xdd, // be: 1.0435749292373657 0xbf, 0xf4, 0x8c, 0xc5, // be: -1.9105459451675415 }; const unsigned int bufferLength = 32; shared_int tempInt; shared_real tempReal; swapped.init((const char*) buffer, bufferLength); ASSERT_EQ(swapped.getBufferSize(), bufferLength); ASSERT_TRUE(swapped.unload(tempReal)); EXPECT_FLOAT_EQ(tempReal, -1.9105459451675415); ASSERT_TRUE(swapped.unload(tempReal)); EXPECT_FLOAT_EQ(tempReal, 1.0435749292373657); ASSERT_TRUE(swapped.unload(tempReal)); EXPECT_FLOAT_EQ(tempReal, 1.3173162937164307); ASSERT_TRUE(swapped.unload(tempReal)); EXPECT_FLOAT_EQ(tempReal, 0.68865138292312622); ASSERT_TRUE(swapped.unload(tempReal)); EXPECT_FLOAT_EQ(tempReal, 0.25233757495880127); ASSERT_TRUE(swapped.unload(tempInt)); EXPECT_EQ(tempInt, 1); ASSERT_TRUE(swapped.unload(tempInt)); EXPECT_EQ(tempInt, 10); ASSERT_TRUE(swapped.unload(tempInt)); EXPECT_EQ(tempInt, 56); ASSERT_EQ(swapped.getBufferSize(), 0); } }
void SimpleMessage::toByteArray(ByteArray & msg) { msg.init(); msg.load(this->getMessageType()); msg.load(this->getCommType()); msg.load(this->getReplyCode()); if (this->data_.getBufferSize() > 0 ) { msg.load(this->getData().getRawDataPtr(), this->data_.getBufferSize()); } }
bool UdpServer::makeConnect() { ByteArray send; char sendHS = this->CONNECT_HANDSHAKE; char recvHS = 0; int bytesRcvd = 0; const int timeout = 1000; // Time (ms) between handshake sends bool rtn = false; send.load((void*)&sendHS, sizeof(sendHS)); if (!this->isConnected()) { this->setConnected(false); // Listen for handshake. Once received, break // listen loop. do { ByteArray recv; recvHS = 0; if (this->isReadyReceive(timeout)) { bytesRcvd = this->rawReceiveBytes(this->buffer_, 0); if (bytesRcvd > 0) { LOG_DEBUG("UDP server received %d bytes while waiting for handshake", bytesRcvd); recv.init(&this->buffer_[0], bytesRcvd); recv.unload((void*)&recvHS, sizeof(recvHS)); } } } while(recvHS != sendHS); // Send a reply handshake this->rawSendBytes(send.getRawDataPtr(), send.getBufferSize()); this->setConnected(true); rtn = true; } else { LOG_WARN("Tried to connect when socket already in connected state"); rtn = true; } return rtn; }
TEST(ByteArraySuite, init) { const shared_int SIZE = 100; ByteArray bytes; char buffer[SIZE]; // Valid byte arrays EXPECT_TRUE(bytes.init(&buffer[0], SIZE)); EXPECT_EQ((shared_int)bytes.getBufferSize(), SIZE); // Invalid init (too big) if (bytes.getMaxBufferSize() < std::numeric_limits<shared_int>::max()) { shared_int TOO_BIG = bytes.getMaxBufferSize()+1; char bigBuffer[TOO_BIG]; EXPECT_FALSE(bytes.init(&bigBuffer[0], TOO_BIG)); } else std::cout << std::string(15, ' ') << "ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl; }
bool UdpClient::makeConnect() { ByteArray send; char sendHS = this->CONNECT_HANDSHAKE; char recvHS = 0; bool rtn = false; const int timeout = 1000; // Time (ms) between handshake sends int bytesRcvd = 0; if (!this->isConnected()) { this->setConnected(false); send.load((void*)&sendHS, sizeof(sendHS)); do { ByteArray recv; recvHS = 0; LOG_DEBUG("UDP client sending handshake"); this->rawSendBytes(send.getRawDataPtr(), send.getBufferSize()); if (this->isReadyReceive(timeout)) { bytesRcvd = this->rawReceiveBytes(this->buffer_, 0); LOG_DEBUG("UDP client received possible handshake"); recv.init(&this->buffer_[0], bytesRcvd); recv.unload((void*)&recvHS, sizeof(recvHS)); } } while(recvHS != sendHS); LOG_INFO("UDP client connected"); rtn = true; this->setConnected(true); } else { rtn = true; LOG_WARN("Tried to connect when socket already in connected state"); } return rtn; }
bool SimpleMessage::init(int msgType, int commType, int replyCode) { ByteArray data; data.init(); return this->init(msgType, commType, replyCode, data); }