summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/sockstreambuf
diff options
context:
space:
mode:
Diffstat (limited to 'ml/dlib/dlib/sockstreambuf')
-rw-r--r--ml/dlib/dlib/sockstreambuf/sockstreambuf.cpp177
-rw-r--r--ml/dlib/dlib/sockstreambuf/sockstreambuf.h172
-rw-r--r--ml/dlib/dlib/sockstreambuf/sockstreambuf_abstract.h127
-rw-r--r--ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.cpp168
-rw-r--r--ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.h118
5 files changed, 0 insertions, 762 deletions
diff --git a/ml/dlib/dlib/sockstreambuf/sockstreambuf.cpp b/ml/dlib/dlib/sockstreambuf/sockstreambuf.cpp
deleted file mode 100644
index e328e4259..000000000
--- a/ml/dlib/dlib/sockstreambuf/sockstreambuf.cpp
+++ /dev/null
@@ -1,177 +0,0 @@
-// Copyright (C) 2003 Davis E. King (davis@dlib.net)
-// License: Boost Software License See LICENSE.txt for the full license.
-#ifndef DLIB_SOCKStREAMBUF_CPp_
-#define DLIB_SOCKStREAMBUF_CPp_
-
-#include "sockstreambuf.h"
-#include "../assert.h"
-
-#include <cstring>
-
-namespace dlib
-{
-
-// ----------------------------------------------------------------------------------------
- // output functions
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf::int_type sockstreambuf::
- overflow (
- int_type c
- )
- {
- if (c != EOF)
- {
- *pptr() = c;
- pbump(1);
- }
- if (flush_out_buffer() == EOF)
- {
- // an error occurred
- return EOF;
- }
- return c;
- }
-
-// ----------------------------------------------------------------------------------------
-
- std::streamsize sockstreambuf::
- xsputn (
- const char* s,
- std::streamsize num
- )
- {
- // Add a sanity check here
- DLIB_ASSERT(num >= 0,
- "\tstd::streamsize sockstreambuf::xsputn"
- << "\n\tThe number of bytes to write can't be negative"
- << "\n\tnum: " << num
- << "\n\tthis: " << this
- );
-
- std::streamsize space_left = static_cast<std::streamsize>(epptr()-pptr());
- if (num <= space_left)
- {
- std::memcpy(pptr(),s,static_cast<size_t>(num));
- pbump(static_cast<int>(num));
- return num;
- }
- else
- {
- std::memcpy(pptr(),s,static_cast<size_t>(space_left));
- s += space_left;
- pbump(space_left);
- std::streamsize num_left = num - space_left;
-
- if (flush_out_buffer() == EOF)
- {
- // the write was not successful so return that 0 bytes were written
- return 0;
- }
-
- if (num_left < out_buffer_size)
- {
- std::memcpy(pptr(),s,static_cast<size_t>(num_left));
- pbump(num_left);
- return num;
- }
- else
- {
- if (con.write(s,num_left) != num_left)
- {
- // the write was not successful so return that 0 bytes were written
- return 0;
- }
- return num;
- }
- }
- }
-
-// ----------------------------------------------------------------------------------------
- // input functions
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf::int_type sockstreambuf::
- underflow(
- )
- {
- if (gptr() < egptr())
- {
- return static_cast<unsigned char>(*gptr());
- }
-
- int num_put_back = static_cast<int>(gptr() - eback());
- if (num_put_back > max_putback)
- {
- num_put_back = max_putback;
- }
-
- // 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);
-
- 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);
- if (num <= 0)
- {
- // an error occurred or the connection is over which is EOF
- return EOF;
- }
-
- // reset in_buffer pointers
- setg (in_buffer+(max_putback-num_put_back),
- in_buffer+max_putback,
- in_buffer+max_putback+num);
-
- return static_cast<unsigned char>(*gptr());
- }
-
-// ----------------------------------------------------------------------------------------
-
- std::streamsize sockstreambuf::
- xsgetn (
- char_type* s,
- std::streamsize n
- )
- {
- std::streamsize temp = n;
- while (n > 0)
- {
- int num = static_cast<int>(egptr() - gptr());
- if (num >= n)
- {
- // copy data from our buffer
- std::memcpy(s, gptr(), static_cast<size_t>(n));
- gbump(static_cast<int>(n));
- return temp;
- }
-
- // read more data into our buffer
- if (num == 0)
- {
- if (underflow() == EOF)
- break;
- continue;
- }
-
- // copy all the data from our buffer
- std::memcpy(s, gptr(), num);
- n -= num;
- gbump(num);
- s += num;
- }
- return temp-n;
- }
-
-// ----------------------------------------------------------------------------------------
-
-}
-#endif // DLIB_SOCKStREAMBUF_CPp_
-
diff --git a/ml/dlib/dlib/sockstreambuf/sockstreambuf.h b/ml/dlib/dlib/sockstreambuf/sockstreambuf.h
deleted file mode 100644
index f5b450e78..000000000
--- a/ml/dlib/dlib/sockstreambuf/sockstreambuf.h
+++ /dev/null
@@ -1,172 +0,0 @@
-// Copyright (C) 2003 Davis E. King (davis@dlib.net)
-// License: Boost Software License See LICENSE.txt for the full license.
-#ifndef DLIB_SOCKStREAMBUF_Hh_
-#define DLIB_SOCKStREAMBUF_Hh_
-
-#include <iosfwd>
-#include <streambuf>
-#include "../sockets.h"
-#include "sockstreambuf_abstract.h"
-#include "sockstreambuf_unbuffered.h"
-
-namespace dlib
-{
-
-// ----------------------------------------------------------------------------------------
-
- class sockstreambuf : public std::streambuf
- {
- /*!
- INITIAL VALUE
- - con == a connection
- - in_buffer == an array of in_buffer_size bytes
- - out_buffer == an array of out_buffer_size bytes
-
- CONVENTION
- - in_buffer == the input buffer used by this streambuf
- - out_buffer == the output buffer used by this streambuf
- - max_putback == the maximum number of chars to have in the put back buffer.
- !*/
-
- public:
-
- // These typedefs are here for backwards compatibility with previous versions of
- // dlib.
- typedef sockstreambuf_unbuffered kernel_1a;
- typedef sockstreambuf kernel_2a;
-
- sockstreambuf (
- connection* con_
- ) :
- con(*con_),
- out_buffer(0),
- in_buffer(0),
- autoflush(false)
- {
- init();
- }
-
- sockstreambuf (
- const std::unique_ptr<connection>& con_
- ) :
- con(*con_),
- out_buffer(0),
- in_buffer(0),
- autoflush(false)
- {
- init();
- }
-
- virtual ~sockstreambuf (
- )
- {
- sync();
- delete [] out_buffer;
- delete [] in_buffer;
- }
-
- connection* get_connection (
- ) { 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:
-
- void init (
- )
- {
- try
- {
- out_buffer = new char[out_buffer_size];
- in_buffer = new char[in_buffer_size];
- }
- catch (...)
- {
- if (out_buffer) delete [] out_buffer;
- throw;
- }
- setp(out_buffer, out_buffer + (out_buffer_size-1));
- setg(in_buffer+max_putback,
- in_buffer+max_putback,
- in_buffer+max_putback);
- }
-
- int flush_out_buffer (
- )
- {
- int num = static_cast<int>(pptr()-pbase());
- if (con.write(out_buffer,num) != num)
- {
- // the write was not successful so return EOF
- return EOF;
- }
- pbump(-num);
- return num;
- }
-
- // output functions
- int_type overflow (
- int_type c
- );
-
- int sync (
- )
- {
- if (flush_out_buffer() == EOF)
- {
- // an error occurred
- return -1;
- }
- return 0;
- }
-
- std::streamsize xsputn (
- const char* s,
- std::streamsize num
- );
-
- // input functions
- int_type underflow(
- );
-
- std::streamsize xsgetn (
- char_type* s,
- std::streamsize n
- );
-
- private:
-
- // member data
- connection& con;
- static const std::streamsize max_putback = 4;
- static const std::streamsize out_buffer_size = 10000;
- static const std::streamsize in_buffer_size = 10000;
- char* out_buffer;
- char* in_buffer;
- bool autoflush;
-
- };
-
-// ----------------------------------------------------------------------------------------
-
-}
-
-#ifdef NO_MAKEFILE
-#include "sockstreambuf.cpp"
-#endif
-
-#endif // DLIB_SOCKStREAMBUF_Hh_
-
diff --git a/ml/dlib/dlib/sockstreambuf/sockstreambuf_abstract.h b/ml/dlib/dlib/sockstreambuf/sockstreambuf_abstract.h
deleted file mode 100644
index 12be84193..000000000
--- a/ml/dlib/dlib/sockstreambuf/sockstreambuf_abstract.h
+++ /dev/null
@@ -1,127 +0,0 @@
-// Copyright (C) 2003 Davis E. King (davis@dlib.net)
-// License: Boost Software License See LICENSE.txt for the full license.
-#undef DLIB_SOCKSTREAMBUF_ABSTRACT_
-#ifdef DLIB_SOCKSTREAMBUF_ABSTRACT_
-
-#include <iosfwd>
-#include <memory>
-#include <streambuf>
-
-#include "../sockets/sockets_kernel_abstract.h"
-
-namespace dlib
-{
-
-// ----------------------------------------------------------------------------------------
-
- class sockstreambuf : public std::streambuf
- {
- /*!
- WHAT THIS OBJECT REPRESENTS
- This object represents a stream buffer capable of writing to and
- reading from TCP connections.
-
- NOTE:
- For a sockstreambuf EOF is when the connection has closed or otherwise
- returned some kind of error.
-
- Also note that any data written to the streambuf may be buffered
- internally. So if you need to ensure that data is actually sent then you
- should flush the stream.
-
- A read operation is guaranteed to block until the number of bytes
- requested has arrived on the connection. It will never keep blocking
- once enough data has arrived.
-
- THREADING
- Generally speaking, this object has the same kind of threading
- restrictions as a connection object. those being:
- - Do not try to write to a sockstreambuf from more than one thread.
- - Do not try to read from a sockstreambuf from more than one thread.
- - You may call shutdown() on the connection object and this will
- cause any reading or writing calls to end. To the sockstreambuf it
- will appear the same as hitting EOF. (note that EOF for a sockstreambuf
- means that the connection has closed)
- - It is safe to read from and write to the sockstreambuf at the same time
- from different threads so long as flushes_output_on_read()==false.
- - It is not safe to try to putback a char and read from the stream from
- different threads
- !*/
- public:
- sockstreambuf (
- connection* con
- );
- /*!
- requires
- - con == a valid connection object
- ensures
- - *this will read from and write to con
- - #flushes_output_on_read() == false
- throws
- - std::bad_alloc
- !*/
-
- sockstreambuf (
- const std::unique_ptr<connection>& con
- );
- /*!
- requires
- - con == a valid connection object
- ensures
- - *this will read from and write to con
- - #flushes_output_on_read() == false
- throws
- - std::bad_alloc
- !*/
-
- ~sockstreambuf (
- );
- /*!
- requires
- - get_connection() object has not been deleted
- ensures
- - sockstream buffer is destructed but the connection object will
- NOT be closed.
- - Any buffered data is flushed to the connection.
- !*/
-
- connection* get_connection (
- );
- /*!
- ensures
- - returns a pointer to the connection object which this buffer
- 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.
- - if (flushes_output_on_read() == true)
- - It is not safe to make concurrent read and write calls to this object.
- !*/
-
- void do_not_flush_output_on_read (
- );
- /*!
- ensures
- - #flushes_output_on_read() == false
- !*/
-
- };
-
-// ----------------------------------------------------------------------------------------
-
-}
-
-#endif // DLIB_SOCKSTREAMBUF_ABSTRACT_
-
diff --git a/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.cpp b/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.cpp
deleted file mode 100644
index 4dcefc17b..000000000
--- a/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.cpp
+++ /dev/null
@@ -1,168 +0,0 @@
-// Copyright (C) 2003 Davis E. King (davis@dlib.net)
-// License: Boost Software License See LICENSE.txt for the full license.
-#ifndef DLIB_SOCKSTrEAMBUF_UNBUFFERED_CPp_
-#define DLIB_SOCKSTrEAMBUF_UNBUFFERED_CPp_
-
-#include "sockstreambuf_unbuffered.h"
-
-
-namespace dlib
-{
-
-// ----------------------------------------------------------------------------------------
- // output functions
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf_unbuffered::int_type sockstreambuf_unbuffered::
- overflow (
- int_type c
- )
- {
- if (c != EOF)
- {
- char temp = static_cast<char>(c);
- if (con.write(&temp,1) != 1)
- {
- // if the write was not successful
- return EOF;
- }
- }
- return c;
- }
-
-// ----------------------------------------------------------------------------------------
-
- std::streamsize sockstreambuf_unbuffered::
- xsputn (
- const char* s,
- std::streamsize num
- )
- {
- if (con.write(s,static_cast<int>(num)) != num)
- {
- // the write was not successful so return that 0 bytes were written
- return 0;
- }
- return num;
- }
-
-// ----------------------------------------------------------------------------------------
- // input functions
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf_unbuffered::int_type sockstreambuf_unbuffered::
- underflow(
- )
- {
- if (lastread_next)
- {
- return lastread;
- }
- else if (peek != EOF)
- {
- return peek;
- }
- else
- {
- char temp;
- if (con.read(&temp,1) != 1)
- {
- // some error occurred
- return EOF;
- }
- peek = static_cast<unsigned char>(temp);
- return peek;
- }
- }
-
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf_unbuffered::int_type sockstreambuf_unbuffered::
- uflow(
- )
- {
- if (lastread_next)
- {
- lastread_next = false;
- return lastread;
- }
- else if (peek != EOF)
- {
- lastread = peek;
- peek = EOF;
- return lastread;
- }
- else
- {
- char temp;
- if (con.read(&temp,1) != 1)
- {
- // some error occurred
- return EOF;
- }
- lastread = static_cast<unsigned char>(temp);
- return lastread;
- }
- }
-
-// ----------------------------------------------------------------------------------------
-
- sockstreambuf_unbuffered::int_type sockstreambuf_unbuffered::
- pbackfail(
- int_type c
- )
- {
- // if they are trying to push back a character that they didn't read last
- // that is an error
- if (c != EOF && c != lastread)
- return EOF;
-
- // if they are trying to push back a second character then thats an error
- if (lastread_next)
- return EOF;
-
- lastread_next = true;
- return 1;
- }
-
-// ----------------------------------------------------------------------------------------
-
- std::streamsize sockstreambuf_unbuffered::
- xsgetn (
- char_type* s,
- std::streamsize n
- )
- {
- std::streamsize temp = n;
- if (lastread_next && n > 0)
- {
- *s = lastread;
- lastread_next = false;
- ++s;
- --n;
- }
- if (peek != EOF && n > 0)
- {
- *s = peek;
- peek = EOF;
- ++s;
- --n;
- }
-
- while (n>0)
- {
- int status = con.read(s,static_cast<int>(n));
- if (status < 1)
- break;
- n -= status;
- s += status;
- }
-
- return temp-n;
- }
-
-// ----------------------------------------------------------------------------------------
-
-}
-#endif // DLIB_SOCKSTrEAMBUF_UNBUFFERED_CPp_
-
diff --git a/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.h b/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.h
deleted file mode 100644
index 8aa5992db..000000000
--- a/ml/dlib/dlib/sockstreambuf/sockstreambuf_unbuffered.h
+++ /dev/null
@@ -1,118 +0,0 @@
-// Copyright (C) 2003 Davis E. King (davis@dlib.net)
-// License: Boost Software License See LICENSE.txt for the full license.
-#ifndef DLIB_SOCKSTrEAMBUF_UNBUFFERED_Hh_
-#define DLIB_SOCKSTrEAMBUF_UNBUFFERED_Hh_
-
-#include <iosfwd>
-#include <streambuf>
-#include "../sockets.h"
-#include "sockstreambuf_abstract.h"
-
-namespace dlib
-{
-
-// ----------------------------------------------------------------------------------------
-
- class sockstreambuf_unbuffered : public std::streambuf
- {
- /*!
- WHAT THIS OBJECT REPRESENTS
- This is an implementation of the interface defined in
- sockstreambuf_abstract.h except that it doesn't do any kind of buffering at
- 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
- con == a connection
- lastread_next == false
- peek == EOF
-
- CONVENTION
- if (peek != EOF) then
- peek == the last character read from the connection object and
- is used to store the char in the event the user peeks by
- calling sgetc()
- if (lastread != EOF) then
- lastread == the last character read and consumed by the user
-
- if (lastread_next) then
- the next character to be returned to the user is lastread because
- the user put it back into the buffer
-
- !*/
-
- public:
-
-
- sockstreambuf_unbuffered (
- connection* con_
- ) :
- con(*con_),
- peek(EOF),
- lastread_next(false)
- {}
-
- sockstreambuf_unbuffered (
- const std::unique_ptr<connection>& con_
- ) :
- con(*con_),
- peek(EOF),
- lastread_next(false)
- {}
-
- connection* get_connection (
- ) { return &con; }
-
-
- protected:
-
- // output functions
- int_type overflow (
- int_type c
- );
-
- std::streamsize xsputn (
- const char* s,
- std::streamsize num
- );
-
- // input functions
- int_type underflow(
- );
-
- int_type uflow(
- );
-
- int_type pbackfail(
- int_type c
- );
-
- std::streamsize xsgetn (
- char_type* s,
- std::streamsize n
- );
-
- private:
-
- // member data
- connection& con;
- int_type peek;
- int_type lastread;
- bool lastread_next;
-
- };
-
-// ----------------------------------------------------------------------------------------
-
-}
-
-#ifdef NO_MAKEFILE
-#include "sockstreambuf_unbuffered.cpp"
-#endif
-
-#endif // DLIB_SOCKSTrEAMBUF_UNBUFFERED_Hh_
-