From 6c24180516f823985f564ba013607b47f70afa81 Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Tue, 18 May 2004 09:54:45 +0000 Subject: [PATCH] inputStream updates from httpTest git-svn-id: https://svn.musicpd.org/mpd/trunk@1059 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/inputStream.c | 4 ++ src/inputStream.h | 3 ++ src/inputStream_file.c | 5 ++ src/inputStream_file.h | 2 + src/inputStream_http.c | 116 ++++++++++++++++++++++++----------------- src/inputStream_http.h | 2 + 6 files changed, 83 insertions(+), 49 deletions(-) diff --git a/src/inputStream.c b/src/inputStream.c index 578749e24..6ff2d1bd5 100644 --- a/src/inputStream.c +++ b/src/inputStream.c @@ -49,4 +49,8 @@ int closeInputStream(InputStream * inStream) { int inputStreamAtEOF(InputStream * inStream) { return inStream->atEOFFunc(inStream); } + +int bufferInputStream(InputStream * inStream) { + return inStream->bufferFunc(inStream); +} /* vim:set shiftwidth=8 tabstop=8 expandtab: */ diff --git a/src/inputStream.h b/src/inputStream.h index bcf57c85b..963e3d2c9 100644 --- a/src/inputStream.h +++ b/src/inputStream.h @@ -29,6 +29,7 @@ typedef size_t (* InputStreamReadFunc) (InputStream * inStream, void * ptr, size size_t nmemb); typedef int (* InputStreamCloseFunc) (InputStream * inStream); typedef int (* InputStreamAtEOFFunc) (InputStream * inStream); +typedef int (* InputStreamBufferFunc) (InputStream * inStream); struct _InputStream { int error; @@ -42,6 +43,7 @@ struct _InputStream { InputStreamReadFunc readFunc; InputStreamCloseFunc closeFunc; InputStreamAtEOFFunc atEOFFunc; + InputStreamBufferFunc bufferFunc; void * data; }; @@ -51,6 +53,7 @@ int openInputStream(InputStream * inStream, char * url); int seekInputStream(InputStream * inStream, long offset, int whence); int closeInputStream(InputStream * inStream); int inputStreamAtEOF(InputStream * inStream); +int bufferInputStream(InputStream * inStream); size_t readFromInputStream(InputStream * inStream, void * ptr, size_t size, size_t nmemb); diff --git a/src/inputStream_file.c b/src/inputStream_file.c index 9c03fe826..b71beea95 100644 --- a/src/inputStream_file.c +++ b/src/inputStream_file.c @@ -45,6 +45,7 @@ int inputStream_fileOpen(InputStream * inStream, char * filename) { inStream->closeFunc = inputStream_fileClose; inStream->readFunc = inputStream_fileRead; inStream->atEOFFunc = inputStream_fileAtEOF; + inStream->bufferFunc = inputStream_fileBuffer; return 0; } @@ -85,4 +86,8 @@ int inputStream_fileClose(InputStream * inStream) { int inputStream_fileAtEOF(InputStream * inStream) { return feof((FILE *)inStream->data); } + +int inputStream_fileBuffer(InputStream * inStream) { + return 0; +} /* vim:set shiftwidth=4 tabstop=8 expandtab: */ diff --git a/src/inputStream_file.h b/src/inputStream_file.h index 694606cc9..b4b53f3c0 100644 --- a/src/inputStream_file.h +++ b/src/inputStream_file.h @@ -32,5 +32,7 @@ int inputStream_fileClose(InputStream * inStream); int inputStream_fileAtEOF(InputStream * inStream); +int inputStream_fileBuffer(InputStream * inStream); + #endif /* vim:set shiftwidth=8 tabstop=8 expandtab: */ diff --git a/src/inputStream_http.c b/src/inputStream_http.c index 330d5905a..c2c1b0904 100644 --- a/src/inputStream_http.c +++ b/src/inputStream_http.c @@ -423,6 +423,7 @@ int inputStream_httpOpen(InputStream * inStream, char * url) { inStream->closeFunc = inputStream_httpClose; inStream->readFunc = inputStream_httpRead; inStream->atEOFFunc = inputStream_httpAtEOF; + inStream->bufferFunc = inputStream_httpBuffer; inStream->offset = 0; inStream->size = 0; @@ -443,21 +444,8 @@ size_t inputStream_httpRead(InputStream * inStream, void * ptr, size_t size, InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data; int readed = 0; int inlen = size*nmemb; - fd_set readSet; - struct timeval tv; - int ret; - if(data->connState == HTTP_CONN_STATE_REOPEN) { - if(initHTTPConnection(inStream) < 0) return 0; - } - - if(data->connState == HTTP_CONN_STATE_INIT) { - if(finishHTTPInit(inStream) < 0) return 0; - } - - if(data->connState == HTTP_CONN_STATE_HELLO) { - if(getHTTPHello(inStream) < 0) return 0; - } + inputStream_httpBuffer(inStream); switch(data->connState) { case HTTP_CONN_STATE_OPEN: @@ -467,41 +455,6 @@ size_t inputStream_httpRead(InputStream * inStream, void * ptr, size_t size, return 0; } - if(data->connState == HTTP_CONN_STATE_OPEN && - data->buflen < HTTP_BUFFER_SIZE-1) - { - FD_ZERO(&readSet); - FD_SET(data->sock, &readSet); - - tv.tv_sec = 0; - tv.tv_usec = 0; - - ret = select(data->sock+1,&readSet,NULL,NULL,&tv); - if(ret == 0 || (ret < 0 && errno == EINTR)) ret = 0; - else if(ret < 0) { - data->connState = HTTP_CONN_STATE_CLOSED; - close(data->sock); - return 0; - } - - if(ret == 1) { - readed = recv(data->sock, - data->buffer+data->buflen, - HTTP_BUFFER_SIZE-1-data->buflen, 0); - - if(readed < 0 && (errno == EAGAIN || - errno == EINTR)) - { - readed = 0; - } - else if(readed <= 0) { - close(data->sock); - data->connState = HTTP_CONN_STATE_CLOSED; - } - else data->buflen += readed; - } - } - readed = inlen > data->buflen ? data->buflen : inlen; memcpy(ptr, data->buffer, readed); @@ -538,4 +491,69 @@ int inputStream_httpAtEOF(InputStream * inStream) { return 0; } } + +int inputStream_httpBuffer(InputStream * inStream) { + InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data; + int readed = 0; + fd_set readSet; + struct timeval tv; + int ret; + + if(data->connState == HTTP_CONN_STATE_REOPEN) { + if(initHTTPConnection(inStream) < 0) return -1; + } + + if(data->connState == HTTP_CONN_STATE_INIT) { + if(finishHTTPInit(inStream) < 0) return -1; + } + + if(data->connState == HTTP_CONN_STATE_HELLO) { + if(getHTTPHello(inStream) < 0) return -1; + } + + switch(data->connState) { + case HTTP_CONN_STATE_OPEN: + case HTTP_CONN_STATE_CLOSED: + break; + default: + return -1; + } + + if(data->connState == HTTP_CONN_STATE_OPEN && + data->buflen < HTTP_BUFFER_SIZE-1) + { + FD_ZERO(&readSet); + FD_SET(data->sock, &readSet); + + tv.tv_sec = 0; + tv.tv_usec = 0; + + ret = select(data->sock+1,&readSet,NULL,NULL,&tv); + if(ret == 0 || (ret < 0 && errno == EINTR)) ret = 0; + else if(ret < 0) { + data->connState = HTTP_CONN_STATE_CLOSED; + close(data->sock); + return 0; + } + + if(ret == 1) { + readed = recv(data->sock, + data->buffer+data->buflen, + HTTP_BUFFER_SIZE-1-data->buflen, 0); + + if(readed < 0 && (errno == EAGAIN || + errno == EINTR)) + { + readed = 0; + } + else if(readed <= 0) { + close(data->sock); + data->connState = HTTP_CONN_STATE_CLOSED; + } + else data->buflen += readed; + } + } + + return 0; +} /* vim:set shiftwidth=8 tabstop=8 expandtab: */ diff --git a/src/inputStream_http.h b/src/inputStream_http.h index 4fea5f79e..021ae2c76 100644 --- a/src/inputStream_http.h +++ b/src/inputStream_http.h @@ -32,5 +32,7 @@ int inputStream_httpClose(InputStream * inStream); int inputStream_httpAtEOF(InputStream * inStream); +int inputStream_httpBuffer(InputStream * inStream); + #endif /* vim:set shiftwidth=8 tabstop=8 expandtab: */