inputStream updates from httpTest
git-svn-id: https://svn.musicpd.org/mpd/trunk@1059 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
d4c7d6df6d
commit
6c24180516
@ -49,4 +49,8 @@ int closeInputStream(InputStream * inStream) {
|
|||||||
int inputStreamAtEOF(InputStream * inStream) {
|
int inputStreamAtEOF(InputStream * inStream) {
|
||||||
return inStream->atEOFFunc(inStream);
|
return inStream->atEOFFunc(inStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int bufferInputStream(InputStream * inStream) {
|
||||||
|
return inStream->bufferFunc(inStream);
|
||||||
|
}
|
||||||
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
||||||
|
@ -29,6 +29,7 @@ typedef size_t (* InputStreamReadFunc) (InputStream * inStream, void * ptr, size
|
|||||||
size_t nmemb);
|
size_t nmemb);
|
||||||
typedef int (* InputStreamCloseFunc) (InputStream * inStream);
|
typedef int (* InputStreamCloseFunc) (InputStream * inStream);
|
||||||
typedef int (* InputStreamAtEOFFunc) (InputStream * inStream);
|
typedef int (* InputStreamAtEOFFunc) (InputStream * inStream);
|
||||||
|
typedef int (* InputStreamBufferFunc) (InputStream * inStream);
|
||||||
|
|
||||||
struct _InputStream {
|
struct _InputStream {
|
||||||
int error;
|
int error;
|
||||||
@ -42,6 +43,7 @@ struct _InputStream {
|
|||||||
InputStreamReadFunc readFunc;
|
InputStreamReadFunc readFunc;
|
||||||
InputStreamCloseFunc closeFunc;
|
InputStreamCloseFunc closeFunc;
|
||||||
InputStreamAtEOFFunc atEOFFunc;
|
InputStreamAtEOFFunc atEOFFunc;
|
||||||
|
InputStreamBufferFunc bufferFunc;
|
||||||
void * data;
|
void * data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -51,6 +53,7 @@ int openInputStream(InputStream * inStream, char * url);
|
|||||||
int seekInputStream(InputStream * inStream, long offset, int whence);
|
int seekInputStream(InputStream * inStream, long offset, int whence);
|
||||||
int closeInputStream(InputStream * inStream);
|
int closeInputStream(InputStream * inStream);
|
||||||
int inputStreamAtEOF(InputStream * inStream);
|
int inputStreamAtEOF(InputStream * inStream);
|
||||||
|
int bufferInputStream(InputStream * inStream);
|
||||||
|
|
||||||
size_t readFromInputStream(InputStream * inStream, void * ptr, size_t size,
|
size_t readFromInputStream(InputStream * inStream, void * ptr, size_t size,
|
||||||
size_t nmemb);
|
size_t nmemb);
|
||||||
|
@ -45,6 +45,7 @@ int inputStream_fileOpen(InputStream * inStream, char * filename) {
|
|||||||
inStream->closeFunc = inputStream_fileClose;
|
inStream->closeFunc = inputStream_fileClose;
|
||||||
inStream->readFunc = inputStream_fileRead;
|
inStream->readFunc = inputStream_fileRead;
|
||||||
inStream->atEOFFunc = inputStream_fileAtEOF;
|
inStream->atEOFFunc = inputStream_fileAtEOF;
|
||||||
|
inStream->bufferFunc = inputStream_fileBuffer;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -85,4 +86,8 @@ int inputStream_fileClose(InputStream * inStream) {
|
|||||||
int inputStream_fileAtEOF(InputStream * inStream) {
|
int inputStream_fileAtEOF(InputStream * inStream) {
|
||||||
return feof((FILE *)inStream->data);
|
return feof((FILE *)inStream->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int inputStream_fileBuffer(InputStream * inStream) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
/* vim:set shiftwidth=4 tabstop=8 expandtab: */
|
/* vim:set shiftwidth=4 tabstop=8 expandtab: */
|
||||||
|
@ -32,5 +32,7 @@ int inputStream_fileClose(InputStream * inStream);
|
|||||||
|
|
||||||
int inputStream_fileAtEOF(InputStream * inStream);
|
int inputStream_fileAtEOF(InputStream * inStream);
|
||||||
|
|
||||||
|
int inputStream_fileBuffer(InputStream * inStream);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
||||||
|
@ -423,6 +423,7 @@ int inputStream_httpOpen(InputStream * inStream, char * url) {
|
|||||||
inStream->closeFunc = inputStream_httpClose;
|
inStream->closeFunc = inputStream_httpClose;
|
||||||
inStream->readFunc = inputStream_httpRead;
|
inStream->readFunc = inputStream_httpRead;
|
||||||
inStream->atEOFFunc = inputStream_httpAtEOF;
|
inStream->atEOFFunc = inputStream_httpAtEOF;
|
||||||
|
inStream->bufferFunc = inputStream_httpBuffer;
|
||||||
|
|
||||||
inStream->offset = 0;
|
inStream->offset = 0;
|
||||||
inStream->size = 0;
|
inStream->size = 0;
|
||||||
@ -443,21 +444,8 @@ size_t inputStream_httpRead(InputStream * inStream, void * ptr, size_t size,
|
|||||||
InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data;
|
InputStreamHTTPData * data = (InputStreamHTTPData *)inStream->data;
|
||||||
int readed = 0;
|
int readed = 0;
|
||||||
int inlen = size*nmemb;
|
int inlen = size*nmemb;
|
||||||
fd_set readSet;
|
|
||||||
struct timeval tv;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if(data->connState == HTTP_CONN_STATE_REOPEN) {
|
inputStream_httpBuffer(inStream);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(data->connState) {
|
switch(data->connState) {
|
||||||
case HTTP_CONN_STATE_OPEN:
|
case HTTP_CONN_STATE_OPEN:
|
||||||
@ -467,41 +455,6 @@ size_t inputStream_httpRead(InputStream * inStream, void * ptr, size_t size,
|
|||||||
return 0;
|
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;
|
readed = inlen > data->buflen ? data->buflen : inlen;
|
||||||
|
|
||||||
memcpy(ptr, data->buffer, readed);
|
memcpy(ptr, data->buffer, readed);
|
||||||
@ -538,4 +491,69 @@ int inputStream_httpAtEOF(InputStream * inStream) {
|
|||||||
return 0;
|
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: */
|
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
||||||
|
@ -32,5 +32,7 @@ int inputStream_httpClose(InputStream * inStream);
|
|||||||
|
|
||||||
int inputStream_httpAtEOF(InputStream * inStream);
|
int inputStream_httpAtEOF(InputStream * inStream);
|
||||||
|
|
||||||
|
int inputStream_httpBuffer(InputStream * inStream);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
/* vim:set shiftwidth=8 tabstop=8 expandtab: */
|
||||||
|
Loading…
Reference in New Issue
Block a user