source: abuse/trunk/src/netface.hpp @ 97

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