本文整理汇总了C++中stream::ptr::read方法的典型用法代码示例。如果您正苦于以下问题:C++ ptr::read方法的具体用法?C++ ptr::read怎么用?C++ ptr::read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream::ptr
的用法示例。
在下文中一共展示了ptr::read方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: baseStream
MORDOR_UNITTEST(BufferedStream, partiallyBufferedReadRawBuffer)
{
MemoryStream::ptr baseStream(new MemoryStream("0123456789"));
BufferedStream::ptr bufferedStream(new BufferedStream(baseStream));
bufferedStream->bufferSize(3);
Stream::ptr stream = bufferedStream;
char buffer[3];
buffer[2] = '\0';
MORDOR_TEST_ASSERT_EQUAL(stream->read(buffer, 2), 2u);
MORDOR_TEST_ASSERT_EQUAL((const char *)buffer, "01");
MORDOR_TEST_ASSERT_EQUAL(stream->read(buffer, 2), 2u);
MORDOR_TEST_ASSERT_EQUAL((const char *)buffer, "23");
}
示例2: sslserver
MORDOR_UNITTEST(SSLStream, basic)
{
WorkerPool pool;
std::pair<Stream::ptr, Stream::ptr> pipes = pipeStream();
SSLStream::ptr sslserver(new SSLStream(pipes.first, false));
SSLStream::ptr sslclient(new SSLStream(pipes.second, true));
pool.schedule(boost::bind(&accept, sslserver));
sslclient->connect();
pool.dispatch();
Stream::ptr server = sslserver, client = sslclient;
char buf[6];
buf[5] = '\0';
client->write("hello");
client->flush(false);
MORDOR_TEST_ASSERT_EQUAL(server->read(buf, 5), 5u);
MORDOR_TEST_ASSERT_EQUAL((const char *)buf, "hello");
server->write("world");
server->flush(false);
MORDOR_TEST_ASSERT_EQUAL(client->read(buf, 5), 5u);
MORDOR_TEST_ASSERT_EQUAL((const char *)buf, "world");
}
示例3: blockingWrite
static void blockingWrite(Stream::ptr stream, int &sequence)
{
MORDOR_TEST_ASSERT_EQUAL(++sequence, 3);
Buffer output;
MORDOR_TEST_ASSERT_EQUAL(stream->read(output, 10), 5u);
MORDOR_TEST_ASSERT(output == "hello");
}
示例4: readWorld
static void readWorld(Stream::ptr stream, int &sequence)
{
MORDOR_TEST_ASSERT_EQUAL(++sequence, 1);
char buf[6];
buf[5] = '\0';
MORDOR_TEST_ASSERT_EQUAL(stream->read(buf, 5), 5u);
MORDOR_TEST_ASSERT_EQUAL(++sequence, 3);
MORDOR_TEST_ASSERT_EQUAL((const char *)buf, "world");
}
示例5: threadStress
void threadStress(Stream::ptr stream)
{
size_t totalRead = 0;
size_t totalWritten = 0;
size_t buf[64];
Buffer buffer;
for (int i = 0; i < 10000; ++i) {
if (i % 2) {
size_t toRead = 64;
size_t read = stream->read(buffer, toRead * sizeof(size_t));
MORDOR_TEST_ASSERT(read % sizeof(size_t) == 0);
buffer.copyOut(&buf, read);
for (size_t j = 0; read > 0; read -= sizeof(size_t), ++j) {
MORDOR_TEST_ASSERT_EQUAL(buf[j], ++totalRead);
}
buffer.clear();
} else {
size_t toWrite = 64;
for (size_t j = 0; j < toWrite; ++j) {
buf[j] = ++totalWritten;
}
buffer.copyIn(buf, toWrite * sizeof(size_t));
size_t written = stream->write(buffer, toWrite * sizeof(size_t));
totalWritten -= (toWrite - written / sizeof(size_t));
buffer.clear();
}
}
stream->close(Stream::WRITE);
while (true) {
size_t toRead = 64;
size_t read = stream->read(buffer, toRead);
if (read == 0)
break;
MORDOR_TEST_ASSERT(read % sizeof(size_t) == 0);
buffer.copyOut(&buf, read);
for (size_t i = 0; read > 0; read -= sizeof(size_t), ++i) {
MORDOR_TEST_ASSERT_EQUAL(buf[i], ++totalRead);
}
buffer.clear();
}
stream->flush();
}
示例6: readALot
static void readALot(Stream::ptr stream)
{
unsigned char buffer;
for (size_t i = 0; i < A_LOT_OF_ITERATIONS; ++i)
MORDOR_TEST_ASSERT_EQUAL(stream->read(&buffer, 1u), 1u);
}
示例7: tunnel
Stream::ptr tunnel(HTTP::StreamBroker::ptr streamBroker, const URI &proxy,
IPAddress::ptr targetIP, const std::string &targetDomain,
unsigned short targetPort, unsigned char version)
{
MORDOR_ASSERT(version == 4 || version == 5);
MORDOR_ASSERT(version == 5 || !targetIP ||
targetIP->family() == AF_INET);
MORDOR_ASSERT(streamBroker);
MORDOR_ASSERT(targetIP || !targetDomain.empty());
std::string buffer;
buffer.resize(std::max<size_t>(targetDomain.size() + 1u, 16u) + 9);
Stream::ptr stream = streamBroker->getStream(proxy);
if (version == 5) {
buffer[0] = version;
buffer[1] = 1;
buffer[2] = 0;
size_t written = 0;
while (written < 3)
written += stream->write(buffer.data() + written, 3 - written);
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
if (buffer[0] != 5)
MORDOR_THROW_EXCEPTION(ProtocolViolationException());
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
if ((unsigned char)buffer[0] == 0xff)
MORDOR_THROW_EXCEPTION(NoAcceptableAuthenticationMethodException());
if (buffer[0] != 0)
MORDOR_THROW_EXCEPTION(ProtocolViolationException());
}
buffer[0] = version;
buffer[1] = 1;
size_t size;
if (version == 4) {
if (targetIP)
*(unsigned short *)&buffer[2] = htons(targetIP->port());
else
*(unsigned short *)&buffer[2] = htons(targetPort);
if (targetIP)
*(unsigned int *)&buffer[4] = htonl((unsigned int)(((sockaddr_in *)targetIP->name())->sin_addr.s_addr));
else
*(unsigned int *)&buffer[4] = htonl(0x00000001);
buffer[8] = 0;
if (!targetIP) {
memcpy(&buffer[9], targetDomain.c_str(), targetDomain.size());
buffer[9 + targetDomain.size()] = 0;
}
size = 9 + targetDomain.size() + (targetDomain.empty() ? 0 : 1);
} else {
buffer[2] = 0;
if (targetIP) {
if (targetIP->family() == AF_INET) {
buffer[3] = 1;
*(unsigned int *)&buffer[4] = htonl((unsigned int)(((sockaddr_in *)targetIP->name())->sin_addr.s_addr));
size = 7;
} else {
buffer[3] = 4;
memcpy(&buffer[4], &((sockaddr_in6 *)targetIP->name())->sin6_addr, 16);
size = 19;
}
} else {
buffer[3] = 3;
buffer[4] = (unsigned char)targetDomain.size();
memcpy(&buffer[5], targetDomain.c_str(), targetDomain.size());
size = 5 + targetDomain.size();
}
if (targetIP)
*(unsigned short *)&buffer[size] = htons(targetIP->port());
else
*(unsigned short *)&buffer[size] = htons(targetPort);
size += 2;
}
size_t written = 0;
while (written < size)
written += stream->write(buffer.data() + written, size - written);
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
if ((version == 4 && buffer[0] != 0) || (version == 5 && buffer[0] != 5))
MORDOR_THROW_EXCEPTION(ProtocolViolationException());
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
if ((version == 4 && buffer[0] != 0x5a) || (version == 5 && buffer[0] != 0))
MORDOR_THROW_EXCEPTION(InvalidResponseException(buffer[0]));
if (version == 4) {
size = 0;
while (size < 6) {
written = stream->read(&buffer[0], 6 - size);
if (written == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
size += written;
}
} else {
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
if (buffer[0] != 0)
MORDOR_THROW_EXCEPTION(InvalidResponseException(buffer[0]));
if (stream->read(&buffer[0], 1) == 0)
MORDOR_THROW_EXCEPTION(UnexpectedEofException());
//.........这里部分代码省略.........