--- sa.c 2001/10/05 11:48:49 1.5
+++ sa.c 2001/10/05 13:17:36 1.6
@@ -531,19 +531,14 @@
sa->eType = SA_TYPE_STREAM;
sa->sSocket = -1;
sa->bTimeout = 0;
+ sa->tvTimeout.tv_sec = 0;
+ sa->tvTimeout.tv_usec = 0;
sa->nReadLen = 0;
- sa->nReadSize = 1024;
- if ((sa->cpReadBuf = (char *)malloc(sa->nReadSize)) == NULL) {
- free(sa);
- return SA_ERR_MEM;
- }
+ sa->nReadSize = 0;
+ sa->cpReadBuf = NULL;
sa->nWriteLen = 0;
- sa->nWriteSize = 1024;
- if ((sa->cpWriteBuf = (char *)malloc(sa->nWriteSize)) == NULL) {
- free(sa->cpReadBuf);
- free(sa);
- return SA_ERR_MEM;
- }
+ sa->nWriteSize = 0;
+ sa->cpWriteBuf = NULL;
*sap = sa;
return SA_OK;
}
@@ -596,14 +591,38 @@
return SA_ERR_ARG;
if (sa->nReadLen > rsize || sa->nWriteLen > wsize)
return SA_ERR_ARG;
- if ((cp = (char *)realloc(sa->cpReadBuf, rsize)) == NULL)
- return SA_ERR_SYS;
- sa->cpReadBuf = cp;
- sa->nReadSize = rsize;
- if ((cp = (char *)realloc(sa->cpWriteBuf, wsize)) == NULL)
- return SA_ERR_SYS;
- sa->cpWriteBuf = cp;
- sa->nWriteSize = wsize;
+ if (rsize > 0) {
+ if (sa->cpReadBuf == NULL)
+ cp = (char *)malloc(rsize);
+ else
+ cp = (char *)realloc(sa->cpReadBuf, rsize);
+ if (cp == NULL)
+ return SA_ERR_SYS;
+ sa->cpReadBuf = cp;
+ sa->nReadSize = rsize;
+ }
+ else {
+ if (sa->cpReadBuf != NULL)
+ free(sa->cpReadBuf);
+ sa->cpReadBuf = NULL;
+ sa->nReadSize = 0;
+ }
+ if (wsize > 0) {
+ if (sa->cpWriteBuf == NULL)
+ cp = (char *)malloc(wsize);
+ else
+ cp = (char *)realloc(sa->cpWriteBuf, wsize);
+ if (cp == NULL)
+ return SA_ERR_SYS;
+ sa->cpWriteBuf = cp;
+ sa->nWriteSize = wsize;
+ }
+ else {
+ if (sa->cpWriteBuf != NULL)
+ free(sa->cpWriteBuf);
+ sa->cpWriteBuf = NULL;
+ sa->nWriteSize = 0;
+ }
return SA_OK;
}
@@ -877,7 +896,7 @@
sa_rc_t sa_read(sa_t *sa, char *cpBuf, size_t nBufReq, size_t *nBufRes)
{
int n;
- int rv;
+ sa_rc_t rv;
size_t res;
if (sa == NULL || cpBuf == NULL || nBufReq == 0)
@@ -890,45 +909,56 @@
/* flush write buffer */
if (sa->nWriteLen > 0)
sa_flush(sa);
+
+ /* perform read operation */
rv = SA_OK;
- res = 0;
- while (1) {
- if (nBufReq <= sa->nReadLen) {
- /* buffer holds enough data, so use this */
- memmove(cpBuf, sa->cpReadBuf, nBufReq);
- memmove(sa->cpReadBuf, sa->cpReadBuf+nBufReq, sa->nReadLen-nBufReq);
- sa->nReadLen -= nBufReq;
- res += nBufReq;
- }
- else {
- if (sa->nReadLen > 0) {
- /* fetch already existing buffer contents as a start */
- memmove(cpBuf, sa->cpReadBuf, sa->nReadLen);
- nBufReq -= sa->nReadLen;
- cpBuf += sa->nReadLen;
- res += sa->nReadLen;
- sa->nReadLen = 0;
- }
- if (nBufReq >= sa->nReadSize) {
- /* buffer is too small at all, so read directly */
- n = sa_read_raw(sa, cpBuf, nBufReq);
- if (n > 0)
- res += n;
- else if (n <= 0)
- rv = SA_ERR_SYS;
+ if (sa->nReadSize == 0) {
+ /* unbuffered I/O */
+ res = sa_read_raw(sa, cpBuf, nBufReq);
+ if (res <= 0)
+ rv = SA_ERR_SYS;
+ }
+ else {
+ /* buffered I/O */
+ res = 0;
+ while (1) {
+ if (nBufReq <= sa->nReadLen) {
+ /* buffer holds enough data, so use this */
+ memmove(cpBuf, sa->cpReadBuf, nBufReq);
+ memmove(sa->cpReadBuf, sa->cpReadBuf+nBufReq, sa->nReadLen-nBufReq);
+ sa->nReadLen -= nBufReq;
+ res += nBufReq;
}
else {
- /* fill buffer with new data */
- n = sa_read_raw(sa, sa->cpReadBuf, sa->nReadSize);
- if (n <= 0)
- rv = SA_ERR_SYS;
+ if (sa->nReadLen > 0) {
+ /* fetch already existing buffer contents as a start */
+ memmove(cpBuf, sa->cpReadBuf, sa->nReadLen);
+ nBufReq -= sa->nReadLen;
+ cpBuf += sa->nReadLen;
+ res += sa->nReadLen;
+ sa->nReadLen = 0;
+ }
+ if (nBufReq >= sa->nReadSize) {
+ /* buffer is too small at all, so read directly */
+ n = sa_read_raw(sa, cpBuf, nBufReq);
+ if (n > 0)
+ res += n;
+ else if (n <= 0)
+ rv = SA_ERR_SYS;
+ }
else {
- sa->nReadLen = n;
- continue;
+ /* fill buffer with new data */
+ n = sa_read_raw(sa, sa->cpReadBuf, sa->nReadSize);
+ if (n <= 0)
+ rv = SA_ERR_SYS;
+ else {
+ sa->nReadLen = n;
+ continue;
+ }
}
}
+ break;
}
- break;
}
if (nBufRes != NULL)
*nBufRes = res;
@@ -1032,6 +1062,7 @@
{
size_t n;
size_t res;
+ sa_rc_t rv;
if (sa == NULL)
return SA_ERR_ARG;
@@ -1040,31 +1071,45 @@
if (sa->sSocket == -1)
return SA_ERR_USE;
- if (nBufReq > (sa->nWriteSize - sa->nWriteLen)) {
- /* not enough space in buffer, so flush buffer */
- sa_flush(sa);
- }
- res = 0;
- if (nBufReq >= sa->nWriteSize) {
- /* buffer too small at all, so write immediately */
- while (nBufReq > 0) {
- n = sa_write_raw(sa, cpBuf, nBufReq);
- if (n <= 0)
- break;
- nBufReq -= n;
- cpBuf += n;
- res += n;
+ rv = SA_OK;
+ if (sa->nWriteSize == 0) {
+ /* unbuffered I/O */
+ res = sa_write_raw(sa, cpBuf, nBufReq);
+ if (res < 0) {
+ res = 0;
+ rv = SA_ERR_SYS;
}
}
else {
- /* (again) enough sprace in buffer, so store data */
- memmove(sa->cpWriteBuf+sa->nWriteLen, cpBuf, nBufReq);
- sa->nWriteLen += nBufReq;
- res = nBufReq;
+ /* buffered I/O */
+ if (nBufReq > (sa->nWriteSize - sa->nWriteLen)) {
+ /* not enough space in buffer, so flush buffer */
+ sa_flush(sa);
+ }
+ res = 0;
+ if (nBufReq >= sa->nWriteSize) {
+ /* buffer too small at all, so write immediately */
+ while (nBufReq > 0) {
+ n = sa_write_raw(sa, cpBuf, nBufReq);
+ if (n < 0)
+ rv = SA_ERR_SYS;
+ if (n <= 0)
+ break;
+ nBufReq -= n;
+ cpBuf += n;
+ res += n;
+ }
+ }
+ else {
+ /* (again) enough sprace in buffer, so store data */
+ memmove(sa->cpWriteBuf+sa->nWriteLen, cpBuf, nBufReq);
+ sa->nWriteLen += nBufReq;
+ res = nBufReq;
+ }
}
if (nBufRes != NULL)
*nBufRes = res;
- return SA_OK;
+ return rv;
}
sa_rc_t sa_writeto(sa_t *sa, const char *buf, size_t buflen, size_t *bufdone, sa_addr_t *raddr)
@@ -1125,14 +1170,16 @@
return SA_ERR_USE;
if (sa->sSocket == -1)
return SA_ERR_USE;
- while (sa->nWriteLen > 0) {
- n = sa_write_raw(sa, sa->cpWriteBuf, sa->nWriteLen);
- if (n <= 0)
- break;
- memmove(sa->cpWriteBuf, sa->cpWriteBuf+n, sa->nWriteLen-n);
- sa->nWriteLen -= n;
+ if (sa->nWriteSize > 0) {
+ while (sa->nWriteLen > 0) {
+ n = sa_write_raw(sa, sa->cpWriteBuf, sa->nWriteLen);
+ if (n <= 0)
+ break;
+ memmove(sa->cpWriteBuf, sa->cpWriteBuf+n, sa->nWriteLen-n);
+ sa->nWriteLen -= n;
+ }
+ sa->nWriteLen = 0;
}
- sa->nWriteLen = 0;
return SA_OK;
}
|