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

Last change on this file since 682 was 555, checked in by Sam Hocevar, 9 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

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