/usr/include/dlib/iosockstream/iosockstream.h is in libdlib-dev 18.18-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | // Copyright (C) 2012 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_IOSOCKSTrEAM_Hh_
#define DLIB_IOSOCKSTrEAM_Hh_
#include "iosockstream_abstract.h"
#include <iostream>
#include "../sockstreambuf.h"
#include "../smart_pointers_thread_safe.h"
#include "../timeout.h"
#ifdef _MSC_VER
// Disable the warning about inheriting from std::iostream 'via dominance' since this warning is a warning about
// visual studio conforming to the standard and is ignorable.
// See http://connect.microsoft.com/VisualStudio/feedback/details/733720/inheriting-from-std-fstream-produces-c4250-warning
// for further details if interested.
#pragma warning(disable : 4250)
#endif // _MSC_VER
namespace dlib
{
// ----------------------------------------------------------------------------------------
class iosockstream : public std::iostream
{
public:
iosockstream(
) :
std::iostream(0)
{
}
iosockstream(
const network_address& addr
) :
std::iostream(0)
{
open(addr);
}
iosockstream(
const network_address& addr,
unsigned long timeout
) :
std::iostream(0)
{
open(addr, timeout);
}
~iosockstream()
{
close();
}
void open (
const network_address& addr
)
{
auto_mutex lock(class_mutex);
close();
con.reset(connect(addr));
buf.reset(new sockstreambuf(con.get()));
// Note that we use the sockstreambuf's ability to autoflush instead of
// telling the iostream::tie() function to tie the stream to itself even though
// that should work fine. The reason we do it this way is because there is a
// bug in visual studio 2012 that causes a program to crash when a stream is
// tied to itself and then used. See
// http://connect.microsoft.com/VisualStudio/feedback/details/772293/tying-a-c-iostream-object-to-itself-causes-a-stack-overflow-in-visual-studio-2012
// for further details.
buf->flush_output_on_read();
rdbuf(buf.get());
clear();
}
void open (
const network_address& addr,
unsigned long timeout
)
{
auto_mutex lock(class_mutex);
close(timeout);
con.reset(connect(addr.host_address, addr.port, timeout));
buf.reset(new sockstreambuf(con.get()));
buf->flush_output_on_read();
rdbuf(buf.get());
clear();
}
void close(
unsigned long timeout = 10000
)
{
auto_mutex lock(class_mutex);
rdbuf(0);
try
{
if (buf)
{
dlib::timeout t(*con,&connection::shutdown,timeout);
// This will flush the sockstreambuf and also destroy it.
buf.reset();
if(con->shutdown_outgoing())
{
// there was an error so just close it now and return
con->shutdown();
}
else
{
char junk[100];
// wait for the other end to close their side
while (con->read(junk,sizeof(junk)) > 0);
}
}
}
catch (...)
{
con.reset();
throw;
}
con.reset();
}
void terminate_connection_after_timeout (
unsigned long timeout
)
{
auto_mutex lock(class_mutex);
if (con)
{
con_timeout.reset(new dlib::timeout(*this,&iosockstream::terminate_connection,timeout,con));
}
}
void shutdown (
)
{
auto_mutex lock(class_mutex);
if (con)
con->shutdown();
}
private:
void terminate_connection(
shared_ptr_thread_safe<connection> thecon
)
{
thecon->shutdown();
}
scoped_ptr<timeout> con_timeout;
rmutex class_mutex;
shared_ptr_thread_safe<connection> con;
scoped_ptr<sockstreambuf> buf;
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_IOSOCKSTrEAM_Hh_
|