18 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__CYGWIN32__) || defined (__GNU__) 19 # include <sys/ioctl.h> 22 #if defined (__NetBSD__) 23 # include <sys/filio.h> 24 # include <sys/ioctl.h> 28 # define O_NONBLOCK 04000 29 typedef unsigned int socklen_t;
57 ret = ioctlsocket (
m_fd, FIONREAD, &ba);
59 ret = ioctl (
m_fd, FIONREAD, &ba);
63 EL((
ASSAERR,
"ioctl(2) failed with ret: %d\n", ret));
68 DL((
SOCKTRACE,
"%ld bytes available for reading\n", ba));
77 if (
rdbuf ()->pubsync () == EOF) {
88 int ret = setsockopt (
m_fd,
113 if ((val = ioctlsocket (
m_fd, FIONBIO, &set_nonblock)) == 0) {
114 m_nonblocking =
true;
121 if ((val = ::fcntl (
m_fd, F_GETFL, 0)) < 0) {
129 ret = ::fcntl (
m_fd, F_SETFL, val);
131 val = ::fcntl (
m_fd, F_GETFL, 0);
132 DL ((
SOCKTRACE,
"Flags are set to %s via fcntl(25)\n",
157 if ((ret = ioctlsocket (
m_fd, FIONBIO, &set_block)) == 0) {
158 m_nonblocking =
false;
165 if ((oldflags = ::fcntl (
m_fd, F_GETFL, 0)) < 0) {
168 newflags = oldflags & ~flags_;
173 ret = ::fcntl (
m_fd, F_SETFL, newflags);
175 newflags = ::fcntl (
m_fd, F_GETFL, 0);
176 DL ((
SOCKTRACE,
"Flags are set to %s via fcntl(%d)\n",
194 optname = SO_REUSEADDR;
199 return set_option (SOL_SOCKET, optname, 1) == 0;
213 optname = SO_REUSEADDR;
218 return set_option (SOL_SOCKET, optname, 0) == 0;
234 optname = SO_RCVLOWAT;
237 optname = SO_SNDLOWAT;
243 return set_option (SOL_SOCKET, optname, arg_) == 0;
256 return (m_nonblocking ? 1 : 0);
258 if ((optval = ::fcntl (
m_fd, F_GETFL, 0)) < 0) {
261 return ((optval & O_NONBLOCK) == O_NONBLOCK ? 1 : 0);
266 int level = SOL_SOCKET;
269 socklen_t len =
sizeof (optval);
273 optname = SO_RCVLOWAT;
276 optname = SO_SNDLOWAT;
279 optname = SO_REUSEADDR;
287 #if defined (__CYGWIN32__) || defined (WIN32) 289 ret = getsockopt (
m_fd, level, optname, (
char*) &optval, (
int*)&len);
293 ret = getsockopt (
m_fd, level, optname, (
char*) &optval, &len);
301 return (ret == 0 ? 0 : 1);
312 register int count = 0;
315 if (n_ == INT_MAX && delim_ == EOF) {
323 for (; n_; n_--, count++) {
324 if ( (b =
read (&c, 1)) == 0 ) {
363 int len =
sizeof (int);
365 xdrmem_create (&xdrs, (caddr_t) &c, len, XDR_DECODE);
367 if (
read ((
char* ) &c, len) == len) {
368 xdr_char (&xdrs, &n_);
393 while (len-- &&
read (&c, 1) == 1) {
405 if (
read ((
char*) &val,
sizeof(
short)) ==
sizeof(
short)) {
406 n_ = (short) ntohs ((
short)val);
436 #define READ_INT(TYPE) \ 437 Socket& Socket::operator>>(TYPE& n_) \ 440 int typesz = sizeof(TYPE);\ 441 if (read ( (char* ) &val, typesz) == typesz) {\ 442 if (sizeof(int32_t) <= typesz) {\ 443 n_ = (TYPE) ntohl (val); \ 446 if (Socket::is_little_endian ()) {\ 447 *((int32_t*)(&n_)+1) = ntohl (*((int32_t*)(&val)) );\ 448 *((int32_t*)(&n_) ) = ntohl (*((int32_t*)(&val))+1);\ 451 *((int32_t*)(&n_) ) = ntohl (*((int32_t*)(&val)) );\ 452 *((int32_t*)(&n_)+1) = ntohl (*((int32_t*)(&val))+1);\ 457 setstate (Socket::eofbit|Socket::failbit);\ 473 xdrmem_create (&xdrs, (caddr_t) &val,
sizeof(
float), XDR_DECODE);
475 if (
read ((
char*) &val,
sizeof(
float)) ==
sizeof(
float)) {
476 xdr_float (&xdrs, &n_);
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_);
513 int len =
sizeof (int);
516 xdrmem_create (&xdrs, (caddr_t) &buf, len, XDR_ENCODE);
517 xdr_char (&xdrs, &n_);
519 if (
write ((
const char*) &buf, len) != len) {
548 static const char pad [4] = { 0, 0, 0, 0 };
550 (*this) << s_.length ();
551 int ret =
write (s_.c_str (), s_.length ());
552 if ( ret != s_.length () ) {
555 size_t r = 4 - s_.length() % 4;
557 if (
write (pad, r) != r) {
567 short val = (short) htons((
short)n_);
569 if (
write ((
const char*) &val,
sizeof(
short)) !=
sizeof(short))
588 #define WRITE_INT(TYPE) \ 589 Socket& Socket::operator<< (TYPE n_) \ 592 int typesz = sizeof(TYPE);\ 593 if (sizeof(int32_t) <= typesz) {\ 594 val = (TYPE) ntohl (n_); \ 597 if (Socket::is_little_endian ()) {\ 598 *((int32_t*)(&val)+1) = htonl (*((int32_t*)(&n_)) );\ 599 *((int32_t*)(&val) ) = htonl (*((int32_t*)(&n_))+1);\ 602 *((int32_t*)(&val) ) = htonl (*((int32_t*)(&n_)) );\ 603 *((int32_t*)(&val)+1) = htonl (*((int32_t*)(&n_))+1);\ 606 if (write ((const char*) &val, typesz) != typesz) {\ 607 setstate (Socket::eofbit|Socket::failbit);\ 623 xdrmem_create (&xdrs, (caddr_t) &buf,
sizeof(
float), XDR_ENCODE);
624 xdr_float (&xdrs, &f);
626 int ret =
write ((
const char*) &buf,
sizeof(
float));
628 if ( ret !=
sizeof(
float) ) {
640 xdrmem_create (&xdrs, (caddr_t) &buf,
sizeof(
double), XDR_ENCODE);
641 xdr_double (&xdrs, &f);
643 int ret =
write ((
const char*) &buf,
sizeof(
double));
645 if ( ret !=
sizeof(
double) ) {
657 char state_set[] =
"[ set]\n";
658 char state_not_set[] =
"[not set]\n";
660 std::ostringstream msg;
663 msg <<
"\tTesting good() ....... ";
665 if (this->
good ()) msg << state_set;
666 else msg << state_not_set;
668 msg <<
"\tTesting eof() ........ ";
669 if (this->
eof ()) msg << state_set;
670 else msg << state_not_set;
672 msg <<
"\tTesting fail() ....... ";
673 if (this->
fail ()) msg << state_set;
674 else msg << state_not_set;
676 msg <<
"\tTesting bad() ........ ";
677 if (this->
bad ()) msg << state_set;
678 else msg << state_not_set;
680 msg <<
"\tTesting !() .......... ";
681 if ( !(*
this) ) msg << state_set;
682 else msg << state_not_set;
684 msg <<
"\tTesting void *() ..... ";
685 if ( *
this ) msg << state_set;
686 else msg << state_not_set;
688 msg <<
"\tTesting nonblocking... ";
690 else msg << state_not_set;
703 char c [
sizeof (short)];
708 return (endian_u.c [0] == 0);
719 if (mask_ & O_RDONLY) {
720 answer =
"O_RDONLY|";
722 if (mask_ & O_WRONLY) {
723 answer +=
"O_WRONLY|";
725 if (mask_ & O_RDWR) {
728 if (mask_ & O_APPEND) {
729 answer +=
"O_APPEND|";
731 if (mask_ & O_NONBLOCK) {
732 answer +=
"O_NONBLOCK|";
734 if (mask_ & O_SYNC) {
738 if (mask_ & O_ASYNC) {
739 answer +=
"O_ASYNC|";
742 answer.erase (answer.end () - 1);
void dumpState() const
Write state bits of the socket to the log file.
static string decode_fcntl_flags(long mask_)
Decipher flags packed into mask_ used in fcntl() call.
virtual int write(const char *buf_, const u_int size_)
Write specified number of bytes to the socket.
int ignore(int n_=INT_MAX, int delim_=EOF)
Extracts bytes and discards them.
indicates that an input operation failed to read the expected characters, or that an output operation...
int getOption(opt_t opt_) const
Get current value of a socket option.
bool turnOptionOff(opt_t opt_)
Disable socket option.
bool bad() const
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer...
int in_avail()
This function returns the number of characters immediately available in the get area.
int getBytesAvail(void) const
Return number of bytes available in socket receive buffer.
bool fail() const
Indicates that earlier extraction opeartion has failed to match the required pattern of input...
#define DL(X)
A macro for writing debug message to the Logger.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
bool eof() const
An earlier extraction operation has encountered the end of file of the input stream (peer closed its ...
bool setOption(opt_t opt_, int arg_)
Set socket option to value required.
virtual int read(char *buf_, const u_int size_)
Read expected number of bytes from the socket.
#define EL(X)
A macro for writing error message to the Logger.
int set_fd_options(long flags_)
Gateway method for setting file descriptor options.
bool good() const
Indicates no error on the socket.
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
int clear_fd_options(long flags_)
Gateway method for clearing file descriptor options.
Socket & ends(Socket &os_)
ends manipulator.
static const int PGSIZE
Size of bytes of a kernel page.
virtual Socket & flush()
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the assoc...
XDRHack provides XDR definitions for systems that have them missing.
Socket & operator>>(char &c)
Input of built-in char type. The value will be XDR-decoded.
virtual Streambuf * rdbuf()
Return a pointer to the Streambuf associated with the stream.
handler_t m_fd
File descriptor.
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Socket & operator<<(char c)
Output of built-in char type. The value will be XDR-encoded.
static bool is_little_endian()
Determine the endianess of the platform we are on.
The receiver low-water mark is the amount of data that must be in the socket receive buffer for selec...
bool turnOptionOn(opt_t opt_)
Enable socket option.
Allow local address reuse.
The send low-water mark si the amount of available space that must exist in the socket send buffer fo...
int set_option(int level_, int optname_, int val_)
Gateway method of setting socket options.
indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
indicates that an input operation reached the end of an input sequence
Abstraction of socket data type.
Extended Socket & friends messages.