added Socket::setNonBlock(), Socket::initConnectNB(), Socket::finiConnectNB()

This commit is contained in:
hyung-hwan 2020-08-23 17:58:55 +00:00
parent a83005ca99
commit 24d8c70835
6 changed files with 64 additions and 17 deletions

View File

@ -78,11 +78,15 @@ public:
int setTcpNodelay (int n) QSE_CPP_NOEXCEPT;
int setOobInline (int n) QSE_CPP_NOEXCEPT;
int setIpv6Only (int n) QSE_CPP_NOEXCEPT;
int setNonBlock (int n) QSE_CPP_NOEXCEPT;
// --------------------------------------------------------------------
int shutdown (int how = 2) QSE_CPP_NOEXCEPT;
int connect (const SocketAddress& target) QSE_CPP_NOEXCEPT;
int initConnectNB (const SocketAddress& target) QSE_CPP_NOEXCEPT;
int finiConnectNB () QSE_CPP_NOEXCEPT;
// --------------------------------------------------------------------
int bind (const SocketAddress& target) QSE_CPP_NOEXCEPT;
// bind to the ip address of the interface

View File

@ -73,7 +73,12 @@ typedef enum qse_mux_errnum_t qse_mux_errnum_t;
enum qse_mux_evtmask_t
{
QSE_MUX_IN = (1 << 0),
QSE_MUX_OUT = (1 << 1)
QSE_MUX_OUT = (1 << 1),
/* output only below - available if the underlying system multiplexer returns relevant information */
QSE_MUX_RDHUP = (1 << 2),
QSE_MUX_HUP = (1 << 3),
QSE_MUX_ERR = (1 << 4)
};
typedef enum qse_mux_evtmask_t qse_mux_evtmask_t;

View File

@ -100,7 +100,7 @@ QSE_EXPORT int qse_set_sck_cloexec (
QSE_EXPORT int qse_init_sck_conn (
qse_sck_hnd_t handle,
const qse_nwad_t* nwad
const qse_skad_t* skad
);
QSE_EXPORT int qse_fini_sck_conn (

View File

@ -286,6 +286,19 @@ int Socket::setIpv6Only (int n) QSE_CPP_NOEXCEPT
#endif
}
int Socket::setNonBlock (int n) QSE_CPP_NOEXCEPT
{
QSE_ASSERT (qse_is_sck_valid(this->handle));
if (qse_set_sck_nonblock(this->handle, n) <= -1)
{
this->setErrorFmt (syserr_to_errnum(errno), QSE_T("%hs"), strerror(errno));
return -1;
}
return 0;
}
int Socket::shutdown (int how) QSE_CPP_NOEXCEPT
{
if (this->handle != QSE_INVALID_SCKHND)
@ -314,6 +327,33 @@ int Socket::connect (const SocketAddress& target) QSE_CPP_NOEXCEPT
return 0;
}
int Socket::initConnectNB (const SocketAddress& target) QSE_CPP_NOEXCEPT
{
QSE_ASSERT (qse_is_sck_valid(this->handle));
if (qse_init_sck_conn(this->handle, target.getAddrPtr()) <= -1)
{
this->setErrorFmt (syserr_to_errnum(errno), QSE_T("%hs"), strerror(errno));
return -1;
}
return 0;
}
int Socket::finiConnectNB () QSE_CPP_NOEXCEPT
{
QSE_ASSERT (qse_is_sck_valid(this->handle));
if (qse_fini_sck_conn(this->handle) <= -1)
{
this->setErrorFmt (syserr_to_errnum(errno), QSE_T("%hs"), strerror(errno));
return -1;
}
return 0;
}
int Socket::bind (const SocketAddress& target) QSE_CPP_NOEXCEPT
{
QSE_ASSERT (qse_is_sck_valid(this->handle));

View File

@ -1000,9 +1000,12 @@ int qse_mux_poll (qse_mux_t* mux, const qse_ntime_t* tmout)
xevt.mask = 0;
if (mux->ee.ptr[i].events & EPOLLIN) xevt.mask |= QSE_MUX_IN;
if (mux->ee.ptr[i].events & EPOLLOUT) xevt.mask |= QSE_MUX_OUT;
if (mux->ee.ptr[i].events & (EPOLLHUP | EPOLLERR))
if (mux->ee.ptr[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))
{
if (mux->ee.ptr[i].events & EPOLLRDHUP) xevt.mask |= QSE_MUX_RDHUP;
if (mux->ee.ptr[i].events & EPOLLHUP) xevt.mask |= QSE_MUX_HUP;
if (mux->ee.ptr[i].events & EPOLLERR) xevt.mask |= QSE_MUX_ERR;
if (evt->mask & QSE_MUX_IN) xevt.mask |= QSE_MUX_IN;
if (evt->mask & QSE_MUX_OUT) xevt.mask |= QSE_MUX_OUT;
}

View File

@ -197,7 +197,7 @@ int qse_set_sck_cloexec (qse_sck_hnd_t handle, int enabled)
#endif
}
int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_nwad_t* nwad)
int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_skad_t* skad)
{
int n;
#if defined(_WIN32)
@ -207,11 +207,6 @@ int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_nwad_t* nwad)
#else
int saved = 0;
#endif
qse_skad_t skad;
int skadlen;
skadlen = qse_nwadtoskad (nwad, &skad);
if (skadlen <= -1) return -1;
#if defined(_WIN32)
/* switch to the non-blocking mode */
@ -223,7 +218,7 @@ int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_nwad_t* nwad)
}
/* attempt to connet */
n = connect(handle, (struct sockaddr*)&skad, skadlen);
n = connect(handle, (struct sockaddr*)skad, qse_skadsize(skad));
if (n == -1 && WSAGetLastError() != WSAEWOULDBLOCK)
{
/* attempt to restore to the blocking mode upon failure.
@ -238,7 +233,7 @@ int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_nwad_t* nwad)
if (ioctl(handle, FIONBIO, (char*)&enabled, sizeof(enabled)) <= -1) return -1;
/* attempt to connet */
n = connect (handle, (struct sockaddr*)&skad, skadlen);
n = connect (handle, (struct sockaddr*)skad, qse_skadsize(skad));
if (n == -1 && sock_errno() != EINPROGRESS)
{
/* attempt to restore to the blocking mode upon failure.
@ -250,21 +245,21 @@ int qse_init_sck_conn (qse_sck_hnd_t handle, const qse_nwad_t* nwad)
#else
/* switch to the non-blocking mode */
/* switch to the non-blocking mode if necessary */
saved = fcntl (handle, F_GETFL, 0);
if (saved == -1) return -1;
if (fcntl(handle, F_SETFL, saved | O_NONBLOCK) == -1) return -1;
if (!(saved & O_NONBLOCK) && fcntl(handle, F_SETFL, saved | O_NONBLOCK) == -1) return -1;
/* attempt to connet */
do
{
n = connect(handle, (struct sockaddr*)&skad, skadlen);
n = connect(handle, (struct sockaddr*)skad, qse_skadsize(skad));
}
while (n == -1 && errno == EINTR);
if (n == -1 && errno != EINPROGRESS)
{
fcntl (handle, F_SETFL, saved); /* restore the flags upon failure */
if (!(saved & O_NONBLOCK)) fcntl (handle, F_SETFL, saved); /* restore the flags upon failure */
return -1;
}
#endif