15 # include <ws2tcpip.h>
17 # include <sys/errno.h>
31 if (sb_ == 0 || sb_ ==
m_rdbuf) {
42 open (
const int domain_)
48 m_fd = ::socket(domain_, SOCK_STREAM, 0);
56 DL ((
SOCK,
"domain = %d, m_fd = %d\n", domain_,
m_fd));
107 if (e == EINPROGRESS || e == EWOULDBLOCK) {
108 DL((
SOCK,
"FD: %d OS::connect() error\n",
m_fd));
111 EL((
SOCK,
"FD: %d OS::connect() error\n",
m_fd));
118 DL((
SOCK,
"Connection opened on FD: %d\n",
m_fd));
134 m_path =
new char[strlen(p)+1];
138 if (stat (
m_path, &sb) == 0) {
139 if ( S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode) ) {
181 socklen_t length = 0;
182 SA* remote_address = NULL;
188 length =
sizeof(
struct sockaddr_in);
189 remote_address = (
SA*)
new SA_IN;
193 remote_address = (
SA*)
new SA_UN;
194 length =
sizeof(
struct sockaddr_un);
196 memset(remote_address, 0, length);
198 #if !defined (_CYGWIN32__)
205 EL((
ASSAERR,
"::accept() failed (new_fd=%d)\n", new_fd));
209 if (length ==
sizeof(
SA_IN)) {
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)));
218 DL((
SOCK,
"Accepted new UNIX connection from %s\n", sa_un->sun_path));
221 delete remote_address;
231 read (
char* packet_,
const unsigned int size_)
236 register int sz = size_;
274 if ((size_ - sz) == 0)
276 DL((
SOCK,
"Peer has dropped connection FD: %d\n",
m_fd));
294 write(
const char* packet_,
const unsigned int size_)
307 char* p = (
char*) packet_;
321 DL((
SOCK,
"<= FD: %d Wrote %d bytes (requested %d bytes)\n",
332 const char self[] =
"IPv4Socket::clone";
338 DL((
SOCK,
"Original socket has %d bytes in its get_area\n",
#define Assure_return(exp_)
Test condition and return bool from a function if assertion fails.
Class IPv4Socket covers domain types AF_INET and AF_UNIX.
#define EL(X)
A macro for writing error message to the Logger.
#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.
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
virtual SA * getAddress() const =0
Retrieve pointer to the address structure.
virtual const int getLength() const =0
Return length of the underlying address structure.
IPv4Socket * accept()
Accept connection on the listening socket.
int write(const char *buf_, const unsigned int size_)
Perform blocking write by writing packet of specified size.
IPv4Socket()
Default constructor.
IPv4Socket * clone() const
"Virtual constructor".
virtual Streambuf * rdbuf()
Return a pointer to the Socketbuf associated with the stream.
bool close()
Close socket connection.
bool connect(const Address &address_)
Client makes connection with the server at address_.
bool open(const int domain_)
Create socket.
Streambuf * m_rdbuf
Socketbuf.
virtual bool bind(const Address &my_address_)
Server binds listening socket to its local well-known port.
const int getDomain() const
Get socket domain type.
int read(char *buf_, const unsigned int size_)
Read packet of specified size and save it to the given buffer.
char * m_path
Path of UNIX domain socket.
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.
int m_type
Socket domain type.
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
static void close_handler(handler_t &socket_)
Close socket endpoint in a portable way.
bool turnOptionOn(opt_t opt_)
Enable socket option.
@ reuseaddr
Allow local address reuse.
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
handler_t m_fd
File descriptor.
bool good() const
Indicates no error on the socket.
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
virtual Socket & flush()
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the assoc...
@ 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...
int sputc(char c_)
This function stores c just after the put pointer, and advances the pointer one position,...
int sbumpc()
This function should probably have been called `‘sgetc’'.
int in_avail()
This function returns the number of characters immediately available in the get area.
void unbuffered(int i_)
If i_ is non-zero, then all IO operations are buffered.
int sputn(char *b_, int len_)
From the location pointed to by ptr, stores exactly len characters after the put pointer,...
int sgetn(char *b_, int len_)
This function gets the next len_ characters following the get pointer, copying them to the char array...
@ SOCK
Class Socket & friends messages
@ SOCKTRACE
Extended Socket & friends messages
@ ASSAERR
ASSA and system errors
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
int get_errno()
Fetch error number in a portable way.