optimized read() on clients, should be much, much faster now
git-svn-id: https://svn.musicpd.org/mpd/trunk@2649 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
2
TODO
2
TODO
@@ -34,8 +34,6 @@
|
|||||||
|
|
||||||
*) Cleanup Config File Code
|
*) Cleanup Config File Code
|
||||||
|
|
||||||
*) Optimize read() on clients
|
|
||||||
|
|
||||||
*) Handle mp1 and mp2 files (including mp2's that are disguised as mp3's with
|
*) Handle mp1 and mp2 files (including mp2's that are disguised as mp3's with
|
||||||
a mp3 suffix)
|
a mp3 suffix)
|
||||||
|
|
||||||
|
@@ -83,9 +83,9 @@
|
|||||||
#define COMMAND_URL_HANDLERS "urlhandlers"
|
#define COMMAND_URL_HANDLERS "urlhandlers"
|
||||||
#define COMMAND_PLCHANGES "plchanges"
|
#define COMMAND_PLCHANGES "plchanges"
|
||||||
#define COMMAND_CURRENTSONG "currentsong"
|
#define COMMAND_CURRENTSONG "currentsong"
|
||||||
#define COMMAND_ENABLE_DEV "enabledevice"
|
#define COMMAND_ENABLE_DEV "enableoutput"
|
||||||
#define COMMAND_DISABLE_DEV "disabledevice"
|
#define COMMAND_DISABLE_DEV "disableoutput"
|
||||||
#define COMMAND_DEVICES "devices"
|
#define COMMAND_DEVICES "outputs"
|
||||||
#define COMMAND_COMMANDS "commands"
|
#define COMMAND_COMMANDS "commands"
|
||||||
|
|
||||||
#define COMMAND_STATUS_VOLUME "volume"
|
#define COMMAND_STATUS_VOLUME "volume"
|
||||||
|
243
src/interface.c
243
src/interface.c
@@ -62,8 +62,9 @@ static size_t interface_max_output_buffer_size =
|
|||||||
INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT;
|
INTERFACE_MAX_OUTPUT_BUFFER_SIZE_DEFAULT;
|
||||||
|
|
||||||
typedef struct _Interface {
|
typedef struct _Interface {
|
||||||
char buffer[INTERFACE_MAX_BUFFER_LENGTH+2];
|
char buffer[INTERFACE_MAX_BUFFER_LENGTH];
|
||||||
int bufferLength;
|
int bufferLength;
|
||||||
|
int bufferPos;
|
||||||
int fd; /* file descriptor */
|
int fd; /* file descriptor */
|
||||||
FILE * fp; /* file pointer */
|
FILE * fp; /* file pointer */
|
||||||
int open; /* open/used */
|
int open; /* open/used */
|
||||||
@@ -94,6 +95,7 @@ void openInterface(Interface * interface, int fd) {
|
|||||||
assert(interface->open==0);
|
assert(interface->open==0);
|
||||||
|
|
||||||
interface->bufferLength = 0;
|
interface->bufferLength = 0;
|
||||||
|
interface->bufferPos = 0;
|
||||||
interface->fd = fd;
|
interface->fd = fd;
|
||||||
/* fcntl(interface->fd,F_SETOWN,(int)getpid()); */
|
/* fcntl(interface->fd,F_SETOWN,(int)getpid()); */
|
||||||
while((flags = fcntl(fd,F_GETFL))<0 && errno==EINTR);
|
while((flags = fcntl(fd,F_GETFL))<0 && errno==EINTR);
|
||||||
@@ -200,138 +202,135 @@ void openAInterface(int fd, struct sockaddr * addr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int interfaceReadInput(Interface * interface) {
|
static int proccessLineOfInput(Interface * interface) {
|
||||||
blockSignals();
|
int ret = 1;
|
||||||
if(read(interface->fd,interface->buffer+interface->bufferLength,1)>0) {
|
char * line = interface->buffer+interface->bufferPos;
|
||||||
int ret = 1;
|
|
||||||
int bytesRead = 1;
|
|
||||||
while(bytesRead>0) {
|
|
||||||
interface->buffer[interface->bufferLength+1] = '\0';
|
|
||||||
if(interface->buffer[interface->bufferLength]!='\r') {
|
|
||||||
interface->bufferLength++;
|
|
||||||
}
|
|
||||||
if(interface->bufferLength>=INTERFACE_MAX_BUFFER_LENGTH) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(interface->buffer[interface->bufferLength-1]=='\n') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bytesRead = read(interface->fd,interface->buffer+
|
|
||||||
interface->bufferLength,1);
|
|
||||||
}
|
|
||||||
unblockSignals();
|
|
||||||
if(interface->bufferLength>=INTERFACE_MAX_BUFFER_LENGTH) {
|
|
||||||
ERROR("interface %i: buffer overflow\n",
|
|
||||||
interface->num);
|
|
||||||
closeInterface(interface);
|
|
||||||
}
|
|
||||||
else if(interface->buffer[interface->bufferLength-1]=='\n') {
|
|
||||||
interface->buffer[interface->bufferLength-1] = '\0';
|
|
||||||
interface->bufferLength = 0;
|
|
||||||
|
|
||||||
if(interface->commandList) {
|
if(interface->bufferLength - interface->bufferPos > 1) {
|
||||||
if(strcmp(interface->buffer,
|
if(interface->buffer[interface->bufferLength-2] == '\r') {
|
||||||
INTERFACE_LIST_MODE_END)==0)
|
interface->buffer[interface->bufferLength-2] = '\0';
|
||||||
{
|
}
|
||||||
DEBUG("interface %i: process command "
|
}
|
||||||
"list\n",interface->num);
|
|
||||||
ret = proccessListOfCommands(
|
|
||||||
interface->fp,
|
|
||||||
&(interface->permission),
|
|
||||||
&(interface->expired),
|
|
||||||
interface->commandListOK,
|
|
||||||
interface->commandList);
|
|
||||||
DEBUG("interface %i: process command "
|
|
||||||
"list returned %i\n",
|
|
||||||
interface->num,
|
|
||||||
ret);
|
|
||||||
if(ret==0) {
|
|
||||||
commandSuccess(interface->fp);
|
|
||||||
}
|
|
||||||
else if(ret==COMMAND_RETURN_CLOSE ||
|
|
||||||
interface->expired) {
|
|
||||||
closeInterface(interface);
|
|
||||||
}
|
|
||||||
printInterfaceOutBuffer(interface);
|
|
||||||
|
|
||||||
freeList(interface->commandList);
|
if(interface->commandList) {
|
||||||
interface->commandList = NULL;
|
if(strcmp(line, INTERFACE_LIST_MODE_END)==0) {
|
||||||
}
|
DEBUG("interface %i: process command "
|
||||||
else {
|
"list\n",interface->num);
|
||||||
interface->commandListSize+=
|
ret = proccessListOfCommands(
|
||||||
sizeof(ListNode);
|
interface->fp,
|
||||||
interface->commandListSize+=
|
&(interface->permission),
|
||||||
strlen(interface->buffer)+1;
|
&(interface->expired),
|
||||||
if(interface->commandListSize >
|
interface->commandListOK,
|
||||||
interface_max_command_list_size)
|
interface->commandList);
|
||||||
{
|
DEBUG("interface %i: process command "
|
||||||
ERROR("interface %i: command "
|
"list returned %i\n",
|
||||||
"list size (%lli) is "
|
interface->num,
|
||||||
"larger than the max "
|
ret);
|
||||||
"(%lli)\n",
|
if(ret==0) commandSuccess(interface->fp);
|
||||||
interface->num,
|
else if(ret==COMMAND_RETURN_CLOSE || interface->expired)
|
||||||
interface->
|
{
|
||||||
commandListSize,
|
|
||||||
interface_max_command_list_size);
|
|
||||||
closeInterface(interface);
|
|
||||||
|
|
||||||
}
|
closeInterface(interface);
|
||||||
else {
|
}
|
||||||
insertInListWithoutKey(
|
printInterfaceOutBuffer(interface);
|
||||||
interface->commandList,
|
|
||||||
strdup(interface->
|
freeList(interface->commandList);
|
||||||
buffer));
|
interface->commandList = NULL;
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
|
interface->commandListSize+= sizeof(ListNode);
|
||||||
|
interface->commandListSize+= strlen(line)+1;
|
||||||
|
if(interface->commandListSize >
|
||||||
|
interface_max_command_list_size)
|
||||||
|
{
|
||||||
|
ERROR("interface %i: command "
|
||||||
|
"list size (%lli) is "
|
||||||
|
"larger than the max "
|
||||||
|
"(%lli)\n",
|
||||||
|
interface->num,
|
||||||
|
interface->
|
||||||
|
commandListSize,
|
||||||
|
interface_max_command_list_size)
|
||||||
|
;
|
||||||
|
closeInterface(interface);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(strcmp(interface->buffer,
|
insertInListWithoutKey(interface->commandList,
|
||||||
INTERFACE_LIST_MODE_BEGIN)==0)
|
strdup(line));
|
||||||
{
|
|
||||||
interface->commandList = makeList(free,
|
|
||||||
1);
|
|
||||||
interface->commandListSize =
|
|
||||||
sizeof(List);
|
|
||||||
interface->commandListOK = 0;
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
else if(strcmp(interface->buffer,
|
|
||||||
INTERFACE_LIST_OK_MODE_BEGIN)
|
|
||||||
==0)
|
|
||||||
{
|
|
||||||
interface->commandList = makeList(free,
|
|
||||||
1);
|
|
||||||
interface->commandListSize =
|
|
||||||
sizeof(List);
|
|
||||||
interface->commandListOK = 1;
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
DEBUG("interface %i: process command \"%s\"\n",interface->num,interface->buffer);
|
|
||||||
ret = processCommand(
|
|
||||||
interface->fp,
|
|
||||||
&(interface->
|
|
||||||
permission),
|
|
||||||
interface->buffer);
|
|
||||||
DEBUG("interface %i: command returned %i\n",interface->num,ret);
|
|
||||||
if(ret==0) {
|
|
||||||
commandSuccess(interface->fp);
|
|
||||||
}
|
|
||||||
else if(ret==COMMAND_RETURN_CLOSE ||
|
|
||||||
interface->expired) {
|
|
||||||
closeInterface(interface);
|
|
||||||
}
|
|
||||||
printInterfaceOutBuffer(interface);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
unblockSignals();
|
if(strcmp(line, INTERFACE_LIST_MODE_BEGIN) == 0) {
|
||||||
closeInterface(interface);
|
interface->commandList = makeList(free, 1);
|
||||||
|
interface->commandListSize = sizeof(List);
|
||||||
|
interface->commandListOK = 0;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
else if(strcmp(line, INTERFACE_LIST_OK_MODE_BEGIN) == 0) {
|
||||||
|
interface->commandList = makeList(free, 1);
|
||||||
|
interface->commandListSize = sizeof(List);
|
||||||
|
interface->commandListOK = 1;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DEBUG("interface %i: process command \"%s\"\n",
|
||||||
|
interface->num, line);
|
||||||
|
ret = processCommand(interface->fp,
|
||||||
|
&(interface->permission),
|
||||||
|
line);
|
||||||
|
DEBUG("interface %i: command returned %i\n",
|
||||||
|
interface->num, ret);
|
||||||
|
if(ret==0) commandSuccess(interface->fp);
|
||||||
|
else if(ret==COMMAND_RETURN_CLOSE || interface->expired)
|
||||||
|
{
|
||||||
|
closeInterface(interface);
|
||||||
|
}
|
||||||
|
printInterfaceOutBuffer(interface);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int processBytesRead(Interface * interface, int bytesRead) {
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
|
while(bytesRead > 0) {
|
||||||
|
interface->bufferLength++;
|
||||||
|
bytesRead--;
|
||||||
|
if(interface->buffer[interface->bufferLength-1]=='\n') {
|
||||||
|
interface->buffer[interface->bufferLength-1] = '\0';
|
||||||
|
ret = proccessLineOfInput(interface);
|
||||||
|
interface->bufferPos = interface->bufferLength;
|
||||||
|
}
|
||||||
|
if(interface->bufferLength==INTERFACE_MAX_BUFFER_LENGTH)
|
||||||
|
{
|
||||||
|
if(interface->bufferPos == 0) {
|
||||||
|
ERROR("interface %i: buffer overflow\n",
|
||||||
|
interface->num);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
interface->bufferLength-= interface->bufferPos;
|
||||||
|
memmove(interface->buffer,
|
||||||
|
interface->buffer+interface->bufferPos,
|
||||||
|
interface->bufferLength);
|
||||||
|
interface->bufferPos = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int interfaceReadInput(Interface * interface) {
|
||||||
|
int bytesRead = read(interface->fd,
|
||||||
|
interface->buffer+interface->bufferLength,
|
||||||
|
INTERFACE_MAX_BUFFER_LENGTH-interface->bufferLength+1);
|
||||||
|
|
||||||
|
if(bytesRead > 0) return processBytesRead(interface, bytesRead);
|
||||||
|
else if(bytesRead == 0 && errno!=EINTR) closeInterface(interface);
|
||||||
|
else return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user