source: abuse/trunk/src/netface.h @ 494

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

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