-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cc
319 lines (291 loc) · 5.11 KB
/
client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
client.cc
(c) Richard Thrippleton
Licensing terms are in the 'LICENSE' file
If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/
#include <string.h>
#include "calc.h"
#include "sockhelper.h"
#include "protocol.h"
#include "sound.h"
#include "presence.h"
#include "camera.h"
#include "error.h"
#include "interface.h"
#include "graphic.h"
#include "client.h"
void client::init()
{
sock=NULL;
hlpr=NULL;
edit=false;
}
void client::stop()
{
if(sock)
{
SDLNet_TCP_Close(sock);
sock=NULL;
}
if(hlpr)
{
delete hlpr;
hlpr=NULL;
}
}
void client::connect(char* host)
{
IPaddress serv; //Server address
SDLNet_ResolveHost(&serv,host,PORT);
if(serv.host==INADDR_NONE)
throw error("Couldn't resolve hostname");
sock=SDLNet_TCP_Open(&serv);
if(!sock)
throw error("Could not connect to server");
hlpr=new sockhelper(sock);
hlpr->send((unsigned char*)SIGN,strlen(SIGN));
btck=0;
}
void client::flush()
{
hlpr->pump();
}
void client::poll()
{
unsigned char otyp; //Incoming object type
unsigned char* buf; //Incoming buffer to use
sound* snd; //Sound to play
graphic* cspr; //Console sprite
int n; //Array subscript to access
long cbnd; //Client bandwidth usage to report
presence* from; //Sound source if applicable
int len; //Length of console/message text if applicable
char txt[1025]; //Text to print to console
bool exl; //Loop exiting flag
hlpr->pump();
btck=(btck+1)%24;
if(btck==0)
{
cbnd=hlpr->getcount();
action(CLIENT_BANDWIDTH,cbnd);
}
exl=false;
while(!exl && hlpr->request(1))
{
otyp=*hlpr->request(1);
switch(otyp)
{
case SERV_SELF:
buf=hlpr->request(SERV_SELF_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_NEW:
buf=hlpr->request(SERV_NEW_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_NAME:
buf=hlpr->request(SERV_NAME_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_UPD:
buf=hlpr->request(SERV_UPD_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_DEL:
buf=hlpr->request(SERV_DEL_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_HILIGHT:
buf=hlpr->request(SERV_HILIGHT_SZ);
if(buf)
{
presence::feed(buf);
hlpr->suck();
}
else
exl=true;
break;
case SERV_SND:
buf=hlpr->request(SERV_SND_SZ);
if(buf)
{
snd=sound::get(calc::dattoint(buf+1));
if(snd)
snd->play(1);
hlpr->suck();
}
else
exl=true;
break;
case SERV_NOISE:
buf=hlpr->request(SERV_NOISE_SZ);
if(buf)
{
snd=sound::get(calc::dattoint(buf+1));
n=calc::dattoint(buf+3);
if(n>=0 && n<presence::ISIZE)
from=presence::get(n);
else
from=NULL;
if(snd && from)
camera::noise(snd,from);
hlpr->suck();
}
else
exl=true;
break;
case SERV_SHAKE:
buf=hlpr->request(SERV_SHAKE_SZ);
if(buf)
{
camera::shake(calc::dattoint(buf+1));
hlpr->suck();
}
else
exl=true;
break;
case SERV_CONS:
buf=hlpr->request(3);
if(buf)
{
edit=false;
len=calc::dattoint(buf+1);
if(len>1024)
throw error("Overflow from server in SERV_CONS");
buf=hlpr->request(3+len);
if(buf)
{
memcpy(txt,buf+3,len);
txt[len]='\0';
interface::printtocons("%s\n",txt);
hlpr->suck();
}
else
exl=true;
}
else
exl=true;
break;
case SERV_READLN:
buf=hlpr->request(SERV_READLN_SZ);
if(buf)
{
edit=true;
if(buf[1])
hide=true;
else
hide=false;
hlpr->suck();
}
else
exl=true;
break;
case SERV_MESG:
buf=hlpr->request(3);
if(buf)
{
len=calc::dattoint(buf+1);
if(len>128)
throw error("Overflow from server in SERV_MESG");
buf=hlpr->request(3+len);
if(buf)
{
memcpy(txt,buf+3,len);
txt[len]='\0';
interface::printtomesg("%s",txt);
hlpr->suck();
}
}
else
exl=true;
break;
case SERV_CSPR:
buf=hlpr->request(3);
if(buf)
{
cspr=graphic::get(calc::dattoint(buf+1));
if(cspr)
interface::spritetocons(cspr);
hlpr->suck();
}
else
exl=true;
break;
case SERV_FLOOD:
buf=hlpr->request(SERV_FLOOD_SZ);
if(buf)
{
hlpr->suck();
}
hlpr->pump();
break;
default:
error::debug("Strange default packet",otyp);
break;
}
}
hlpr->pump();
if(edit)
readln();
}
void client::action(int typ,long opr)
{
unsigned char buf[3]; //Outgoing buffer
if(opr>32767)
opr=32767;
if(opr<-32766)
opr=-32766;
buf[0]=(unsigned char)typ;
calc::inttodat(opr,buf+1);
hlpr->send(buf,3);
}
void client::readln()
{
char txt[65]; //Readline text
if(interface::getline(txt,hide))
{
edit=false;
for(int i=0;i<65;i++)
{
action(CLIENT_CHAR,txt[i]);
if(txt[i]=='\0')
break;
}
}
}
bool client::edit;
bool client::hide;
TCPsocket client::sock;
sockhelper* client::hlpr;
int client::btck;