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);
121 , m_isConnected(true)
123 , m_localAddress(SocketAddress::getAnyLocalHost())
124 , m_peerAddress(SocketAddress::allocEmptyAddress(addrType))
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)
155 , m_localAddress(SocketAddress::getAnyLocalHost())
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)
232 if ((n = ::
connect(sockfd.get(),
const_cast<SocketAddress_t *
>(addr.getNativeForm()), addr.getNativeFormSize())) < 0)
234 if ((n = ::
connect(sockfd.get(), addr.getNativeForm(), addr.getNativeFormSize())) < 0)
237 if (errno != EINPROGRESS)
240 Format(
"Failed to connect to: %1", addr.toString()).c_str());
252 lUPipe = foo.cast_to<PosixUnnamedPipe>();
254 pipefd = lUPipe->getInputHandle();
257 Select::SelectObject sockSo(sockfd.get());
258 sockSo.waitForRead =
true;
259 sockSo.waitForWrite =
true;
260 selra.push_back(sockSo);
263 Select::SelectObject pipeSo(pipefd);
264 pipeSo.waitForRead =
true;
265 selra.push_back(pipeSo);
279 BLOCXX_THROW(SocketException,
"SocketBaseImpl::connect() select timedout");
290 if (selra.size() == 2 && selra[1].readAvailable)
292 BLOCXX_THROW(SocketException,
"Sockets have been shutdown");
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");
316 BLOCXX_THROW(SocketException,
"SocketBaseImpl::connect(). Logic error, sockfd not in FD set.");
319 ::fcntl(sockfd.get(), F_SETFL, flags);
338 MutexLock ml(g_guard);
341 ofstream comboTraceFile(combofilename.c_str(), std::ios::app);
346 DateTime curDateTime;
347 curDateTime.setToCurrent();
348 comboTraceFile << Format(
"\n--->fd: %1 opened to \"%2\" at %3.%4 <---\n",
getfd(),
350 curDateTime.toString(
"%X"), curDateTime.getMicrosecond());
359 m_in.clear(ios::eofbit);
363 m_out.clear(ios::eofbit);
381 MutexLock ml(g_guard);
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);
510 MutexLock ml(g_guard);
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)