33 template<
typename CharT,
typename Traits>
43 template<
typename CharT,
typename Traits>
45 : m_read_limit(read_limit), m_input_buffer(NULL), m_input_buffer_size(0),
46 m_output_buffer(NULL), m_output_buffer_size(0)
55 template<
typename CharT,
typename Traits>
69 template<
typename CharT,
typename Traits>
72 (
const std::string& address,
int port )
79 if ( connect( address, port ) )
97 template<
typename CharT,
typename Traits>
128 template<
typename CharT,
typename Traits>
142 template<
typename CharT,
typename Traits>
154 template<
typename CharT,
typename Traits>
158 m_read_limit = read_limit;
166 template<
typename CharT,
typename Traits>
172 ssize_t write_count = 0;
173 ssize_t length = (this->pptr() - this->pbase()) *
sizeof(
char_type);
177 write_count = send(m_descriptor, static_cast<const char*>(this->pbase()),
180 if ( write_count >= 0 )
181 this->setp( m_output_buffer, m_output_buffer + m_output_buffer_size );
195 template<
typename CharT,
typename Traits>
203 ssize_t length = m_input_buffer_size *
sizeof(
char_type);
204 int_type result = traits_type::eof();
210 read_count = recv(m_descriptor, static_cast<char*>(m_input_buffer), length,
215 if ( read_count > 0 )
217 this->setg( m_input_buffer, m_input_buffer, m_input_buffer + read_count);
218 result = this->sgetc();
221 this->setg( m_input_buffer, m_input_buffer + m_input_buffer_size,
222 m_input_buffer + m_input_buffer_size );
232 template<
typename CharT,
typename Traits>
239 int_type result = traits_type::eof();
243 result = traits_type::not_eof(c);
245 if ( !traits_type::eq_int_type(c, traits_type::eof()) )
260 template<
typename CharT,
typename Traits>
262 (
const std::string& addr,
int port )
274 template<
typename CharT,
typename Traits>
280 m_input_buffer_size = m_output_buffer_size = s_buffer_size;
282 m_input_buffer =
new char_type[m_input_buffer_size];
283 m_output_buffer =
new char_type[m_output_buffer_size];
285 this->setg( m_input_buffer, m_input_buffer + m_input_buffer_size,
286 m_input_buffer + m_input_buffer_size );
287 this->setp( m_output_buffer, m_output_buffer + m_output_buffer_size );
295 template<
typename CharT,
typename Traits>
298 if ( m_input_buffer )
300 delete[] m_input_buffer;
301 m_input_buffer = NULL;
304 if ( m_output_buffer )
306 delete[] m_output_buffer;
307 m_output_buffer = NULL;
310 this->setg( NULL, NULL, NULL );
311 this->setp( NULL, NULL );
319 template<
typename CharT,
typename Traits>
322 return this->pbase() && this->pptr() && this->epptr()
323 && this->eback() && this->gptr() && this->egptr();
basic_socket * open()
Open the socket.
virtual int_type overflow(int_type c=traits_type::eof())
Synchronize the output buffer (ie. write in the socket).
traits_type::int_type int_type
The type used to represent integers.
virtual int_type underflow()
Fill the input buffer.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
CharT char_type
The type of the characters in the stream.
int descriptor
Type of the system description of the socket.
bool is_open() const
Tell if the socket is open.
self_type * close()
Close the socket.
basic_socket * close()
Close the socket.
bool is_open() const
Tell if the socket is open.
virtual int sync()
Write the buffered data in the socket.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
basic_socketbuf(int read_limit=-1)
Constructor.
Some assert macros to strengthen you code.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
Socket buffer to be used with std::basic_stream, for easy socket reading and writing.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
void set_read_time_limit(int read_limit)
Set the number of second to wait before considering nothing will come in the socket.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
virtual ~basic_socketbuf()
Destructor.