38#include "blocxx/BLOCXX_config.h"
40#if !defined(BLOCXX_WIN32)
64#include <sys/socket.h>
70#include <netinet/in.h>
100 , m_isConnected(false)
104 , m_recvTimeoutExprd(false)
107 , m_out(&m_streamBuf)
108 , m_inout(&m_streamBuf)
109 , m_recvTimeout(
Timeout::infinite)
110 , m_sendTimeout(
Timeout::infinite)
111 , m_connectTimeout(
Timeout::infinite)
113 m_out.exceptions(std::ios::badbit);
114 m_inout.exceptions(std::ios::badbit);
121 , m_isConnected(true)
125 , m_recvTimeoutExprd(false)
128 , m_out(&m_streamBuf)
129 , m_inout(&m_streamBuf)
130 , m_recvTimeout(
Timeout::infinite)
131 , m_sendTimeout(
Timeout::infinite)
132 , m_connectTimeout(
Timeout::infinite)
134 m_out.exceptions(std::ios::badbit);
135 m_inout.exceptions(std::ios::badbit);
153 , m_isConnected(false)
156 , m_peerAddress(addr)
157 , m_recvTimeoutExprd(false)
160 , m_out(&m_streamBuf)
161 , m_inout(&m_streamBuf)
162 , m_recvTimeout(
Timeout::infinite)
163 , m_sendTimeout(
Timeout::infinite)
164 , m_connectTimeout(
Timeout::infinite)
166 m_out.exceptions(std::ios::badbit);
167 m_inout.exceptions(std::ios::badbit);
203 int domain_type = PF_UNIX;
206 domain_type = PF_INET;
207#ifdef BLOCXX_HAVE_IPV6
209 if(
reinterpret_cast<const sockaddr*
>(addr.
getInetAddress())->sa_family == AF_INET6)
211 domain_type = PF_INET6;
217 if (sockfd.
get() == -1)
220 "Failed to create a socket");
224 if (::fcntl(sockfd.
get(), F_SETFD, FD_CLOEXEC) == -1)
229 int flags = ::fcntl(sockfd.
get(), F_GETFL, 0);
230 ::fcntl(sockfd.
get(), F_SETFL, flags | O_NONBLOCK);
231#if defined(BLOCXX_NCR)
237 if (errno != EINPROGRESS)
254 pipefd = lUPipe->getInputHandle();
290 if (selra.
size() == 2 && selra[1].readAvailable)
294 else if (selra[0].readAvailable || selra[0].writeAvailable)
298#if defined(BLOCXX_NCR)
299 if (::getsockopt(sockfd.
get(), SOL_SOCKET, SO_ERROR, (
char*)&error, &len) < 0)
301 if (::getsockopt(sockfd.
get(), SOL_SOCKET, SO_ERROR, &error, &len) < 0)
305 "SocketBaseImpl::connect() getsockopt() failed");
311 "SocketBaseImpl::connect() failed");
319 ::fcntl(sockfd.
get(), F_SETFL, flags);
341 ofstream comboTraceFile(combofilename.
c_str(), std::ios::app);
348 comboTraceFile <<
Format(
"\n--->fd: %1 opened to \"%2\" at %3.%4 <---\n",
getfd(),
359 m_in.clear(ios::eofbit);
363 m_out.clear(ios::eofbit);
384 ofstream comboTraceFile(combofilename.
c_str(), std::ios::app);
391 comboTraceFile <<
"\n--->fd: " <<
getfd() <<
" closed at " << curDateTime.
toString(
"%X") <<
405 struct sockaddr *p_addr;
407 memset(&ss, 0,
sizeof(ss));
409 p_addr =
reinterpret_cast<struct sockaddr*
>(&ss);
410 if (getsockname(
m_sockfd, p_addr, &len) != -1)
414 memset(&ss, 0,
sizeof(ss));
416 if (getpeername(
m_sockfd, p_addr, &len) != -1)
427 memset(&addr, 0,
sizeof(addr));
429 if (getsockname(
m_sockfd,
reinterpret_cast<struct sockaddr*
>(&addr), &len) == -1)
441 bool isError =
false;
460 if (!traceFile.write(
static_cast<const char*
>(dataOut), rc))
466 ofstream comboTraceFile(combofilename.
c_str(), std::ios::app);
473 comboTraceFile <<
"\n--->fd: " <<
getfd() <<
" Out " << rc <<
" bytes at " << curDateTime.
toString(
"%X") <<
475 if (!comboTraceFile.write(
static_cast<const char*
>(dataOut), rc))
497 bool isError =
false;
507 rc =
readAux(dataIn, dataInLen);
516 if (!traceFile.write(
reinterpret_cast<const char*
>(dataIn), rc))
522 ofstream comboTraceFile(combofilename.
c_str(), std::ios::app);
529 comboTraceFile <<
"\n--->fd: " <<
getfd() <<
" In " << rc <<
" bytes at " << curDateTime.
toString(
"%X") <<
531 if (!comboTraceFile.write(
reinterpret_cast<const char*
>(dataIn), rc))
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
#define BLOCXX_THROW_ERRNO_MSG(exType, msg)
Throw an exception using FILE, LINE, errno and strerror(errno)
#define BLOCXX_GLOBAL_MUTEX_INIT()
#define BLOCXX_LOG_ERROR(logger, message)
Log message to logger with the Error level.
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
void push_back(const T &x)
Append an element to the end of the Array.
PURPOSE: The AutoResource class template is an analog of std::auto_ptr for managing arbitrary resourc...
handle_type get() const
Return handle of resource, retaining ownership.
handle_type release()
Relinquish ownership of resource and return its handle.
The DateTime class is an abstraction for date time data.
UInt32 getMicrosecond() const
Get the microsecond of the second for this DateTime object.
String toString(ETimeOffset timeOffset=E_LOCAL_TIME) const
void setToCurrent()
Set this DateTime to the current system time.
IntrusiveReference< U > cast_to() const
void assignFromNativeForm(const UnixSocketAddress_t *address, size_t len)
AddressType getType() const
size_t getNativeFormSize() const
const SocketAddress_t * getNativeForm() const
const InetSocketAddress_t * getInetAddress() const
Get a pointer to the InetSocketAddress_t precondition: getType() == INET.
const String toString() const
Returns the IP address and the port with a colon in between.
std::istream & getInputStream()
virtual void disconnect()
virtual void connect(const SocketAddress &addr)
SocketStreamBuffer m_streamBuf
std::ostream & getOutputStream()
virtual ~SocketBaseImpl()
SocketAddress m_peerAddress
static String m_traceFileOut
virtual int readAux(void *dataIn, int dataInLen)=0
Select_t getSelectObj() const
virtual bool waitForInput(const Timeout &timeout)
SocketAddress m_localAddress
static String m_traceFileIn
virtual int writeAux(const void *dataOut, int dataOutLen)=0
bool waitForOutput(const Timeout &timeout)
int write(const void *dataOut, int dataOutLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
Write a specified number of bytes to the device that is exposing the IOIFC interface.
std::iostream & getIOStream()
static void setDumpFiles(const String &in, const String &out)
int read(void *dataIn, int dataInLen, ErrorAction errorAsException=E_RETURN_ON_ERROR)
Read a specified number of bytes from the device that is exposing the IOIFC interface.
SocketHandle_t getfd() const
static ShutDownMechanism_t getShutDownMechanism()
This String class is an abstract data type that represents as NULL terminated string of characters.
const char * c_str() const
static void testCancel()
Test if this thread has been cancelled.
A timeout can be absolute, which means that it will happen at the specified DateTime.
A TimeoutTimer is used by an algorithm to determine when a timeout has expired.
bool expired() const
Indicates whether the last loop time has exceeded the timeout.
void start()
Meant to be called by timeout functions which loop.
void loop()
Meant to be called by timeout functions which loop, but don't want to reset the interval.
Timeout asRelativeTimeout() const
const int SELECT_TIMEOUT
The value returned from select when the timeout value has expired.
int selectRW(SelectObjectArray &selarray, UInt32 ms)
const int SELECT_ERROR
The value returned from select when any error occurs other than timeout.
int waitForIO(SocketHandle_t fd, int timeOutSecs, SocketFlags::EWaitDirectionFlag waitFlag)
Wait for input or output on a socket.
sockaddr_in InetSocketAddress_t
sockaddr_un UnixSocketAddress_t
LazyGlobal< Mutex, int, GlobalMutexFactory > GlobalMutex
bool waitForRead
Input parameter. Set it to true to indicate that waiting for read availability on s is desired.
bool waitForWrite
Input parameter. Set it to true to indicate that waiting for write availability on s is desired.