//
//
//
//
// Lab. Calcolo II - Esempio di codice
//
//
//
//
//
//
// "C++ in a nutshell" basic_networkbuf class tempate
#ifndef NETSTREAM_H
#define NETSTREAM_H
#include
#include
#include
#include
#include
#include "basic_networkstream.hpp"
template >
class basic_networkbuf : public ::std::basic_streambuf {
public:
typedef charT char_type;
typedef traits traits_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
basic_networkbuf();
virtual ~basic_networkbuf();
bool is_connected();
const std::string &caller() const;
basic_networkbuf* connect(const char* hostname, int port, ::std::ios_base::openmode mode);
basic_networkbuf* connect(int listen_fd, ::std::ios_base::openmode mode);
basic_networkbuf* disconnect();
protected:
virtual ::std::streamsize showmanyc();
virtual int_type underflow();
virtual int_type overflow(int_type c = traits::eof());
virtual basic_networkbuf* setbuf(char_type* buf, ::std::streamsize size);
virtual int sync();
virtual void imbue(const ::std::locale&);
virtual pos_type seekoff(off_type offset, ::std::ios_base::seekdir dir,
::std::ios_base::openmode mode);
virtual pos_type seekpos(pos_type sp, ::std::ios_base::openmode mode);
private:
char_type* buffer_;
::std::streamsize size_;
bool ownbuf_;
static const int DEFAULT_BUFSIZ = 1024;
basic_networkstream f;
};
template
basic_networkbuf::basic_networkbuf()
: buffer_(new char_type[DEFAULT_BUFSIZ]), size_(DEFAULT_BUFSIZ), ownbuf_(true)
{
this->setg(buffer_, buffer_ + size_, buffer_ + size_);
this->setp(buffer_, buffer_ + size_);
}
template
basic_networkbuf::~basic_networkbuf()
{
if (ownbuf_)
delete[] buffer_;
}
template
basic_networkbuf* basic_networkbuf::setbuf(char_type* buf, ::std::streamsize size)
{
if (ownbuf_) {
ownbuf_ = false;
delete [] buffer_;
}
buffer_ = buf;
size_ = size;
this->setg(buffer_, buffer_ + size_, buffer_ + size_);
this->setp(buffer_, buffer_ + size_ - 1);
return this;
}
template
bool basic_networkbuf::is_connected()
{
return f.is_open();
}
template
const std::string &basic_networkbuf::caller() const
{
return f.caller();
}
template
basic_networkbuf* basic_networkbuf::connect(const char* hostname, int port, ::std::ios_base::openmode mode)
{
f.open(hostname, port, mode);
if (f.good())
return this;
else
return 0;
}
template
basic_networkbuf* basic_networkbuf::connect(int listen_fd, ::std::ios_base::openmode mode)
{
f.open(listen_fd, mode);
if (f.good())
return this;
else
return 0;
}
template
basic_networkbuf* basic_networkbuf::disconnect()
{
f.close();
if (f.good())
return this;
else
return 0;
}
template
typename basic_networkbuf::pos_type
basic_networkbuf::seekoff(off_type offset, ::std::ios_base::seekdir dir, ::std::ios_base::openmode mode)
{
if (offset == 0 && dir == ::std::ios_base::cur)
return f.tellg();
else
return static_cast(-1);
}
template
typename basic_networkbuf::pos_type
basic_networkbuf::seekpos(pos_type sp, ::std::ios_base::openmode mode)
{
return static_cast(-1);
}
template
::std::streamsize basic_networkbuf::showmanyc()
{
return this->egptr() - this->gptr();
}
template
typename basic_networkbuf::int_type
basic_networkbuf::underflow()
{
f.read(buffer_, size_);
this->setg(buffer_, buffer_, buffer_ + f.gcount());
if (this->egptr() == this->gptr())
return traits::eof();
else
return traits::to_int_type(*this->gptr());
}
template
typename basic_networkbuf::int_type
basic_networkbuf::overflow(int_type c)
{
if (c != traits::eof()) {
*(this->pptr()) = c;
this->pbump(1);
}
f.write(this->pbase(), this->pptr() - this->pbase());
this->setp(buffer_, buffer_ + size_ - 1);
return traits::not_eof(c);
}
template
int basic_networkbuf::sync()
{
overflow(traits::eof());
f.sync();
return 0;
}
template
void basic_networkbuf::imbue(const ::std::locale&)
{}
template >
class basic_netstream : public ::std::basic_iostream
{
public:
typedef charT char_type;
typedef traits traits_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
basic_netstream(const char* hostname, int port, ::std::ios_base::openmode mode = ::std::ios_base::in) : ::std::basic_iostream(0)
{
this->init(&sb);
connect(hostname, port, mode);
}
basic_netstream(int listen_socket, ::std::ios_base::openmode mode = ::std::ios_base::in) : ::std::basic_iostream(0)
{
this->init(&sb);
connect(listen_socket,mode);
}
basic_netstream(): ::std::basic_iostream(0) { this->init(&sb); }
~basic_netstream() { if (is_connected()) disconnect(); }
void connect(const char* hostname, int port, ::std::ios_base::openmode mode = ::std::ios_base::in) { sb.connect(hostname, port, mode); }
void connect(int listen_socket, ::std::ios_base::openmode mode = ::std::ios_base::in) { sb.connect(listen_socket, mode); }
void disconnect() { sb.pubsync(); sb.disconnect(); }
bool is_connected() { return sb.is_connected(); }
const std::string &caller() { return sb.caller(); }
private:
basic_networkbuf sb;
};
template >
class basic_onetstream : public ::std::basic_ostream
{
public:
typedef charT char_type;
typedef traits traits_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
basic_onetstream(const char* hostname, int port) : ::std::basic_ostream(0)
{
this->init(&sb);
sb.connect(hostname, port, ::std::ios_base::out);
}
basic_onetstream(): ::std::basic_ostream(0) { this->init(&sb); }
~basic_onetstream() { if (is_connected()) disconnect(); }
void connect(const char* hostname, int port) { sb.connect(hostname, port, ::std::ios_base::out); }
void disconnect() { sb.pubsync(); sb.disconnect(); }
bool is_connected() { return sb.is_connected(); }
private:
basic_networkbuf sb;
};
template >
class basic_inetstream : public ::std::basic_istream
{
public:
typedef charT char_type;
typedef traits traits_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
basic_inetstream(const char* hostname, int port) : ::std::basic_istream(0)
{
this->init(&sb);
sb.connect(hostname, port, ::std::ios_base::in);
}
basic_inetstream(int listen_socket) : ::std::basic_istream(0)
{
this->init(&sb);
sb.connect(listen_socket, ::std::ios_base::in);
}
basic_inetstream(): ::std::basic_istream(0) { this->init(&sb); }
~basic_inetstream() { if (is_connected()) disconnect(); }
void connect(const char* hostname, int port) { sb.connect(hostname, port, ::std::ios_base::in); }
void disconnect() { sb.disconnect(); }
bool is_connected() { return sb.is_connected(); }
const std::string &caller() { return sb.caller(); }
private:
basic_networkbuf sb;
};
typedef basic_netstream netstream;
typedef basic_netstream wnetstream;
typedef basic_inetstream inetstream;
typedef basic_inetstream winetstream;
typedef basic_onetstream onetstream;
typedef basic_onetstream wonetstream;
#endif // NETSTREAM_H