瀏覽代碼

Now can handle events on script side

Beoran 9 年之前
父節點
當前提交
6866cc6927
共有 4 個文件被更改,包括 159 次插入12 次删除
  1. 9 1
      data/script/main.rb
  2. 11 2
      include/client.h
  3. 105 5
      src/client.c
  4. 34 4
      src/server.c

+ 9 - 1
data/script/main.rb

@@ -17,7 +17,7 @@ class Client
   end
   
   def self.add(client_id)
-    @clients ||= {}
+    @clients2 ||= {}
     @clients[client_id] = Client.new(client_id)
   end
   
@@ -78,6 +78,14 @@ def woe_on_input(client_id, buf)
   end  
 end
 
+def woe_on_negotiate(client_id, how, option) 
+  p "Client #{client} #{client.id} negotiating."
+end
+
+def woe_on_subnegotiate(client_id, option, buffer) 
+  p "Client #{client} #{client.id} negotiating."
+end
+
 
 def woe_on_signal(signal)
   log "Received signal in script #{signal}"

+ 11 - 2
include/client.h

@@ -50,9 +50,18 @@ struct woe_client {
 
 int woe_clients_max(void);
 
+int woe_client_iac(struct woe_client * cli, int cmd);
 int woe_client_input(struct woe_client * cli, const char * buffer, size_t size);
-int woe_client_zmp(struct woe_client * cli, int argc, char *argv[]);
-
+int woe_client_zmp(struct woe_client * cli, int argc, const char *argv[]);
+int woe_client_negotiate(struct woe_client * cli, int how, int option);
+int woe_client_subnegotiate(struct woe_client * cli, const char * buf, int len, int telopt);
+int woe_client_ttype(struct woe_client * cli, int cmd, const char * name);
+int woe_client_error(struct woe_client * cli, int code, const char * msg);
+int woe_client_warning(struct woe_client * cli, int code, const char * msg);
+int woe_client_compress(struct woe_client * cli, int state);
+int woe_client_environ(struct woe_client * cli, int cmd, const struct telnet_environ_t *values, size_t size);
+int woe_client_mssp(struct woe_client * cli, const struct telnet_environ_t *values, size_t size);
+ 
 
 struct woe_client * 
 woe_client_new(struct woe_server * srv,  int index, int socket, 

+ 105 - 5
src/client.c

@@ -112,9 +112,8 @@ int woe_client_send(struct woe_client * client, const char *buffer, unsigned int
 }
 
 int woe_client_input(struct woe_client * cli, const char *buffer, size_t size) {
-  unsigned int i;
   mrb_state * mrb;
-  LOG_NOTE("Received input for client %d");
+  LOG_NOTE("Received input for client %d", cli->index);
   mrb = woe_server_get_mrb(cli->server);  
   if (mrb) {  
     rh_run_toplevel(mrb, "woe_on_input", "is", cli->index, buffer, size);
@@ -122,21 +121,122 @@ int woe_client_input(struct woe_client * cli, const char *buffer, size_t size) {
   return 0;
 }
 
-int woe_client_zmp(struct woe_client * cli, int argc, char *argv[]) {
+int woe_client_zmp(struct woe_client * cli, int argc, const char *argv[]) {
   unsigned int i;
   mrb_state * mrb;
-  LOG_NOTE("Received ZMP reply for client %d");
+  LOG_NOTE("Received ZMP reply for client %d", cli->index);
   mrb = woe_server_get_mrb(cli->server);  
   if (mrb) {  
     rh_run_toplevel(mrb, "woe_begin_zmp", "ii", cli->index, argc);
     for (i=0; i < argc; i++) {
-      rh_run_toplevel(mrb, "woe_zmp_arg", "is", cli->index, argv[i]);
+      rh_run_toplevel(mrb, "woe_zmp_arg", "iiz", cli->index, i, argv[i]);
     }  
     rh_run_toplevel(mrb, "woe_finish_zmp", "ii", cli->index, argc);
   }
   return 0;
 }
 
+int woe_client_iac(struct woe_client * cli, int cmd) {
+  mrb_state * mrb;
+  LOG_NOTE("Received iac for client %d %d", cli->index, cmd);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_iac", "i", cli->index, cmd);
+  }
+  return 0;
+}
+
+
+int woe_client_negotiate(struct woe_client * cli, int how, int option) {
+  mrb_state * mrb;
+  LOG_NOTE("Received negotiate for client %d %d %d", cli->index, how, option);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_negotiate", "iii", cli->index, how, option);
+  }
+  return 0;
+}
+
+int woe_client_subnegotiate(struct woe_client * cli, const char * buf, int len, int telopt) {
+  mrb_state * mrb;
+  LOG_NOTE("Received subnegotiate for client %d", cli->index);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_subnegotiate", "iis", cli->index, telopt, buf, len);
+  }
+  return 0;
+}
+
+int woe_client_ttype(struct woe_client * cli, int cmd, const char * name) {
+  mrb_state * mrb;
+  LOG_NOTE("Received ttype for client %d %d %s", cli->index, cmd, name);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_ttype", "iz", cli->index, cmd, name);
+  }
+  return 0;
+}
 
 
+int woe_client_error(struct woe_client * cli, int code, const char * msg) {
+  mrb_state * mrb;
+  LOG_NOTE("Received error for client %d %d %s", cli->index, code, msg);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_error", "iz", cli->index, code, msg);
+  }
+  return 0;
+}
+
+
+int woe_client_warning(struct woe_client * cli, int code, const char * msg) {
+  mrb_state * mrb;
+  LOG_NOTE("Received warning for client %d %d %s", cli->index, code, msg);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_warning", "iz", cli->index, code, msg);
+  }
+  return 0;
+}
+
+
+int woe_client_compress(struct woe_client * cli, int state) {
+  mrb_state * mrb;
+  LOG_NOTE("Received compress for client %d %d", cli->index, state);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_on_compress", "ii", cli->index, state);
+  }
+  return 0;
+}
+
+int woe_client_environ(struct woe_client * cli, int cmd, const struct telnet_environ_t *values, size_t size) {
+  int i;
+  mrb_state * mrb;
+  LOG_NOTE("Received environ for client %d %d", cli->index, cmd);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_begin_environ", "iii", cli->index, cmd, size);
+    for (i=0; i < size; i++) {
+      rh_run_toplevel(mrb, "woe_environ_arg", "iiizz", cli->index, i, values[i].type, values[i].var, values[i].value);
+    }  
+   rh_run_toplevel(mrb, "woe_end_environ", "iii", cli->index, cmd, size);
+  }
+  return 0;
+}
+
+int woe_client_mssp(struct woe_client * cli, const struct telnet_environ_t *values, size_t size) {
+  int i;
+  mrb_state * mrb;
+  LOG_NOTE("Received mssp for client %d", cli->index);
+  mrb = woe_server_get_mrb(cli->server);  
+  if (mrb) {  
+    rh_run_toplevel(mrb, "woe_begin_mssp", "ii", cli->index, size);
+    for (i=0; i < size; i++) {
+      rh_run_toplevel(mrb, "woe_mssp_arg", "iiizz", cli->index, i, values[i].type, values[i].var, values[i].value);
+    }  
+   rh_run_toplevel(mrb, "woe_end_mssp", "iii", cli->index, size);
+  }
+  return 0;
+}
 

+ 34 - 4
src/server.c

@@ -121,17 +121,47 @@ static void woe_event_handler(telnet_t *telnet, telnet_event_t *ev, void *user_d
     break;
     
   case TELNET_EV_WONT:  
-    woe_client_negotiate(client, TELNET_WILL, ev->neg.telopt);
+    woe_client_negotiate(client, TELNET_WONT, ev->neg.telopt);
+    break;
+    
+  case TELNET_EV_IAC:
+    woe_client_iac(client, ev->iac.cmd);
+    break;
+    
+  case TELNET_EV_SUBNEGOTIATION:
+    woe_client_subnegotiate(client, ev->sub.buffer, ev->sub.size, ev->sub.telopt);
+    break; 
+
+  case TELNET_EV_TTYPE:
+    woe_client_ttype(client, ev->ttype.cmd, ev->ttype.name);
     break;
-  
     
+  case TELNET_EV_COMPRESS: 
+    woe_client_compress(client, ev->compress.state);
+    break;
+  
+  case TELNET_EV_ENVIRON:
+    woe_client_environ(client, ev->environ.cmd, ev->environ.values, ev->environ.size);
+    break;
+  
+  case TELNET_EV_MSSP:
+    woe_client_mssp(client, ev->mssp.values, ev->mssp.size);
+    break; 
+
+  /* warning */
+  case TELNET_EV_WARNING:  
+    LOG_WARNING("Telnet warning for client %d %s.\n", client->index, ev->error.msg);    
+    woe_client_warning(client, ev->error.errcode, ev->error.msg);
+    break;
+  
   /* error */
   case TELNET_EV_ERROR:  
-    LOG_ERROR("Telnet error for client.\n");    
+    LOG_ERROR("Telnet error for client %d %s.\n", client->index, ev->error.msg);    
+    woe_client_error(client, ev->error.errcode, ev->error.msg);
     woe_server_disconnect(client->server, client);
     break;
   
-  case TELNET_EV_ZMP
+  case TELNET_EV_ZMP:
     woe_client_zmp(client, ev->zmp.argc, ev->zmp.argv);
     break;