1 /*
2  * file com_dgram.h - base struct und functions for datagram connections
3  *
4  * $Id: com_dgram.h,v 1.14 2006/02/09 21:21:23 fzago Exp $
5  *
6  * Program XBLAST
7  * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published
11  * by the Free Software Foundation; either version 2; or (at your option)
12  * any later version
13  *
14  * This program is distributed in the hope that it will be entertaining,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details.
18  */
19 #ifndef XBLAST_COM_DGRAM_H
20 #define XBLAST_COM_DGRAM_H
21 
22 /*
23  * macros
24  */
25 #define MAX_DGRAM_BUFFER   32
26 #define LINK_LOST          10L
27 #define NUM_PLAYER_ACTION (GAME_TIME+3)
28 
29 /*
30  * type definitions
31  */
32 typedef struct _xb_comm_dgram XBCommDgram;
33 typedef enum
34 {
35 	XBDI_LOSS,					/* dataloss occurred */
36 	XBDI_CONSUCC,				/* connection success */
37 	XBDI_CONFAIL,				/* connection failure */
38 	XBDI_PARSED,				/* data is completely parsed */
39 	XBDI_FINISH,				/* FINISH received */
40 	XBDI_WRITEERR,				/* write error */
41 	XBDI_IGNORE,				/* data for a game time has been ignored */
42 	XBDI_CLOSE,					/* structure removed */
43 } XBDgramInfo;
44 
45 typedef void (*DgramPingFunc) (XBCommDgram *, unsigned, unsigned short);
46 typedef void (*DgramFinishFunc) (XBCommDgram *);
47 typedef void (*DgramActionFunc) (XBCommDgram *, int, const PlayerAction *);
48 typedef XBBool (*DgramInfoFunc) (XBCommDgram *, XBDgramInfo);
49 
50 /*
51  * local types
52  */
53 typedef struct packed
54 {
55 	size_t numBytes;
56 	unsigned char mask[MAX_MASK_BYTES];
57 	unsigned char action[MAX_PLAYER];
58 	unsigned char pad;
59 } PackedPlayerAction;
60 
61 struct _xb_comm_dgram
62 {
63 	XBComm comm;
64 	unsigned port;				/* target port for connect */
65 	const char *host;			/* expected host name for unconnected */
66 	XBBool connected;			/* connect called or not */
67 	/* extra data for frames */
68 	size_t maskbytes;			/* byte count for action mask */
69 	size_t rcvfirst;			/* first game time for last rcv */
70 	size_t rcvnext;				/* next game time for last rcv */
71 	size_t buffirst;			/* first game time in buffer */
72 	size_t bufnext;				/* next game time to add in buffer */
73 	size_t sndfirst;			/* first game time for last send */
74 	size_t sndnext;				/* next game time for last send */
75 	size_t ignore;				/* ignored gametime, for XBDI_IGNORE */
76 	size_t queue;				/* first  game time for next queuing */
77 	size_t expect;				/* expected game time for next receive */
78 	/* datagram to send on next writeability */
79 	XBDatagram *snd;			/* queued datagram for next send */
80 	/* ping data */
81 	struct timeval lastSnd;		/* time of last send */
82 	struct timeval lastRcv;		/* time of last receive */
83 	/* handlers */
84 	DgramPingFunc pingFunc;		/* ping handling */
85 	DgramActionFunc actionFunc;	/* action handling */
86 	DgramInfoFunc infoFunc;		/* info handling */
87 	/* received player actions per game time */
88 	PackedPlayerAction ppa[NUM_PLAYER_ACTION];
89 };
90 
91 /*
92  * global prototypes
93  */
94 extern XBComm *Dgram_CommInit (XBCommDgram *, XBCommType, XBSocket *, DgramPingFunc, DgramInfoFunc,
95 							   DgramActionFunc);
96 extern unsigned short Dgram_Port (const XBCommDgram *);
97 extern void Dgram_Reset (XBCommDgram *);
98 extern void Dgram_SetMaskBytes (XBCommDgram *, unsigned);
99 extern void Dgram_SendPing (XBCommDgram *);
100 extern void Dgram_SendPingData (XBCommDgram *, const int pingData[]);
101 extern void Dgram_SendPlayerAction (XBCommDgram *, int, const PlayerAction *);
102 extern void Dgram_SendFinish (XBCommDgram *, int gameTime);
103 extern void Dgram_SendChat (XBCommDgram *, unsigned, unsigned, const char *);
104 extern XBBool Dgram_Flush (XBCommDgram * dgram);
105 
106 #endif
107 /*
108  * end of file com_dgram.h
109  */
110