123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /* $Id: network.c,v 1.11 2001/04/28 15:29:25 kilobug Exp $ */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <server.h>
- #include <network.h>
- #include <pulsarnet.h>
- #include <general.h>
- #include <private.h>
- #include <action.h>
- void gfx_trash_client(net_type_t get, net_type_t wanted, void *data)
- {
- gfx_client_t *cl;
- cl = data;
- fprintf(stderr, "Network warning: reading %s, wanted %s.\n",
- net_type_name(get), net_type_name(wanted));
- close(cl->sock);
- cl->alive = False;
- }
- boolean_t gfx_get_ack(gfx_client_t *cl)
- {
- gfx_t *gfx;
- fflush(cl->wsock);
- gfx = gl_config->gfx_clients;
- if (!net_wait_ack(cl->sock))
- {
- gfx_trash_client(net_type_none, net_type_int, cl);
- return False;
- }
- return True;
- }
- void gfx_write_object(FILE *file, item_t *obj)
- {
- net_wr_float(file, obj->obj.x);
- net_wr_float(file, obj->obj.y);
- net_wr_int(file, obj->obj.team_id);
- if (obj->obj.type == obj_akx)
- {
- net_wr_int(file, obj->akx.action.type);
- switch (obj->akx.action.type)
- {
- case act_akx_move:
- break;
- case act_akx_pulse:
- net_wr_float(file, obj->akx.action.act.pulse.angle);
- net_wr_float(file, obj->akx.action.act.pulse.x);
- net_wr_float(file, obj->akx.action.act.pulse.y);
- break;
- case act_akx_link:
- net_wr_int(file, obj->akx.action.act.link.target_id);
- break;
- }
- }
- }
- static boolean_t gfx_do_send_updates(void *data, void *foo)
- {
- GSList *l;
- player_t *play;
- item_t *obj;
- int i;
- gfx_client_t *cl;
- cl = data;
- if (cl == NULL)
- {
- return True;
- }
- if (!cl->alive)
- {
- return True;
- }
- net_set_error_handler(gfx_trash_client, cl);
- net_wr_string(cl->wsock, "TRN");
- net_wr_int(cl->wsock, gl_config->turn_num);
- if (!gfx_get_ack(cl))
- {
- return False;
- }
- for (l = gl_config->players ; l != NULL ; l = l->next)
- {
- net_wr_string(cl->wsock, "UPL");
- play = l->data;
- net_wr_int(cl->wsock, play->team_id);
- net_wr_float(cl->wsock, play->score);
- }
- net_wr_string(cl->wsock, "END");
- if (!gfx_get_ack(cl))
- {
- return False;
- }
- for (i = 0 ; i < gl_config->nb_objects ; i++)
- {
- if (gl_objects[i].obj.change)
- {
- net_wr_string(cl->wsock, "UOB");
- obj = gl_objects + i;
- net_wr_int(cl->wsock, obj->obj.id);
- gfx_write_object(cl->wsock, obj);
- }
- }
- net_wr_string(cl->wsock, "END");
- if (!gfx_get_ack(cl))
- {
- return False;
- }
- return True;
- }
- static void gfx_send_updates(void *data, void *foo)
- {
- gfx_client_t *cl;
- if (!gfx_do_send_updates(data, foo))
- {
- cl = data;
- cl->alive = False;
- }
- }
- static void gfx_send_goodbye(void *data, void *foo)
- {
- gfx_client_t *cl;
- cl = data;
- net_wr_string(cl->wsock, "BYE");
- fflush(cl->wsock);
- net_wait_ack(cl->sock);
- }
- void gfx_client_new_turn(boolean_t last)
- {
- gfx_t *gfx;
- int i;
- if (!gl_config->gfx)
- {
- return;
- }
- gfx = gl_config->gfx_clients;
- i = net_chk_str_list(gfx->master, "GO", "END", NULL);
- if (i == -1)
- {
- fprintf(stderr, "Protocol error.\n");
- exit(0);
- }
- net_send_ack(gfx->wmaster);
- net_set_error_handler(NULL, NULL);
- if ((i == 0) && (!last))
- {
- while (gfx_new_client(gfx->sock, False))
- {
- }
- g_slist_foreach(gfx->clients, gfx_send_updates, NULL);
- }
- else
- {
- g_slist_foreach(gfx->clients, gfx_send_goodbye, NULL);
- exit(0);
- }
- }
- static void gfx_clean()
- {
- close(gl_config->gfx_clients->sock);
- }
- void gfx_init()
- {
- if (!gl_config->gfx)
- {
- return;
- gl_config->gfx_clients = NULL;
- }
- gl_config->gfx_clients = malloc(sizeof(*(gl_config->gfx_clients)));
- gl_config->gfx_clients->master = 0;
- gl_config->gfx_clients->wmaster = NULL;
- gl_config->gfx_clients->clients = NULL;
- gl_config->gfx_clients->sock = net_listen_at(gl_config->port, 15,
- NULL, 0);
- atexit(gfx_clean);
- while (!gfx_new_client(gl_config->gfx_clients->sock, True))
- {
- }
- }
|