File: NetworkMessage.cpp

package info (click to toggle)
blobby 1.1.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,944 kB
  • sloc: cpp: 22,442; xml: 779; python: 56; makefile: 3
file content (129 lines) | stat: -rw-r--r-- 3,920 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
/*=============================================================================
Blobby Volley 2
Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)

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
=============================================================================*/

/* header include */
#include "NetworkMessage.h"

/* includes */
#include <cstring>
#include <ostream>
#include <cassert>

#include "UserConfig.h"
#include "PlayerIdentity.h"

/* implementation */
ServerInfo::ServerInfo(RakNet::BitStream& stream, const char* ip, uint16_t p)
{
	strncpy(hostname, ip, sizeof(hostname));
	hostname[sizeof(hostname) - 1] = 0;

	port = p;

	stream.Read(activegames);
	stream.Read(name, sizeof(name));
	stream.Read(waitingplayers);
	stream.Read(description, sizeof(description));
}

ServerInfo::ServerInfo(const IUserConfigReader& config)
{
	/// \todo we only need a config reader here!

	// default values
	std::string n = "Blobby Volley 2 Server";
	std::string d = "no description available";

	activegames = 0;
	memset(hostname, 0, sizeof(hostname));
	memset(name, 0, sizeof(name));
	memset(description, 0, sizeof(description));
	waitingplayers = 0;

	std::string tmp;
	tmp = config.getString("name", n);
	strncpy(name, tmp.c_str(), sizeof(name) - 1);
	tmp = config.getString("description", d);
	strncpy(description, tmp.c_str(), sizeof(description) - 1);
	port = config.getInteger("port", BLOBBY_PORT);
}

ServerInfo::ServerInfo(const std::string& playername)
{
	activegames = 0;
	memset(hostname, 0, sizeof(hostname));
	memset(name, 0, sizeof(name));
	memset(description, 0, sizeof(description));
	waitingplayers = 0;

	std::strncpy(hostname, "localhost", sizeof(hostname));
	port = BLOBBY_PORT;

	std::strncpy(name, std::string(playername + "'s game").c_str(), sizeof(name) - 1);
	std::strncpy(description, "client hosted game", sizeof(description) - 1);
}

void ServerInfo::writeToBitstream(RakNet::BitStream& stream)
{
	stream.Write(activegames);
	stream.Write(name, sizeof(name));
	stream.Write(waitingplayers);
	stream.Write(description, sizeof(description));
	assert( stream.GetNumberOfBytesUsed() == static_cast<int>(BLOBBY_SERVER_PRESENT_PACKET_SIZE) );
}

const size_t ServerInfo::BLOBBY_SERVER_PRESENT_PACKET_SIZE = sizeof((unsigned char)ID_BLOBBY_SERVER_PRESENT)
		+ 2 * sizeof(int) 	// activegames &  waiting players
		+ 32				// name
		+ 192;			// description



void makeEnterServerPacket(RakNet::BitStream& stream, const PlayerIdentity& player)
{
	stream.Write((unsigned char)ID_ENTER_SERVER);

	// Send preferred side
	stream.Write( player.getPreferredSide() );

	// Send playername
	char myname[16];
	strncpy(myname, player.getName().c_str(), sizeof(myname));
	stream.Write(myname, sizeof(myname));

	// send color settings
	stream.Write(player.getStaticColor().toInt());
}



bool operator == (const ServerInfo& lval, const ServerInfo& rval)
{
	// check if ip and port are identical!
	/// \todo maybe we should user raknets pladerId directly?
	return lval.port == rval.port && !strncmp(lval.hostname, rval.hostname,
			sizeof(lval.hostname));
}

std::ostream& operator<<(std::ostream& stream, const ServerInfo& val)
{
	return stream << val.name << " (" << val.hostname << ":" << val.port << ")";
}