File: proto.c

package info (click to toggle)
s3d 0.2.2.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,356 kB
  • sloc: ansic: 21,128; python: 488; perl: 98; makefile: 31; sh: 29
file content (412 lines) | stat: -rw-r--r-- 11,367 bytes parent folder | download | duplicates (3)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SPDX-License-Identifier: GPL-2.0-or-later
/* SPDX-FileCopyrightText: 2004-2015  Simon Wunderlich <sw@simonwunderlich.de>
 */

#include "global.h"
#include "proto.h"
#include <stdlib.h>		/*  malloc() */
#include <string.h>		/*  strncpy(),memset() */
#include <stdint.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>		/*  htonl(),htons() */
#endif
/*  this code should do the protocol work .... */
/*  */
int focus_oid = -1;		/*  initially focus the pid */
/*  handle an incoming command from the client .. */

int prot_com_in(struct t_process *p, uint8_t * pbuf)
{
	uint8_t command;
	int i;
	char name[S3D_NAME_MAX];
	struct t_process *np;
	uint8_t *buf, *cptr = NULL;
	uint16_t length;
	uint16_t num;
	uint16_t w, h, x, y;
	uint32_t oid, toid;
	uint8_t type;
	int32_t flags, mcp_oid = -1;
	command = pbuf[0];
	if (p->id != 0) {
		mcp_oid = p->mcp_oid;	/*  get mcp-oid if we need to report something to */
		/*  the mcp */
		if ((mcp_oid == -1) && (command != S3D_P_C_INIT)) {
			s3dprintf(MED, "prot_com_in(): commands without beeing initialized ?! no way, kicking ...");
			event_quit(p);
		}
	}
	length = ntohs(*((uint16_t *) ((uint8_t *) pbuf + 1)));
	cptr = buf = pbuf + sizeof(int_least32_t);
	switch (command) {
	case S3D_P_C_INIT:
		memset(name, 0, S3D_NAME_MAX);
		if (length > S3D_NAME_MAX)
			i = S3D_NAME_MAX;
		else
			i = length;
		strncpy(name, (char *)buf, i);
		name[sizeof(name) - 1] = '\0';
		s3dprintf(LOW, "[%d]\"%s\" logged in", p->id, name);
		if (NULL == (np = process_protinit(p, name)))
			event_quit(p);	/*  couldn't get process */
		else
			event_init(np);
		break;
	case S3D_P_C_NEW_OBJ:
		oid = htonl(obj_new(p));
		prot_com_out(p, S3D_P_S_NEWOBJ, (uint8_t *) & oid, 4);
		break;
	case S3D_P_C_DEL_OBJ:
		if (length == 4) {
			oid = ntohl(*((uint32_t *) cptr));
			obj_del(p, oid);
		}
		break;
	case S3D_P_C_CLONE:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			obj_clone_change(p, oid, toid);
		}
		break;
	case S3D_P_C_LINK:
		if (length == 4) {
			oid = ntohl(*((uint32_t *) cptr));
			obj_unlink(p, oid);
		}
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			obj_link(p, oid, toid);
		}
		break;
	case S3D_P_C_QUIT:
		s3dprintf(LOW, "QUIT issued");
		event_quit(p);
		break;
	case S3D_P_C_PUSH_VERTEX:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 3);
			ntohfb((float *)cptr, num * 3);
			obj_push_vertex(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PUSH_MAT:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 12);
			ntohfb((float *)cptr, num * 12);
			obj_push_mat(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PUSH_POLY:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 4);
			ntohlb((uint32_t *) cptr, num * 4);
			/*  convert index names */
			obj_push_poly(p, oid, (uint32_t *) cptr, num);
		}
		break;
	case S3D_P_C_PUSH_LINE:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 3);
			s3dprintf(VLOW, "received %d new lines for object oid...%d", num, oid);
			ntohlb((uint32_t *) cptr, num * 3);
			/*  convert index names */
			obj_push_line(p, oid, (uint32_t *) cptr, num);
		}
		break;
	case S3D_P_C_PUSH_TEX:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (2 * 2);
			s3dprintf(LOW, "received %d new textures for object oid...%d", num, oid);
			ntohsb((uint16_t *) cptr, num * 2);
			/*  convert index names */
			obj_push_tex(p, oid, (uint16_t *) cptr, num);
		}
		break;
	case S3D_P_C_PEP_POLY_NORMAL:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (9 * 4);
			ntohfb((float *)cptr, num * 9);
			s3dprintf(VLOW, "PEP_POLY_NORMAL[%d]: oid %d, %f polys", length, oid, (length - 4) / (9.0 * 4.0));
			obj_pep_poly_normal(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PEP_LINE_NORMAL:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (6 * 4);
			ntohfb((float *)cptr, num * 6);
			s3dprintf(VLOW, "PEP_LINE_NORMAL[%d]: oid %d, %.1f lines", length, oid, (length - 4) / (6.0 * 4.0));
			obj_pep_line_normal(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PEP_POLY_TEXC:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (6 * 4);
			ntohfb((float *)cptr, num * 6);
			s3dprintf(VLOW, "PEP_POLY_TEXC[%d]: oid %d, %f polys", length, oid, (length - 4) / (6.0 * 4.0));
			obj_pep_poly_texc(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PEP_MAT:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 12);
			ntohfb((float *)cptr, num * 12);
			s3dprintf(VLOW, "PEP_MAT[%d]: %d materials for object oid...%d", length, num, oid);
			obj_pep_mat(p, oid, (float *)cptr, num);
		}
		break;
	case S3D_P_C_PEP_VERTEX:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 3);
			ntohfb((float *)cptr, num * 3);
			s3dprintf(VLOW, "pepping %d new vertices for object oid...%d", num, oid);
			obj_pep_vertex(p, oid, (float *)cptr, num);
		}
		break;

	case S3D_P_C_PEP_MAT_TEX:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4);
			s3dprintf(VLOW, "PEP_MAT_TEX[%d]: %d materials for object oid...%d", length, num, oid);
			ntohlb((uint32_t *) cptr, num);
			obj_pep_mat_tex(p, oid, (uint32_t *) cptr, num);
		}
		break;
	case S3D_P_C_PEP_LINE:
		if (length > 4) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 4) / (4 * 3);
			s3dprintf(VLOW, "pepping %d new lines for object oid...%d", num, oid);
			ntohlb((uint32_t *) cptr, num * 3);
			obj_pep_line(p, oid, (uint32_t *) cptr, num);
		}
		break;
	case S3D_P_C_LOAD_LINE_NORMAL:
		if (length > 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 8) / (6 * 4);
			ntohfb((float *)cptr, num * 6);
			s3dprintf(VLOW, "LOAD_POLY_NORMAL[%d]: oid %d, %.2f lines", length, oid, (length - 8) / (6.0 * 4.0));
			obj_load_line_normal(p, oid, (float *)cptr, toid, num);
		}
		break;
	case S3D_P_C_LOAD_POLY_NORMAL:
		if (length > 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 8) / (9 * 4);
			ntohfb((float *)cptr, num * 9);
			s3dprintf(MED, "LOAD_POLY_NORMAL[%d]: oid %d, %f polys", length, oid, (length - 8) / (9.0 * 4.0));
			obj_load_poly_normal(p, oid, (float *)cptr, toid, num);
		}
		break;
	case S3D_P_C_LOAD_POLY_TEXC:
		if (length > 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 8) / (6 * 4);
			ntohfb((float *)cptr, num * 6);
			s3dprintf(MED, "LOAD_POLY_TEXC[%d]: oid %d, %f polys", length, oid, (length - 8) / (6.0 * 4.0));
			obj_load_poly_texc(p, oid, (float *)cptr, toid, num);
		}
		break;
	case S3D_P_C_LOAD_MAT:
		if (length > 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			num = (length - 8) / (4 * 12);
			ntohfb((float *)cptr, num * 12);
			s3dprintf(LOW, "LOAD_MAT[%d]: %d materials for object oid...%d", length, num, oid);
			obj_load_mat(p, oid, (float *)cptr, toid, num);
		}
		break;
	case S3D_P_C_LOAD_TEX:
		if (length > 16) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			toid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			x = ntohs(*((uint16_t *) cptr));
			cptr += 2;
			y = ntohs(*((uint16_t *) cptr));
			cptr += 2;
			w = ntohs(*((uint16_t *) cptr));
			cptr += 2;
			h = ntohs(*((uint16_t *) cptr));
			cptr += 2;
			num = length - 16;
			s3dprintf(MED, "LOAD_TEX[%d]: oid %d, texture %d, [%d x %d] data at [%d x %d] (%d = %d)", length, oid, toid, w, h, x, y, num, w * h * 4);
			if ((w * h * 4) == num)	/*  check correct size */
				obj_load_tex(p, oid, toid, x, y, w, h, cptr);
		}
		break;
	case S3D_P_C_UPDATE_TEX:
		if (length == 16) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			toid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			x = ntohs(*((uint16_t *) cptr));
			cptr += sizeof(uint16_t);
			y = ntohs(*((uint16_t *) cptr));
			cptr += sizeof(uint16_t);
			w = ntohs(*((uint16_t *) cptr));
			cptr += sizeof(uint16_t);
			h = ntohs(*((uint16_t *) cptr));

			s3dprintf(VLOW, "UPDATE_TEX[%d]: oid %d, texture %d, [%d x %d] data at [%d x %d] ", length, oid, toid, w, h, x, y);
			obj_update_tex(p, oid, toid, x, y, w, h, NULL);
		}
		break;

	case S3D_P_C_DEL_VERTEX:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			num = ntohl(*((uint32_t *) cptr));

			obj_del_vertex(p, oid, num);
		}
		break;
	case S3D_P_C_DEL_POLY:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			num = ntohl(*((uint32_t *) cptr));

			obj_del_poly(p, oid, num);
		}
		break;
	case S3D_P_C_DEL_LINE:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			num = ntohl(*((uint32_t *) cptr));

			s3dprintf(VLOW, "deleting %d lines for object oid...%d", num, oid);
			obj_del_line(p, oid, num);
		}
		break;

	case S3D_P_C_DEL_MAT:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			num = ntohl(*((uint32_t *) cptr));

			obj_del_mat(p, oid, num);
		}
		break;
	case S3D_P_C_DEL_TEX:
		if (length == 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			num = ntohl(*((uint32_t *) cptr));

			obj_del_tex(p, oid, num);
		}
		break;
	case S3D_P_C_TOGGLE_FLAGS:
		if (length == 9) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);;
			type = *cptr;
			cptr += sizeof(unsigned char);
			flags = ntohl(*((uint32_t *) cptr));

			obj_toggle_flags(p, oid, type, flags);
		}
		break;
	case S3D_P_C_TRANSLATE:
		if (length >= 16) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += sizeof(uint32_t);
			ntohfb((float *)cptr, 3);
			obj_translate(p, oid, (float *)cptr);
		}
		break;
	case S3D_P_C_ROTATE:
		if (length >= 16) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			ntohfb((float *)cptr, 3);
			obj_rotate(p, oid, (float *)cptr);
		}
		break;
	case S3D_P_C_SCALE:
		if (length >= 8) {
			oid = ntohl(*((uint32_t *) cptr));
			cptr += 4;
			ntohfb((float *)cptr, 3);
			obj_scale(p, oid, *((float *)cptr));
		}
		break;
	case S3D_P_MCP_FOCUS:
		if ((p->id == MCP) && (length == 4)) {
			oid = ntohl(*((uint32_t *) cptr));
			mcp_focus(oid);
		}
		break;
	default:
		s3dprintf(LOW, "don't know this command (%d)", command);
	}
	return 0;
}

/*  this pushes some buffer out on the wire... */
int prot_com_out(struct t_process *p, uint8_t opcode, uint8_t * buf, uint16_t length)
{
	uint8_t *ptr;
	if (p->con_type != CON_NULL) {
		*(obuf) = opcode;
		ptr = obuf + 1;
		*((uint16_t *) ptr) = htons(length);
		if (length)
			memcpy(obuf + 3, buf, length);
		if (n_writen(p, obuf, length + 3) < 0) {
			s3dprintf(LOW, "prot_com_out():n_writen(): connection seems to be dead (pid %d)", p->id);
			process_del(p->id);
		}
		return 0;
	}
	return -1;
}