Commit 8b1b82b3 authored by Max Kellermann's avatar Max Kellermann

client: pass the client struct to processCommand()

Start exporting the client struct as an opaque struct. For now, pass it only to processCommand() and processListOfCommands(), and provide a function to extract the socket handle. Later, we will propagate the pointer to all command implementations, and of course to client_print() etc.
parent 2c8aa8ef
...@@ -324,6 +324,14 @@ void client_new(int fd, const struct sockaddr *addr) ...@@ -324,6 +324,14 @@ void client_new(int fd, const struct sockaddr *addr)
sockaddr_to_tmp_string(addr)); sockaddr_to_tmp_string(addr));
} }
int client_get_fd(struct client *client)
{
assert(client != NULL);
assert(client->fd >= 0);
return client->fd;
}
static int client_process_line(struct client *client) static int client_process_line(struct client *client)
{ {
int ret = 1; int ret = 1;
...@@ -335,7 +343,7 @@ static int client_process_line(struct client *client) ...@@ -335,7 +343,7 @@ static int client_process_line(struct client *client)
"list\n", client->num); "list\n", client->num);
global_expired = 0; global_expired = 0;
ret = processListOfCommands(client->fd, ret = processListOfCommands(client,
&(client->permission), &(client->permission),
&global_expired, &global_expired,
client->cmd_list_OK, client->cmd_list_OK,
...@@ -384,7 +392,7 @@ static int client_process_line(struct client *client) ...@@ -384,7 +392,7 @@ static int client_process_line(struct client *client)
} else { } else {
DEBUG("client %i: process command \"%s\"\n", DEBUG("client %i: process command \"%s\"\n",
client->num, line); client->num, line);
ret = processCommand(client->fd, ret = processCommand(client,
&(client->permission), line); &(client->permission), line);
DEBUG("client %i: command returned %i\n", DEBUG("client %i: command returned %i\n",
client->num, ret); client->num, ret);
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include "os_compat.h" #include "os_compat.h"
struct client;
void client_manager_init(void); void client_manager_init(void);
void client_manager_deinit(void); void client_manager_deinit(void);
int client_manager_io(void); int client_manager_io(void);
...@@ -28,6 +30,13 @@ void client_manager_expire(void); ...@@ -28,6 +30,13 @@ void client_manager_expire(void);
void client_new(int fd, const struct sockaddr *addr); void client_new(int fd, const struct sockaddr *addr);
/**
* Return the file descriptor of this client's socket. This function
* will be removed once we have migrated to passing the client struct
* everywhere.
*/
int client_get_fd(struct client *client);
int client_print(int fd, const char *buffer, size_t len); int client_print(int fd, const char *buffer, size_t len);
#endif #endif
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "ack.h" #include "ack.h"
#include "audio.h" #include "audio.h"
#include "dbUtils.h" #include "dbUtils.h"
#include "client.h"
#include "os_compat.h" #include "os_compat.h"
#define COMMAND_PLAY "play" #define COMMAND_PLAY "play"
...@@ -1204,9 +1205,11 @@ static CommandEntry *getCommandEntryFromString(char *string, int *permission) ...@@ -1204,9 +1205,11 @@ static CommandEntry *getCommandEntryFromString(char *string, int *permission)
return cmd; return cmd;
} }
static int processCommandInternal(int fd, mpd_unused int *permission, static int processCommandInternal(struct client *client,
mpd_unused int *permission,
char *commandString, struct strnode *cmdnode) char *commandString, struct strnode *cmdnode)
{ {
int fd = client_get_fd(client);
int argc; int argc;
char *argv[COMMAND_ARGV_MAX] = { NULL }; char *argv[COMMAND_ARGV_MAX] = { NULL };
CommandEntry *cmd; CommandEntry *cmd;
...@@ -1232,9 +1235,10 @@ static int processCommandInternal(int fd, mpd_unused int *permission, ...@@ -1232,9 +1235,10 @@ static int processCommandInternal(int fd, mpd_unused int *permission,
return ret; return ret;
} }
int processListOfCommands(int fd, int *permission, int *expired, int processListOfCommands(struct client *client, int *permission, int *expired,
int listOK, struct strnode *list) int listOK, struct strnode *list)
{ {
int fd = client_get_fd(client);
struct strnode *cur = list; struct strnode *cur = list;
int ret = 0; int ret = 0;
...@@ -1243,7 +1247,7 @@ int processListOfCommands(int fd, int *permission, int *expired, ...@@ -1243,7 +1247,7 @@ int processListOfCommands(int fd, int *permission, int *expired,
while (cur) { while (cur) {
DEBUG("processListOfCommands: process command \"%s\"\n", DEBUG("processListOfCommands: process command \"%s\"\n",
cur->data); cur->data);
ret = processCommandInternal(fd, permission, cur->data, cur); ret = processCommandInternal(client, permission, cur->data, cur);
DEBUG("processListOfCommands: command returned %i\n", ret); DEBUG("processListOfCommands: command returned %i\n", ret);
if (ret != 0 || (*expired) != 0) if (ret != 0 || (*expired) != 0)
goto out; goto out;
...@@ -1257,9 +1261,9 @@ out: ...@@ -1257,9 +1261,9 @@ out:
return ret; return ret;
} }
int processCommand(int fd, int *permission, char *commandString) int processCommand(struct client *client, int *permission, char *commandString)
{ {
return processCommandInternal(fd, permission, commandString, NULL); return processCommandInternal(client, permission, commandString, NULL);
} }
mpd_fprintf_ void commandError(int fd, int error, const char *fmt, ...) mpd_fprintf_ void commandError(int fd, int error, const char *fmt, ...)
......
...@@ -27,10 +27,14 @@ ...@@ -27,10 +27,14 @@
#define COMMAND_RETURN_CLOSE 20 #define COMMAND_RETURN_CLOSE 20
#define COMMAND_MASTER_READY 30 #define COMMAND_MASTER_READY 30
int processListOfCommands(int fd, int *permission, int *expired, struct client;
int processListOfCommands(struct client *client,
int *permission, int *expired,
int listOK, struct strnode *list); int listOK, struct strnode *list);
int processCommand(int fd, int *permission, char *commandString); int processCommand(struct client *client,
int *permission, char *commandString);
void initCommands(void); void initCommands(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment