1 /* -*- Mode: C++; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*-
2  *
3  * Quadra, an action puzzle game
4  * Copyright (C) 1998-2000  Ludus Design
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef _HEADER_NET_BUF
22 #define _HEADER_NET_BUF
23 
24 #include <string.h>
25 #include "types.h"
26 #include "net.h"
27 
28 class Net_buf {
29 public:
30 	Byte *point;
31 	Net_connection *from;
32 	Dword from_addr;
33 	Byte buf[NETBUF_SIZE];
write_dword(Dword v)34 	void write_dword(Dword v) {
35     *point++ = (v >> 24) & 0xff;
36     *point++ = (v >> 16) & 0xff;
37     *point++ = (v >> 8)  & 0xff;
38     *point++ = v & 0xff;
39 	}
write_word(Word v)40 	void write_word(Word v) {
41     *point++ = (v >> 8)  & 0xff;
42     *point++ = v & 0xff;
43 	}
write_byte(Byte v)44 	void write_byte(Byte v) {
45 		*(Byte *) point = v;
46 		point += sizeof(Byte);
47 	}
write_bool(bool b)48 	void write_bool(bool b) {
49 		write_byte(b? 1:0);
50 	}
write_mem(const void * v,int num)51 	void write_mem(const void *v, int num) {
52 		memcpy(point, v, num);
53 		point += num;
54 	}
write_string(const char * v)55 	void write_string(const char *v) {
56 		write_mem(v, strlen(v)+1); // write a string with its '0'
57 	}
read_dword()58 	Dword read_dword() {
59 		if(len() <= NETBUF_SIZE-sizeof(Dword)) {
60       Dword ret;
61       ret = *point << 24; point++;
62       ret |= *point << 16; point++;
63       ret |= *point << 8; point++;
64       ret |= *point; point++;
65       return ret;
66 		}
67 		else
68 			return 0;
69 	}
read_word()70 	Word read_word() {
71 		if(len() <= NETBUF_SIZE-sizeof(Word)) {
72       Dword ret;
73       ret = *point << 8; point++;
74       ret |= *point; point++;
75 			return ret;
76 		}
77 		else
78 			return 0;
79 	}
read_byte()80 	Byte read_byte() {
81 		if(len() <= NETBUF_SIZE-sizeof(Byte)) {
82 			Byte ret = *(Byte *) point;
83 			point += sizeof(Byte);
84 			return ret;
85 		}
86 		else
87 			return 0;
88 	}
read_bool()89 	bool read_bool() {
90 		if(read_byte())
91 			return true;
92 		else
93 			return false;
94 	}
read_mem(void * v,int num)95 	void read_mem(void *v, int num) {
96 		if(len() <= NETBUF_SIZE - num) {
97 			memcpy(v, point, num);
98 			point += num;
99 		}
100 		else
101 			memset(v, 0, num);
102 	}
read_string(char * v,int size)103 	bool read_string(char *v, int size) { // read a string with its '0'
104 		do {
105 			char c=(char)read_byte();
106 			if(c>0 && c<' ')
107 				c=' ';
108 			*v=c;
109 			if(!*v)
110 				return true;
111 			v++;
112 			size--;
113 		} while(size);
114 		if(v[-1]) {
115 			v[-1]=0;
116 			return false;
117 		}
118 		return true;
119 	}
Net_buf()120 	Net_buf() {
121 		reset();
122 		from=NULL;
123 		memset(buf, 0, sizeof(buf));
124 	}
reset()125 	void reset() {
126 		point = buf;
127 	}
len()128 	const unsigned int len() const {
129 		return point - buf;
130 	}
131 };
132 
133 #endif
134