qse/qse/lib/cmn/nwio.c

887 lines
17 KiB
C
Raw Normal View History

2012-04-27 14:33:14 +00:00
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
2012-04-29 15:26:44 +00:00
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
2012-04-27 14:33:14 +00:00
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/cmn/nwio.h>
#include "mem.h"
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
# include <winsock2.h>
# include <ws2tcpip.h> /* sockaddr_in6 */
# include <windows.h>
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
# include <types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <nerrno.h>
2012-05-01 01:36:18 +00:00
# pragma library("tcpip32.lib")
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
2012-05-01 01:36:18 +00:00
/* TODO: consider watt-32 */
2012-04-27 14:33:14 +00:00
#else
# include "syscall.h"
# include <sys/socket.h>
# include <netinet/in.h>
#endif
2012-04-30 15:15:41 +00:00
QSE_IMPLEMENT_COMMON_FUNCTIONS (nwio)
2012-04-29 15:26:44 +00:00
enum
{
2012-04-30 15:15:41 +00:00
STATUS_UDP_CONNECT = (1 << 0)
2012-04-29 15:26:44 +00:00
};
2012-05-01 01:36:18 +00:00
static qse_ssize_t socket_output (
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size);
static qse_ssize_t socket_input (
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size);
#if defined(AF_INET)
2012-04-29 15:26:44 +00:00
union sockaddr_t
{
struct sockaddr_in in4;
2012-04-30 15:15:41 +00:00
#if defined(AF_INET6) && !defined(__OS2__)
2012-04-29 15:26:44 +00:00
struct sockaddr_in6 in6;
2012-04-30 10:03:55 +00:00
#endif
2012-04-29 15:26:44 +00:00
};
2012-05-01 01:36:18 +00:00
#endif
2012-04-30 15:15:41 +00:00
2012-04-27 14:33:14 +00:00
static int nwad_to_sockaddr (const qse_nwad_t* nwad, int* family, void* addr)
{
int addrsize = -1;
switch (nwad->type)
{
case QSE_NWAD_IN4:
{
2012-05-01 01:36:18 +00:00
#if defined(AF_INET)
2012-04-27 14:33:14 +00:00
struct sockaddr_in* in;
in = (struct sockaddr_in*)addr;
addrsize = QSE_SIZEOF(*in);
QSE_MEMSET (in, 0, addrsize);
*family = AF_INET;
in->sin_family = AF_INET;
in->sin_addr.s_addr = nwad->u.in4.addr.value;
in->sin_port = nwad->u.in4.port;
2012-05-01 01:36:18 +00:00
#endif
2012-04-27 14:33:14 +00:00
break;
}
case QSE_NWAD_IN6:
{
2012-04-30 15:15:41 +00:00
#if defined(AF_INET6) && !defined(__OS2__)
2012-04-27 14:33:14 +00:00
struct sockaddr_in6* in;
in = (struct sockaddr_in6*)addr;
addrsize = QSE_SIZEOF(*in);
QSE_MEMSET (in, 0, addrsize);
*family = AF_INET6;
in->sin6_family = AF_INET6;
memcpy (&in->sin6_addr, &nwad->u.in6.addr, QSE_SIZEOF(nwad->u.in6.addr));
in->sin6_scope_id = nwad->u.in6.scope;
in->sin6_port = nwad->u.in6.port;
#endif
break;
}
}
return addrsize;
}
#if defined(_WIN32)
static qse_nwio_errnum_t syserr_to_errnum (DWORD e)
{
switch (e)
{
2012-04-30 09:46:58 +00:00
case WSA_NOT_ENOUGH_MEMORY:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_ENOMEM;
2012-04-30 09:46:58 +00:00
case WSA_INVALID_PARAMETER:
case WSA_INVALID_HANDLE:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EINVAL;
2012-04-30 09:46:58 +00:00
case WSAEACCES:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EACCES;
2012-04-30 09:46:58 +00:00
case WSAEINTR:
return QSE_NWIO_EINTR;
2012-04-27 14:33:14 +00:00
2012-04-30 09:46:58 +00:00
case WSAECONNREFUSED:
case WSAENETUNREACH:
case WSAEHOSTUNREACH:
case WSAEHOSTDOWN:
return QSE_NWIO_ECONN;
2012-04-27 14:33:14 +00:00
default:
return QSE_NWIO_ESYSERR;
}
}
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
static qse_nwio_errnum_t syserr_to_errnum (int e)
2012-04-27 14:33:14 +00:00
{
switch (e)
{
2012-04-30 15:15:41 +00:00
case SOCENOMEM:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_ENOMEM;
2012-04-30 15:15:41 +00:00
case SOCEINVAL:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EINVAL;
2012-04-30 15:15:41 +00:00
case SOCEACCES:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EACCES;
2012-04-30 15:15:41 +00:00
case SOCENOENT:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_ENOENT;
2012-04-30 15:15:41 +00:00
case SOCEEXIST:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EEXIST;
2012-04-30 15:15:41 +00:00
case SOCEINTR:
return QSE_NWIO_EINTR;
2012-04-27 14:33:14 +00:00
2012-04-30 15:15:41 +00:00
case SOCEPIPE:
2012-04-27 14:33:14 +00:00
return QSE_NWIO_EPIPE;
2012-04-30 15:15:41 +00:00
case SOCECONNREFUSED:
case SOCENETUNREACH:
case SOCEHOSTUNREACH:
case SOCEHOSTDOWN:
return QSE_NWIO_ECONN;
2012-04-27 14:33:14 +00:00
default:
return QSE_NWIO_ESYSERR;
}
}
2012-04-30 15:15:41 +00:00
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
static qse_nwio_errnum_t syserr_to_errnum (int e)
{
2012-05-01 01:36:18 +00:00
/* TODO: */
return QSE_NWIO_ESYSERR;
2012-04-27 14:33:14 +00:00
}
#else
static qse_nwio_errnum_t syserr_to_errnum (int e)
{
switch (e)
{
case ENOMEM:
return QSE_NWIO_ENOMEM;
case EINVAL:
return QSE_NWIO_EINVAL;
case EACCES:
return QSE_NWIO_EACCES;
case ENOENT:
return QSE_NWIO_ENOENT;
case EEXIST:
return QSE_NWIO_EEXIST;
case EINTR:
return QSE_NWIO_EINTR;
case EPIPE:
return QSE_NWIO_EPIPE;
2012-04-30 09:46:58 +00:00
#if defined(ECONNREFUSED) || defined(ENETUNREACH) || defined(EHOSTUNREACH) || defined(EHOSTDOWN)
2012-04-27 14:33:14 +00:00
#if defined(ECONNREFUSED)
case ECONNREFUSED:
#endif
#if defined(ENETUNREACH)
case ENETUNREACH:
2012-04-30 09:46:58 +00:00
#endif
#if defined(EHOSTUNREACH)
case EHOSTUNREACH:
#endif
#if defined(EHOSTDOWN)
case EHOSTDOWN:
2012-04-27 14:33:14 +00:00
#endif
return QSE_NWIO_ECONN;
#endif
default:
return QSE_NWIO_ESYSERR;
}
}
#endif
static qse_nwio_errnum_t tio_errnum_to_nwio_errnum (qse_tio_t* tio)
{
switch (tio->errnum)
{
case QSE_TIO_ENOMEM:
return QSE_NWIO_ENOMEM;
case QSE_TIO_EINVAL:
return QSE_NWIO_EINVAL;
case QSE_TIO_EACCES:
return QSE_NWIO_EACCES;
case QSE_TIO_ENOENT:
return QSE_NWIO_ENOENT;
case QSE_TIO_EILSEQ:
return QSE_NWIO_EILSEQ;
case QSE_TIO_EICSEQ:
return QSE_NWIO_EICSEQ;
case QSE_TIO_EILCHR:
return QSE_NWIO_EILCHR;
default:
return QSE_NWIO_EOTHER;
}
}
qse_nwio_t* qse_nwio_open (
qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_nwad_t* nwad, int flags)
{
qse_nwio_t* nwio;
nwio = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_nwio_t) + xtnsize);
if (nwio == QSE_NULL) return QSE_NULL;
if (qse_nwio_init (nwio, mmgr, nwad, flags) <= -1)
{
QSE_MMGR_FREE (mmgr, nwio);
return QSE_NULL;
}
return nwio;
}
void qse_nwio_close (qse_nwio_t* nwio)
{
qse_nwio_fini (nwio);
QSE_MMGR_FREE (nwio->mmgr, nwio);
}
int qse_nwio_init (
qse_nwio_t* nwio, qse_mmgr_t* mmgr, const qse_nwad_t* nwad, int flags)
{
2012-05-01 01:36:18 +00:00
#if defined(AF_INET)
2012-04-29 15:26:44 +00:00
union sockaddr_t addr;
2012-05-01 01:36:18 +00:00
#endif
#if defined(HAVE_SOCKLEN_T)
2012-04-29 15:26:44 +00:00
socklen_t addrlen;
#else
2012-04-27 14:33:14 +00:00
int addrlen;
2012-04-29 15:26:44 +00:00
#endif
2012-04-30 10:25:19 +00:00
int family, type;
int tmp;
2012-04-27 14:33:14 +00:00
QSE_MEMSET (nwio, 0, QSE_SIZEOF(*nwio));
nwio->mmgr = mmgr;
nwio->flags = flags;
2012-04-30 15:15:41 +00:00
nwio->errnum = QSE_NWIO_ENOERR;
2012-04-27 14:33:14 +00:00
2012-05-01 01:36:18 +00:00
#if defined(AF_INET)
2012-04-30 10:25:19 +00:00
tmp = nwad_to_sockaddr (nwad, &family, &addr);
if (tmp <= -1)
{
nwio->errnum = QSE_NWIO_EINVAL;
return -1;
}
addrlen = tmp;
2012-04-27 14:33:14 +00:00
2012-04-29 15:26:44 +00:00
if (flags & QSE_NWIO_TCP) type = SOCK_STREAM;
else if (flags & QSE_NWIO_UDP) type = SOCK_DGRAM;
else
2012-05-01 01:36:18 +00:00
#endif
2012-04-29 15:26:44 +00:00
{
nwio->errnum = QSE_NWIO_EINVAL;
return -1;
}
2012-04-30 09:46:58 +00:00
#if defined(_WIN32)
nwio->handle = socket (family, type, 0);
if (nwio->handle == INVALID_SOCKET)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
goto oops;
}
if (flags & QSE_NWIO_PASSIVE)
{
qse_nwio_hnd_t handle;
if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) == SOCKET_ERROR)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
goto oops;
}
if (flags & QSE_NWIO_TCP)
{
if (listen (nwio->handle, 10) == SOCKET_ERROR)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
goto oops;
}
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle == INVALID_SOCKET)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
goto oops;
}
closesocket (nwio->handle);
nwio->handle = handle;
}
else if (flags & QSE_NWIO_UDP)
{
2012-04-30 15:15:41 +00:00
nwio->status |= STATUS_UDP_CONNECT;
2012-04-30 09:46:58 +00:00
}
}
else
{
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) == SOCKET_ERROR)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
goto oops;
}
}
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
nwio->handle = socket (family, type, 0);
if (nwio->handle <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
goto oops;
}
if (flags & QSE_NWIO_PASSIVE)
{
qse_nwio_hnd_t handle;
if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
goto oops;
}
if (flags & QSE_NWIO_TCP)
{
if (listen (nwio->handle, 10) <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
goto oops;
}
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
goto oops;
}
soclose (nwio->handle);
nwio->handle = handle;
}
else if (flags & QSE_NWIO_UDP)
{
nwio->status |= STATUS_UDP_CONNECT;
}
}
else
{
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
goto oops;
}
}
2012-04-30 09:46:58 +00:00
#elif defined(__DOS__)
nwio->errnum = QSE_NWIO_ENOIMPL;
return -1;
#else
2012-04-29 15:26:44 +00:00
nwio->handle = socket (family, type, 0);
2012-04-27 14:33:14 +00:00
if (nwio->handle <= -1)
{
nwio->errnum = syserr_to_errnum (errno);
goto oops;
}
2012-04-30 09:46:58 +00:00
#if defined(FD_CLOEXEC)
2012-04-27 14:33:14 +00:00
{
int tmp = fcntl (nwio->handle, F_GETFD);
if (tmp >= 0) fcntl (nwio->handle, F_SETFD, tmp | FD_CLOEXEC);
}
2012-04-30 09:46:58 +00:00
#endif
2012-04-27 14:33:14 +00:00
2012-04-29 15:26:44 +00:00
if (flags & QSE_NWIO_PASSIVE)
2012-04-27 14:33:14 +00:00
{
qse_nwio_hnd_t handle;
2012-04-29 15:26:44 +00:00
if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
2012-04-27 14:33:14 +00:00
{
nwio->errnum = syserr_to_errnum (errno);
goto oops;
}
2012-04-29 15:26:44 +00:00
if (flags & QSE_NWIO_TCP)
2012-04-27 14:33:14 +00:00
{
2012-04-29 15:26:44 +00:00
if (listen (nwio->handle, 10) <= -1)
{
nwio->errnum = syserr_to_errnum (errno);
goto oops;
}
handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen);
if (handle <= -1)
{
nwio->errnum = syserr_to_errnum (errno);
goto oops;
}
QSE_CLOSE (nwio->handle);
nwio->handle = handle;
}
else if (flags & QSE_NWIO_UDP)
{
2012-04-30 15:15:41 +00:00
nwio->status |= STATUS_UDP_CONNECT;
2012-04-27 14:33:14 +00:00
}
}
else
{
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (errno);
goto oops;
}
}
#endif
if (flags & QSE_NWIO_TEXT)
{
int topt = 0;
if (flags & QSE_NWIO_IGNOREMBWCERR) topt |= QSE_TIO_IGNOREMBWCERR;
if (flags & QSE_NWIO_NOAUTOFLUSH) topt |= QSE_TIO_NOAUTOFLUSH;
nwio->tio = qse_tio_open (mmgr, QSE_SIZEOF(qse_nwio_t*), topt);
if (nwio->tio == QSE_NULL)
{
nwio->errnum = QSE_NWIO_ENOMEM;
goto oops;
}
/* store the back-reference to nwio in the extension area.*/
*(qse_nwio_t**)QSE_XTN(nwio->tio) = nwio;
if (qse_tio_attachin (nwio->tio, socket_input, QSE_NULL, 4096) <= -1 ||
qse_tio_attachout (nwio->tio, socket_output, QSE_NULL, 4096) <= -1)
{
if (nwio->errnum == QSE_NWIO_ENOERR)
nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio);
goto oops;
}
}
return 0;
oops:
2012-04-30 09:46:58 +00:00
if (nwio->tio)
{
qse_tio_close (nwio->tio);
nwio->tio = QSE_NULL;
}
2012-04-27 14:33:14 +00:00
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
if (nwio->handle != INVALID_SOCKET) closesocket (nwio->handle);
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
if (nwio->handle >= 0) soclose (nwio->handle);
2012-04-30 09:46:58 +00:00
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
2012-04-30 15:15:41 +00:00
/* TODO: */
2012-04-27 14:33:14 +00:00
#else
2012-04-30 09:46:58 +00:00
if (nwio->handle >= 0) QSE_CLOSE (nwio->handle);
2012-04-27 14:33:14 +00:00
#endif
return -1;
}
void qse_nwio_fini (qse_nwio_t* nwio)
{
/*if (qse_nwio_flush (nwio) <= -1) return -1;*/
qse_nwio_flush (nwio);
if (nwio->tio)
{
qse_tio_close (nwio->tio);
nwio->tio = QSE_NULL;
}
2012-04-30 09:46:58 +00:00
#if defined(_WIN32)
closesocket (nwio->handle);
#elif defined(__OS2__)
/* TODO: */
#elif defined(__DOS__)
/* TODO: */
#else
QSE_CLOSE (nwio->handle);
#endif
2012-04-27 14:33:14 +00:00
}
qse_nwio_errnum_t qse_nwio_geterrnum (const qse_nwio_t* nwio)
{
return nwio->errnum;
}
qse_cmgr_t* qse_nwio_getcmgr (qse_nwio_t* nwio)
{
return nwio->tio? qse_tio_getcmgr (nwio->tio): QSE_NULL;
}
void qse_nwio_setcmgr (qse_nwio_t* nwio, qse_cmgr_t* cmgr)
{
if (nwio->tio) qse_tio_setcmgr (nwio->tio, cmgr);
}
qse_nwio_hnd_t qse_nwio_gethandle (const qse_nwio_t* nwio)
{
2012-04-30 09:46:58 +00:00
return nwio->handle;
2012-04-27 14:33:14 +00:00
}
qse_ubi_t qse_nwio_gethandleasubi (const qse_nwio_t* nwio)
{
qse_ubi_t ubi;
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
ubi.intptr = nwio->handle;
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
ubi.i = nwio->handle;
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
2012-04-30 15:15:41 +00:00
ubi.i = nwio->handle;
2012-04-27 14:33:14 +00:00
#else
ubi.i = nwio->handle;
#endif
return ubi;
}
qse_ssize_t qse_nwio_flush (qse_nwio_t* nwio)
{
qse_ssize_t n;
if (nwio->tio)
{
nwio->errnum = QSE_NWIO_ENOERR;
n = qse_tio_flush (nwio->tio);
if (n <= -1 && nwio->errnum == QSE_NWIO_ENOERR)
nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio);
}
else n = 0;
return n;
}
void qse_nwio_purge (qse_nwio_t* nwio)
{
if (nwio->tio) qse_tio_purge (nwio->tio);
}
/* ---------------------------------------------------------- */
static qse_ssize_t nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
{
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
int count;
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
int n;
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
int n;
#else
qse_ssize_t n;
#endif
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
2012-04-30 15:15:41 +00:00
if (nwio->status & STATUS_UDP_CONNECT)
2012-04-30 09:46:58 +00:00
{
union sockaddr_t addr;
int addrlen;
addrlen = QSE_SIZEOF(addr);
count = recvfrom (
nwio->handle, buf, size, 0,
(struct sockaddr*)&addr, &addrlen);
2012-04-30 15:15:41 +00:00
if (count == SOCKET_ERROR)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
}
2012-04-30 09:46:58 +00:00
else if (count >= 1)
{
/* for udp, it just creates a stream with the
* first sender */
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (WSAGetLastError());
return -1;
}
2012-04-30 15:15:41 +00:00
nwio->status &= ~STATUS_UDP_CONNECT;
2012-04-30 09:46:58 +00:00
}
}
else
{
count = recv (nwio->handle, buf, size, 0);
if (count == SOCKET_ERROR) nwio->errnum = syserr_to_errnum (WSAGetLastError());
}
return count;
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
if (nwio->status & STATUS_UDP_CONNECT)
{
union sockaddr_t addr;
int addrlen;
addrlen = QSE_SIZEOF(addr);
n = recvfrom (
nwio->handle, buf, size, 0,
(struct sockaddr*)&addr, &addrlen);
if (n <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
}
else if (n >= 1)
{
/* for udp, it just creates a stream with the
* first sender */
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (sock_errno());
return -1;
}
nwio->status &= ~STATUS_UDP_CONNECT;
}
}
else
{
n = recv (nwio->handle, buf, size, 0);
if (n <= -1) nwio->errnum = syserr_to_errnum (sock_errno());
}
return n;
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
2012-04-30 09:46:58 +00:00
nwio->errnum = QSE_NWIO_ENOIMPL;
return -1;
2012-04-27 14:33:14 +00:00
#else
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
reread:
2012-04-30 15:15:41 +00:00
if (nwio->status & STATUS_UDP_CONNECT)
2012-04-27 14:33:14 +00:00
{
2012-04-29 15:26:44 +00:00
union sockaddr_t addr;
2012-05-01 01:36:18 +00:00
#if defined(HAVE_SOCKLEN_T)
2012-04-29 15:26:44 +00:00
socklen_t addrlen;
#else
int addrlen;
#endif
addrlen = QSE_SIZEOF(addr);
n = recvfrom (
nwio->handle, buf, size, 0,
(struct sockaddr*)&addr, &addrlen);
if (n <= -1)
2012-04-27 14:33:14 +00:00
{
2012-04-29 15:26:44 +00:00
if (errno == EINTR)
{
if (nwio->flags & QSE_NWIO_READNORETRY)
nwio->errnum = QSE_NWIO_EINTR;
else goto reread;
}
else
{
nwio->errnum = syserr_to_errnum (errno);
}
2012-04-27 14:33:14 +00:00
}
2012-04-29 15:26:44 +00:00
else if (n >= 1)
2012-04-27 14:33:14 +00:00
{
2012-04-29 15:26:44 +00:00
/* for udp, it just creates a stream with the
* first sender */
if (connect (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1)
{
nwio->errnum = syserr_to_errnum (errno);
return -1;
}
2012-04-30 15:15:41 +00:00
nwio->status &= ~STATUS_UDP_CONNECT;
2012-04-29 15:26:44 +00:00
}
}
else
{
n = recv (nwio->handle, buf, size, 0);
if (n <= -1)
{
if (errno == EINTR)
{
if (nwio->flags & QSE_NWIO_READNORETRY)
nwio->errnum = QSE_NWIO_EINTR;
else goto reread;
}
else
{
nwio->errnum = syserr_to_errnum (errno);
}
2012-04-27 14:33:14 +00:00
}
}
return n;
#endif
}
qse_ssize_t qse_nwio_read (qse_nwio_t* nwio, void* buf, qse_size_t size)
{
if (nwio->tio == QSE_NULL)
return nwio_read (nwio, buf, size);
else
{
qse_ssize_t n;
nwio->errnum = QSE_NWIO_ENOERR;
n = qse_tio_read (nwio->tio, buf, size);
if (n <= -1 && nwio->errnum == QSE_NWIO_ENOERR)
nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio);
return n;
}
}
static qse_ssize_t nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t size)
{
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
int count;
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
int n;
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
int n;
#else
qse_ssize_t n;
#endif
#if defined(_WIN32)
2012-04-30 09:46:58 +00:00
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
count = send (nwio->handle, data, size, 0);
if (count == SOCKET_ERROR) nwio->errnum = syserr_to_errnum (WSAGetLastError());
return count;
2012-04-27 14:33:14 +00:00
#elif defined(__OS2__)
2012-04-30 15:15:41 +00:00
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(int);
n = send (nwio->handle, data, size, 0);
if (n <= -1) nwio->errnum = syserr_to_errnum (sock_errno());
return n;
2012-04-27 14:33:14 +00:00
#elif defined(__DOS__)
2012-04-30 09:46:58 +00:00
nwio->errnum = QSE_NWIO_ENOIMPL;
return -1;
2012-04-27 14:33:14 +00:00
#else
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
rewrite:
n = send (nwio->handle, data, size, 0);
if (n <= -1)
{
if (errno == EINTR)
{
if (nwio->flags & QSE_NWIO_WRITENORETRY)
nwio->errnum = QSE_NWIO_EINTR;
else goto rewrite;
}
else
{
nwio->errnum = syserr_to_errnum (errno);
}
}
return n;
#endif
}
qse_ssize_t qse_nwio_write (qse_nwio_t* nwio, const void* data, qse_size_t size)
{
if (nwio->tio == QSE_NULL)
return nwio_write (nwio, data, size);
else
{
qse_ssize_t n;
nwio->errnum = QSE_NWIO_ENOERR;
n = qse_tio_write (nwio->tio, data, size);
if (n <= -1 && nwio->errnum == QSE_NWIO_ENOERR)
nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio);
return n;
}
}
/* ---------------------------------------------------------- */
static qse_ssize_t socket_input (
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size)
{
if (cmd == QSE_TIO_DATA)
{
qse_nwio_t* nwio;
nwio = *(qse_nwio_t**)QSE_XTN(tio);
QSE_ASSERT (nwio != QSE_NULL);
return nwio_read (nwio, buf, size);
}
return 0;
}
static qse_ssize_t socket_output (
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size)
{
if (cmd == QSE_TIO_DATA)
{
qse_nwio_t* nwio;
nwio = *(qse_nwio_t**)QSE_XTN(tio);
QSE_ASSERT (nwio != QSE_NULL);
return nwio_write (nwio, buf, size);
}
return 0;
}