libassa 3.5.1
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
ASSA::Socket Class Referenceabstract

#include <Socket.h>

Inheritance diagram for ASSA::Socket:
ASSA::IPv4Socket ASSA::UDPSocket ASSA::ConUDPSocket ASSA::UnConUDPSocket

Public Types

enum  io_state_t { goodbit = 0 , eofbit = 1 , failbit = 2 , badbit = 4 }
 State bits: goodbit, eofbit, failbit, badbit. More...
 
enum  opt_t { reuseaddr , rcvlowat , sndlowat , nonblocking }
 Socket options. More...
 
typedef int iostate
 
typedef unsigned char IOState
 

Public Member Functions

 Socket ()
 Constructor.
 
virtual ~Socket ()
 Destructor.
 
virtual bool open (const int domain_)=0
 Open socket.
 
virtual bool close ()=0
 Close socket.
 
virtual bool connect (const Address &address_)
 Make a connection.
 
virtual bool bind (const Address &my_address_)=0
 Server binds listening socket to its local well-known port.
 
virtual int write (const char *buf_, const u_int size_)
 Write specified number of bytes to the socket.
 
int getBytesAvail (void) const
 Return number of bytes available in socket receive buffer.
 
virtual int read (char *buf_, const u_int size_)
 Read expected number of bytes from the socket.
 
int ignore (int n_=INT_MAX, int delim_=EOF)
 Extracts bytes and discards them.
 
virtual handler_t getHandler () const =0
 Get file descriptor.
 
virtual const int getDomain () const =0
 Get socket domain.
 
virtual Streambufrdbuf ()
 Return a pointer to the Streambuf associated with the stream.
 
virtual Streambufrdbuf (Streambuf *)
 Virtual function that sets new socket buffer and returns the old one.
 
virtual int in_avail () const =0
 This function returns the number of characters
immediately available in the get area of the underlying Socketbuf buffer without making a system call if Socket is doing buffering I/O.
 
virtual Socketflush ()
 This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the associated streambuf object is present).
 
bool turnOptionOn (opt_t opt_)
 Enable socket option.
 
bool turnOptionOff (opt_t opt_)
 Disable socket option.
 
bool setOption (opt_t opt_, int arg_)
 Set socket option to value required.
 
int getOption (opt_t opt_) const
 Get current value of a socket option.
 
 operator void * () const
 Convertion to void* (for testing where bool is required)
 
bool operator! () const
 Alias to fail()
 
iostate rdstate () const
 Retrieve state of the socket.
 
void clear (iostate state_=Socket::goodbit)
 Clear the socket state. Closed socket remains in bad state.
 
void setstate (iostate flag_)
 Set socket state to flag_ by adding flag_ to the existing state.
 
bool good () const
 Indicates no error on the socket.
 
bool eof () const
 An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket).
 
bool fail () const
 Indicates that earlier extraction opeartion has failed to match the required pattern of input.
 
bool bad () const
 Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.
 
void dumpState () const
 Write state bits of the socket to the log file.
 
Socketoperator>> (char &c)
 Input of built-in char type. The value will be XDR-decoded.
 
Socketoperator>> (unsigned char &c_)
 Input of built-in u_char type. The value will be XDR-decoded.
 
Socketoperator>> (signed char &c_)
 Input of built-in signed char type. The value will be XDR-decoded.
 
Socketoperator>> (std::string &s_)
 Input of STL string type. The string content will be XDR-decoded.
 
Socketoperator>> (short &n_)
 Input of built-in short type. The value will be XDR-decoded.
 
Socketoperator>> (unsigned short &n_)
 Input of built-in u_short type. The value will be XDR-decoded.
 
Socketoperator>> (int &n_)
 Input of built-in integer type. The value will be XDR-decoded.
 
Socketoperator>> (unsigned int &n_)
 Input of built-in u_int type. The value will be XDR-decoded.
 
Socketoperator>> (long &n_)
 Input of built-in long type. The value will be XDR-decoded.
 
Socketoperator>> (unsigned long &n_)
 Input of built-in u_long type. The value will be XDR-decoded.
 
Socketoperator>> (float &n_)
 Input of built-in float type. The value will be XDR-decoded.
 
Socketoperator>> (double &n_)
 Input of built-in double type. The value will be XDR-decoded.
 
Socketoperator<< (char c)
 Output of built-in char type. The value will be XDR-encoded.
 
Socketoperator<< (unsigned char c_)
 Output of built-in u_char type. The value will be XDR-encoded.
 
Socketoperator<< (signed char c_)
 Output of built-in signed char type. The value will be XDR-encoded.
 
Socketoperator<< (const std::string &s_)
 Output of STL string type. The value will be XDR-encoded.
 
Socketoperator<< (short n_)
 Output of built-in short type. The value will be XDR-encoded.
 
Socketoperator<< (unsigned short n_)
 Output of built-in u_short type. The value will be XDR-encoded.
 
Socketoperator<< (int n_)
 Output of built-in integer type. The value will be XDR-encoded.
 
Socketoperator<< (unsigned int n_)
 Output of built-in u_int type. The value will be XDR-encoded.
 
Socketoperator<< (long n_)
 Output of built-in long type. The value will be XDR-encoded.
 
Socketoperator<< (unsigned long n_)
 Output of built-in u_long type. The value will be XDR-encoded.
 
Socketoperator<< (float n_)
 Output of built-in float type. The value will be XDR-encoded.
 
Socketoperator<< (double n_)
 Output of built-in double type. The value will be XDR-encoded.
 
Socketoperator<< (Socket &(*f)(Socket &))
 Manipulators plug-in operator.
 

Static Public Member Functions

static size_t xdr_length (const std::string &s_)
 Give the true length of the XDR-encoded STL string.
 
static bool is_little_endian ()
 Determine the endianess of the platform we are on.
 
static void close_handler (handler_t &socket_)
 Close socket endpoint in a portable way.
 
static string decode_fcntl_flags (long mask_)
 Decipher flags packed into mask_ used in fcntl() call.
 

Static Public Attributes

static const int PGSIZE = 4096
 Size of bytes of a kernel page.
 

Protected Member Functions

int set_option (int level_, int optname_, int val_)
 Gateway method of setting socket options.
 
int set_fd_options (long flags_)
 Gateway method for setting file descriptor options.
 
int clear_fd_options (long flags_)
 Gateway method for clearing file descriptor options.
 

Protected Attributes

handler_t m_fd
 File descriptor.
 
int m_type
 Socket domain type.
 
IOState m_state
 Control state of the socket.
 

Private Member Functions

 Socket (const Socket &)
 The copy constructor and assignment operator are private to prevent copying of Socket objects, since the effect of such copying is not well defined.
 
Socketoperator= (const Socket &)
 

Detailed Description

Definition at line 71 of file Socket.h.

Member Typedef Documentation

◆ iostate

Definition at line 92 of file Socket.h.

◆ IOState

Definition at line 93 of file Socket.h.

Member Enumeration Documentation

◆ io_state_t

State bits: goodbit, eofbit, failbit, badbit.

Meaning to these is explained in class' documentation.

Enumerator
goodbit 

indicates that socket is ready for use

eofbit 

indicates that an input operation reached the
end of an input sequence

failbit 

indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.

badbit 

indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file)

Definition at line 80 of file Socket.h.

80 {
81 goodbit = 0,
82 eofbit = 1,
84 failbit = 2,
87 badbit = 4
90 };
@ eofbit
indicates that an input operation reached the end of an input sequence
Definition Socket.h:82
@ goodbit
indicates that socket is ready for use
Definition Socket.h:81
@ badbit
indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition Socket.h:87
@ failbit
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition Socket.h:84

◆ opt_t

Socket options.

Enumerator
reuseaddr 

Allow local address reuse.

rcvlowat 

The receiver low-water mark is the amount of data that must be in the socket receive buffer for select(3) to return "readable".

It defaults to 1 for a TCP and UDP socket (NOTE: Posix.1g does not require support for this option).

sndlowat 

The send low-water mark si the amount of available space that must exist in the socket send buffer for select to return "writable".

This low-water mark normally defaults to 2048 for TCP socket. UDP socket is always writable (NOTE: Posix.1g does not require support for this option)

nonblocking 

Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).

            The default setup for a socket is BLOCKING (O_RDWR),
            Blocking implies being suspended while waiting
            for data to arrive on read. On write, it means
            that there is no room to send all data out and
            the process is being suspended until more room
            in the output buffer becomes available.

            Use nonblocking option to set the socket to
            non-blocking mode.

            For input operations, if an operation cannot be
            satisfied (at least 1 byte of data for a TCP socket 
            or a complete datagram for a UDP socket), return is 
            made immediately with an error of EWOULDBLOCK.

            For output operations, if there is no room at
            all in the socket send buffer, return is made 
            immediately with an error of EWOULDBLOCK. If, 
            however, there is some room in the socket send buffer, 
            the return value will be the number of bytes that the 
            kernel was able to copy into the buffer
            (This is called a short count).

            NOTE: To go back to blocking mode, clear nonblocking
            with turnOptionOff().

Definition at line 98 of file Socket.h.

99 {
100 reuseaddr,
102 rcvlowat,
108 sndlowat,
143 };
@ reuseaddr
Allow local address reuse.
Definition Socket.h:100
@ 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
@ 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
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition Socket.h:115

Constructor & Destructor Documentation

◆ Socket() [1/2]

ASSA::Socket::Socket ( )
inline

Constructor.

Definition at line 518 of file Socket.h.

519 :
521 m_type(0),
522#if defined (WIN32)
523 m_nonblocking (false),
524#endif
526{
527 trace_with_mask("Socket::Socket",SOCKTRACE);
528}
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
#define BAD_HANDLER
Sort out WIN32/mingw oddities.
Definition Logger_Impl.h:81
int m_type
Socket domain type.
Definition Socket.h:488
handler_t m_fd
File descriptor.
Definition Socket.h:485
IOState m_state
Control state of the socket.
Definition Socket.h:496
@ SOCKTRACE
Extended Socket & friends messages
Definition LogMask.h:42

References ASSA::SOCKTRACE, and trace_with_mask.

◆ ~Socket()

ASSA::Socket::~Socket ( )
inlinevirtual

Destructor.

Definition at line 531 of file Socket.h.

532{
533 trace_with_mask("Socket::~Socket",SOCKTRACE);
534}

References ASSA::SOCKTRACE, and trace_with_mask.

◆ Socket() [2/2]

ASSA::Socket::Socket ( const Socket )
private

The copy constructor and assignment operator are private to prevent copying of Socket objects, since the effect of such copying is not well defined.

Usually you want to copy a pointer to the object, or pass a reference to a function.

Member Function Documentation

◆ bad()

bool ASSA::Socket::bad ( ) const
inline

Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.

Returns
true if badbit is set, false otherwise

Definition at line 337 of file Socket.h.

337{ return m_state & Socket::badbit; }

References badbit, and m_state.

Referenced by dumpState().

◆ bind()

virtual bool ASSA::Socket::bind ( const Address my_address_)
pure virtual

Server binds listening socket to its local well-known port.

Parameters
my_address_address to bind to
Returns
true if success, false otherwise

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

◆ clear()

void ASSA::Socket::clear ( iostate  state_ = Socket::goodbit)
inline

Clear the socket state. Closed socket remains in bad state.

Definition at line 571 of file Socket.h.

572{
573 m_state = is_valid_handler (m_fd) ? state_ : state_ | Socket::badbit;
574}
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.

References badbit, ASSA::is_valid_handler(), m_fd, and m_state.

Referenced by ASSA::IPv4Socket::connect(), ASSA::IPv4Socket::open(), and ASSA::UDPSocket::open().

◆ clear_fd_options()

int Socket::clear_fd_options ( long  flags_)
protected

Gateway method for clearing file descriptor options.

This is a private function used only by class Socket.

Returns
0 on success, -1 on error (fcntl(2) failed)

The only value for flags_ is O_NONBLOCK - set the socket back to the blocking mode.

Definition at line 146 of file Socket.cpp.

148{
149 trace_with_mask("Socket::clear_fd_options",SOCKTRACE);
150 long oldflags;
151 long newflags;
152 int ret;
153
154#if defined (WIN32)
155
156 u_long set_block = 0;
157 if ((ret = ioctlsocket (m_fd, FIONBIO, &set_block)) == 0) {
158 m_nonblocking = false;
159 return 0;
160 }
161 return -1;
162
163#else
164
165 if ((oldflags = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
166 return -1;
167 }
168 newflags = oldflags & ~flags_; // clear flags
169
170 DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd,
171 decode_fcntl_flags (newflags).c_str ()));
172
174
176 DL ((SOCKTRACE,"Flags are set to %s via fcntl(%d)\n",
177 decode_fcntl_flags (newflags).c_str ()));
178
179 return (ret);
180#endif
181}
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
static string decode_fcntl_flags(long mask_)
Decipher flags packed into mask_ used in fcntl() call.
Definition Socket.cpp:713

References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by setOption(), and turnOptionOff().

◆ close()

virtual bool ASSA::Socket::close ( )
pure virtual

Close socket.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

Referenced by close_handler().

◆ close_handler()

static void ASSA::Socket::close_handler ( handler_t socket_)
inlinestatic

Close socket endpoint in a portable way.

Socket is also set to an invalid value.

Definition at line 448 of file Socket.h.

449 {
450#if defined (WIN32)
451 closesocket (socket_);
452#else
453 ::close (socket_);
454#endif
455 disable_handler (socket_);
456 }
virtual bool close()=0
Close socket.
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.

References close(), and ASSA::disable_handler().

Referenced by ASSA::IPv4Socket::close().

◆ connect()

bool ASSA::Socket::connect ( const Address address_)
inlinevirtual

Make a connection.

Parameters
address_address of the server to connect to

Reimplemented in ASSA::IPv4Socket, and ASSA::ConUDPSocket.

Definition at line 537 of file Socket.h.

538{
539 trace_with_mask("Socket::connect",SOCKTRACE);
540 return false;
541}

References ASSA::SOCKTRACE, and trace_with_mask.

◆ decode_fcntl_flags()

string Socket::decode_fcntl_flags ( long  mask_)
static

Decipher flags packed into mask_ used in fcntl() call.

Definition at line 712 of file Socket.cpp.

714{
715 string answer;
716
717#if !defined (WIN32)
718
719 if (mask_ & O_RDONLY) {
720 answer = "O_RDONLY|"; // 000000
721 }
722 if (mask_ & O_WRONLY) {
723 answer += "O_WRONLY|"; // 000001
724 }
725 if (mask_ & O_RDWR) {
726 answer += "O_RDWR|"; // 000002
727 }
728 if (mask_ & O_APPEND) {
729 answer += "O_APPEND|"; // 001000
730 }
731 if (mask_ & O_NONBLOCK) {
732 answer += "O_NONBLOCK|";// 004000
733 }
734 if (mask_ & O_SYNC) {
735 answer += "O_SYNC|"; // 010000
736 }
737
738 if (mask_ & O_ASYNC) { // 020000
739 answer += "O_ASYNC|";
740 }
741
742 answer.erase (answer.end () - 1);
743
744#endif
745
746 return answer;
747}

Referenced by clear_fd_options(), and set_fd_options().

◆ dumpState()

void Socket::dumpState ( void  ) const

Write state bits of the socket to the log file.

Definition at line 652 of file Socket.cpp.

654{
655 trace_with_mask("Socket::dumpState",SOCKTRACE);
656
657 char state_set[] = "[ set]\n";
658 char state_not_set[] = "[not set]\n";
659
660 std::ostringstream msg;
661
662 msg << "\n";
663 msg << "\tTesting good() ....... ";
664
665 if (this->good ()) msg << state_set;
666 else msg << state_not_set;
667
668 msg << "\tTesting eof() ........ ";
669 if (this->eof ()) msg << state_set;
670 else msg << state_not_set;
671
672 msg << "\tTesting fail() ....... ";
673 if (this->fail ()) msg << state_set;
674 else msg << state_not_set;
675
676 msg << "\tTesting bad() ........ ";
677 if (this->bad ()) msg << state_set;
678 else msg << state_not_set;
679
680 msg << "\tTesting !() .......... ";
681 if ( !(*this) ) msg << state_set;
682 else msg << state_not_set;
683
684 msg << "\tTesting void *() ..... ";
685 if ( *this ) msg << state_set;
686 else msg << state_not_set;
687
688 msg << "\tTesting nonblocking... ";
689 if (getOption (nonblocking) == 1) msg << state_set;
690 else msg << state_not_set;
691
692 /*--- Terminate stream buffer ---*/
693 msg << std::ends;
694
695 DL((SOCKTRACE,"%s\n", msg.str ().c_str ()));
696}
bool fail() const
Indicates that earlier extraction opeartion has failed to match the required pattern of input.
Definition Socket.h:328
bool eof() const
An earlier extraction operation has encountered the end of file of the input stream (peer closed its ...
Definition Socket.h:321
bool bad() const
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.
Definition Socket.h:337
int getOption(opt_t opt_) const
Get current value of a socket option.
Definition Socket.cpp:248
bool good() const
Indicates no error on the socket.
Definition Socket.h:315

References bad(), DL, eof(), fail(), getOption(), good(), nonblocking, ASSA::SOCKTRACE, and trace_with_mask.

◆ eof()

bool ASSA::Socket::eof ( ) const
inline

An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket).

Returns
true if peer closed the socket; false otherwise

Definition at line 321 of file Socket.h.

321{ return m_state & Socket::eofbit; }

References eofbit, and m_state.

Referenced by dumpState().

◆ fail()

bool ASSA::Socket::fail ( ) const
inline

Indicates that earlier extraction opeartion has failed to match the required pattern of input.

Socket should be closed at this point by the owner.

Returns
true if failbit or badbit is set, false otherwise

Definition at line 328 of file Socket.h.

329 {
331 }

References badbit, failbit, and m_state.

Referenced by dumpState(), and operator!().

◆ flush()

Socket & Socket::flush ( )
virtual

This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the associated streambuf object is present).

Typically, such an operation flushes an output stream to the associated external pipe.

Definition at line 73 of file Socket.cpp.

75{
76 if (good () && rdbuf ()) {
77 if (rdbuf ()->pubsync () == EOF) {
79 }
80 }
81 return (*this);
82}
virtual Streambuf * rdbuf()
Return a pointer to the Streambuf associated with the stream.
Definition Socket.h:240
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition Socket.h:577

References badbit, good(), rdbuf(), and setstate().

Referenced by ASSA::IPv4Socket::close().

◆ getBytesAvail()

int Socket::getBytesAvail ( void  ) const

Return number of bytes available in socket receive buffer.

There can be bytes already stored in the Streambuf buffer, in addition to bytes that arrived and are ready for reading in the socket buffer (kernel address space).

Sum of these two numbers is the true number of bytes available for immediate reading.

Definition at line 47 of file Socket.cpp.

49{
50 trace_with_mask("Socket::getBytesAvail",SOCKTRACE);
51
52 Socket* This = (Socket*) this;
53 u_long ba = 0;
54 int ret = 0;
55
56#if defined(WIN32)
58#else
59 ret = ioctl (m_fd, FIONREAD, &ba);
60#endif
61
62 if (ret == -1) {
63 EL((ASSAERR,"ioctl(2) failed with ret: %d\n", ret));
64 return ret;
65 }
66 ba += This->rdbuf ()->in_avail ();
67
68 DL((SOCKTRACE,"%ld bytes available for reading\n", ba));
69 return (int(ba));
70}
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
@ ASSAERR
ASSA and system errors
Definition LogMask.h:34

References ASSA::ASSAERR, DL, EL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::Socketbuf::showmanyc().

◆ getDomain()

virtual const int ASSA::Socket::getDomain ( ) const
pure virtual

Get socket domain.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

◆ getHandler()

virtual handler_t ASSA::Socket::getHandler ( ) const
pure virtual

Get file descriptor.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

Referenced by ASSA::Socketbuf::sys_read(), and ASSA::Socketbuf::sys_write().

◆ getOption()

int Socket::getOption ( opt_t  opt_) const

Get current value of a socket option.

Parameters
opt_option name
Returns
option value on success (for binary: 0 - disable, 1 - enabled); -1 if error

Definition at line 247 of file Socket.cpp.

249{
250 trace_with_mask("Socket::getOption",SOCKTRACE);
251 int optval = 0;
252
253 if (nonblocking == opt_)
254 {
255#if defined (WIN32)
256 return (m_nonblocking ? 1 : 0);
257#else
258 if ((optval = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
259 return -1;
260 }
261 return ((optval & O_NONBLOCK) == O_NONBLOCK ? 1 : 0);
262#endif
263 }
264
265 int optname;
266 int level = SOL_SOCKET;
267 bool bin = false;
268
269 socklen_t len = sizeof (optval);
270 int ret;
271
272 if (rcvlowat == opt_) {
274 }
275 else if (sndlowat == opt_) {
277 }
278 else if (reuseaddr == opt_) {
280 bin = true;
281 }
282 else {
283 EL((ASSAERR,"Invalid socket option\n"));
284 return (-1);
285 }
286
287#if defined (__CYGWIN32__) || defined (WIN32)
288
289 ret = getsockopt (m_fd, level, optname, (char*) &optval, (int*)&len);
290
291#else // posix/unix
292
293 ret = getsockopt (m_fd, level, optname, (char*) &optval, &len);
294
295#endif
296
297 if (ret < 0) {
298 return (-1);
299 }
300 if (bin) {
301 return (ret == 0 ? 0 : 1);
302 }
303 return (ret);
304}

References ASSA::ASSAERR, EL, m_fd, nonblocking, rcvlowat, reuseaddr, sndlowat, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by dumpState().

◆ good()

bool ASSA::Socket::good ( ) const
inline

Indicates no error on the socket.

Returns
true if goodbit is set, false otherwise

Definition at line 315 of file Socket.h.

315{ return m_state == 0; }

References m_state.

Referenced by ASSA::IPv4Socket::clone(), dumpState(), flush(), and ASSA::RemoteLogger::log_func().

◆ ignore()

int Socket::ignore ( int  n_ = INT_MAX,
int  delim_ = EOF 
)

Extracts bytes and discards them.

With no arguments, read and discard until eof is encountered.

Bytes are extracted in the following manner:

n_ delim_ Action
1 EOF Read and discard 1 byte
k EOF Read and discard at most k bytes
INT_MAX EOF Read and discard till eof is reached
INT_MAX 'c' Read and discard till either 'c' or eof is found
k 'c' Read and discard at most k bytes, but stop if 'c' is found
Parameters
n_number of bytes to ignore (default=INT_MAX)
delim_delimiter to search for (default=EOF)
Returns
number of bytes discarded

Definition at line 307 of file Socket.cpp.

309{
310 trace_with_mask("Socket::ignore",SOCKTRACE);
311 register int b;
312 register int count = 0;
313 register char c;
314
315 if (n_ == INT_MAX && delim_ == EOF) {
316 char buf[PGSIZE];
317 while ((b = read (buf, PGSIZE))) {
318 count += b;
319 }
321 return count;
322 }
323 for (; n_; n_--, count++) {
324 if ( (b = read (&c, 1)) == 0 ) {
326 break;
327 }
328 if ( c == delim_ )
329 break;
330 }
331 return count;
332}
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
Definition Socket.h:551
static const int PGSIZE
Size of bytes of a kernel page.
Definition Socket.h:74

References eofbit, failbit, PGSIZE, read(), setstate(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by operator>>().

◆ in_avail()

virtual int ASSA::Socket::in_avail ( ) const
pure virtual

This function returns the number of characters
immediately available in the get area of the underlying Socketbuf buffer without making a system call if Socket is doing buffering I/O.

It is certain that returned number of characters may be fetched without error, and without accessing any external device.

Implemented in ASSA::ConUDPSocket, ASSA::IPv4Socket, and ASSA::UnConUDPSocket.

◆ is_little_endian()

bool Socket::is_little_endian ( )
static

Determine the endianess of the platform we are on.

Returns
true if it is a little-endian host; false if a big-endian host.

Definition at line 699 of file Socket.cpp.

701{
702 union {
703 char c [sizeof (short)];
704 short v;
705 } endian_u;
706
707 endian_u.v = 256;
708 return (endian_u.c [0] == 0);
709}

◆ open()

virtual bool ASSA::Socket::open ( const int  domain_)
pure virtual

Open socket.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

◆ operator void *()

ASSA::Socket::operator void * ( ) const
inline

Convertion to void* (for testing where bool is required)

Definition at line 558 of file Socket.h.

559{
560 return fail() ? (void *)0 : (void *)(-1);
561}

◆ operator!()

bool ASSA::Socket::operator! ( ) const
inline

Alias to fail()

Definition at line 564 of file Socket.h.

565{
566 return fail();
567}

References fail().

◆ operator<<() [1/13]

Socket & Socket::operator<< ( char  c)

Output of built-in char type. The value will be XDR-encoded.

Definition at line 506 of file Socket.cpp.

509{
510 /* See comment to operator>>(char n_) */
511
512 int buf = 0;
513 int len = sizeof (int);
514 XDR xdrs;
515
517 xdr_char (&xdrs, &n_);
518
519 if (write ((const char*) &buf, len) != len) {
521 }
522 xdr_destroy (&xdrs);
523 return *this;
524}
virtual int write(const char *buf_, const u_int size_)
Write specified number of bytes to the socket.
Definition Socket.h:544

References eofbit, failbit, and write().

◆ operator<<() [2/13]

Socket & Socket::operator<< ( const std::string &  s_)

Output of STL string type. The value will be XDR-encoded.

XDR STRING representation: (from RFC 1832 - http://www.faqs.org/rfcs/rfc1832.html)

0 1 2 3 4 5 ... +--—+--—+--—+--—+--—+--—+...+--—+--—+...+--—+ | length n |byte0|byte1|...| n-1 | 0 |...| 0 | +--—+--—+--—+--—+--—+--—+...+--—+--—+...+--—+ |<----—4 bytes----—>|<---—n bytes---—>|<—r bytes—>| |<-—n+r (where (n+r) mod 4 = 0)-—>|

If n is not a multiple of four, then the n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four. In other words, (4 - n % 4) == r.

We follow the format, but send data bytes as-is - no conversion is required. We also pad data with r bytes.

Definition at line 544 of file Socket.cpp.

547{
548 static const char pad [4] = { 0, 0, 0, 0 };
549
550 (*this) << s_.length ();
551 int ret = write (s_.c_str (), s_.length ());
552 if ( ret != s_.length () ) {
554 }
555 size_t r = 4 - s_.length() % 4;
556 if (r) {
557 if (write (pad, r) != r) {
559 }
560 }
561 return *this;
562}

References eofbit, failbit, setstate(), and write().

◆ operator<<() [3/13]

Socket & Socket::operator<< ( double  n_)

Output of built-in double type. The value will be XDR-encoded.

Definition at line 634 of file Socket.cpp.

637{
638 double buf, f = n_;
639 XDR xdrs;
640 xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(double), XDR_ENCODE);
641 xdr_double (&xdrs, &f);
642
643 int ret = write ((const char*) &buf, sizeof(double));
644 xdr_destroy (&xdrs);
645 if ( ret != sizeof(double) ) {
647 }
648 return *this;
649}

References eofbit, failbit, setstate(), and write().

◆ operator<<() [4/13]

Socket & Socket::operator<< ( float  n_)

Output of built-in float type. The value will be XDR-encoded.

Definition at line 617 of file Socket.cpp.

620{
621 float buf, f = n_;
622 XDR xdrs;
623 xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(float), XDR_ENCODE);
624 xdr_float (&xdrs, &f);
625
626 int ret = write ((const char*) &buf, sizeof(float));
627 xdr_destroy (&xdrs);
628 if ( ret != sizeof(float) ) {
630 }
631 return *this;
632}

References eofbit, failbit, setstate(), and write().

◆ operator<<() [5/13]

Socket & Socket::operator<< ( int  n_)

Output of built-in integer type. The value will be XDR-encoded.

Definition at line 612 of file Socket.cpp.

◆ operator<<() [6/13]

Socket & Socket::operator<< ( long  n_)

Output of built-in long type. The value will be XDR-encoded.

Definition at line 614 of file Socket.cpp.

◆ operator<<() [7/13]

Socket & Socket::operator<< ( short  n_)

Output of built-in short type. The value will be XDR-encoded.

Definition at line 564 of file Socket.cpp.

566{
567 short val = (short) htons((short)n_);
568
569 if (write ((const char*) &val, sizeof(short)) != sizeof(short))
570 {
572 }
573 return *this;
574}

References eofbit, failbit, setstate(), and write().

◆ operator<<() [8/13]

Socket & ASSA::Socket::operator<< ( signed char  c_)
inline

Output of built-in signed char type. The value will be XDR-encoded.

Definition at line 400 of file Socket.h.

401 {
402 return (*this) << (char) c_;
403 }

◆ operator<<() [9/13]

Socket & ASSA::Socket::operator<< ( Socket &(*)(Socket &)  f)
inline

Manipulators plug-in operator.

Definition at line 433 of file Socket.h.

434 {
435 return (f (*this));
436 }

◆ operator<<() [10/13]

Socket & ASSA::Socket::operator<< ( unsigned char  c_)
inline

Output of built-in u_char type. The value will be XDR-encoded.

Definition at line 394 of file Socket.h.

395 {
396 return (*this) << (char) c_;
397 }

◆ operator<<() [11/13]

Socket & Socket::operator<< ( unsigned int  n_)

Output of built-in u_int type. The value will be XDR-encoded.

Definition at line 613 of file Socket.cpp.

◆ operator<<() [12/13]

Socket & Socket::operator<< ( unsigned long  n_)

Output of built-in u_long type. The value will be XDR-encoded.

Definition at line 615 of file Socket.cpp.

◆ operator<<() [13/13]

Socket & Socket::operator<< ( unsigned short  n_)

Output of built-in u_short type. The value will be XDR-encoded.

Definition at line 576 of file Socket.cpp.

578{
580
581 if (write ((const char*) &val, sizeof(u_short)) != sizeof(u_short))
582 {
584 }
585 return *this;
586}
unsigned short u_short
Definition Logger_Impl.h:39

References eofbit, failbit, setstate(), and write().

◆ operator=()

Socket & ASSA::Socket::operator= ( const Socket )
private

◆ operator>>() [1/12]

Socket & Socket::operator>> ( char n_)

Input of built-in char type. The value will be XDR-decoded.

From xdr_simple(3N) man page:

"xdr_char() translates between C characters and their external representations. Note: encoded characters are not packed, and occupy 4 bytes each."

From SunOS 5.3 Network Interfaces Programmer's Guide, "XDR Protocol Specification":

"The representation of all items requires a multipe of four bytes (or 32 bits) of data. ... The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four."

The implication if this is that even though we transerfer 1 byte, we still have to allocate and transfer 4 bytes to hold it.

Definition at line 358 of file Socket.cpp.

360{
361
362 int c = 0;
363 int len = sizeof (int);
364 XDR xdrs;
366
367 if (read ((char* ) &c, len) == len) {
368 xdr_char (&xdrs, &n_);
369 }
370 else {
372 }
373 xdr_destroy(&xdrs);
374 return *this;
375}

References eofbit, failbit, read(), and setstate().

Referenced by operator>>(), and operator>>().

◆ operator>>() [2/12]

Socket & Socket::operator>> ( double n_)

Input of built-in double type. The value will be XDR-decoded.

Definition at line 486 of file Socket.cpp.

488{
489 double val = 0;
490 XDR xdrs;
491 xdrmem_create (&xdrs, (caddr_t) &val, sizeof(double), XDR_DECODE);
492 if (read ((char*) &val, sizeof(double)) == sizeof(double)) {
493 xdr_double (&xdrs, &n_);
494 }
495 else {
497 }
498 xdr_destroy (&xdrs);
499 return *this;
500}

References eofbit, failbit, read(), and setstate().

◆ operator>>() [3/12]

Socket & Socket::operator>> ( float n_)

Input of built-in float type. The value will be XDR-decoded.

Definition at line 468 of file Socket.cpp.

470{
471 float val;
472 XDR xdrs;
473 xdrmem_create (&xdrs, (caddr_t) &val, sizeof(float), XDR_DECODE);
474
475 if (read ((char*) &val, sizeof(float)) == sizeof(float)) {
476 xdr_float (&xdrs, &n_);
477 }
478 else {
480 }
481 xdr_destroy (&xdrs);
482 return *this;
483}

References eofbit, failbit, read(), and setstate().

◆ operator>>() [4/12]

Socket & Socket::operator>> ( int n_)

Input of built-in integer type. The value will be XDR-decoded.

Definition at line 462 of file Socket.cpp.

◆ operator>>() [5/12]

Socket & Socket::operator>> ( long n_)

Input of built-in long type. The value will be XDR-decoded.

Definition at line 464 of file Socket.cpp.

◆ operator>>() [6/12]

Socket & Socket::operator>> ( short n_)

Input of built-in short type. The value will be XDR-decoded.

Definition at line 401 of file Socket.cpp.

403{
404 short val;
405 if (read ((char*) &val, sizeof(short)) == sizeof(short)) {
406 n_ = (short) ntohs ((short)val);
407 }
408 else {
410 }
411 return *this;
412}

References eofbit, failbit, read(), and setstate().

◆ operator>>() [7/12]

Socket & ASSA::Socket::operator>> ( signed char c_)
inline

Input of built-in signed char type. The value will be XDR-decoded.

Definition at line 358 of file Socket.h.

359 {
360 return operator>>((char&) c_);
361 }
Socket & operator>>(char &c)
Input of built-in char type. The value will be XDR-decoded.
Definition Socket.cpp:359

References operator>>().

◆ operator>>() [8/12]

Socket & Socket::operator>> ( std::string &  s_)

Input of STL string type. The string content will be XDR-decoded.

For format explanation, see comment to operator<<(string&)

Definition at line 380 of file Socket.cpp.

382{
383 char c = 0;
384 size_t n = 0;
385 s_ = "";
386
387 (*this) >> n;
388
389 if (n == 0) {
390 return *this;
391 }
392 size_t len = n;
393 while (len-- && read (&c, 1) == 1) {
394 s_ += c;
395 }
396 ignore (4 - n % 4);
397 return *this;
398}
int ignore(int n_=INT_MAX, int delim_=EOF)
Extracts bytes and discards them.
Definition Socket.cpp:308

References ignore(), and read().

◆ operator>>() [9/12]

Socket & ASSA::Socket::operator>> ( unsigned char c_)
inline

Input of built-in u_char type. The value will be XDR-decoded.

Definition at line 352 of file Socket.h.

353 {
354 return operator>>((char&) c_);
355 }

References operator>>().

◆ operator>>() [10/12]

Socket & Socket::operator>> ( unsigned int n_)

Input of built-in u_int type. The value will be XDR-decoded.

Definition at line 463 of file Socket.cpp.

◆ operator>>() [11/12]

Socket & Socket::operator>> ( unsigned long n_)

Input of built-in u_long type. The value will be XDR-decoded.

Definition at line 465 of file Socket.cpp.

◆ operator>>() [12/12]

Socket & Socket::operator>> ( unsigned short n_)

Input of built-in u_short type. The value will be XDR-decoded.

Definition at line 415 of file Socket.cpp.

417{
418 u_short val;
419 if (read ((char*) &val, sizeof(u_short)) == sizeof(u_short)) {
420 n_ = (u_short) ntohs ((u_short)val);
421 }
422 else {
424 }
425 return *this;
426}

References eofbit, failbit, read(), and setstate().

◆ rdbuf() [1/2]

virtual Streambuf * ASSA::Socket::rdbuf ( )
inlinevirtual

Return a pointer to the Streambuf associated with the stream.

This is part of the construction of a stream, and the buffer class object is not normally changed. This function may be used to get at Streambuf functionality directly, given a Socket object. Default behavior is to return NULL.

Returns
NULL

Reimplemented in ASSA::IPv4Socket.

Definition at line 240 of file Socket.h.

240{ return 0; }

Referenced by flush().

◆ rdbuf() [2/2]

virtual Streambuf * ASSA::Socket::rdbuf ( Streambuf )
inlinevirtual

Virtual function that sets new socket buffer and returns the old one.

Default behavior is to return NULL.

Returns
Old Socketbuf object.

Reimplemented in ASSA::IPv4Socket.

Definition at line 247 of file Socket.h.

247{ return 0; }

◆ rdstate()

iostate ASSA::Socket::rdstate ( ) const
inline

Retrieve state of the socket.

Returns
control state of the socket

Definition at line 302 of file Socket.h.

302{ return m_state; }

References m_state.

◆ read()

int ASSA::Socket::read ( char buf_,
const u_int  size_ 
)
inlinevirtual

Read expected number of bytes from the socket.

Parameters
buf_buffer to save received packet to
size_size of the packet

Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.

Definition at line 551 of file Socket.h.

552{
553 trace_with_mask("Socket::read()",SOCKTRACE);
554 return -1;
555}

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ignore(), operator>>(), operator>>(), operator>>(), operator>>(), operator>>(), and operator>>().

◆ set_fd_options()

int Socket::set_fd_options ( long  flags_)
protected

Gateway method for setting file descriptor options.

This is a private function used only by class Socket.

Returns
0 on success, -1 on error (fcntl(2) failed)

The only value for flags_ is O_NONBLOCK.

Definition at line 103 of file Socket.cpp.

105{
106 trace_with_mask("Socket::set_fd_options",SOCKTRACE);
107 int val;
108 int ret;
109
110#if defined (WIN32)
111
113 if ((val = ioctlsocket (m_fd, FIONBIO, &set_nonblock)) == 0) {
114 m_nonblocking = true;
115 return 0;
116 }
117 return -1;
118
119#else // POSIX/UNIX
120
121 if ((val = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
122 return -1;
123 }
124 val |= flags_; // turn flags on
125
126 DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd,
127 decode_fcntl_flags (val).c_str ()));
128
129 ret = ::fcntl (m_fd, F_SETFL, val);
130
131 val = ::fcntl (m_fd, F_GETFL, 0);
132 DL ((SOCKTRACE,"Flags are set to %s via fcntl(25)\n",
133 decode_fcntl_flags (val).c_str ()));
134
135 return (ret);
136
137#endif
138}

References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by setOption(), and turnOptionOn().

◆ set_option()

int Socket::set_option ( int  level_,
int  optname_,
int  val_ 
)
protected

Gateway method of setting socket options.

Returns
0 on success, -1 on error (setsockopt(2) failed)

Definition at line 85 of file Socket.cpp.

87{
88 int ret = setsockopt (m_fd,
89 level_,
90 optname_,
91 (const char*) &val_,
92 sizeof (val_));
93 if (ret < 0) {
95 }
96 return ret;
97}

References failbit, m_fd, and setstate().

Referenced by setOption(), turnOptionOff(), and turnOptionOn().

◆ setOption()

bool Socket::setOption ( opt_t  opt_,
int  arg_ 
)

Set socket option to value required.

Parameters
opt_option name
arg_value to set (for binary: 0 - disable, 1 - enable).
Returns
true on success_; false if error

Definition at line 222 of file Socket.cpp.

224{
225 trace_with_mask("Socket::setOption(,)",SOCKTRACE);
226 int optname;
227
228 if (nonblocking == opt_) {
229 return (arg_ == 1) ? set_fd_options (O_NONBLOCK)
231 }
232
233 if (rcvlowat == opt_) {
235 }
236 else if (sndlowat == opt_) {
238 }
239 else {
240 EL((ASSAERR,"Invalid socket option\n"));
241 return false;
242 }
243 return set_option (SOL_SOCKET, optname, arg_) == 0;
244}
int clear_fd_options(long flags_)
Gateway method for clearing file descriptor options.
Definition Socket.cpp:147
int set_fd_options(long flags_)
Gateway method for setting file descriptor options.
Definition Socket.cpp:104
int set_option(int level_, int optname_, int val_)
Gateway method of setting socket options.
Definition Socket.cpp:86

References ASSA::ASSAERR, clear_fd_options(), EL, nonblocking, rcvlowat, set_fd_options(), set_option(), sndlowat, ASSA::SOCKTRACE, and trace_with_mask.

◆ setstate()

void ASSA::Socket::setstate ( iostate  flag_)
inline

◆ turnOptionOff()

bool Socket::turnOptionOff ( opt_t  opt_)

Disable socket option.

Parameters
opt_option name
Returns
true on success; false if error

Definition at line 203 of file Socket.cpp.

205{
206 trace_with_mask("Socket::turnOptionOff",SOCKTRACE);
207
208 if (nonblocking == opt_)
210
211 int optname;
212 if (reuseaddr == opt_)
214 else {
215 EL((ASSAERR,"Invalid socket option\n"));
216 return false;
217 }
218 return set_option (SOL_SOCKET, optname, 0) == 0;
219}

References ASSA::ASSAERR, clear_fd_options(), EL, nonblocking, reuseaddr, set_option(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::RemoteLogger::log_open().

◆ turnOptionOn()

bool Socket::turnOptionOn ( opt_t  opt_)

Enable socket option.

Parameters
opt_option name
Returns
true on success; false if error

Definition at line 184 of file Socket.cpp.

186{
187 trace_with_mask("Socket::turnOptionOn",SOCKTRACE);
188
189 if (nonblocking == opt_)
190 return set_fd_options (O_NONBLOCK);
191
192 int optname;
193 if (reuseaddr == opt_)
195 else {
196 EL((ASSAERR,"Invalid socket option\n"));
197 return false;
198 }
199 return set_option (SOL_SOCKET, optname, 1) == 0;
200}

References ASSA::ASSAERR, EL, nonblocking, reuseaddr, set_fd_options(), set_option(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::IPv4Socket::bind(), and ASSA::IPv4Socket::open().

◆ write()

int ASSA::Socket::write ( const char buf_,
const u_int  size_ 
)
inlinevirtual

Write specified number of bytes to the socket.

Parameters
buf_packet to send
size_size of the packet

Reimplemented in ASSA::IPv4Socket, and ASSA::ConUDPSocket.

Definition at line 544 of file Socket.h.

545{
546 trace_with_mask("Socket::write",SOCKTRACE);
547 return -1;
548}

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().

◆ xdr_length()

static size_t ASSA::Socket::xdr_length ( const std::string &  s_)
inlinestatic

Give the true length of the XDR-encoded STL string.

Definition at line 343 of file Socket.h.

344 {
345 return (4 + s_.length () + s_.length () % 4);
346 }

Referenced by ASSA::RemoteLogger::log_func(), ASSA::RemoteLogger::log_msg(), ASSA::RemoteLogger::log_open(), and ASSA::xdrIOBuffer::operator>>().

Member Data Documentation

◆ m_fd

handler_t ASSA::Socket::m_fd
protected

◆ m_state

IOState ASSA::Socket::m_state
protected

Control state of the socket.

Definition at line 496 of file Socket.h.

Referenced by bad(), clear(), eof(), fail(), good(), rdstate(), and setstate().

◆ m_type

int ASSA::Socket::m_type
protected

◆ PGSIZE

const int ASSA::Socket::PGSIZE = 4096
static

Size of bytes of a kernel page.

Definition at line 74 of file Socket.h.

Referenced by ignore().


The documentation for this class was generated from the following files: