diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2014-01-10 21:07:58 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@intersec.com> | 2014-01-18 13:46:58 +0100 |
commit | ff98ee6f9e66ba850274463ca7f125c01a02d73e (patch) | |
tree | 56b4075f92cd90d855ae05c4a92d193d03297c7e | |
parent | c5b339e3964009d7eb917ff52e60ba69ff170c3c (diff) |
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
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | callbacks.c | 34 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | pub_callbacks.h | 26 | ||||
-rw-r--r-- | request.h | 10 |
5 files changed, 48 insertions, 27 deletions
@@ -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; @@ -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 <stdint.h> + +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 */ @@ -1,14 +1,8 @@ #ifndef REQUEST_H #define REQUEST_H -#include <stdint.h> +#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 */ |