Index: plugins/roard/protocol-esound.c
===================================================================
--- plugins/roard/protocol-esound.c	(revision 5640)
+++ plugins/roard/protocol-esound.c	(revision 5739)
@@ -28,4 +28,40 @@
 #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
 #ifdef ROAR_HAVE_H_ESD
+#include <esd.h>
+
+
+struct emul_esd_command {
+ int    cmd;
+ size_t datalen;
+#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_MINIMAL)
+ char name[COMMAND_MAX_NAMELEN];
+#else
+ char * name;
+#endif
+ int (*handler)(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+};
+
+static int emul_esd_exec_command  (int client, int cmd, struct roar_vio_calls * vio);
+static int emul_esd_check_client  (int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara);
+
+static int emul_esd_int_read_buf  (int client, int * data, void * buf);
+static int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio);
+static int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio);
+static int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio);
+static int emul_esd_test_byteorder(int client, void * data);
+
+static int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+static int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+static int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+static int emul_esd_on_standby    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+static int emul_esd_on_standbymode(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+
+static int emul_esd_on_stream_pan (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+
+static int emul_esd_on_server_info(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+
+static int emul_esd_on_all_info   (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio);
+
+
 
 #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_MINIMAL)
@@ -45,5 +81,5 @@
 #define _ESD2ROAR(x) ((x)-1)
 
-struct emul_esd_command g_emul_esd_commands[] = {
+static struct emul_esd_command g_emul_esd_commands[] = {
  {ESD_PROTO_CONNECT,      ESD_KEY_LEN  +     _INTSIZE, _NAME("CONNECT"),      emul_esd_on_connect},
  {ESD_PROTO_LOCK,         ESD_KEY_LEN  +     _INTSIZE, _NAME("LOCK"),         NULL},
@@ -75,5 +111,5 @@
 
 // command handling:
-int emul_esd_exec_command(int client, int cmd, struct roar_vio_calls * vio) {
+static int emul_esd_exec_command(int client, int cmd, struct roar_vio_calls * vio) {
  struct emul_esd_command * cur;
  void * data = NULL;
@@ -95,5 +131,4 @@
     if ( (data = roar_mm_malloc(cur->datalen)) == NULL ) {
      // we will do a protocol error in case we do not drop the client
-     clients_delete(client);
      return -1;
     }
@@ -104,5 +139,4 @@
      if ( ret < 1 ) {
       roar_mm_free(data);
-      clients_delete(client);
       return -1;
      } else {
@@ -116,5 +150,4 @@
                client, cur->name, cmd, vio
              );
-    clients_delete(client);
     r = -1;
    } else {
@@ -132,20 +165,19 @@
 }
 
-int emul_esd_check_client(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara) {
- struct roar_vio_calls calls;
+static int emul_esd_set_proto(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara) {
+ if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, vio) == -1 )
+  return -1;
+
+ return 0;
+}
+
+static int emul_esd_check_client(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * protopara, ssize_t protoparalen, struct roar_dl_librarypara * pluginpara) {
  _cmd_t cmd;
 
  if ( client == -1 )
   return -1;
-
- if ( vio == NULL ) {
-  vio = &calls;
-  if ( roar_vio_open_fh_socket(vio, clients_get_fh(client)) == -1 )
-   return -1;
- }
 
  if ( roar_vio_read(vio, &cmd, _INTSIZE) != _INTSIZE ) {
   // really bad protocol error
-  clients_delete(client);
   return -1;
  }
@@ -167,5 +199,5 @@
  return 0;
 }
-int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio) {
+static int emul_esd_int_read      (int client, int * data, struct roar_vio_calls * vio) {
  _cmd_t d;
 
@@ -181,5 +213,5 @@
 }
 
-int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio) {
+static int emul_esd_int_write     (int client, int   data, struct roar_vio_calls * vio) {
  _cmd_t d = data;
 
@@ -187,10 +219,10 @@
 }
 
-int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_test_auth     (int client, void * data, struct roar_vio_calls * vio) {
  // accept all clients for the moment.
  return emul_esd_int_write(client, 1, vio);
 }
 
-int emul_esd_test_byteorder(int client, void * data) {
+static int emul_esd_test_byteorder(int client, void * data) {
  struct roar_client * c;
 
@@ -214,5 +246,5 @@
 
 // handler:
-int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_connect    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
 
  ROAR_DBG("emul_esd_on_connect(client=%i, cmd=%p, data=%p, vio=%p) = ?", client, cmd, data, vio);
@@ -236,5 +268,5 @@
 }
 
-int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_stream     (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  struct roar_stream_server * ss;
  struct roar_stream        *  s;
@@ -254,5 +286,4 @@
   case ESD_PROTO_STREAM_FILT: dir = ROAR_DIR_FILTER;  break;
   default:
-    clients_delete(client);
     return -1;
  }
@@ -264,5 +295,4 @@
  ROAR_DBG("emul_esd_on_stream(client=%i, ...): creating stream...", client);
  if ((stream = streams_new()) == -1 ) {
-  clients_delete(client);
   return -1;
  }
@@ -271,5 +301,4 @@
  if ( streams_get(stream, &ss) == -1 ) {
   streams_delete(stream);
-  clients_delete(client);
   return -1;
  }
@@ -280,5 +309,4 @@
  if ( client_stream_add(client, stream) == -1 ) {
   streams_delete(stream);
-  clients_delete(client);
   return -1;
  }
@@ -299,5 +327,4 @@
   default:
     streams_delete(stream);
-    clients_delete(client);
     return -1;
  }
@@ -308,5 +335,4 @@
   default:
     streams_delete(stream);
-    clients_delete(client);
     return -1;
  }
@@ -317,17 +343,15 @@
 
  if ( streams_set_dir(stream, dir, 1) == -1 ) {
-  clients_delete(client);
   return -1;
  }
 
  if ( client_stream_exec(client, stream) == -1 ) {
-  clients_delete(client);
-  return -1;
- }
-
- return 0;
-}
-
-int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+  return -1;
+ }
+
+ return 0;
+}
+
+static int emul_esd_on_latency    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  int lag = ROAR_OUTPUT_CFREQ;
 
@@ -337,5 +361,5 @@
 }
 
-int emul_esd_on_standby    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_standby    (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  int ok = 0;
 
@@ -356,5 +380,5 @@
 }
 
-int emul_esd_on_standbymode(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_standbymode(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  int mode = ESM_ERROR;
 
@@ -372,5 +396,5 @@
 }
 
-int emul_esd_on_stream_pan (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_stream_pan (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  struct roar_stream_server * ss;
  int stream = -1;
@@ -395,5 +419,5 @@
 }
 
-int emul_esd_on_server_info(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_server_info(int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  int version = 0;
  int rate    = g_sa->rate;
@@ -422,5 +446,5 @@
 }
 
-int emul_esd_on_all_info   (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
+static int emul_esd_on_all_info   (int client, struct emul_esd_command * cmd, void * data, struct roar_vio_calls * vio) {
  struct roar_stream_server *   ss;
  struct roar_audio_info    * info;
@@ -532,4 +556,12 @@
 }
 
+struct roar_dl_proto __proto_common_esd = {
+ .proto = ROAR_PROTO_ESOUND,
+ .description = "EsounD emulation",
+ .flags = ROAR_DL_PROTO_FLAGS_NONE,
+ .set_proto = emul_esd_set_proto,
+ .handle = emul_esd_check_client
+};
+
 #endif
 #endif
