File: servemsg.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (142 lines) | stat: -rw-r--r-- 3,643 bytes parent folder | download | duplicates (2)
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
/**
 ** Servemsg.cc - Server msgs.
 ** NOTE:   This is for inclusion by both client and server.
 **
 ** Written: 5/28/2001 - JSF
 **/

/*
Copyright (C) 2001-2022 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "servemsg.h"

#include "ignore_unused_variable_warning.h"

#include <unistd.h>

#include <cstring>
#include <iostream> /* For debugging msgs. */

#ifdef _WIN32
#	include "servewin32.h"
#endif

using std::cout;
using std::endl;

namespace Exult_server {

	/*
	 *  Send data.
	 *
	 *  Output: -1 if error.
	 */

	int Send_data(
			int socket, Msg_type id, const unsigned char* data, int datalen) {
#ifdef USE_EXULTSTUDIO
		unsigned char buf[maxlength + hdrlength];
		buf[0] = magic & 0xff;    // Store magic (low-byte first).
		buf[1] = (magic >> 8) & 0xff;
		buf[2] = datalen & 0xff;    // Data length.
		buf[3] = (datalen >> 8) & 0xff;
		buf[4] = id;
		if (datalen > 0) {
			std::memcpy(&buf[5], data, datalen);    // The data itself.
		}
		const int len = datalen + hdrlength;

		return write(socket, buf, len) == len ? 0 : -1;
#else  /* USE_EXULTSTUDIO */
		ignore_unused_variable_warning(socket, id, data, datalen);
		return -1;
#endif /* USE_EXULTSTUDIO */
	}

	/*
	 *  Read message from client.
	 *
	 *  Output: Length of data, else -1.
	 */

	int Receive_data(
			int&           socket,    // Closed, set to -1 if disconnected.
			Msg_type&      id,        // ID returned.
			unsigned char* data, int datalen) {
#ifdef USE_EXULTSTUDIO
		unsigned char buf[hdrlength];
		const int     len = read(socket, buf, 2);    // Get magic.
		if (!len) {                                  // Closed?
			close(socket);
			socket = -1;
			return -1;
		}
		if (len == -1) {    // Nothing available?
			return -1;
		}
		const int magic = buf[0] + (buf[1] << 8);
		if (magic != Exult_server::magic) {
			cout << "Bad magic read" << endl;
			return -1;
		}
		if (read(socket, buf, 3) != 3) {
			cout << "Couldn't read length+type" << endl;
			return -1;
		}
		const int dlen = buf[0] | (buf[1] << 8);
		// Message type.
		id = static_cast<Exult_server::Msg_type>(buf[2]);
		if (dlen > Exult_server::maxlength || dlen > datalen) {
			cout << "Length " << datalen << " exceeds max" << endl;
			//+++++++++Eat the chars.
			return -1;
		}
		datalen = read(socket, data, dlen);    // Read data.

		if (datalen < dlen) {
			cout << "Failed to read all " << dlen << " bytes" << endl;
			return -1;
		}
		return datalen;
#else  /* USE_EXULTSTUDIO */
		ignore_unused_variable_warning(socket, id, data, datalen);
		return -1;
#endif /* USE_EXULTSTUDIO */
	}

	bool wait_for_response(int socket, int ms) {
		ignore_unused_variable_warning(socket, ms);
#if defined(_WIN32) && defined(USE_EXULTSTUDIO)
		/*
		int ticks = GetTickCount();
		while(GetTickCount() < ticks+ms) {
			if (peek_pipe() > 0) return true;
			SleepEx(1, TRUE);
		}
		if (peek_pipe() > 0) return true;
		return false;
		*/
#endif
		return true;
	}

}    // namespace Exult_server