void IWebClientBase::listenMoreBytes(ByteArray &piece, boost::system::error_code &error) { ByteArray myPiece; myPiece.assign(BUFFER_SIZE(), NULL); socket->read_some(boost::asio::buffer(piece), error); if (error) return; piece.write(myPiece); }
sf::Error GlobPromise::read (const ds::Range & range, ByteArray & dst) { if (!mReady) { assert (false); return error::InvalidArgument; } if (mError) { return mError; } if (!range.isValid()) return error::InvalidArgument; ds::Range r = range.clipTo (ds::Range(0, mSerializedListing.size())); bool eof = r.to == (int64_t) mSerializedListing.size(); dst.assign (mSerializedListing.begin() + r.from, mSerializedListing.begin() + r.to); if (eof) return error::Eof; return NoError; }
void IWebClientBase::listenBinary(size_t size, ByteArray &piece, shared_ptr<Invoke> invoke, boost::system::error_code &error) { ByteArray *data = nullptr; size_t paramIndex; for (paramIndex = 0; paramIndex < invoke->size(); paramIndex++) if (invoke->at(paramIndex)->getType() == "ByteArray") { const ByteArray &byteArray = invoke->at(paramIndex)->referValue<ByteArray>(); if (byteArray.size() == 0 && byteArray.capacity() == size) { data = (ByteArray*)&byteArray; break; } } if (data == nullptr) return; data->insert ( data->end(), piece.begin(), piece.begin() + std::min(piece.size(), size) ); while (data->size() < size) { piece.assign(BUFFER_SIZE(), 1000); socket->read_some(boost::asio::buffer(piece), error); data->insert ( data->end(), piece.begin(), piece.begin() + std::min(piece.size(), size - data->size()) ); } for(size_t i = paramIndex + 1; i < invoke->size(); i++) if(invoke->at(i)->getType() == "ByteArray") return; _replyData(invoke); }
TEST_F(ByteArrayTest, BasicTest) { ByteArray data; EXPECT_EQ(true, data.isNull()); data = ByteArray(10); EXPECT_EQ(false, data.isNull()); EXPECT_EQ(10, data.length()); data.assign("hogehoge", 0, 8); EXPECT_EQ(10, data.length()); EXPECT_EQ("hogehoge", string(data.data(), 8)); EXPECT_NE("fuga", string(data.data(), 8)); data = passArray(data); EXPECT_EQ(false, data.isNull()); EXPECT_EQ(10, data.length()); EXPECT_EQ("hogehoge", string(data.data(), 8)); EXPECT_NE("fuga", string(data.data(), 8)); ByteArray data2("0123456789abcdef", 16); EXPECT_EQ(false, data2.isNull()); EXPECT_EQ(16, data2.length()); EXPECT_EQ("0123456789abcdef", string(data2.data(), 16)); ByteArray data3("xyz", 3); ByteArray concated = data2 + data3; EXPECT_EQ(false, concated.isNull()); EXPECT_EQ(16 + 3, concated.length()); EXPECT_EQ("0123456789abcdefxyz", string(concated.data(), 16 + 3)); concated[10] = 'p'; EXPECT_EQ("0123456789pbcdefxyz", string(concated.data(), 16 + 3)); data3.data()[1] = 'c'; concated = data3 + data2; EXPECT_EQ(false, concated.isNull()); EXPECT_EQ(16 + 3, concated.length()); EXPECT_EQ("xcz0123456789abcdef", string(concated.data(), 16 + 3)); data.reset(); EXPECT_EQ(true, data.isNull()); }
/* ----------------------------------------------------------------------------------- LISTENERS ----------------------------------------------------------------------------------- */ void IClient::listen() { //BASIC DATA string str = ""; std::shared_ptr<Invoke> ba_invoke(nullptr); while (true) { ByteArray piece; boost::system::error_code error; piece.assign(BUFFER_SIZE(), NULL); size_t size = socket->read_some(boost::asio::buffer(piece), error); if (error) break; if(ba_invoke == nullptr) handleString(piece, str, ba_invoke, size); else handleBinary(piece, str, ba_invoke, size); } }
/* ----------------------------------------------------------------------- LISTEN MESSAGE ----------------------------------------------------------------------- */ void IWebClientBase::listen() { shared_ptr<Invoke> binaryInvoke; // CLIENT ADDS MASK unsigned char mask = DIRECTION() == SERVER ? 128 : 0; while (true) { ByteArray piece; boost::system::error_code error; piece.assign(BUFFER_SIZE(), NULL); socket->read_some(boost::asio::buffer(piece), error); while (piece.getPosition() != piece.size()) { if (error) return; // GET TYPE unsigned char typeHeader = piece.read<unsigned char>(); if (piece.leftSize() == 0) continue; // GET SIZE TYPE unsigned char sizeHeader = piece.read<unsigned char>() - mask; if (piece.leftSize() == 0) continue; // GET SIZE size_t size = 125; if (sizeHeader == 126) { if (piece.leftSize() < sizeof(unsigned short)) listenMoreBytes(piece, error); if (error) break; size = (size_t)piece.readReversely<unsigned short>(); } else if (sizeHeader == 127) { if (piece.leftSize() < sizeof(unsigned long long)) listenMoreBytes(piece, error); if (error) break; size = (size_t)piece.readReversely<unsigned long long>(); } // GET DATA FOLLOWING TYPE if (typeHeader == (unsigned char)129) binaryInvoke = listenString(size, piece, error); else if (typeHeader == (unsigned char)130 && binaryInvoke != nullptr) listenBinary(size, piece, binaryInvoke, error); else break; } } }