📄 swisocket.cpp
字号:
SWIstream::Result SWIsocket::OutputStream::close()
{
if (_sock == NULL)
return SWIstream::ILLEGAL_STATE;
return _sock->shutdown(shut_write);
}
SWIstream::Result SWIsocket::OutputStream::waitReady(long timeoutMs)
{
if (_sock == NULL)
return SWIstream::ILLEGAL_STATE;
switch (_sock->is_writeready(timeoutMs))
{
case 1:
return SUCCESS;
case 0:
return TIMED_OUT;
default:
return FAILURE;
}
}
int SWIsocket::read(void* buf, int len)
{
#ifdef _WIN32
return recv(buf, len); // Winsock uses recv
#else
if (_rtmo != -1 && is_readready(_rtmo) == 0)
return SWIstream::TIMED_OUT;
int rval = ::read(_sd, (char *) buf, len);
switch (rval)
{
case -1:
{
int wserr = WSA_LAST_ERROR;
if (wserr == WSAECONNABORTED || wserr == WSAECONNRESET)
{
rval = SWIstream::CONNECTION_ABORTED;
}
else
{
error(L"read", 500, wserr);
rval = SWIstream::READ_ERROR;
}
break;
}
case 0:
rval = SWIstream::END_OF_FILE;
break;
default:
break;
}
return rval;
#endif // _WIN32
}
int SWIsocket::recv(void* buf, int len, int msgf)
{
if (_rtmo != -1 && is_readready (_rtmo)==0)
return SWIstream::TIMED_OUT;
int rval = ::recv(_sd, (char*) buf, len, msgf);
switch (rval)
{
case -1:
{
int wserr = WSA_LAST_ERROR;
if (wserr == WSAECONNABORTED || wserr == WSAECONNRESET)
{
rval = SWIstream::CONNECTION_ABORTED;
}
else
{
error(L"recv", 500, wserr);
rval = SWIstream::READ_ERROR;
}
}
break;
case 0:
rval = SWIstream::END_OF_FILE;
break;
default:
break;
}
return rval;
}
int SWIsocket::recvfrom (SWIipAddress& sa, void* buf, int len, int msgf)
{
if (_rtmo != -1 && is_readready (_rtmo)==0)
return SWIstream::TIMED_OUT;
socklen_t sa_len = sa.size ();
int rval = ::recvfrom(_sd, (char*) buf, len,
msgf, sa.addr(), &sa_len);
switch (rval)
{
case -1:
error(L"recvfrom", 500, WSA_LAST_ERROR);
rval = SWIstream::READ_ERROR;
break;
case 0:
rval = SWIstream::END_OF_FILE;
break;
default:
break;
}
return rval;
}
int SWIsocket::write(const void* buf, int len)
{
#ifdef _WIN32
return send(buf, len); // Winsock uses send
#else
if (_stmo != -1 && is_writeready (_stmo)==0) return 0;
int wlen = 0;
while (len > 0)
{
int wval;
if ((wval = ::write (_sd, (char*) buf, len)) == -1)
{
error(L"write", 500, WSA_LAST_ERROR);
return SWIstream::WRITE_ERROR;
}
len -= wval;
wlen += wval;
}
return wlen; // == len if every thing is all right
#endif // _WIN32
}
int SWIsocket::send (const void* buf, int len, int msgf)
{
if (_stmo != -1 && is_writeready (_stmo)==0) return 0;
int wlen=0;
while(len>0) {
int wval;
if ((wval = ::send (_sd, (char*) buf, len, msgf)) == -1)
{
int wserr = WSA_LAST_ERROR;
if (wserr == WSAECONNRESET || wserr == WSAECONNABORTED)
{
return SWIstream::CONNECTION_ABORTED;
}
else
{
error(L"send", 500, WSA_LAST_ERROR);
return SWIstream::WRITE_ERROR;
}
}
len -= wval;
wlen += wval;
}
return wlen;
}
int SWIsocket::sendto(const SWIipAddress& sa, const void* buf, int len, int msgf)
{
if (_stmo != -1 && is_writeready (_stmo)==0) return 0;
int wlen=0;
while(len>0)
{
int wval;
if ((wval = ::sendto (_sd, (char*) buf, len,
msgf, sa.addr (), sa.size())) == -1)
{
error(L"sendto", 500, WSA_LAST_ERROR);
return SWIstream::WRITE_ERROR;
}
len -= wval;
wlen += wval;
}
return wlen;
}
long SWIsocket::sendtimeout(long wp)
{
long old_stmo = _stmo;
_stmo = (wp < 0) ? -1: wp;
return old_stmo;
}
long SWIsocket::recvtimeout(long wp)
{
long old_rtmo = _rtmo;
_rtmo = (wp < 0) ? -1: wp;
return old_rtmo;
}
int SWIsocket::is_readready(timeval *tv) const
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (_sd, &rfds);
fd_set efds;
FD_ZERO (&efds);
FD_SET (_sd, &efds);
int ret = select (_sd + 1, &rfds, 0, &efds, tv);
if (ret <= 0)
{
if (ret < 0) error(L"select", 500, WSA_LAST_ERROR);
return ret;
}
if (FD_ISSET(_sd, &efds))
{
int val;
socklen_t rlen = sizeof val;
::getsockopt(_sd, SOL_SOCKET, SO_ERROR, (char *) &val, &rlen);
error(L"select", 502, val);
return -1;
}
return FD_ISSET(_sd, &rfds);
}
int SWIsocket::is_readready(long timeoutMs) const
{
timeval tv;
timeval *ptv = millisecToTimeval(timeoutMs, tv);
return is_readready(ptv);
}
int SWIsocket::is_writeready(timeval *tv) const
{
fd_set wfds;
FD_ZERO (&wfds);
FD_SET (_sd, &wfds);
fd_set efds;
FD_ZERO (&efds);
FD_SET (_sd, &efds);
int ret = select (_sd+1, 0, &wfds, &efds, tv);
if (ret <= 0)
{
if (ret < 0) error(L"select", 500, WSA_LAST_ERROR);
return ret;
}
if (FD_ISSET(_sd, &efds))
{
int val;
socklen_t rlen = sizeof val;
getsockopt(_sd, SOL_SOCKET, SO_ERROR, (char *) &val, &rlen);
error(L"select", 502, val);
return -1;
}
return FD_ISSET(_sd,&wfds);
}
int SWIsocket::is_writeready(long timeoutMs) const
{
timeval tv;
timeval *ptv = millisecToTimeval(timeoutMs, tv);
return is_writeready(ptv);
}
int SWIsocket::is_exceptionpending(timeval *tv) const
{
fd_set fds;
FD_ZERO (&fds);
FD_SET (_sd, &fds);
int ret = select(_sd+1, 0, 0, &fds, tv);
if (ret == -1)
{
error(L"select", 500);
return 0;
}
return ret;
}
int SWIsocket::is_exceptionpending (long timeoutMs) const
{
timeval tv;
timeval *ptv = millisecToTimeval(timeoutMs, tv);
return is_exceptionpending(ptv);
}
SWIstream::Result SWIsocket::shutdown (shuthow sh)
{
if (::shutdown(_sd, sh) != 0)
{
error(L"shutdown", 500);
return SWIstream::FAILURE;
}
return SWIstream::SUCCESS;
}
int SWIsocket::getopt (option op, void* buf, int len, level l) const
{
socklen_t rlen = len;
if (::getsockopt (_sd, l, op, (char*) buf, &rlen) == -1)
error(L"getsockopt", 500);
return rlen;
}
void SWIsocket::setopt (option op, void* buf, int len, level l) const
{
if (::setsockopt (_sd, l, op, (char*) buf, len) == -1)
error (L"setsockopt", 500);
}
SWIsocket::Type SWIsocket::gettype() const
{
int ty=0;
getopt (so_type, &ty, sizeof (ty));
return SWIsocket::Type(ty);
}
int SWIsocket::clearerror () const
{
int err=0;
getopt (so_error, &err, sizeof (err));
return err;
}
int SWIsocket::debug (int opt) const
{
int old=0;
getopt (so_debug, &old, sizeof (old));
if (opt != -1)
setopt (so_debug, &opt, sizeof (opt));
return old;
}
int SWIsocket::reuseaddr (int opt) const
{
int old=0;
getopt (so_reuseaddr, &old, sizeof (old));
if (opt != -1)
setopt (so_reuseaddr, &opt, sizeof (opt));
return old;
}
int SWIsocket::keepalive (int opt) const
{
int old=0;
getopt (so_keepalive, &old, sizeof (old));
if (opt != -1)
setopt (so_keepalive, &opt, sizeof (opt));
return old;
}
int SWIsocket::dontroute (int opt) const
{
int old=0;
getopt (so_dontroute, &old, sizeof (old));
if (opt != -1)
setopt (so_dontroute, &opt, sizeof (opt));
return old;
}
int SWIsocket::broadcast (int opt) const
{
int old=0;
getopt (so_broadcast, &old, sizeof (old));
if (opt != -1)
setopt (so_broadcast, &opt, sizeof (opt));
return old;
}
int SWIsocket::oobinline (int opt) const
{
int old=0;
getopt (so_oobinline, &old, sizeof (old));
if (opt != -1)
setopt (so_oobinline, &opt, sizeof (opt));
return old;
}
int SWIsocket::linger (int opt) const
{
socklinger old (0, 0);
getopt (so_linger, &old, sizeof (old));
if (opt > 0) {
socklinger nw (1, opt);
setopt (so_linger, &nw, sizeof (nw));
}else if (opt == 0) {
socklinger nw (0, old.l_linger);
setopt (so_linger, &nw, sizeof (nw));
}
return old.l_onoff ? old.l_linger: -1;
}
int SWIsocket::sendbufsz (int sz) const
{
int old=0;
getopt (so_sndbuf, &old, sizeof (old));
if (sz >= 0)
setopt (so_sndbuf, &sz, sizeof (sz));
return old;
}
int SWIsocket::recvbufsz (int sz) const
{
int old=0;
getopt (so_rcvbuf, &old, sizeof (old));
if (sz >= 0)
setopt (so_rcvbuf, &sz, sizeof (sz));
return old;
}
void SWIsocket::error(const VXIchar* func, VXIunsigned errorId,
const VXIchar *errorName, int errorCode) const
{
if (_logger)
{
_logger->Error(errorId, L"%s%s%s%i",
L"Func", func,
errorName, errorCode);
}
}
void SWIsocket::error(const VXIchar* func, VXIunsigned errorId, int errorCode) const
{
if (_logger)
{
_logger->Error(errorId, L"%s%s%s%i",
L"Func",
func,
WSA_ERROR_NAME, errorCode);
}
}
void SWIsocket::error(const VXIchar* func, VXIunsigned errorId) const
{
if (_logger)
{
_logger->Error(errorId, L"%s%s", L"Func", func);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -