libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Static Public Attributes | Private Member Functions | List of all members
ASSA::IPv4Socket Class Reference

#include <IPv4Socket.h>

Inheritance diagram for ASSA::IPv4Socket:
ASSA::Socket

Public Member Functions

 IPv4Socket ()
 Default constructor. More...
 
 IPv4Socket (const handler_t fd_)
 Constructor from file descriptor. More...
 
 ~IPv4Socket ()
 Destructor will close connection. More...
 
IPv4Socketclone () const
 "Virtual constructor". More...
 
bool open (const int domain_)
 Create socket. More...
 
bool close ()
 Close socket connection. More...
 
bool connect (const Address &address_)
 Client makes connection with the server at address_. More...
 
virtual bool bind (const Address &my_address_)
 Server binds listening socket to its local well-known port. More...
 
IPv4Socketaccept ()
 Accept connection on the listening socket. More...
 
int read (char *buf_, const unsigned int size_)
 Read packet of specified size and save it to the given buffer. More...
 
int write (const char *buf_, const unsigned int size_)
 Perform blocking write by writing packet of specified size. More...
 
handler_t getHandler () const
 Get socket file descriptor. More...
 
const int getDomain () const
 Get socket domain type. More...
 
virtual Streambufrdbuf ()
 Return a pointer to the Socketbuf associated with the stream. More...
 
virtual Streambufrdbuf (Streambuf *sb_)
 Set new Socketbuf for internal IO buffering. More...
 
virtual int in_avail () const
 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. More...
 
- Public Member Functions inherited from ASSA::Socket
 Socket ()
 Constructor. More...
 
virtual ~Socket ()
 Destructor. More...
 
int getBytesAvail (void) const
 Return number of bytes available in socket receive buffer. More...
 
int ignore (int n_=INT_MAX, int delim_=EOF)
 Extracts bytes and discards them. More...
 
virtual Socketflush ()
 This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the associated streambuf object is present). More...
 
bool turnOptionOn (opt_t opt_)
 Enable socket option. More...
 
bool turnOptionOff (opt_t opt_)
 Disable socket option. More...
 
bool setOption (opt_t opt_, int arg_)
 Set socket option to value required. More...
 
int getOption (opt_t opt_) const
 Get current value of a socket option. More...
 
 operator void * () const
 Convertion to void* (for testing where bool is required) More...
 
bool operator! () const
 Alias to fail() More...
 
iostate rdstate () const
 Retrieve state of the socket. More...
 
void clear (iostate state_=Socket::goodbit)
 Clear the socket state. Closed socket remains in bad state. More...
 
void setstate (iostate flag_)
 Set socket state to flag_ by adding flag_ to the existing state. More...
 
bool good () const
 Indicates no error on the socket. More...
 
bool eof () const
 An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket). More...
 
bool fail () const
 Indicates that earlier extraction opeartion has failed to match the required pattern of input. More...
 
bool bad () const
 Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer. More...
 
void dumpState () const
 Write state bits of the socket to the log file. More...
 
Socketoperator>> (char &c)
 Input of built-in char type. The value will be XDR-decoded. More...
 
Socketoperator>> (unsigned char &c_)
 Input of built-in u_char type. The value will be XDR-decoded. More...
 
Socketoperator>> (signed char &c_)
 Input of built-in signed char type. The value will be XDR-decoded. More...
 
Socketoperator>> (std::string &s_)
 Input of STL string type. The string content will be XDR-decoded. More...
 
Socketoperator>> (short &n_)
 Input of built-in short type. The value will be XDR-decoded. More...
 
Socketoperator>> (unsigned short &n_)
 Input of built-in u_short type. The value will be XDR-decoded. More...
 
Socketoperator>> (int &n_)
 Input of built-in integer type. The value will be XDR-decoded. More...
 
Socketoperator>> (unsigned int &n_)
 Input of built-in u_int type. The value will be XDR-decoded. More...
 
Socketoperator>> (long &n_)
 Input of built-in long type. The value will be XDR-decoded. More...
 
Socketoperator>> (unsigned long &n_)
 Input of built-in u_long type. The value will be XDR-decoded. More...
 
Socketoperator>> (float &n_)
 Input of built-in float type. The value will be XDR-decoded. More...
 
Socketoperator>> (double &n_)
 Input of built-in double type. The value will be XDR-decoded. More...
 
Socketoperator<< (char c)
 Output of built-in char type. The value will be XDR-encoded. More...
 
Socketoperator<< (unsigned char c_)
 Output of built-in u_char type. The value will be XDR-encoded. More...
 
Socketoperator<< (signed char c_)
 Output of built-in signed char type. The value will be XDR-encoded. More...
 
Socketoperator<< (const std::string &s_)
 Output of STL string type. The value will be XDR-encoded. More...
 
Socketoperator<< (short n_)
 Output of built-in short type. The value will be XDR-encoded. More...
 
Socketoperator<< (unsigned short n_)
 Output of built-in u_short type. The value will be XDR-encoded. More...
 
Socketoperator<< (int n_)
 Output of built-in integer type. The value will be XDR-encoded. More...
 
Socketoperator<< (unsigned int n_)
 Output of built-in u_int type. The value will be XDR-encoded. More...
 
Socketoperator<< (long n_)
 Output of built-in long type. The value will be XDR-encoded. More...
 
Socketoperator<< (unsigned long n_)
 Output of built-in u_long type. The value will be XDR-encoded. More...
 
Socketoperator<< (float n_)
 Output of built-in float type. The value will be XDR-encoded. More...
 
Socketoperator<< (double n_)
 Output of built-in double type. The value will be XDR-encoded. More...
 
Socketoperator<< (Socket &(*f)(Socket &))
 Manipulators plug-in operator. More...
 

Static Public Attributes

static const int MAXTCPBUFSZ
 Maximum TCP data frame (no options) More...
 
- Static Public Attributes inherited from ASSA::Socket
static const int PGSIZE = 4096
 Size of bytes of a kernel page. More...
 

Private Member Functions

 IPv4Socket (const IPv4Socket &)
 
IPv4Socketoperator= (const IPv4Socket &)
 

Private Attributes

char * m_path
 Path of UNIX domain socket. More...
 
Streambufm_rdbuf
 Socketbuf. More...
 

Additional Inherited Members

- Public Types inherited from ASSA::Socket
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
 
- Static Public Member Functions inherited from ASSA::Socket
static size_t xdr_length (const std::string &s_)
 Give the true length of the XDR-encoded STL string. More...
 
static bool is_little_endian ()
 Determine the endianess of the platform we are on. More...
 
static void close_handler (handler_t &socket_)
 Close socket endpoint in a portable way. More...
 
static string decode_fcntl_flags (long mask_)
 Decipher flags packed into mask_ used in fcntl() call. More...
 
- Protected Member Functions inherited from ASSA::Socket
int set_option (int level_, int optname_, int val_)
 Gateway method of setting socket options. More...
 
int set_fd_options (long flags_)
 Gateway method for setting file descriptor options. More...
 
int clear_fd_options (long flags_)
 Gateway method for clearing file descriptor options. More...
 
- Protected Attributes inherited from ASSA::Socket
handler_t m_fd
 File descriptor. More...
 
int m_type
 Socket domain type. More...
 
IOState m_state
 Control state of the socket. More...
 

Detailed Description

Definition at line 25 of file IPv4Socket.h.

Constructor & Destructor Documentation

ASSA::IPv4Socket::IPv4Socket ( )
inline

Default constructor.

Definition at line 32 of file IPv4Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by accept(), and clone().

33  : m_path (0), m_rdbuf (new Socketbuf (this)) {
34  trace_with_mask("IPv4Socket::IPv4Socket()",SOCKTRACE);
35  }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
char * m_path
Path of UNIX domain socket.
Definition: IPv4Socket.h:173
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
ASSA::IPv4Socket::IPv4Socket ( const handler_t  fd_)
inline

Constructor from file descriptor.

Parameters
fd_file descriptor to use

Definition at line 40 of file IPv4Socket.h.

References ASSA::Socket::m_fd, ASSA::SOCKTRACE, and trace_with_mask.

41  : m_path (0), m_rdbuf (new Socketbuf (this))
42  {
43  trace_with_mask("IPv4Socket::IPv4Socket(fd_)",SOCKTRACE);
44 
45  m_fd = fd_; // inherited from the parent class
46  }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
char * m_path
Path of UNIX domain socket.
Definition: IPv4Socket.h:173
handler_t m_fd
File descriptor.
Definition: Socket.h:485
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
ASSA::IPv4Socket::~IPv4Socket ( )
inline

Destructor will close connection.

Definition at line 49 of file IPv4Socket.h.

References close(), m_rdbuf, ASSA::SOCKTRACE, and trace_with_mask.

50  {
51  trace_with_mask("IPv4Socket::~IPv4Socket",SOCKTRACE);
52  this->close ();
53 
54  if (m_rdbuf != 0) {
55  delete m_rdbuf;
56  }
57  }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
bool close()
Close socket connection.
Definition: IPv4Socket.cpp:66
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
ASSA::IPv4Socket::IPv4Socket ( const IPv4Socket )
private

Member Function Documentation

IPv4Socket * IPv4Socket::accept ( )

Accept connection on the listening socket.

Here's an interesting spot - because accept() suppose to work both for INET and UNIX domain socket addresses, we have to allocate enough space and pass exact size of the address type expected.

Returned is a COMPLETED connection (meaning that socket pair is ready for data transfer and doesn't need call to open()). This method will block waiting on connection to come if there is no connection requests waiting on the listenning socket queue. To avoid blocking, use select(3P) first.

Returns
newly created connected socket to the client, or 0 if error.

Otherwise, if we use, for example, struct sockaddr_un as max. and accept returns struct sockaddr_in, we can cast back to struct sockaddr_in, but internally address data members are not guaranteed to be aligned correctly!!!

Definition at line 177 of file IPv4Socket.cpp.

References ASSA::ASSAERR, ASSA::Socket::clear(), close(), ASSA::disable_handler(), DL, EL, getDomain(), IPv4Socket(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, ASSA::Socket::nonblocking, ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Socket::turnOptionOn().

178 {
179  trace_with_mask("IPv4Socket::accept",SOCKTRACE);
180 
181  socklen_t length = 0;
182  SA* remote_address = NULL;
183  handler_t new_fd;
184 
185  disable_handler (new_fd);
186 
187  if ( getDomain() == AF_UNIX ) {
188  length = sizeof(struct sockaddr_in);
189  remote_address = (SA*) new SA_IN;
190  }
191  else /* AF_INET */
192  {
193  remote_address = (SA*) new SA_UN;
194  length = sizeof(struct sockaddr_un);
195  }
196  memset(remote_address, 0, length);
197 
198 #if !defined (_CYGWIN32__)
199  new_fd = ::accept(m_fd, remote_address, &length);
200 #else
201  new_fd = ::accept(m_fd, remote_address, (int*)&length);
202 #endif
203 
204  if (!is_valid_handler (new_fd)) {
205  EL((ASSAERR,"::accept() failed (new_fd=%d)\n", new_fd));
206  close();
207  return NULL;
208  }
209  if (length == sizeof(SA_IN)) {
210  SA_IN* sa_in = (SA_IN*) remote_address;
211 
212  DL((SOCK,"Accepted new TCP connection from Addr %s, port %d\n",
213  inet_ntoa(sa_in->sin_addr), ntohs( sa_in->sin_port)));
214  }
215  else {
216 #if !defined(WIN32)
217  SA_UN* sa_un = (SA_UN*) remote_address;
218  DL((SOCK,"Accepted new UNIX connection from %s\n", sa_un->sun_path));
219 #endif
220  }
221  delete remote_address;
222 
223  IPv4Socket* s = new IPv4Socket (new_fd);
224  s->clear ();
226  return s;
227 }
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
IPv4Socket * accept()
Accept connection on the listening socket.
Definition: IPv4Socket.cpp:177
ASSA and system errors.
Definition: LogMask.h:34
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition: Logger_Impl.h:108
bool close()
Close socket connection.
Definition: IPv4Socket.cpp:66
const int getDomain() const
Get socket domain type.
Definition: IPv4Socket.h:138
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
struct sockaddr SA
Definition: Address.h:33
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition: Socket.h:115
int handler_t
Definition: Logger_Impl.h:82
handler_t m_fd
File descriptor.
Definition: Socket.h:485
bool turnOptionOn(opt_t opt_)
Enable socket option.
Definition: Socket.cpp:185
Class Socket &amp; friends messages.
Definition: LogMask.h:41
struct sockaddr_un SA_UN
Definition: Address.h:44
IPv4Socket()
Default constructor.
Definition: IPv4Socket.h:32
struct sockaddr_in SA_IN
Definition: Address.h:34
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
bool IPv4Socket::bind ( const Address my_address_)
virtual

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

This call should follow the call to open() and precede the call to accept().

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

If UNIX domain, save the path

Implements ASSA::Socket.

Definition at line 124 of file IPv4Socket.cpp.

References Assure_return, EL, ASSA::Socket::failbit, ASSA::Address::getAddress(), getDomain(), ASSA::Address::getLength(), ASSA::Socket::m_fd, m_path, ASSA::Socket::reuseaddr, ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Socket::turnOptionOn().

125 {
126  trace_with_mask("IPv4Socket::bind",SOCKTRACE);
127 
128 #if !defined(WIN32)
129 
132  if ( getDomain() == AF_UNIX ) {
133  char* p = ((SA_UN *) addr_.getAddress())->sun_path;
134  m_path = new char[strlen(p)+1];
135  strcpy(m_path, p);
136  struct stat sb;
137 
138  if (stat (m_path, &sb) == 0) {
139  if ( S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode) ) {
140  unlink(m_path);
141  }
142  }
143  }
144 #endif
145 
146  /*---
147  From Stevens, Ch 7.5 (p.196):
148  "Set the SO_REUSEADDR socket option before calling bind(2)
149  in all TCP servers."
150  ---*/
152 
153  int rt = ::bind(m_fd, addr_.getAddress(), addr_.getLength());
154 
155  if ( rt < 0) {
156  EL((SOCK,"::bind() FD: %d failed\n",m_fd));
158  return (false);
159  }
160  Assure_return ( (::listen(m_fd, 5) == 0) );
161  return (true);
162 }
#define Assure_return(exp_)
Test condition and return bool from a function if assertion fails.
Definition: Assure.h:64
virtual bool bind(const Address &my_address_)
Server binds listening socket to its local well-known port.
Definition: IPv4Socket.cpp:124
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
const int getDomain() const
Get socket domain type.
Definition: IPv4Socket.h:138
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
char * m_path
Path of UNIX domain socket.
Definition: IPv4Socket.h:173
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
bool turnOptionOn(opt_t opt_)
Enable socket option.
Definition: Socket.cpp:185
Class Socket &amp; friends messages.
Definition: LogMask.h:41
struct sockaddr_un SA_UN
Definition: Address.h:44
Allow local address reuse.
Definition: Socket.h:100
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
IPv4Socket * IPv4Socket::clone ( ) const

"Virtual constructor".

clone() function creates an exact copy of Socket by dup(2)-ing file descriptor and copying Socket's internal state.

Definition at line 330 of file IPv4Socket.cpp.

References ASSA::Socket::clear(), DL, ASSA::Socket::failbit, ASSA::Socket::good(), ASSA::Streambuf::in_avail(), IPv4Socket(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.

331 {
332  const char self[] = "IPv4Socket::clone";
334 
335  handler_t nfd = dup (m_fd);
336  IPv4Socket* s = new IPv4Socket (nfd);
337 
338  DL((SOCK,"Original socket has %d bytes in its get_area\n",
339  m_rdbuf->in_avail ()));
340 
341  if (!is_valid_handler (nfd) || !good ()) {
343  }
344  else {
345  s->clear ();
346  }
347 
348  return s;
349 }
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
int in_avail()
This function returns the number of characters immediately available in the get area.
Definition: Streambuf.h:399
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
int handler_t
Definition: Logger_Impl.h:82
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
bool good() const
Indicates no error on the socket.
Definition: Socket.h:315
Class Socket &amp; friends messages.
Definition: LogMask.h:41
IPv4Socket()
Default constructor.
Definition: IPv4Socket.h:32
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
bool IPv4Socket::close ( void  )
virtual

Close socket connection.

Returns
true if success, fail if call to close() failed.

Implements ASSA::Socket.

Definition at line 66 of file IPv4Socket.cpp.

References ASSA::Socket::close_handler(), DL, ASSA::Socket::failbit, ASSA::Socket::flush(), ASSA::Streambuf::in_avail(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::Streambuf::sbumpc(), ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by accept(), and ~IPv4Socket().

67 {
68  trace_with_mask("IPv4Socket::close()",SOCKTRACE);
69 
70  if (is_valid_handler (m_fd)) {
71  DL((SOCK,"Closed FD: %d\n",m_fd));
72 
73  /*--- Flush data in output stream buffer ---*/
74  flush ();
77 
78  /*---
79  Socket can be re-opened in the future.
80  If there is some bytes left in it since last read(2),
81  clean them up.
82  ---*/
83 
84  if (m_rdbuf && m_rdbuf->in_avail ()) {
85  for (int c; (c=m_rdbuf->sbumpc ()) != EOF;) { }
86  }
87  }
88  return (true);
89 }
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
int in_avail()
This function returns the number of characters immediately available in the get area.
Definition: Streambuf.h:399
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
virtual Socket & flush()
This function simply calls the public &quot;synchronizing&quot; function rdbuf()-&gt;pubsync() (assuming the assoc...
Definition: Socket.cpp:74
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
Class Socket &amp; friends messages.
Definition: LogMask.h:41
int sbumpc()
This function should probably have been called ``sgetc&#39;&#39;.
Definition: Streambuf.h:539
static void close_handler(handler_t &socket_)
Close socket endpoint in a portable way.
Definition: Socket.h:448
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
bool IPv4Socket::connect ( const Address address_)
virtual

Client makes connection with the server at address_.

If socket is set to non-blocking mode, most likely connect() would return false with errno set to EINPROGRESS. See connect(2) manpage for details.

Parameters
address_peer address to connect with
Returns
true for success, false for error

Reimplemented from ASSA::Socket.

Definition at line 93 of file IPv4Socket.cpp.

References ASSA::Socket::clear(), DL, EL, ASSA::get_errno(), ASSA::Address::getAddress(), getDomain(), ASSA::Address::getLength(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, open(), ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.

94 {
95  trace_with_mask("IPv4Socket::connect()",SOCKTRACE);
96 
97  if (!is_valid_handler (m_fd) && open (getDomain()) == false) {
98  return false;
99  }
100 
101  int ret = ::connect (m_fd,
102  (SA*) his_address_.getAddress(),
103  his_address_.getLength());
104  if (ret < 0)
105  {
106  int e = get_errno (); // is ASYNC connect in progress?
107  if (e == EINPROGRESS || e == EWOULDBLOCK) {
108  DL((SOCK,"FD: %d OS::connect() error\n",m_fd));
109  }
110  else {
111  EL((SOCK,"FD: %d OS::connect() error\n",m_fd));
112  }
113  return (false);
114  }
115 
116  clear ();
117 
118  DL((SOCK,"Connection opened on FD: %d\n", m_fd));
119  return (true);
120 }
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
bool connect(const Address &address_)
Client makes connection with the server at address_.
Definition: IPv4Socket.cpp:93
const int getDomain() const
Get socket domain type.
Definition: IPv4Socket.h:138
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
struct sockaddr SA
Definition: Address.h:33
bool open(const int domain_)
Create socket.
Definition: IPv4Socket.cpp:42
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
int get_errno()
Fetch error number in a portable way.
Definition: Logger_Impl.h:115
handler_t m_fd
File descriptor.
Definition: Socket.h:485
Class Socket &amp; friends messages.
Definition: LogMask.h:41
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
const int ASSA::IPv4Socket::getDomain ( ) const
inlinevirtual

Get socket domain type.

Implements ASSA::Socket.

Definition at line 138 of file IPv4Socket.h.

References ASSA::Socket::m_type.

Referenced by accept(), bind(), and connect().

138 { return m_type; }
int m_type
Socket domain type.
Definition: Socket.h:488
handler_t ASSA::IPv4Socket::getHandler ( ) const
inlinevirtual

Get socket file descriptor.

Implements ASSA::Socket.

Definition at line 135 of file IPv4Socket.h.

References ASSA::Socket::m_fd.

135 { return m_fd; }
handler_t m_fd
File descriptor.
Definition: Socket.h:485
virtual int ASSA::IPv4Socket::in_avail ( ) const
inlinevirtual

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.

Implements ASSA::Socket.

Definition at line 162 of file IPv4Socket.h.

References ASSA::Streambuf::in_avail(), and m_rdbuf.

162 { return m_rdbuf->in_avail (); }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
int in_avail()
This function returns the number of characters immediately available in the get area.
Definition: Streambuf.h:399
bool IPv4Socket::open ( const int  domain_)
virtual

Create socket.

Socket domain type is specified as AF_INET for internet socket and AF_UNIX for UNIX domain socket (full duplex pipe).

Parameters
domain_domain
Returns
true if socket is created successfully, false otherwise

Implements ASSA::Socket.

Definition at line 42 of file IPv4Socket.cpp.

References ASSA::ASSAERR, ASSA::Socket::clear(), ASSA::disable_handler(), DL, EL, ASSA::Socket::failbit, ASSA::is_valid_handler(), ASSA::Socket::m_fd, ASSA::Socket::m_type, ASSA::Socket::nonblocking, ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Socket::turnOptionOn().

Referenced by connect().

43 {
44  trace_with_mask("IPv4Socket::open",SOCKTRACE);
45 
46  m_type = domain_;
47 
48  m_fd = ::socket(domain_, SOCK_STREAM, 0);
49 
50  if (!is_valid_handler (m_fd)) {
51  EL((ASSAERR,"OS::socket() error: m_fd = %d\n", m_fd));
54  return (false);
55  }
56  DL ((SOCK,"domain = %d, m_fd = %d\n", domain_, m_fd));
57 
58  clear ();
60 
61  return (true);
62 }
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
ASSA and system errors.
Definition: LogMask.h:34
int m_type
Socket domain type.
Definition: Socket.h:488
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition: Logger_Impl.h:108
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition: Socket.h:115
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
bool turnOptionOn(opt_t opt_)
Enable socket option.
Definition: Socket.cpp:185
Class Socket &amp; friends messages.
Definition: LogMask.h:41
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
IPv4Socket& ASSA::IPv4Socket::operator= ( const IPv4Socket )
private
virtual Streambuf* ASSA::IPv4Socket::rdbuf ( )
inlinevirtual

Return a pointer to the Socketbuf 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 Socketbuf functionality directly, given a Socket object.

Reimplemented from ASSA::Socket.

Definition at line 146 of file IPv4Socket.h.

References m_rdbuf.

146 { return m_rdbuf; }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
Streambuf * IPv4Socket::rdbuf ( Streambuf sb_)
virtual

Set new Socketbuf for internal IO buffering.

IPv4Socket object assumes full ownership of the memory pointed by sb_ (it will be release when ~IPv4Socket destructor is called).

Returns
Old Socketbuf object or sb_ if it is either NULL or matches old Socketbuf object.

Reimplemented from ASSA::Socket.

Definition at line 27 of file IPv4Socket.cpp.

References m_rdbuf, ASSA::SOCKTRACE, and trace_with_mask.

28 {
29  trace_with_mask("IPv4Socket::rdbuf(sb_)",SOCKTRACE);
30 
31  if (sb_ == 0 || sb_ == m_rdbuf) {
32  return (sb_);
33  }
34  Streambuf* old = m_rdbuf;
35  m_rdbuf = sb_;
36  return (old);
37 }
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
Streambuf class.
Definition: Streambuf.h:90
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
int IPv4Socket::read ( char *  buf_,
const unsigned int  size_ 
)
virtual

Read packet of specified size and save it to the given buffer.

Parameters
buf_buffer where packet will be stored
size_size of the packet to expect
Returns
number of bytes read or -1 on error indicating the reason in errno. 0 is returned if remote host closed its socket connection.

Non-blocking socket delivered partial packet.

Reimplemented from ASSA::Socket.

Definition at line 231 of file IPv4Socket.cpp.

References ASSA::ASSAERR, DL, ASSA::MemDump::dump_to_log(), EL, ASSA::Socket::eofbit, ASSA::Socket::failbit, ASSA::get_errno(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::Streambuf::sbumpc(), ASSA::Socket::setstate(), ASSA::Streambuf::sgetn(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Streambuf::unbuffered().

232 {
233  trace_with_mask("IPv4Socket::read",SOCKTRACE);
234 
235  register int len;
236  register int sz = size_;
237  char* tmp = packet_;
238 
239  if (!is_valid_handler (m_fd) < 0) {
240  return -1;
241  }
242 
243  len = 0;
244  if (m_rdbuf->unbuffered ()) {
245  /*
246  --- This needs to be redesigned ---
247  I should read a character at a time in loop,
248  until I get all characters, or EWOULDBLOCK or EOF.
249  If ::read() returns 0 or -1, it will be converted
250  by sbumpc() into EOF. Otherwise, sbumpc() returns
251  character read. Is this the right thing here to do?
252  */
253  if ((len = m_rdbuf->sbumpc ()) >= 0) {
254  *tmp = len;
255  len = 1;
256  }
257  }
258  else {
259  len = m_rdbuf->sgetn (tmp, sz);
260  }
261  if (len == -1)
262  {
265  if (get_errno () != EWOULDBLOCK) {
266  EL((ASSAERR,"::read (fd=%d) failed.\n",m_fd));
268  }
269  return len;
270  }
271  tmp += len;
272  sz -= len;
273 
274  if ((size_ - sz) == 0)
275  {
276  DL((SOCK,"Peer has dropped connection FD: %d\n",m_fd));
278  return 0;
279  }
280 
281  DL((SOCKTRACE,"==> FD: %d Received %d bytes\n", m_fd, size_ - sz));
282  MemDump::dump_to_log (SOCKTRACE, "Data received:", packet_, size_ - sz);
283 
284  /*
285  Return number of bytes read. If all requested bytes have been
286  read, then sz is 0 and size_ is returned. If sz != 0, then
287  writer has sent us a partial packet.
288  */
289  return (size_ - sz);
290 }
ASSA and system errors.
Definition: LogMask.h:34
void unbuffered(int i_)
If i_ is non-zero, then all IO operations are buffered.
Definition: Streambuf.h:417
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
#define EL(X)
A macro for writing error message to the Logger.
Definition: Logger.h:285
static void dump_to_log(unsigned long mask_, const char *info_, const char *msg_, int len_)
Write hex/ascii dump of a memory region to log file.
Definition: MemDump.cpp:111
int get_errno()
Fetch error number in a portable way.
Definition: Logger_Impl.h:115
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
Class Socket &amp; friends messages.
Definition: LogMask.h:41
int sgetn(char *b_, int len_)
This function gets the next len_ characters following the get pointer, copying them to the char array...
Definition: Streambuf.h:559
int sbumpc()
This function should probably have been called ``sgetc&#39;&#39;.
Definition: Streambuf.h:539
indicates that an input operation reached the end of an input sequence
Definition: Socket.h:82
Extended Socket &amp; friends messages.
Definition: LogMask.h:42
int IPv4Socket::write ( const char *  buf_,
const unsigned int  size_ 
)
virtual

Perform blocking write by writing packet of specified size.

Parameters
buf_buffer to send
size_packet size
Returns
number of bytes written or -1 for error

Reimplemented from ASSA::Socket.

Definition at line 294 of file IPv4Socket.cpp.

References DL, ASSA::MemDump::dump_to_log(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::SOCK, ASSA::SOCKTRACE, ASSA::Streambuf::sputc(), ASSA::Streambuf::sputn(), trace_with_mask, and ASSA::Streambuf::unbuffered().

295 {
296  trace_with_mask("IPv4Socket::write()",SOCKTRACE);
297 
298  int ret = 0;
299 
300  if (!is_valid_handler (m_fd)) {
301  return -1;
302  }
303 
304  if (m_rdbuf->unbuffered ())
305  {
306  int wlen = size_;
307  char* p = (char*) packet_;
308 
309  while (wlen-- > 0) {
310  if (m_rdbuf->sputc (*p++) == EOF) {
311  return (EOF);
312  }
313  }
314  ret = p - packet_;
315  }
316  else {
317  ret = m_rdbuf->sputn ((char*) packet_, size_);
318  }
319 
320  if (ret > 0) {
321  DL((SOCK,"<= FD: %d Wrote %d bytes (requested %d bytes)\n",
322  m_fd, ret, size_));
323  MemDump::dump_to_log (SOCK, "Data written", (char*)packet_, ret);
324  }
325  return ret;
326 }
int sputc(char c_)
This function stores c just after the put pointer, and advances the pointer one position, possibly extending the sequence.
Definition: Streambuf.h:568
void unbuffered(int i_)
If i_ is non-zero, then all IO operations are buffered.
Definition: Streambuf.h:417
int sputn(char *b_, int len_)
From the location pointed to by ptr, stores exactly len characters after the put pointer, advancing the put pointer just past the last character.
Definition: Streambuf.h:579
Streambuf * m_rdbuf
Socketbuf.
Definition: IPv4Socket.h:176
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
static void dump_to_log(unsigned long mask_, const char *info_, const char *msg_, int len_)
Write hex/ascii dump of a memory region to log file.
Definition: MemDump.cpp:111
handler_t m_fd
File descriptor.
Definition: Socket.h:485
Class Socket &amp; friends messages.
Definition: LogMask.h:41
Extended Socket &amp; friends messages.
Definition: LogMask.h:42

Member Data Documentation

char* ASSA::IPv4Socket::m_path
private

Path of UNIX domain socket.

Definition at line 173 of file IPv4Socket.h.

Referenced by bind().

Streambuf* ASSA::IPv4Socket::m_rdbuf
private

Socketbuf.

Definition at line 176 of file IPv4Socket.h.

Referenced by clone(), close(), in_avail(), rdbuf(), read(), write(), and ~IPv4Socket().

const int ASSA::IPv4Socket::MAXTCPBUFSZ
static

Maximum TCP data frame (no options)

Definition at line 29 of file IPv4Socket.h.


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