38 #include "blocxx/BLOCXX_config.h" 40 #if !defined(BLOCXX_WIN32) 62 #include <sys/types.h> 64 #include <sys/socket.h> 67 #include <arpa/inet.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);
134 m_out.exceptions(std::ios::badbit);
135 m_inout.exceptions(std::ios::badbit);
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();
259 sockSo.waitForWrite =
true;
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))
601 #endif // #if !defined(BLOCXX_WIN32)
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability...
static void setDumpFiles(const String &in, const String &out)
String toString(ETimeOffset timeOffset=E_LOCAL_TIME) const
sockaddr_un UnixSocketAddress_t
A TimeoutTimer is used by an algorithm to determine when a timeout has expired.
const String toString() const
Returns the IP address and the port with a colon in between.
const SocketAddress_t * getNativeForm() const
IntrusiveReference< U > cast_to() const
size_t getNativeFormSize() const
void loop()
Meant to be called by timeout functions which loop, but don't want to reset the interval.
UInt32 getMicrosecond() const
Get the microsecond of the second for this DateTime object.
#define BLOCXX_LOG_ERROR(logger, message)
Log message to logger with the Error level.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(), it throws an AssertionException.
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.
BLOCXX_COMMON_API int close(const FileHandle &hdl)
Close file handle.
static String m_traceFileIn
This String class is an abstract data type that represents as NULL terminated string of characters...
bool waitForOutput(const Timeout &timeout)
virtual void disconnect()
handle_type get() const
Return handle of resource, retaining ownership.
bool expired() const
Indicates whether the last loop time has exceeded the timeout.
virtual bool waitForInput(const Timeout &timeout)
handle_type release()
Relinquish ownership of resource and return its handle.
virtual int writeAux(const void *dataOut, int dataOutLen)=0
const int SELECT_ERROR
The value returned from select when any error occurs other than timeout.
AddressType getType() const
std::iostream & getIOStream()
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.
const int SELECT_TIMEOUT
The value returned from select when the timeout value has expired.
void assignFromNativeForm(const UnixSocketAddress_t *address, size_t len)
static String m_traceFileOut
A timeout can be absolute, which means that it will happen at the specified DateTime.
PURPOSE: The AutoResource class template is an analog of std::auto_ptr for managing arbitrary resourc...
void setToCurrent()
Set this DateTime to the current system time.
const char * c_str() const
Select_t getSelectObj() const
virtual int readAux(void *dataIn, int dataInLen)=0
void start()
Meant to be called by timeout functions which loop.
const InetSocketAddress_t * getInetAddress() const
Get a pointer to the InetSocketAddress_t precondition: getType() == INET.
bool waitForRead
Input parameter. Set it to true to indicate that waiting for read availability on s is desired...
void push_back(const T &x)
Append an element to the end of the Array.
std::ostream & getOutputStream()
sockaddr_in InetSocketAddress_t
std::istream & getInputStream()
SocketHandle_t getfd() const
static ShutDownMechanism_t getShutDownMechanism()
The DateTime class is an abstraction for date time data.
Timeout asRelativeTimeout() const
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
LazyGlobal< Mutex, int, GlobalMutexFactory > GlobalMutex
int waitForIO(SocketHandle_t fd, int timeOutSecs, SocketFlags::EWaitDirectionFlag waitFlag)
Wait for input or output on a socket.
SocketAddress m_peerAddress
int selectRW(SelectObjectArray &selarray, UInt32 ms)
SocketAddress m_localAddress
static void testCancel()
Test if this thread has been cancelled.
virtual void connect(const SocketAddress &addr)
#define BLOCXX_THROW_ERRNO_MSG(exType, msg)
Throw an exception using FILE, LINE, errno and strerror(errno)
SocketStreamBuffer m_streamBuf
virtual ~SocketBaseImpl()
#define BLOCXX_GLOBAL_MUTEX_INIT()