source: abuse/tags/pd/macabuse/inc/netface.hpp

Last change on this file was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
File size: 5.6 KB
Line 
1// structure used to comminicate with the engine
2
3#ifndef __NETFACE_HPP_
4#define __NETFACE_HPP_
5
6#ifdef __MAC__
7#define PACKET_MAX_SIZE 500    // this is a game data packet (udp/ipx)
8#define READ_PACKET_SIZE 500   // this is a file service packet (tcp/spx)
9#else
10#define PACKET_MAX_SIZE 500    // this is a game data packet (udp/ipx)
11#define READ_PACKET_SIZE 500   // this is a file service packet (tcp/spx)
12#endif
13
14#define NET_CRC_FILENAME "#net_crc"
15#define NET_STARTFILE    "netstart.spe"
16#include "indian.hpp"
17#include <string.h>
18
19
20// list of commands for general networking and file services
21
22enum { NFCMD_OPEN,
23       NFCMD_CLOSE,
24       NFCMD_READ,
25       NFCMD_WRITE,
26       NFCMD_SEEK,
27       NFCMD_SIZE,
28       NFCMD_TELL,
29       NFCMD_SET_FS,            // used to set the default (active) filesever
30       NFCMD_CRCS_CALCED,       // engine sends this to driver after crcs are saved
31       NFCMD_REQUEST_LSF,       // engine sends to driver with remote server name, returns 0 for failure or lsf name
32       NFCMD_PROCESS_LSF,       // remote engine sends to driver with lsf name, when get_lsf is set in base_mem
33       NFCMD_REQUEST_ENTRY,     // sent from joining client engine to driver, who then connects as client_abuse
34       NFCMD_BECOME_SERVER,
35       NFCMD_BLOCK,             // used by UNIX version to have engine give it up it's time-slice 
36       NFCMD_RELOAD_START,
37       NFCMD_RELOAD_END,
38       NFCMD_SEND_INPUT,
39       NFCMD_INPUT_MISSING,     // when engine is waiting for input and suspects packets are missing
40       NFCMD_KILL_SLACKERS,     // when the user decides the clients are taking too long to respond
41       EGCMD_DIE
42     };
43
44// client commands
45enum { CLCMD_JOIN_FAILED,
46       CLCMD_JOIN_SUCCESS,     
47       CLCMD_RELOAD_START,           // will you please load netstart.spe
48       CLCMD_RELOAD_END,            // netstart.spe has been loaded, please continue
49       CLCMD_REQUEST_RESEND,        // input didn't arrive, please resend
50       CLCMD_UNJOIN                 // causes server to delete you (addes your delete command to next out packet)
51     } ;
52       
53
54// return codes for NFCMD_OPEN
55enum { NF_OPEN_FAILED,
56       NF_OPEN_LOCAL_FILE,      // should return path to local file as well
57       NF_OPEN_REMOTE_FILE } ;  // returned to engine for a filename
58
59
60// types of clients allowed to connect
61enum { CLIENT_NFS=50,           // client can read one remote files
62       CLIENT_ABUSE,            // waits for entry into a game
63       CLIENT_CRC_WAITER,       // client waits for crcs to be saved
64       CLIENT_LSF_WAITER        // waits for lsf to be transmitted
65       
66     } ;
67
68// base->input_state will be one of the following
69
70enum { INPUT_COLLECTING,       // waiting for driver to receive input from clients/server
71       INPUT_PROCESSING,       // waiting for engine to process input from last tick
72       INPUT_RELOAD,           // server is waiting on clients to reload, process game packets, but don't store them
73       INPUT_NET_DEAD };       // net driver detected an unrecoverable net error, engine should shut down net services
74
75
76
77// the net driver should not use any of these except SCMD_DELETE_CLIENT (0) because
78// they are subject to change
79enum {
80       SCMD_DELETE_CLIENT,
81       SCMD_VIEW_RESIZE,
82       SCMD_SET_INPUT,
83       SCMD_WEAPON_CHANGE,
84       SCMD_END_OF_PACKET,
85       SCMD_RELOAD,
86       SCMD_KEYPRESS,
87       SCMD_KEYRELEASE,
88       SCMD_EXT_KEYPRESS,
89       SCMD_EXT_KEYRELEASE,
90       SCMD_CHAT_KEYPRESS,
91       SCMD_SYNC
92     };
93
94
95struct join_struct
96{
97  int client_id;
98  char name[100];
99  join_struct *next;
100} ;
101
102struct net_packet
103{
104  unsigned char data[PACKET_MAX_SIZE];
105  int packet_prefix_size()                 { return 5; }    // 2 byte size, 2 byte check sum, 1 byte packet order
106  unsigned short packet_size()             { unsigned short size=(*(unsigned short *)data); return lstl(size); }
107  unsigned char tick_received()            { return data[4]; } 
108  void set_tick_received(unsigned char x)  { data[4]=x; }
109  unsigned char *packet_data()             { return data+packet_prefix_size(); }
110  unsigned short get_checksum()            { unsigned short cs=*((unsigned short *)data+1); return lstl(cs); }
111  unsigned short calc_checksum()
112  {
113    *((unsigned short *)data+1)=0;
114    int i,size=packet_prefix_size()+packet_size();
115    unsigned char c1=0,c2=0,*p=data;
116    for (i=0;i<size;i++,p++)
117    {
118      c1+=*p;
119      c2+=c1;
120    }
121    unsigned short cs=( (((unsigned short)c1)<<8) | c2);
122    *((unsigned short *)data+1)=lstl(cs);
123    return cs;
124  }
125
126
127  void packet_reset()    { set_packet_size(0); }     // 2 bytes for size, 1 byte for tick
128 
129  void add_to_packet(void *buf, int size)
130  {
131    if (size && size+packet_size()+packet_prefix_size()<PACKET_MAX_SIZE)
132    {
133      memcpy(data+packet_size()+packet_prefix_size(),buf,size);
134      set_packet_size(packet_size()+size);
135    }
136  }
137  void write_byte(unsigned char x) { add_to_packet(&x,1); }
138  void write_short(unsigned short x) { x=lstl(x); add_to_packet(&x,2); }
139  void write_long(unsigned long x) { x=lltl(x); add_to_packet(&x,4); }
140
141  void set_packet_size(unsigned short x) { *((unsigned short *)data)=lstl(x); }
142
143
144} ;
145
146struct base_memory_struct
147{
148  net_packet packet,                        // current tick data
149             last_packet;                   // last tick data (in case a client misses input, we can resend)
150
151  short mem_lock;
152  short calc_crcs;
153  short get_lsf;
154  short wait_reload;
155  short need_reload;
156  short input_state;            // COLLECTING or PROCESSING
157  short current_tick;           // set by engine, used by driver to confirm packet is not left over
158 
159  join_struct *join_list;
160} ;
161
162
163
164#endif
Note: See TracBrowser for help on using the repository browser.