streambuf etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
streambuf etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Ocak 2018 Çarşamba

asio streambuf'ı InputStream Haline Getirme

Giriş
Şu satırı dahil ederiz.
#include <boost/asio/streambuf.hpp>
#include <boost/asio/buffer.hpp>
Örnek - primitive okuma
Önce yazıp sonra okumak için şöyle yaparız
boost::asio::streambuf sb;

// now write:
{
    std::ostream os(&sb);
    os << "Hello 1 2 3" << std::flush;
}

// or read:
{   std::istream is(&sb);
    std::string s;
    int a, b, c;
    is >> s >> a >> b >> c;
}
Örnek - satır okuma
Şöyle yaparız.
std::string extract_istream(asio::streambuf& sb)
{
  std::istream is(std::addressof(sb));
  std::string line;
  std::getline(is, line);
  return line;
}
Örnek - satır okuma
streambuf nesnesi şöyle bir string ile dolu olsun.
Love is a beautiful thing\n
Way more beautiful than money\n
It is clear that
Satır satır okumak için şöyle yaparız.
istream is(&buffer);
for (std::string line; getline(is, line); )
  // process the line
3 tane satır okuruz. Son satır \n ile bitmediği halde stream'in sonu olduğu için okunur.

Örnek - satır okuma
Satır okumak için şöyle yaparız.
boost::asio::streambuf buf;

/*put data into buf*/
std::istream is(&buf);
std::string line;
std::getline(is, line);
Örnek - satır okuma
Şöyle yaparız.
boost::asio::streambuf sb;
auto bytes = boost::asio::read_until(socket, sb, "\r\n\r\n");

std::istream is(&sb);
std::string line;
while (getline(is, line) && !line.empty())    {
  std::cout << "Received: '" << line << "'\n";
}

// sb still contains un-consumed data, if any
Örnek - tümünü string olarak okuma
streambuf nesnesine string yazmak ve daha sonra tekrar string elde etmek için şöyle yaparız.
/* Convert std::string --> boost::asio::streambuf */
boost::asio::streambuf sbuf;
std::iostream os(&sbuf);
std::string message("Teststring");
os << message;

/* Convert boost::asio::streambuf --> std::string */
std::string str((std::istreambuf_iterator<char>(&sbuf)),
                 std::istreambuf_iterator<char>());

std::cout << str << std::endl;
Şöyle yaparız.
asio::streambuf request;
asio::read_until(*sock.get(), request, '\n');

std::string s( (std::istreambuf_iterator<char>(&request)),
                std::istreambuf_iterator<char>() );
Örnek - iterator haline getirme
InputStrem'den iterator almak için şöyle yaparız.
std::istream stream(&buf);
std::istream_iterator<char> begin(stream); std::istream_iterator<char> end;

26 Ekim 2017 Perşembe

asio streambuf Sınıfı

Giriş
Bu sınıf asio::async_read_until ile kolayca kullanılır.
std::string until = ...;
asio::async_read_until(socket,streambu, until, readHandler);
Not
Eğer tcp kullanıyorsak bu sınıf yerine tcp::iostream tercih edilmeli.

Constructor - default
Şöyle yaparız.
boost::asio::streambuf buf;
Constructor - size
Açıklaması şöyle
The constructor for basic_streambuf accepts a size_t argument specifying the maximum of the sum of the sizes of the input sequence and output sequence. During the lifetime of the basic_streambuf object, the following invariant holds:
size() <= max_size()
Any member function that would, if successful, cause the invariant to be violated shall throw an exception of class std::length_error.
Şöyle yaparız.
boost::asio::streambuf buf (1024);
Şöyle yaparız.
boost::asio::streambuf buf (100 * 1024); //Limit memory for headers
commit metodu
asio streambuf'a Direkt Yazma yazısına taşıdım.

consume metodu
asio streambuf'tan Direkt Okuma yazısına taşıdım.

data metodu
Döndürdüğü tip şöyledir.
boost::asio::streambuf::const_buffers_type bufs = buf.data();
Sınıfın içindeki veri şöyle pointer haline getirilir.
const char* pData = asio::buffer_cast<const char*>(buf.data());
Şöyle yaparız.
//buf is a streambuf
const char *pData = boost::asio::buffer_cast<const char *> (buf.data() );
auto sz = buf.size();
Verinin bir kısmını string'e kopyalamak için şöyle yaparız.
// Copy 10 bytes from the input sequence.  
auto data = buf.data();
std::string str(boost::asio::buffers_begin(data),
                boost::asio::buffers_begin(data) + 10);
Verinin tamamını string'e kopyalamak için şöyle yaparız.
boost::asio::streambuf buf;...
std::string target{
  buffers_begin (buf.data()),
  buffers_end   (buf.data())
};
Verinin tamamını string'e kopyalamak için şöyle yaparız.
const auto bufferIterator = boost::asio::buffers_begin(buf.data());
const std::string message (bufferIterator, bufferIterator + bytes_transferred);
buf.consume (bytes_transferred);
Verinin tamamını vector'e kopyalamak için şöyle yaparız.
boost::asio::streambuf buf;
...
std::vector<char> target (buf.size());
buffer_copy (boost::asio::buffer(target), buf.data());
Veriyi socket'ten göndermek için şöyle yaparız.
// Write to server.
streambuf buf;std::ostream output(&buf);output << "mystring";
write (socket, buf.data());
Bir projede HTTP cevabını göndermek için şöyle yaptım.

boost::asio::streambuf  buffer (512);
std::ostream os (&buffer);

os << "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n";

socket.send (buffer.data ());

in_avail metodu
Şöyle yaparız.
std::streamsize sa = buf.in_avail();
prepare metodu
Sınıf mutable_buffer haline prepare() metoduyla getirilir.Belirtilen büyüklük kadar bellek ayrılır.Bu bellek yazma işlemi için kullanılır.

Şöyle yaparız.
boost::asio::streambuf streambuf;
boost::asio::streambuf::mutable_buffers_type mutableBuffer = streambuf.prepare(5);
Şöyle yaparız.
// Prepare 1024 bytes for the output sequence.
boost::asio::streambuf streambuf;
streambuf.prepare(1024);
Şöyle yaparız.
buf.prepare(buf.max_size())
Şöyle yaparız.
boost::asio::streambuf streamBuffer;
boost::asio::streambuf::mutable_buffers_type mutableBuffer =
      streamBuffer.prepare(max_length);
socket_.async_read_some(boost::asio::buffer(mutableBuffer),...);
size metodu
Şöyle yaparız.
auto bytes = buf.size();
Diğer Notlar

InputStream Haline Getirme
asio streambuf'ı InputStream Haline Getirme yazısına taşıdım.

OutputStream Haline Getirme
asio streambuf'ı OutputStream Haline Getirme yazısına taşıdım.

buffers_begin metodu
Şöyle yaparız.
asio::streambuf buf;
std::ostream(&buf) << "123, 4, 5; 78, 8, 9;\n888, 8, 8;";

auto itBegin = a::buffers_begin(input.data()),
  itEnd = a::buffers_end(input.data());