Added some socket functions

This commit is contained in:
2018-01-25 09:56:30 +00:00
parent c0a072a054
commit 47f662861f
9 changed files with 337 additions and 32 deletions

View File

@ -25,7 +25,13 @@
*/
#include <qse/si/Socket.hpp>
#include <qse/cmn/str.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
@ -47,6 +53,31 @@ int Socket::fdopen (int handle) QSE_CPP_NOEXCEPT
}
#endif
void Socket::setError (ErrorCode error_code, const qse_char_t* fmt, ...)
{
static const qse_char_t* errstr[] =
{
QSE_T("no error"),
QSE_T("insufficient memory"),
QSE_T("invalid parameter"),
QSE_T("socket not open"),
QSE_T("system error")
};
this->errcode = error_code;
if (fmt)
{
va_list ap;
va_start (ap, fmt);
qse_strxvfmt (this->errmsg, QSE_COUNTOF(errmsg), fmt, ap);
va_end (ap);
}
else
{
qse_strxcpy (this->errmsg, QSE_COUNTOF(errmsg), errstr[error_code]);
}
}
int Socket::open (int domain, int type, int protocol) QSE_CPP_NOEXCEPT
{
int x;
@ -72,16 +103,85 @@ void Socket::close () QSE_CPP_NOEXCEPT
int Socket::connect (const SocketAddress& target) QSE_CPP_NOEXCEPT
{
//if (this->handle == QSE_INVALID_SCKHND)
//{
//
//}
if (this->handle == QSE_INVALID_SCKHND)
{
this->setError (Socket::E_NOTOPEN);
return -1;
}
return -1;
if (::connect(this->handle, (struct sockaddr*)target.getAddrPtr(), target.getAddrSize()) == -1)
{
this->set_error_with_syserr (errno);
return -1;
}
return 0;
}
int Socket::beginConnect (const SocketAddress &target) QSE_CPP_NOEXCEPT
int Socket::bind (const SocketAddress& target) QSE_CPP_NOEXCEPT
{
if (this->handle == QSE_INVALID_SCKHND)
{
this->setError (Socket::E_NOTOPEN);
return -1;
}
if (::bind(this->handle, (struct sockaddr*)target.getAddrPtr(), target.getAddrSize()) == -1)
{
this->set_error_with_syserr (errno);
return -1;
}
return 0;
}
int Socket::accept (Socket* newsck, SocketAddress* newaddr, int flags) QSE_CPP_NOEXCEPT
{
int n;
if (this->handle == QSE_INVALID_SCKHND)
{
this->setError (Socket::E_NOTOPEN);
return -1;
}
#if 0
qse_socklen_t len = newaddr->getAddrSize();
if ((n = ::accept4 (this->handle, newaddr->getAddrPtr(), &len)) == -1)
{
this->set_error_with_syserr (errno);
return -1;
}
newsck->handle = n;
#endif
return 0;
}
void Socket::set_error_with_syserr (int syserr)
{
qse_mchar_t buf[128];
ErrorCode errcode;
switch (errno)
{
case EINVAL:
errcode = this->E_INVAL;
break;
case ENOMEM:
errcode = this->E_NOMEM;
break;
// TODO: translate more system error codes
default:
strerror_r(errno, buf, QSE_COUNTOF(buf));
this->setError (this->E_SYSERR, QSE_T("%hs"), buf);
return;
}
this->setError (errcode);
}
/////////////////////////////////

View File

@ -111,14 +111,6 @@ SocketAddress::SocketAddress (const qse_nwad_t* nwad)
this->set (nwad);
}
SocketAddress::SocketAddress (const struct sockaddr* ptr, int len)
{
if (this->set (ptr, len) <= -1)
{
QSE_MEMSET (&this->skad, 0, QSE_SIZEOF(this->skad));
}
}
int SocketAddress::getFamily () const
{
return FAMILY(&this->skad);
@ -206,14 +198,6 @@ int SocketAddress::set (const qse_nwad_t* nwad)
return qse_nwadtoskad (nwad, &this->skad);
}
int SocketAddress::set (const struct sockaddr* ptr, int len)
{
int exp_size = qse_skadsize((const qse_skad_t*)ptr);
if (len < exp_size) return -1;
QSE_MEMCPY (&this->skad, ptr, exp_size);
return 0;
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)

View File

@ -255,7 +255,7 @@ int qse_getnwifcfg (qse_nwifcfg_t* cfg)
head->flags |= QSE_NWIFCFG_PTOP;
}
if (ioctl (s, SIOCGLIFINDEX, &ifrbuf) <= -1) goto oops;
if (ioctl (s, SIOCGLIFINDEX, &ifrbuf) <= -1) goto oops;
head->index = ifrbuf.lifr_index;
if (ioctl (s, SIOCGLIFNETMASK, &ifrbuf) <= -1) goto oops;