Shadowrun: Awakened 29 September 2011 - Build 871
NetworkHelper.h
Go to the documentation of this file.
00001 /*
00002  * NetworkHelper - A couple of helper methods.
00003  *
00004 * @Author Michael <Tharbas> Matzen
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU General Public License as published by the Free
00008  * Software Foundation; either version 2 of the License, or (at your option)
00009  * any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00014  * more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along with
00017  * this program; if not, write to the Free Software Foundation, Inc., 51
00018  * Franklin St, Fifth Floor, Boston, MA 02110, USA
00019  * 
00020  */
00021 #pragma once
00022 
00023 #include "StringCompressor.h"
00024 #include "SraNetworkShared.h"
00025 #include <locale>
00026 #include <iostream>
00027 #include <string>
00028 #include <sstream>
00029 #include <RakString.h>
00030 
00031 using namespace std;
00032 
00033 namespace SraNetwork
00034 {
00035     class Helpers
00036     {
00037     public:
00038 
00042         static void GetWStringFromBuffer( const char* buffer, const size_t strlen, wchar_t* target)
00043         {
00044             wostringstream wstream;
00045 
00046             // Create the facet
00047             const ctype<wchar_t>& facet =
00048                 use_facet< ctype<wchar_t> >( wstream.getloc() );
00049 
00050             // Now copy the characters
00051             for (size_t idx = 0; idx < strlen; ++idx)
00052             {
00053                 wstream << facet.widen( buffer[idx] );
00054             }
00055 
00056             // And copy to the output
00057             wcscpy( target, wstream.str().c_str());
00058         }
00059 
00060         static RakNet::RakString GetStringFromBuffer(RakNet::BitStream* stream)
00061         {
00062             RakNet::RakString str = "";
00063             RakNet::StringCompressor::Instance()->DecodeString(&str, MAX_STRLEN, stream);           
00064         }
00065 
00066         static int GetIntFromBuffer( const char* buffer, int* offset )
00067         {
00068             int result = 0;
00069             memcpy( &result, buffer+ *offset, sizeof(result));
00070             *offset += sizeof(result);
00071             return result;
00072         }
00073 
00077         static void StoreWStringInBuffer( const wchar_t* source, char* target)
00078         {
00079             ostringstream ststream;
00080             
00081             // Create the facet
00082             const ctype<char>& facet =
00083                 use_facet< ctype<char> >( ststream.getloc() );
00084 
00085             // Now copy the characters
00086             for (size_t idx = 0; idx < wcslen(source); ++idx)
00087             {
00088                 ststream << facet.narrow( source[idx], 0 );
00089             }
00090 
00091             // And copy to the output
00092             strcpy( target, ststream.str().c_str());
00093         }
00094 
00098         static int StoreStringInBuffer( const std::string source, RakNet::BitStream* stream)
00099         {
00100              RakNet::StringCompressor::Instance()->EncodeString(
00101                  source.c_str(), MAX_STRLEN, stream);
00102         }
00103 
00104         static int StoreIntInBuffer( const int source, char* target)
00105         {
00106             memcpy(target, &source, sizeof(source));
00107             return sizeof(source);
00108         }
00109     };
00110 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net