Update of /cvsroot/agd/server/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31282
Modified Files:
net.h net.c
Log Message:
The add_command() system.
Index: net.c
===================================================================
RCS file: /cvsroot/agd/server/src/net.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- net.c 23 Jul 2004 17:06:43 -0000 1.20
+++ net.c 24 Jul 2004 17:51:25 -0000 1.21
@@ -95,8 +95,6 @@
if(p->conn.socket > greatest_sockfd)
greatest_sockfd = p->conn.socket;
-/* p->conn.state = DEFAULT;*/
-
this_player = p;
player_ob = xmalloc(sizeof(object_t));
player_ob->iaob = p;
@@ -107,7 +105,7 @@
login_ob = apply(master, "connect", NULL);
if(!login_ob || login_ob->type != T_OBJECT)
- crash(0, "master::connect() is not working", 3);
+ crash("master::connect() is not working", 3);
if(!login_ob->u.ob) {
fprintf(stderr, "Warning: master::connect() didn't return an object.\n");
@@ -157,18 +155,63 @@
send(p->conn.socket, buf, siz, 0);
}
-void net_read(player_t *p)
+void do_command(player_t *p, char *str)
{
- char *c;
+ char *verb, *c;
+ int len, i;
+ int found_cmd;
+ variable_t *ret;
+
+ if(!p || !str)
+ return;
+
+ ret = apply(p->ob, "process_input", "s", str);
+ if(ret && ret->type == T_INT && ret->u.i) {
+ /* Don't do any further processing. */
+ return;
+ }
- if(!p->input_to) {
+ c = strchr(str, ' ');
+ if(c)
+ len = c - str + 1;
+ else
+ len = strlen(str) + 1;
+
+ verb = xmalloc(len + 1);
+ strncpy(verb, str, len);
+ verb[len - 1] = '\0';
+
#ifdef DEBUG
- if(conf.debuglevel)
- printf("[net] player %p (with iaob %s) doesn't have input_to set\n", p->ob? p->ob->name :NULL, p);
+ if(conf.debuglevel) {
+ printf("verb: %s", verb);
+ if(c)
+ printf("; args: %s\n", c+1);
+ putchar('\n');
+ }
#endif
- goto out;
+
+ found_cmd = 0;
+ for(i=0;i<p->num_cmd;i++) {
+ command_t *cmd = &p->cmds[i];
+ if(strcmp(cmd->verb, verb) == 0) {
+ found_cmd = 1;
+ p->this_verb = i + 1;
+ if(c)
+ apply(p->ob, cmd->fun, "s", c+1);
+ else
+ apply(p->ob, cmd->fun, "");
+ }
}
+ if(!found_cmd) {
+ net_send("What?\n", 7, p);
+ }
+}
+
+void net_read(player_t *p)
+{
+ char *c;
+
if(!p->ob) {
net_send("Oops, looks like you don't have an interactive object!\n", 55, p);
net_disconnect(p);
@@ -176,14 +219,17 @@
}
c = strchr(p->conn.recvbuf, '\r');
- if(c) {
- *c = '\0';
- } else if(c = strchr(p->conn.recvbuf, '\n'))
+ if(c || (c = strchr(p->conn.recvbuf, '\n')))
*c = '\0';
- p->input_to_called = 1;
- apply(p->ob, p->input_to, "s", p->conn.recvbuf);
- if(p->input_to_called) { /* input_to wasn't called again */
- p->input_to = NULL;
+
+ if(p->input_to) {
+ p->input_to_called = 1;
+ apply(p->ob, p->input_to, "s", p->conn.recvbuf);
+ if(p->input_to_called) { /* input_to wasn't called again */
+ p->input_to = NULL;
+ }
+ } else {
+ do_command(p, p->conn.recvbuf);
}
/* The previous apply() might have destructed p->ob */
@@ -200,11 +246,11 @@
switch(ret) {
case -1:
fprintf(stderr, "Error reading from player!\n");
- /* fall through, and make them netdead */
+ /* Fall through, and make them netdead. */
case 0:
apply(pl->ob, "net_dead", "o", pl->ob);
if(pl->ob) {
- /* the login object will have to restore this object's iaob */
+ /* The login object will have to restore this object's iaob. */
pl->ob->iaob = NULL;
}
net_disconnect(pl);
@@ -234,6 +280,7 @@
p->data = NULL;
}
+ list_remove(&to_be_dested, ob);
actual_destruct(ob);
}
}
Index: net.h
===================================================================
RCS file: /cvsroot/agd/server/src/net.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- net.h 8 Jun 2004 20:44:53 -0000 1.12
+++ net.h 24 Jul 2004 17:51:25 -0000 1.13
@@ -27,12 +27,19 @@
time_t last_active;
} player_connection_t;
+typedef struct {
+ char *verb, *fun;
+} command_t;
+
typedef struct player {
player_connection_t conn;
object_t *ob;
char *input_to;
int input_to_called;
+
+ command_t *cmds;
+ int num_cmd, this_verb;
} player_t;
void net_loop(void);
|