Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

gtcspeaker.h

Go to the documentation of this file.
00001 /*
00002     Sebastien Loisel's Toolchest
00003     Copyright (C) 1998 Sebastien Loisel
00004 
00005     $Header: /home/gtc/public_html/gtc-cvs/toolchest/include/gtcspeaker.h,v 1.31 2001/07/09 05:53:43 loisel Exp $
00006     */
00007 
00018 #ifndef Z_SPEAKER_H
00019 #define Z_SPEAKER_H
00020 
00021 #include <gtcutils.h>
00022 #include <unistd.h>
00023 #include <sys/types.h>
00024 
00026 extern int gtcSpeakerverbose;
00027 
00029 typedef unsigned char gtcByte;
00030 
00042 typedef struct
00043 {
00045   gtcByte *data;
00046   int 
00048   size,
00050     refcount;
00051   /* you may use the refcount if you want but it's mostly for sharing packets
00052      to write amongst multiple speakers. */
00053 } gtcPacket;
00054 
00056 typedef struct _gtcPacketList gtcPacketList;
00063 struct _gtcPacketList
00064 {
00066   gtcPacket *p;
00068   gtcPacketList *next;
00069 };
00070 
00091 typedef struct
00092 {
00094   int fd;
00096   int dead;
00097 
00098   gtcPacketList *outhead, *outtail, *inhead, *intail;
00099   int rstate, rpos, wstate, wpos;
00100   unsigned char rfoo[4];
00101   gtcPacket *r; /* for holding half constructed received gtcPackets */
00102 } gtcSpeaker;
00103   
00104 /*****
00105  * Speaker stuff.
00106  *
00107  * The speaker is the basic network object. It does all the necessary grunt
00108  * work to send packets over the wire. Networking code is prone to bugs so
00109  * having this worked out is useful. Typically, you would get a speaker by
00110  * calling gtc_connect_2_host(...). Then you enter a select() loop where at
00111  * each step you do gtc_speaker_fds(...), call gtc_speaker_read() and
00112  * gtc_speaker_write() if the appropriate fd is set and then use
00113  * gtc_speaker_get() and gtc_speaker_put() to read and write to the net
00114  * respectively. When writing to the net, gtc_make_packet() will be useful
00115  * in creating packets. gtc_make_packet() creates packets by COPYING the
00116  * data. gtc_speaker_destroy() can be used to destroy a speaker. This also
00117  * has the effect of closing its fd.
00118  *****/
00119 
00121 void gtc_set_non_block(int filedes);
00123 gtcSpeaker *gtc_connect_2_host(char *host, int port);
00125 gtcSpeaker *gtc_connect_2_fd(int fd);
00127 void gtc_speaker_destroy(gtcSpeaker *foo);
00128 
00130 void gtc_speaker_read(gtcSpeaker *s);
00132 void gtc_speaker_write(gtcSpeaker *s);
00134 int gtc_speaker_long_out_queue(gtcSpeaker *s);
00136 gtcPacket *gtc_speaker_get(gtcSpeaker *s);
00138 void gtc_speaker_put(gtcSpeaker *s, gtcPacket *p); /* send a packet */
00140 gtcPacket *gtc_alloc_packet(int size);
00142 gtcPacket *gtc_make_packet(gtcByte *data, int size);
00144 void gtc_destroy_packet(gtcPacket *p);
00146 void gtc_upcount_packet(gtcPacket *p);
00148 void gtc_downcount_packet(gtcPacket *p);
00150 void gtc_speaker_fds(gtcSpeaker *s, fd_set *readfds, fd_set *writefds);
00152 void gtc_print_packet(gtcPacket *p);
00153 
00154 /*****
00155  * Host stuff.
00156  *
00157  * This here does the basic work you need for a server. You would typically
00158  * use gtc_new_host() to get a host. Then you would enter a select() loop.
00159  * You gtc_host_fds() to set the fd_sets and then you select() and then you
00160  * gtc_host_process() to process all the fds related to the host. You may
00161  * also register callbacks in the gtcHost structure for when clients are
00162  * added and removed, or when they receive or send data.
00163  *****/
00164 
00166 typedef struct _gtcSpeakerNode gtcSpeakerNode;
00168 struct _gtcSpeakerNode
00169 {
00170   gtcSpeaker *s;
00171   gtcSpeakerNode *next;
00172   void *userdata;
00173   void (*destructor)(gtcSpeakerNode *me);
00174   int refcount;
00175 };
00176 
00178 typedef struct _gtcHost gtcHost;
00190 struct _gtcHost
00191 {
00193   gtcSpeakerNode *first, *last; /* list of speakers */
00194   int
00196   lfd,
00198     bigfds; /* listen filedes & biggest filedes */
00200   int verbose; /* verbosity level (0, 1, 2 or 3) */
00201 
00203   void *userdata;
00205   void (*addcb)(gtcHost *h, gtcSpeakerNode *s);
00207   void (*delcb)(gtcHost *h, gtcSpeakerNode *s);
00208 };
00209 
00211 gtcHost *gtc_new_host_by_fd(int fd, int verbose);
00218 gtcHost *gtc_new_host(int port, int verbose);
00227 void gtc_host_fds(gtcHost *h, fd_set *readfds, fd_set *writefds);
00235 void gtc_host_process(gtcHost *h, fd_set *readfds, fd_set *writefds);
00237 void gtc_kill_host(gtcHost *h);
00239 void gtc_speaker_node_destroy(gtcSpeakerNode *node);  
00240 
00242 
00243 typedef struct _gtcClient gtcClient;
00245 struct _gtcClient
00246 {
00248   double sec_per_byte;
00250   double sec_next_send;
00252   gtcSpeaker *s;
00254   void *prev;
00256   int refcount;
00257 };
00258 
00260 enum gtcServerMessages
00261 {
00263   gtcNewClient=0,
00265   gtcDeadClient=1,
00267   gtcReceivedPacket=2,
00269   gtcReadyForSending=3,
00271   gtcTimeout=4,
00272 
00273   gtcNoMessage
00274 };
00275 
00276 typedef struct _gtcServerMessage gtcServerMessage;
00277 struct _gtcServerMessage
00278 {
00279   gtcServerMessage *next;
00281   int t;
00282   int client;
00283   int packet[5];
00284 };
00285 
00287 typedef struct
00288 {
00290   gtcServerMessage mfirst;
00292   gtcServerMessage *mlast;
00293 #if 0
00294 
00295   gtcClient cfirst;
00297   gtcClient *clast;
00298 #endif
00299   gtcClient **clients;
00300   int client_count;
00301   int *client_free_list;
00302   int client_free_first;
00304   double default_sec_per_byte;
00306   int sizeofclient;
00308   int lfd;
00310   int verbose;
00312   int refcount;
00313 } gtcServer;
00314 
00332 gtcServer *gtc_server_new(int port, int verbose,
00333                           int sizeofclient, double sec_per_byte);
00334 void gtc_server_delete(gtcServer *s);
00355 gtcServerMessage *gtc_server_get_message(gtcServer *s, double timeout);
00365 void gtc_server_next_message(gtcServer *s, double timeout,
00366                              gtcServerMessage *msg);
00373 void gtc_client_put(gtcClient *c, gtcPacket *p);
00383 void gtc_client_delete(gtcClient *c);
00384 
00385 enum gtcClientMessages
00386 {
00387   gtcKeyRepeat=0,
00388   gtcKeyPress,
00389   gtcKeyRelease,
00390   gtcConfigureNotify,
00391   gtcButtonPress,
00392   gtcButtonRelease,
00393 
00394   gtcNOP
00395 };
00396 #endif
00397  

Generated at Sat Jul 14 18:53:52 2001 for GTC by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001