Commit e4549f7b authored by Davis King's avatar Davis King

Added an option for a sockstreambuf to automatically flush its output

buffers before performing any network reads.
parent 10cf3028
...@@ -109,6 +109,15 @@ namespace dlib ...@@ -109,6 +109,15 @@ namespace dlib
// copy the putback characters into the putback end of the in_buffer // copy the putback characters into the putback end of the in_buffer
std::memmove(in_buffer+(max_putback-num_put_back), gptr()-num_put_back, num_put_back); std::memmove(in_buffer+(max_putback-num_put_back), gptr()-num_put_back, num_put_back);
if (flushes_output_on_read())
{
if (flush_out_buffer() == EOF)
{
// an error occurred
return EOF;
}
}
int num = con.read(in_buffer+max_putback, in_buffer_size-max_putback); int num = con.read(in_buffer+max_putback, in_buffer_size-max_putback);
if (num <= 0) if (num <= 0)
{ {
......
...@@ -40,7 +40,8 @@ namespace dlib ...@@ -40,7 +40,8 @@ namespace dlib
) : ) :
con(*con_), con(*con_),
out_buffer(0), out_buffer(0),
in_buffer(0) in_buffer(0),
autoflush(false)
{ {
init(); init();
} }
...@@ -50,7 +51,8 @@ namespace dlib ...@@ -50,7 +51,8 @@ namespace dlib
) : ) :
con(*con_), con(*con_),
out_buffer(0), out_buffer(0),
in_buffer(0) in_buffer(0),
autoflush(false)
{ {
init(); init();
} }
...@@ -66,6 +68,20 @@ namespace dlib ...@@ -66,6 +68,20 @@ namespace dlib
connection* get_connection ( connection* get_connection (
) { return &con; } ) { return &con; }
void flush_output_on_read()
{
autoflush = true;
}
bool flushes_output_on_read() const
{
return autoflush;
}
void do_not_flush_output_on_read()
{
autoflush = false;
}
protected: protected:
...@@ -140,6 +156,7 @@ namespace dlib ...@@ -140,6 +156,7 @@ namespace dlib
static const std::streamsize in_buffer_size = 10000; static const std::streamsize in_buffer_size = 10000;
char* out_buffer; char* out_buffer;
char* in_buffer; char* in_buffer;
bool autoflush;
}; };
......
...@@ -53,6 +53,7 @@ namespace dlib ...@@ -53,6 +53,7 @@ namespace dlib
- con == a valid connection object - con == a valid connection object
ensures ensures
- *this will read from and write to con - *this will read from and write to con
- #flushes_output_on_read() == false
throws throws
- std::bad_alloc - std::bad_alloc
!*/ !*/
...@@ -65,6 +66,7 @@ namespace dlib ...@@ -65,6 +66,7 @@ namespace dlib
- con == a valid connection object - con == a valid connection object
ensures ensures
- *this will read from and write to con - *this will read from and write to con
- #flushes_output_on_read() == false
throws throws
- std::bad_alloc - std::bad_alloc
!*/ !*/
...@@ -88,6 +90,28 @@ namespace dlib ...@@ -88,6 +90,28 @@ namespace dlib
reads from and writes to reads from and writes to
!*/ !*/
void flush_output_on_read (
);
/*!
ensures
- #flushes_output_on_read() == true
!*/
bool flushes_output_on_read (
) const;
/*!
ensures
- This function returns true if this object will flush its output buffer
to the network socket before performing any network read.
!*/
void do_not_flush_output_on_read (
);
/*!
ensures
- #flushes_output_on_read() == false
!*/
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -17,9 +17,14 @@ namespace dlib ...@@ -17,9 +17,14 @@ namespace dlib
{ {
/*! /*!
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This is an implementation of the interface defined in sockstreambuf_abstract.h This is an implementation of the interface defined in
except that it doesn't do any kind of buffering at all. It just writes sockstreambuf_abstract.h except that it doesn't do any kind of buffering at
data directly to a connection. all. It just writes data directly to a connection. However, note that we
don't implement the flushes_output_on_read() routine as this object always
flushes immediately (since it isn't buffers. Moreover, it should be
pointed out that this object is deprecated and only present for backwards
compatibility with previous versions of dlib. So you really should use the
sockstreambuf object instead.
INITIAL VALUE INITIAL VALUE
con == a connection con == a connection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment