Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created July 1, 2017 07:30
Show Gist options
  • Save rhulha/374b03a6cb6e70dfb4a3a279aca27cee to your computer and use it in GitHub Desktop.
Save rhulha/374b03a6cb6e70dfb4a3a279aca27cee to your computer and use it in GitHub Desktop.
#define _WIN32_WINNT 0x0601
#ifdef _MSC_VER
#include <boost/config/compiler/visualc.hpp>
#endif
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/concepts.hpp>
#include <boost/make_shared.hpp>
#include <boost/exception/all.hpp>
using namespace boost;
class socket_stream_source : public iostreams::source
{
int max=100;
public:
socket_stream_source(int max)
{
this->max = max;
}
std::streamsize read(char* s, std::streamsize n)
{
printf("n: %lld\n", n);
if (!max)
return -1;
for (int i = 0; i < n; i++) {
if (!max--)
return i;
s[i] = 0;
}
return n;
};
};
template <typename T>
class data_input_stream
{
public:
//data_input_stream(boost::shared_ptr<boost::iostreams::stream<boost::iostreams::source>> stream) : _in(stream)
data_input_stream(T* stream) : _in(stream)
{
}
void readAll(char* s, std::streamsize n)
{
std::streamsize pos = 0;
do {
_in->read(s + pos, n - pos);
pos += _in->gcount();
if (_in->fail() || _in->eof())
throw new std::exception("stream ended too soon.");
} while (n < pos);
}
data_input_stream& operator >> (int32_t &i)
{
i = readInt32();
return *this;
}
int readInt32()
{
int i;
_in->read(reinterpret_cast<char *>(&i), sizeof(i));
return i;
}
private:
//boost::iostreams::stream<boost::iostreams::source>* _in;
T* _in;
};
void basic_test()
{
// iostreams::stream<iostreams::source> sock_stream = new iostreams::stream<socket_stream_source>(); // THIS FAILS
iostreams::stream<socket_stream_source> *sock_stream = new iostreams::stream<socket_stream_source>(7);
// iostreams::stream<socket_stream_source> sock_stream(7); // WITHOUT A PARAMETER WE TRIGGER A NOT INITIALIZED ASSERT EXCEPTION
char *a = new char[15];
sock_stream->read(a, 15);
printf("%d\n", (int)a[0]);
printf("%d\n", (int)a[1]);
printf("%d\n", (int)a[2]);
printf("%d\n", (int)a[3]);
}
void test_array_source()
{
char arr[10] = "example";
iostreams::stream<iostreams::array_source> *array_stream = new iostreams::stream<iostreams::array_source>(arr);
data_input_stream<iostreams::stream<iostreams::array_source>> as(array_stream);
if (false) {
int x;
as >> x;
printf("x from array %d\n", x);
} else {
char *a = new char[8];
as.readAll(a, 7);
a[7] = 0;
printf("%s\n", a);
//printf("%d\n", (int)a[1]);
//printf("%d\n", (int)a[2]);
//printf("%d\n", (int)a[3]);
}
}
int main1()
{
iostreams::stream<socket_stream_source> *sock_stream = new iostreams::stream<socket_stream_source>(7);
int x;
data_input_stream<iostreams::stream<socket_stream_source>> dis(sock_stream);
dis >> x;
printf("x from socket %d\n", x);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment