Loading...
Searching...
No Matches
Socket.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Socket.h
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
//
// This class is a direct derivative from my Unix Network Programming
// class work on generalizing object-oriented network interfaces.
//
//------------------------------------------------------------------------------
// Created: 03/22/1999
//------------------------------------------------------------------------------
#ifndef SOCKET_H
#define SOCKET_H
#include <sys/stat.h>
#include <sys/time.h>
#include <limits.h> // for INT_MAX
#include <stdio.h> // for EOF
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> // for fcntl(2)
#ifdef linux
# include <sys/ioctl.h> // ioctl(2)
#endif
#ifdef sun // ioctl(2)
# include <unistd.h>
# include <stropts.h>
# include <sys/filio.h>
#endif
#include "assa/Address.h"
#define BYTES_LEFT_IN_SOCKBUF(s) ((s).eof () ? -1 : (s).in_avail ())
#define BYTES_LEFT_IN_SIN (cin.eof () ? -1 : cin.rdbuf ()->in_avail ())
namespace ASSA {
class Streambuf; // Forward declaration
class Socket {
public:
static const int PGSIZE;
enum io_state_t {
goodbit = 0,
eofbit = 1,
failbit = 2,
badbit = 4
};
typedef int iostate;
typedef unsigned char IOState;
enum opt_t
{
reuseaddr,
rcvlowat,
sndlowat,
nonblocking
};
Socket();
virtual ~Socket();
virtual bool open(const int domain_) =0;
virtual bool close() =0;
virtual bool connect (const Address& address_);
virtual bool bind (const Address& my_address_) =0;
virtual int write (const char* buf_, const u_int size_);
int getBytesAvail (void) const;
virtual int read (char* buf_, const u_int size_);
int ignore (int n_ = INT_MAX, int delim_ = EOF);
virtual handler_t getHandler() const = 0;
virtual const int getDomain() const = 0;
virtual Streambuf* rdbuf () { return 0; }
virtual Streambuf* rdbuf (Streambuf* /*sb_*/) { return 0; }
virtual int in_avail () const = 0;
virtual Socket& flush ();
bool turnOptionOn (opt_t opt_);
bool turnOptionOff (opt_t opt_);
bool setOption (opt_t opt_, int arg_);
int getOption (opt_t opt_) const;
operator void* () const;
bool operator! () const;
iostate rdstate () const { return m_state; }
void clear (iostate state_ = Socket::goodbit);
void setstate (iostate flag_);
bool good () const { return m_state == 0; }
bool eof () const { return m_state & Socket::eofbit; }
bool fail () const
{
return m_state & (Socket::failbit | Socket::badbit);
}
bool bad () const { return m_state & Socket::badbit; }
void dumpState () const;
static size_t xdr_length (const std::string& s_)
{
return (4 + s_.length () + s_.length () % 4);
}
Socket& operator>> (char& c);
Socket& operator>> (unsigned char& c_)
{
return operator>>((char&) c_);
}
Socket& operator>> (signed char& c_)
{
return operator>>((char&) c_);
}
Socket& operator>> (std::string& s_);
Socket& operator>> (short& n_);
Socket& operator>> (unsigned short& n_);
Socket& operator>> (int& n_);
Socket& operator>> (unsigned int& n_);
Socket& operator>> (long& n_);
Socket& operator>> (unsigned long& n_);
Socket& operator>> (float& n_);
Socket& operator>> (double& n_);
Socket& operator<< (char c);
Socket& operator<< (unsigned char c_)
{
return (*this) << (char) c_;
}
Socket& operator<< (signed char c_)
{
return (*this) << (char) c_;
}
Socket& operator<< (const std::string& s_);
Socket& operator<< (short n_);
Socket& operator<< (unsigned short n_);
Socket& operator<< (int n_);
Socket& operator<< (unsigned int n_);
Socket& operator<< (long n_);
Socket& operator<< (unsigned long n_);
Socket& operator<< (float n_);
Socket& operator<< (double n_);
Socket& operator<< (Socket& (*f) (Socket&))
{
return (f (*this));
}
static bool is_little_endian ();
static void close_handler (handler_t& socket_)
{
#if defined (WIN32)
closesocket (socket_);
#else
::close (socket_);
#endif
disable_handler (socket_);
}
static string decode_fcntl_flags (long mask_);
/*------------------------------------------------------------------
* Protected Members
*------------------------------------------------------------------
*/
protected:
int set_option (int level_, int optname_, int val_);
int set_fd_options (long flags_);
int clear_fd_options (long flags_);
protected:
handler_t m_fd; // u_int, INVALID_SOCKET=(SOCKET)(~0)
int m_type;
#if defined (WIN32)
bool m_nonblocking; // We cannot retrieve the status of the
// socket. So, we remember what it was instead.
#endif
IOState m_state;
//------------------------------------------------------------------------------
// Inline functions
//------------------------------------------------------------------------------
private:
Socket (const Socket&);
Socket& operator= (const Socket&);
};
//------------------------------------------------------------------------------
// Inline functions
//------------------------------------------------------------------------------
inline
Socket::Socket()
:
m_fd (BAD_HANDLER),
m_type(0),
#if defined (WIN32)
m_nonblocking (false),
#endif
m_state(Socket::badbit)
{
trace_with_mask("Socket::Socket",SOCKTRACE);
}
inline
Socket::~Socket ()
{
trace_with_mask("Socket::~Socket",SOCKTRACE);
}
inline bool
Socket::connect (const Address& /* address_ */)
{
trace_with_mask("Socket::connect",SOCKTRACE);
return false;
}
inline int
Socket::write(const char* /*buf_*/, const u_int /*size_*/)
{
trace_with_mask("Socket::write",SOCKTRACE);
return -1;
}
inline int
Socket::read(char* /*buf_*/, const u_int /*size_*/)
{
trace_with_mask("Socket::read()",SOCKTRACE);
return -1;
}
inline
Socket::operator void*() const
{
return fail() ? (void *)0 : (void *)(-1);
}
inline bool
Socket::operator!() const
{
return fail();
}
inline void
Socket::clear(iostate state_)
{
m_state = is_valid_handler (m_fd) ? state_ : state_ | Socket::badbit;
}
inline void
Socket::setstate(iostate flag_)
{
m_state |= flag_;
}
inline
Socket& flush (Socket& os_)
{
os_.flush ();
return (os_);
}
inline
Socket& endl (Socket& os_)
{
char c = '\n';
os_.write (&c, 1);
os_.flush ();
return (os_);
}
inline
Socket& ends (Socket& os_)
{
char c = '\0';
os_.write (&c, 1);
return (os_);
}
} // end namespace ASSA
#include "assa/Streambuf.h"
#endif // SOCKET_H
Address.h
Address is an abstraction for INET or UNIX-domain address data type.
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
BAD_HANDLER
#define BAD_HANDLER
Sort out WIN32/mingw oddities.
Definition Logger_Impl.h:81
handler_t
int handler_t
Definition Logger_Impl.h:82
u_int
unsigned int u_int
Definition Logger_Impl.h:40
Streambuf.h
Streambuf class is based on Standard C++ iostream streambuf class.
ASSA::Address
Definition Address.h:51
ASSA::Socket
Definition Socket.h:71
ASSA::Socket::fail
bool fail() const
Indicates that earlier extraction opeartion has failed to match the required pattern of input.
Definition Socket.h:328
ASSA::Socket::Socket
Socket()
Constructor.
Definition Socket.h:518
ASSA::Socket::clear_fd_options
int clear_fd_options(long flags_)
Gateway method for clearing file descriptor options.
Definition Socket.cpp:147
ASSA::Socket::operator>>
Socket & operator>>(char &c)
Input of built-in char type. The value will be XDR-decoded.
Definition Socket.cpp:359
ASSA::Socket::rdbuf
virtual Streambuf * rdbuf()
Return a pointer to the Streambuf associated with the stream.
Definition Socket.h:240
ASSA::Socket::getDomain
virtual const int getDomain() const =0
Get socket domain.
ASSA::Socket::m_type
int m_type
Socket domain type.
Definition Socket.h:488
ASSA::Socket::dumpState
void dumpState() const
Write state bits of the socket to the log file.
Definition Socket.cpp:653
ASSA::Socket::xdr_length
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition Socket.h:343
ASSA::Socket::setstate
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition Socket.h:577
ASSA::Socket::decode_fcntl_flags
static string decode_fcntl_flags(long mask_)
Decipher flags packed into mask_ used in fcntl() call.
Definition Socket.cpp:713
ASSA::Socket::getHandler
virtual handler_t getHandler() const =0
Get file descriptor.
ASSA::Socket::connect
virtual bool connect(const Address &address_)
Make a connection.
Definition Socket.h:537
ASSA::Socket::close_handler
static void close_handler(handler_t &socket_)
Close socket endpoint in a portable way.
Definition Socket.h:448
ASSA::Socket::close
virtual bool close()=0
Close socket.
ASSA::Socket::Socket
Socket(const Socket &)
The copy constructor and assignment operator are private to prevent copying of Socket objects,...
ASSA::Socket::operator=
Socket & operator=(const Socket &)
ASSA::Socket::rdstate
iostate rdstate() const
Retrieve state of the socket.
Definition Socket.h:302
ASSA::Socket::set_fd_options
int set_fd_options(long flags_)
Gateway method for setting file descriptor options.
Definition Socket.cpp:104
ASSA::Socket::turnOptionOff
bool turnOptionOff(opt_t opt_)
Disable socket option.
Definition Socket.cpp:204
ASSA::Socket::read
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition Socket.h:551
ASSA::Socket::set_option
int set_option(int level_, int optname_, int val_)
Gateway method of setting socket options.
Definition Socket.cpp:86
ASSA::Socket::turnOptionOn
bool turnOptionOn(opt_t opt_)
Enable socket option.
Definition Socket.cpp:185
ASSA::Socket::opt_t
opt_t
Socket options.
Definition Socket.h:99
ASSA::Socket::reuseaddr
@ reuseaddr
Allow local address reuse.
Definition Socket.h:100
ASSA::Socket::sndlowat
@ sndlowat
The send low-water mark si the amount of available space that must exist in the socket send buffer fo...
Definition Socket.h:108
ASSA::Socket::rcvlowat
@ rcvlowat
The receiver low-water mark is the amount of data that must be in the socket receive buffer for selec...
Definition Socket.h:102
ASSA::Socket::nonblocking
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition Socket.h:115
ASSA::Socket::is_little_endian
static bool is_little_endian()
Determine the endianess of the platform we are on.
Definition Socket.cpp:700
ASSA::Socket::eof
bool eof() const
An earlier extraction operation has encountered the end of file of the input stream (peer closed its ...
Definition Socket.h:321
ASSA::Socket::m_fd
handler_t m_fd
File descriptor.
Definition Socket.h:485
ASSA::Socket::bad
bool bad() const
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.
Definition Socket.h:337
ASSA::Socket::getOption
int getOption(opt_t opt_) const
Get current value of a socket option.
Definition Socket.cpp:248
ASSA::Socket::IOState
unsigned char IOState
Definition Socket.h:93
ASSA::Socket::bind
virtual bool bind(const Address &my_address_)=0
Server binds listening socket to its local well-known port.
ASSA::Socket::good
bool good() const
Indicates no error on the socket.
Definition Socket.h:315
ASSA::Socket::iostate
int iostate
Definition Socket.h:92
ASSA::Socket::ignore
int ignore(int n_=INT_MAX, int delim_=EOF)
Extracts bytes and discards them.
Definition Socket.cpp:308
ASSA::Socket::clear
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition Socket.h:571
ASSA::Socket::operator<<
Socket & operator<<(char c)
Output of built-in char type. The value will be XDR-encoded.
Definition Socket.cpp:508
ASSA::Socket::~Socket
virtual ~Socket()
Destructor.
Definition Socket.h:531
ASSA::Socket::open
virtual bool open(const int domain_)=0
Open socket.
ASSA::Socket::rdbuf
virtual Streambuf * rdbuf(Streambuf *)
Virtual function that sets new socket buffer and returns the old one.
Definition Socket.h:247
ASSA::Socket::flush
virtual Socket & flush()
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the assoc...
Definition Socket.cpp:74
ASSA::Socket::io_state_t
io_state_t
State bits: goodbit, eofbit, failbit, badbit.
Definition Socket.h:80
ASSA::Socket::eofbit
@ eofbit
indicates that an input operation reached the end of an input sequence
Definition Socket.h:82
ASSA::Socket::goodbit
@ goodbit
indicates that socket is ready for use
Definition Socket.h:81
ASSA::Socket::badbit
@ badbit
indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition Socket.h:87
ASSA::Socket::failbit
@ failbit
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition Socket.h:84
ASSA::Socket::PGSIZE
static const int PGSIZE
Size of bytes of a kernel page.
Definition Socket.h:74
ASSA::Socket::m_state
IOState m_state
Control state of the socket.
Definition Socket.h:496
ASSA::Socket::setOption
bool setOption(opt_t opt_, int arg_)
Set socket option to value required.
Definition Socket.cpp:223
ASSA::Socket::write
virtual int write(const char *buf_, const u_int size_)
Write specified number of bytes to the socket.
Definition Socket.h:544
ASSA::Socket::operator!
bool operator!() const
Alias to fail().
Definition Socket.h:564
ASSA::Socket::in_avail
virtual int in_avail() const =0
This function returns the number of characters immediately available in the get area of the underly...
ASSA::Socket::getBytesAvail
int getBytesAvail(void) const
Return number of bytes available in socket receive buffer.
Definition Socket.cpp:48
ASSA::Streambuf
Streambuf class.
Definition Streambuf.h:91
ASSA
Definition Acceptor.h:40
ASSA::ends
Socket & ends(Socket &os_)
ends manipulator.
Definition Socket.h:622
ASSA::endl
Socket & endl(Socket &os_)
endl manipulator.
Definition Socket.h:602
ASSA::SOCKTRACE
@ SOCKTRACE
Extended Socket & friends messages.
Definition LogMask.h:42
ASSA::flush
Socket & flush(Socket &os_)
flush manipulator.
Definition Socket.h:587
ASSA::is_valid_handler
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition Logger_Impl.h:100
ASSA::disable_handler
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition Logger_Impl.h:108