ComsConnectAuthMessage.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <coms/ComsConnectAuthMessage.h>
00022 #include <common/Defines.h>
00023 
00024 ComsConnectAuthMessage::ComsConnectAuthMessage()
00025         : ComsMessage("ComsConnectAuthMessage")
00026 {
00027 
00028 }
00029 
00030 ComsConnectAuthMessage::~ComsConnectAuthMessage()
00031 {
00032 
00033 }
00034 
00035 void ComsConnectAuthMessage::setNoPlayers(unsigned int players)
00036 {
00037         char buf[10]; snprintf(buf, 10, "%u", players); setValue("numplayers", buf);
00038 }
00039 
00040 void ComsConnectAuthMessage::setCompatabilityVer(unsigned int compatver)
00041 {
00042         char buf[10]; snprintf(buf, 10, "%u", compatver); setValue("compatver", buf);
00043 }
00044 
00045 bool ComsConnectAuthMessage::writeMessage(NetBuffer &buffer)
00046 {
00047         buffer.addToBuffer((unsigned int) values_.size());
00048         std::map<std::string, std::string>::iterator itor;
00049         for (itor = values_.begin();
00050                 itor != values_.end();
00051                 itor++)
00052         {
00053                 buffer.addToBuffer((*itor).first.c_str());
00054                 buffer.addToBuffer((*itor).second.c_str());
00055         }
00056 
00057         return true;
00058 }
00059 
00060 bool ComsConnectAuthMessage::readMessage(NetBufferReader &reader)
00061 {
00062         unsigned int noV = 0;
00063         values_.clear();
00064         if (!reader.getFromBuffer(noV)) return false;
00065         for (unsigned int i=0; i<noV; i++)
00066         {
00067                 std::string name, value;
00068 
00069                 if (!reader.getFromBuffer(name)) return false;
00070                 if (!reader.getFromBuffer(value)) return false;
00071 
00072                 // Validate the user strings
00073                 if (strlen(value.c_str()) > 50)
00074                 {
00075                         ((char *)value.c_str())[50] = '\0';
00076                 }
00077 
00078                 values_[name] = value;
00079         }
00080 
00081         return true;
00082 }
00083 
00084 const char *ComsConnectAuthMessage::getValue(const char *name)
00085 {
00086         std::map<std::string, std::string>::iterator itor =
00087                 values_.find(name);
00088         if (itor == values_.end()) return "";
00089 
00090         return (*itor).second.c_str();
00091 }
00092 
00093 void ComsConnectAuthMessage::setValue(const char *name, const char *value)
00094 {
00095         values_[name] = value;
00096 }
00097 

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3