1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Scorched3D 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
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <coms/ComsConnectAuthMessage.h>
22 #include <common/Defines.h>
23
24 ComsMessageType ComsConnectAuthMessage::ComsConnectAuthMessageType("ComsConnectAuthMessageType");
25
ComsConnectAuthMessage()26 ComsConnectAuthMessage::ComsConnectAuthMessage() :
27 ComsMessage(ComsConnectAuthMessageType)
28 {
29
30 }
31
~ComsConnectAuthMessage()32 ComsConnectAuthMessage::~ComsConnectAuthMessage()
33 {
34
35 }
36
setNoPlayers(unsigned int players)37 void ComsConnectAuthMessage::setNoPlayers(unsigned int players)
38 {
39 char buf[10]; snprintf(buf, 10, "%u", players); setValue("numplayers", buf);
40 }
41
setCompatabilityVer(unsigned int compatver)42 void ComsConnectAuthMessage::setCompatabilityVer(unsigned int compatver)
43 {
44 char buf[10]; snprintf(buf, 10, "%u", compatver); setValue("compatver", buf);
45 }
46
writeMessage(NetBuffer & buffer)47 bool ComsConnectAuthMessage::writeMessage(NetBuffer &buffer)
48 {
49 buffer.addToBuffer((unsigned int) values_.size());
50 std::map<std::string, std::string>::iterator itor;
51 for (itor = values_.begin();
52 itor != values_.end();
53 ++itor)
54 {
55 buffer.addToBuffer((*itor).first.c_str());
56 buffer.addToBuffer((*itor).second.c_str());
57 }
58
59 return true;
60 }
61
readMessage(NetBufferReader & reader)62 bool ComsConnectAuthMessage::readMessage(NetBufferReader &reader)
63 {
64 unsigned int noV = 0;
65 values_.clear();
66 if (!reader.getFromBuffer(noV)) return false;
67 for (unsigned int i=0; i<noV; i++)
68 {
69 std::string name, value;
70
71 if (!reader.getFromBuffer(name)) return false;
72 if (!reader.getFromBuffer(value)) return false;
73
74 // Validate the user strings
75 if (strlen(value.c_str()) > 50)
76 {
77 ((char *)value.c_str())[50] = '\0';
78 }
79
80 values_[name] = value;
81 }
82
83 return true;
84 }
85
getValue(const char * name)86 const char *ComsConnectAuthMessage::getValue(const char *name)
87 {
88 std::map<std::string, std::string>::iterator itor =
89 values_.find(name);
90 if (itor == values_.end()) return "";
91
92 return (*itor).second.c_str();
93 }
94
setValue(const char * name,const char * value)95 void ComsConnectAuthMessage::setValue(const char *name, const char *value)
96 {
97 values_[name] = value;
98 }
99
100