From ff98ee6f9e66ba850274463ca7f125c01a02d73e Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 10 Jan 2014 21:07:58 +0100 Subject: mplayer_server: change the type of callback_t we need to access the opcode of the callbacks from outside of the server (i.e. in the future clients). the type callback_t is now a pointer to function and can be accessed using callbacks_g[opcode] where the opcodes are enumerated in request.h a new file named pub_callbacks.h has been added. it will be used later by the client and must not be server dependant closes #1 --- Makefile | 2 +- callbacks.c | 34 ++++++++++++++++------------------ main.c | 3 +++ pub_callbacks.h | 26 ++++++++++++++++++++++++++ request.h | 10 ++-------- 5 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 pub_callbacks.h diff --git a/Makefile b/Makefile index b8d27b8..5081274 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC ?= gcc CFLAGS += -W -Wall -std=gnu99 -Wextra CFLAGS += -D _XOPEN_SOURCE -#CFLAGS += -D LOGS +CFLAGS += -D LOGS NAME = mplayer_server SRC = main.c callbacks.c escape.c logs.c diff --git a/callbacks.c b/callbacks.c index e41fe8b..c0b6dce 100644 --- a/callbacks.c +++ b/callbacks.c @@ -16,20 +16,20 @@ CB(mute); extern FILE *stream_g; -typedef struct { - int opcode; - int (*cb)(const byte *, int); -} callback_t; - -static callback_t callbacks_g[] = { - {0, callback_load_url}, - {1, callback_pause}, - {2, callback_quit}, - {3, callback_snd_up}, - {4, callback_snd_down}, - {5, callback_fullscreen}, - {6, callback_mute}, -}; +static callback_t callbacks_g[CALLBACK_COUNT] = {NULL}; + +int callbacks_init(void) +{ + callbacks_g[CALLBACK_LOAD_URL] = callback_load_url; + callbacks_g[CALLBACK_PAUSE] = callback_pause; + callbacks_g[CALLBACK_QUIT] = callback_quit; + callbacks_g[CALLBACK_SND_DOWN] = callback_snd_down; + callbacks_g[CALLBACK_SND_UP] = callback_snd_up; + callbacks_g[CALLBACK_FULLSCREEN] = callback_fullscreen; + callbacks_g[CALLBACK_MUTE] = callback_mute; + + return 0; +} /* * returns a new malloced() null terminated escaped string @@ -40,10 +40,8 @@ char *real_escape_string(const byte *buf, int size); void *get_assoc_cb(int opcode) { - for (size_t i = 0; i < sizeof(callbacks_g) / sizeof(callbacks_g[0]); ++i) { - if (callbacks_g[i].opcode == opcode) { - return callbacks_g[i].cb; - } + if (opcode >= 0 && opcode < CALLBACK_COUNT) { + return callbacks_g[opcode]; } return NULL; diff --git a/main.c b/main.c index 5946a8c..1d0ee2c 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,7 @@ FILE *stream_g; static request_t buffer_g; +int callbacks_init(void); void *get_assoc_cb(int opcode); /* returns a socket listing to port or -1 if something failed */ @@ -81,6 +82,8 @@ int main(int argc, char *argv[]) int sock = bind_socket((argc < 2) ? 4333 : atoi(argv[1])); + callbacks_init(); + if (sock >= 0) { signal(SIGPIPE, SIG_IGN); diff --git a/pub_callbacks.h b/pub_callbacks.h new file mode 100644 index 0000000..a8f6d34 --- /dev/null +++ b/pub_callbacks.h @@ -0,0 +1,26 @@ +#ifndef PUB_CALLBACKS_H +#define PUB_CALLBACKS_H + +#include + +typedef uint8_t byte; + +typedef struct { + byte opcode; + + byte data[512]; +} request_t; + +enum { + CALLBACK_LOAD_URL, + CALLBACK_PAUSE, + CALLBACK_QUIT, + CALLBACK_SND_UP, + CALLBACK_SND_DOWN, + CALLBACK_FULLSCREEN, + CALLBACK_MUTE, + + CALLBACK_COUNT +}; + +#endif /* PUB_CALLBACKS_H */ diff --git a/request.h b/request.h index 6c5925a..cbf7ecb 100644 --- a/request.h +++ b/request.h @@ -1,14 +1,8 @@ #ifndef REQUEST_H #define REQUEST_H -#include +#include "pub_callbacks.h" -typedef uint8_t byte; - -typedef struct { - byte opcode; - - byte data[512]; -} request_t; +typedef int (*callback_t)(const byte *, int); #endif /* REQUEST_H */ -- cgit v1.2.3