Commit ab0e617e authored by Davis King's avatar Davis King

Changed the code a little bit so that it should be more portable and

robust to variations in how std::streamsize is defined.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403009
parent 3fc4e615
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#ifndef DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_ #ifndef DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_
#define DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_ #define DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_
#include "sockstreambuf_kernel_2.h" #include "sockstreambuf_kernel_2.h"
#include "../assert.h"
#include <cstring> #include <cstring>
...@@ -39,7 +40,15 @@ namespace dlib ...@@ -39,7 +40,15 @@ namespace dlib
std::streamsize num std::streamsize num
) )
{ {
int space_left = static_cast<int>(epptr()-pptr()); // 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) if (num <= space_left)
{ {
std::memcpy(pptr(),s,static_cast<size_t>(num)); std::memcpy(pptr(),s,static_cast<size_t>(num));
...@@ -48,10 +57,10 @@ namespace dlib ...@@ -48,10 +57,10 @@ namespace dlib
} }
else else
{ {
std::memcpy(pptr(),s,space_left); std::memcpy(pptr(),s,static_cast<size_t>(space_left));
s += space_left; s += space_left;
pbump(space_left); pbump(space_left);
int num_left = static_cast<int>(num) - space_left; std::streamsize num_left = num - space_left;
if (flush_out_buffer() == EOF) if (flush_out_buffer() == EOF)
{ {
...@@ -61,7 +70,7 @@ namespace dlib ...@@ -61,7 +70,7 @@ namespace dlib
if (num_left < out_buffer_size) if (num_left < out_buffer_size)
{ {
std::memcpy(pptr(),s,num_left); std::memcpy(pptr(),s,static_cast<size_t>(num_left));
pbump(num_left); pbump(num_left);
return num; return num;
} }
......
...@@ -128,9 +128,9 @@ namespace dlib ...@@ -128,9 +128,9 @@ namespace dlib
// member data // member data
connection& con; connection& con;
static const int max_putback = 4; static const std::streamsize max_putback = 4;
static const int out_buffer_size = 10000; static const std::streamsize out_buffer_size = 10000;
static const int in_buffer_size = 10000; static const std::streamsize in_buffer_size = 10000;
char* out_buffer; char* out_buffer;
char* in_buffer; char* in_buffer;
......
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