Changeset 1426

Show
Ignore:
Timestamp:
07/07/05 12:02:02 (3 years ago)
Author:
madcat
Message:

[patch by cyberz] New Utils::putVal/getVal mechanism that allows outputing either LittleEndian? or BigEndian? (independant from host endianess).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hydranode/include/hn/endian.h

    r1424 r1426  
    6060#endif 
    6161 
     62// We don't want these to be defined 
     63#ifdef LITTLE_ENDIAN 
     64        #undef LITTLE_ENDIAN 
     65#endif 
     66#ifdef BIG_ENDIAN 
     67        #undef BIG_ENDIAN 
     68#endif 
     69 
    6270#if defined (__sparc__) || defined (__alpha__) || defined (__PPC__) || defined (__mips__) || defined (__ppc__) 
    6371        #define __BIG_ENDIAN__ 
    6472#endif 
     73 
     74/** 
     75 * Endianess identifier 
     76 */ 
     77enum {  
     78        LITTLE_ENDIAN = false,  
     79        BIG_ENDIAN = true, 
     80#ifdef __BIG_ENDIAN__ 
     81        HOST_ENDIAN = BIG_ENDIAN      //!< Host is big_endian system 
     82#else 
     83        HOST_ENDIAN = LITTLE_ENDIAN   //!< Host is little_endian system 
     84#endif 
     85}; 
     86 
    6587 
    6688#if defined(__BIG_ENDIAN__) 
  • hydranode/include/hn/hash.h

    r1151 r1426  
    378378                                                break; 
    379379                                        } 
    380                                         Utils::getVal<uint16_t>(i); // length 
     380                                        (void)Utils::getVal<uint16_t>(i); // length 
    381381                                        uint8_t id = Utils::getVal<uint8_t>(i); 
    382382                                        if (id != FileHashType::getTypeId()) { 
     
    406406                                                break; 
    407407                                        } 
    408                                         Utils::getVal<uint16_t>(i); // length 
     408                                        (void)Utils::getVal<uint16_t>(i); // length 
    409409                                        uint8_t id = Utils::getVal<uint8_t>(i); 
    410410                                        if (id != HashType::getTypeId()) { 
  • hydranode/include/hn/utils.h

    r1394 r1426  
    3333#include <boost/shared_array.hpp> 
    3434#include <boost/filesystem/path.hpp> 
     35#include <boost/type_traits.hpp> 
    3536 
    3637/** 
     
    3839 */ 
    3940namespace Utils { 
     41 
     42        /** 
     43         * A type to carry stream endianess 
     44         * 
     45         * @param Endianess     Utils::LITTLE_ENDIAN or Utils::BIG_ENDIAN        
     46         */ 
     47        template<bool Endianess> class EndianType { };   
     48 
     49        /** 
     50         * An object who's carrying endianess information. 
     51         * 
     52         * @param T             Stream type 
     53         * @param Endianess     Utils::LITTLE_ENDIAN or Utils::BIG_ENDIAN 
     54         */ 
     55        template<typename T, bool Endianess> class EndianStream  
     56        : public T, public EndianType<Endianess> { }; 
     57 
     58        /** 
     59         * Endianess test. GetEndianInfo<T>::value returns the endianess (if no  
     60         * endianess is available, Utils::LITTLE_ENDIAN is assumed) 
     61         */ 
     62        template<typename T> struct GetEndianInfo { 
     63                enum { value =  
     64                        ::boost::is_base_and_derived< 
     65                                EndianType<BIG_ENDIAN>, T 
     66                        >::value  
     67                }; 
     68        }; 
     69 
     70        /** 
     71         * Swaps endianess if swap is true for various type of data T via the  
     72         * static function swap. 
     73         */ 
     74        template<typename T, bool Swap> struct SwapData; 
     75 
     76        //! @name Specializations for swapping various datas 
     77        //@{ 
     78        // If host endianess is equal to stream's one, then do not swap 
     79        template<typename T> struct SwapData<T, false> { 
     80                static T swap(T t) { return t; }; 
     81        }; 
     82 
     83        // uint8_t never get swapped 
     84        template<> struct SwapData<uint8_t, true> { 
     85                static uint8_t swap(uint8_t t) { return t; }; 
     86        }; 
     87        // uint16_t 
     88        template<> struct SwapData<uint16_t, true> { 
     89                static uint16_t swap(uint16_t t) { return SWAP16_ALWAYS(t); } 
     90        }; 
     91        // uint32_t 
     92        template<> struct SwapData<uint32_t, true> { 
     93                static uint32_t swap(uint32_t t) { return SWAP32_ALWAYS(t); } 
     94        }; 
     95        // uint64_t 
     96        template<> struct SwapData<uint64_t, true> { 
     97                static uint64_t swap(uint64_t t) { return SWAP64_ALWAYS(t); } 
     98        }; 
     99        // float 
     100        template<> struct SwapData<float, true> { 
     101                static float swap(float t) {  
     102                        return (float)SWAP32_ALWAYS((uint32_t)t);  
     103                } 
     104        }; 
     105        // unimplemented 
     106        template<typename T> struct SwapData<T, true> { 
     107                static T swap(T t) { 
     108                        BOOST_STATIC_ASSERT(sizeof(T::__type_not_supported__)); 
     109                } 
     110        }; 
     111 
     112        //@} 
     113 
     114        /** 
     115         * This function takes care of swapping data if streams need it. 
     116         */ 
     117        template<typename T, typename Stream> inline T swapHostToStream(T t) { 
     118                // since values are boolean, !! avoids this warning on gcc4.0.0: 
     119                // comparison between `enum Utils::GetEndianInfo<...> 
     120                // ::<anonymous>` and `enum Utils::<anonymous>` 
     121 
     122                return SwapData< 
     123                        T, !!GetEndianInfo<Stream>::value != !!HOST_ENDIAN 
     124                >::swap(t); 
     125        } 
     126 
    40127        /** 
    41128         * Convert passed data into hexadecimal notation. 
     
    133220        }; 
    134221 
    135         //! Primary template is not implemented, and causes compile-time 
    136         //! error if instanciated. 
    137         template<class T> inline T getVal(std::istream &i) { 
    138                 BOOST_STATIC_ASSERT(sizeof(T::__type_not_supported__)); 
    139         } 
    140  
    141         //! @name Specializations for reading various datas 
    142         //@{ 
     222        /** 
     223         * Generic getVal functor 
     224         */ 
     225        template<typename T = std::string> 
     226        struct getVal { 
     227                //! Conversion to T 
     228                operator T() const { return m_value; } 
     229 
     230                //! Explicit access to read value 
     231                T value() const { return m_value; } 
     232 
     233                //! Generic constructor 
     234                template<typename Stream> 
     235                getVal(Stream &s) { 
     236                        s.read(reinterpret_cast<char *>(&m_value), sizeof(T)); 
     237                 
     238                        if(!s.good()) { 
     239                                throw ReadError("reading from stream"); 
     240                        } 
     241                        m_value = swapHostToStream<T, Stream>(m_value); 
     242                } 
     243        private: 
     244                T m_value; 
     245        }; 
     246 
     247        /** 
     248         * std::string specialization of getVal 
     249         */ 
    143250        template<> 
    144         inline uint8_t getVal(std::istream &i) { 
    145                 uint8_t tmp; 
    146                 i.read(reinterpret_cast<char*>(&tmp), 1); 
    147                 if (!i.good()) { 
    148                         throw ReadError("reading from stream"); 
    149                 } 
    150                 return tmp; 
    151         } 
     251        struct getVal<std::string> { 
     252                //! Conversion to T 
     253                operator std::string() const { return m_value; } 
     254 
     255                //! Explicit access to read value 
     256                std::string value() const { return m_value; } 
     257 
     258                //! @name std::string getVal constructors 
     259                //@{ 
     260                template<typename Stream> 
     261                getVal(Stream &i) { 
     262                        uint16_t len = getVal<uint16_t>(i); 
     263                        boost::scoped_array<char> buf(new char[len]); 
     264                        i.read(buf.get(), len); 
     265 
     266                        if (!i.good()) { 
     267                                throw ReadError("reading from stream"); 
     268                        } 
     269 
     270                        m_value = std::string(buf.get(), len); 
     271                } 
     272                getVal(std::istream &i, uint32_t len) { 
     273                        boost::scoped_array<char> tmp(new char[len]); 
     274                        i.read(tmp.get(), len); 
     275 
     276                        if (!i.good()) { 
     277                                throw ReadError("reading from stream"); 
     278                        } 
     279 
     280                        m_value = std::string(tmp.get(), len); 
     281                } 
     282                //@} 
     283        private: 
     284                std::string m_value; 
     285        }; 
     286 
     287        /** 
     288         * Generic putVal functor 
     289         */ 
     290        template<typename T = std::string> 
     291        struct putVal { 
     292                //! Generic constructor 
     293                template<typename Stream> 
     294                putVal(Stream &s, T t) { 
     295                        T tmp = swapHostToStream<T, Stream>(t); 
     296 
     297                        s.write(reinterpret_cast<char *>(&tmp), sizeof(T)); 
     298                } 
     299        }; 
     300 
     301        /** 
     302         * std::string specialization of putVal 
     303         */ 
    152304        template<> 
    153         inline uint16_t getVal(std::istream &i) { 
    154                 uint16_t tmp; 
    155                 i.read(reinterpret_cast<char*>(&tmp), 2); 
    156                 tmp = SWAP16_ON_BE(tmp); 
    157                 if (!i.good()) { 
    158                         throw ReadError("reading from stream"); 
    159                 } 
    160                 return tmp; 
    161         } 
    162         template<> 
    163         inline uint32_t getVal(std::istream &i) { 
    164                 uint32_t tmp; 
    165                 i.read(reinterpret_cast<char*>(&tmp), 4); 
    166                 tmp = SWAP32_ON_BE(tmp); 
    167                 if (!i.good()) { 
    168                         throw ReadError("reading from stream"); 
    169                 } 
    170                 return tmp; 
    171         } 
    172         template<> 
    173         inline uint64_t getVal(std::istream &i) { 
    174                 uint64_t tmp; 
    175                 i.read(reinterpret_cast<char*>(&tmp), 8); 
    176                 tmp = SWAP64_ON_BE(tmp); 
    177                 if (!i.good()) { 
    178                         throw ReadError("reading from stream"); 
    179                 } 
    180                 return tmp; 
    181         } 
    182         template<> 
    183         inline float getVal(std::istream &i) { 
    184                 float tmp; 
    185                 i.read(reinterpret_cast<char*>(&tmp), 4); 
    186                 tmp = (float)SWAP32_ON_BE(*(uint32_t*)(&tmp)); 
    187                 if (!i.good()) { 
    188                         throw ReadError("reading from stream"); 
    189                 } 
    190                 return tmp; 
    191         } 
    192         template<> 
    193         inline std::string getVal(std::istream &i) { 
    194                 uint16_t len = getVal<uint16_t>(i); 
    195                 boost::scoped_array<char> buf(new char[len]); 
    196                 i.read(buf.get(), len); 
    197                 if (!i.good()) { 
    198                         throw ReadError("reading from stream"); 
    199                 } 
    200                 return std::string(buf.get(), len); 
    201         } 
    202         template<class T> 
    203         inline T getVal(std::istream &i, uint32_t len) { 
    204                 T tmp; 
    205                 i.read(&tmp, len); 
    206                 if (!i.good()) { 
    207                         throw ReadError("reading from stream"); 
    208                 } 
    209                 return tmp; 
    210         } 
    211         template<> 
    212         inline std::string getVal(std::istream &i, uint32_t len) { 
    213                 boost::scoped_array<char> tmp(new char[len]); 
    214                 i.read(tmp.get(), len); 
    215                 if (!i.good()) { 
    216                         throw ReadError("reading from stream"); 
    217                 } 
    218                 return std::string(tmp.get(), len); 
    219         } 
    220         //@} 
    221  
    222         //! Write. Primary template not implemented and causes compile-time 
    223         //! failure if instanciated. 
    224         template<class T>  
    225         inline void putVal(std::ostream &o, T val) { 
    226                 BOOST_STATIC_ASSERT(sizeof(T::__undefined_type__)); 
    227         } 
    228  
    229         //! @name Writing function template specializations. 
    230         //@{ 
    231         template<> 
    232         inline void putVal(std::ostream &o, uint8_t val) { 
    233                 o.put(val); 
    234         } 
    235         template<> 
    236         inline void putVal(std::ostream &o, uint16_t val) { 
    237                 val = SWAP16_ON_BE(val); 
    238                 o.write(reinterpret_cast<char*>(&val), 2); 
    239         } 
    240         template<> 
    241         inline void putVal(std::ostream &o, uint32_t val) { 
    242                 val = SWAP32_ON_BE(val); 
    243                 o.write(reinterpret_cast<char*>(&val), 4); 
    244         } 
    245         template<> 
    246         inline void putVal(std::ostream &o, uint64_t val) { 
    247                 val = SWAP64_ON_BE(val); 
    248                 o.write(reinterpret_cast<char*>(&val), 8); 
    249         } 
    250         template<> 
    251         inline void putVal(std::ostream &o, float val) { 
    252                 val = (float)SWAP32_ON_BE(*(uint32_t*)&val); 
    253                 o.write(reinterpret_cast<char*>(&val), 4); 
    254         } 
    255         template<> 
    256         inline void putVal(std::ostream &o, const std::string &str) { 
    257                 putVal<uint16_t>(o, str.size()); 
    258                 o.write(str.data(), str.size()); 
    259         } 
    260         template<> 
    261         inline void putVal(std::ostream &o, std::string str) { 
    262                 putVal<uint16_t>(o, str.size()); 
    263                 o.write(str.data(), str.size()); 
    264         } 
    265         inline void putVal( 
    266                 std::ostream &o, const std::string &str, uint32_t len 
    267         ) { 
    268                 o.write(str.data(), len); 
    269         } 
    270         inline void putVal( 
    271                 std::ostream &o, const char *const str, uint32_t len 
    272         ) { 
    273                 CHECK_THROW(str); 
    274                 o.write(str, len); 
    275         } 
    276         inline void putVal( 
    277                 std::ostream &o, const uint8_t *const str, uint32_t len 
    278         ) { 
    279                 CHECK_THROW(str); 
    280                 o.write(reinterpret_cast<const char*>(str), len); 
    281         } 
    282         inline void putVal( 
    283                 std::ostream &o, const boost::shared_array<char> &str, 
    284                 uint32_t len 
    285         ) { 
    286                 CHECK_THROW(str); 
    287                 o.write(str.get(), len); 
    288         } 
    289         //@} 
     305        struct putVal<std::string> { 
     306                //! @name std::string putVal constructors 
     307                //@{ 
     308                template<typename Stream> 
     309                putVal(Stream &o, const std::string &str) { 
     310                        putVal<uint16_t>(o, str.size()); 
     311                        o.write(str.data(), str.size()); 
     312                } 
     313 
     314                template<typename Stream> 
     315                putVal(Stream &o, const std::string &str, uint32_t len) { 
     316                        o.write(str.data(), len); 
     317                } 
     318 
     319                template<typename Stream> 
     320                putVal(Stream &o, const char *const str, uint32_t len) { 
     321                        CHECK_THROW(str); 
     322                        o.write(str, len); 
     323                } 
     324 
     325                template<typename Stream> 
     326                putVal(Stream &o, const uint8_t *const str, uint32_t len) { 
     327                        CHECK_THROW(str); 
     328                        o.write(reinterpret_cast<const char*>(str), len); 
     329                } 
     330 
     331                template<typename Stream> 
     332                putVal( 
     333                        Stream &o, 
     334                        const boost::shared_array<char> &str, 
     335                        uint32_t len 
     336                ) { 
     337                        CHECK_THROW(str); 
     338                        o.write(str.get(), len); 
     339                } 
     340                //@} 
     341        }; 
    290342 
    291343        /** 
  • hydranode/modules/ed2k/clients.cpp

    r1382 r1426  
    243243                m_callbackInProgress = true; 
    244244        } catch (std::exception &e) { 
     245                (void)e; 
    245246                logTrace( 
    246247                        TRACE_DEADSRC, boost::format( 
     
    867868                establishConnection(); 
    868869        } catch (std::exception &e) { 
     870                (void)e; 
    869871                logTrace(TRACE_DEADSRC, 
    870872                        boost::format("[%s] Unable to connect to client: %s") 
     
    14171419                % getIpPort() % e.what() 
    14181420        ); 
     1421        (void)e; 
    14191422} 
    14201423 
     
    15331536                % getIpPort() % e.what() 
    15341537        ); 
     1538        (void)e; 
    15351539} 
    15361540 
     
    16451649                        % getIpPort() % e.what() 
    16461650                ); 
     1651                (void)e; 
    16471652        } 
    16481653 
  • hydranode/modules/ed2k/creditsdb.cpp

    r1279 r1426  
    5050// Construct and load 
    5151Credits::Credits(std::istream &i, uint8_t ver) { 
    52         m_hash = Utils::getVal<std::string>(i, 16)
     52        m_hash = Utils::getVal<std::string>(i, 16).value()
    5353        uint32_t uploadLow = Utils::getVal<uint32_t>(i); 
    5454        uint32_t downloadLow = Utils::getVal<uint32_t>(i); 
     
    7979// Write to stream 
    8080std::ostream& operator<<(std::ostream &o, const Credits &c) { 
    81         Utils::putVal(o, c.m_hash.getData(), 16); 
     81        Utils::putVal<std::string>(o, c.m_hash.getData(), 16); 
    8282        Utils::putVal<uint32_t>(o, c.m_uploaded); 
    8383        Utils::putVal<uint32_t>(o, c.m_downloaded); 
     
    8888 
    8989        Utils::putVal<uint8_t>(o, c.m_pubKey.size()); 
    90         Utils::putVal(o, c.m_pubKey.c_str(), c.m_pubKey.size()); 
     90        Utils::putVal<std::string>(o, c.m_pubKey.c_str(), c.m_pubKey.size()); 
    9191 
    9292        // add padding if keysize is < 80 (required for compatibility) 
    9393        if ( c.m_pubKey.size() < ED2K_MaxKeySize ) { 
    9494                std::string dummyStr(ED2K_MaxKeySize - c.m_pubKey.size(), '\0'); 
    95                 Utils::putVal(o, dummyStr, dummyStr.size()); 
     95                Utils::putVal<std::string>(o, dummyStr, dummyStr.size()); 
    9696        } 
    9797 
     
    268268        // construct the message 
    269269        std::ostringstream tmp; 
    270         Utils::putVal(tmp, key.c_str(), key.size()); 
     270        Utils::putVal<std::string>(tmp, key.c_str(), key.size()); 
    271271        Utils::putVal<uint32_t>(tmp, challenge); 
    272272        if (ipType) { 
     
    303303 
    304304        std::ostringstream tmp; 
    305         Utils::putVal
     305        Utils::putVal<std::string>
    306306                tmp, instance().m_pubKey.c_str(), instance().m_pubKey.size() 
    307307        ); 
  • hydranode/modules/ed2k/ed2kfile.cpp

    r818 r1426  
    5454 
    5555std::ostream& operator<<(std::ostream &o, const ED2KFile &f) { 
    56         Utils::putVal(o, f.m_hash.getData(), 16); 
     56        Utils::putVal<std::string>(o, f.m_hash.getData(), 16); 
    5757        if (f.m_flags & ED2KFile::FL_USECOMPLETEINFO) { 
    5858                if (f.m_flags & ED2KFile::FL_COMPLETE) { 
  • hydranode/modules/ed2k/packets.cpp

    r1421 r1426  
    110110        uint16_t cnt = Utils::getVal<uint16_t>(i); 
    111111        while (cnt && i) { 
    112                 std::bitset<8> tmp(Utils::getVal<uint8_t>(i)); 
     112                std::bitset<8> tmp(Utils::getVal<uint8_t>(i).value()); 
    113113                for (uint8_t i = 0; i < (cnt >= 8 ? 8 : cnt); ++i) { 
    114114                        partMap->push_back(tmp[i]); 
     
    161161        Utils::putVal<uint8_t>(tmp, m_proto); 
    162162        Utils::putVal<uint32_t>(tmp, packet.size()); 
    163         Utils::putVal(tmp, packet, packet.size()); 
     163        Utils::putVal<std::string>(tmp, packet, packet.size()); 
    164164#ifndef HEXDUMPS 
    165165        if (hexDump)  
     
    201201        std::ostringstream tmp; 
    202202        Utils::putVal<uint8_t>(tmp, OP_LOGINREQUEST); 
    203         Utils::putVal(tmp, ED2K::instance().getHash().getData(), 16); 
     203        Utils::putVal<std::string>(tmp, ED2K::instance().getHash().getData(), 16); 
    204204        Utils::putVal<uint32_t>(tmp, 0); // clientid 
    205205        Utils::putVal<uint16_t>(tmp, ED2K::instance().getTcpPort()); 
     
    251251// ----------------- 
    252252ServerIdent::ServerIdent(std::istream &i) { 
    253         m_hash = Utils::getVal<std::string>(i, 16)
     253        m_hash = Utils::getVal<std::string>(i, 16).value()
    254254        m_addr.setAddr(Utils::getVal<uint32_t>(i)); 
    255255        m_addr.setPort(Utils::getVal<uint16_t>(i)); 
     
    344344        Utils::putVal<uint8_t>(tmp, stringParameter); 
    345345        Utils::putVal<uint16_t>(tmp, terms.size()); 
    346         Utils::putVal(tmp, terms, terms.size()); 
     346        Utils::putVal<std::string>(tmp, terms, terms.size()); 
    347347        ++paramCount; 
    348348 
     
    354354        Utils::putVal<uint8_t>(tmp, typeParameter); 
    355355        Utils::putVal<uint16_t>(tmp, type.size()); 
    356         Utils::putVal(tmp, type, type.size()); 
     356        Utils::putVal<std::string>(tmp, type, type.size()); 
    357357        // Who on earth came up with the idea of a 3-byte field ? *DOH* 
    358358        uint32_t _tmp(typeNemonic); 
     
    381381        std::ostringstream packet; 
    382382        Utils::putVal<uint8_t>(packet, OP_SEARCH); 
    383         Utils::putVal(packet, tmp.str(), tmp.str().size()); 
     383        Utils::putVal<std::string>(packet, tmp.str(), tmp.str().size()); 
    384384        return makePacket(packet.str()); 
    385385} 
     
    395395        while (count-- && i) { 
    396396                Hash<ED2KHash> h(Utils::getVal<std::string>(i, 16)); 
    397                 Utils::getVal<uint32_t>(i); // id - ignored 
    398                 Utils::getVal<uint16_t>(i); // port - ignored 
     397                (void)Utils::getVal<uint32_t>(i); // id - ignored 
     398                (void)Utils::getVal<uint16_t>(i); // port - ignored 
    399399                uint32_t tagCount = Utils::getVal<uint32_t>(i); 
    400400                std::string name; 
     
    491491        std::ostringstream tmp; 
    492492        Utils::putVal<uint8_t>(tmp, OP_GETSOURCES); 
    493         Utils::putVal(tmp, m_hash.getData(), 16); 
     493        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    494494        return makePacket(tmp.str()); 
    495495} 
     
    498498// ------------------ 
    499499FoundSources::FoundSources(std::istream &i) : m_lowCount() { 
    500         m_hash = Utils::getVal<std::string>(i, 16)
     500        m_hash = Utils::getVal<std::string>(i, 16).value()
    501501        uint8_t cnt = Utils::getVal<uint8_t>(i); 
    502502        while (cnt--) { 
     
    518518        Utils::putVal<uint8_t>(tmp, OP_GLOBGETSOURCES); 
    519519        for (uint32_t i = 0; i < m_hashList.size(); ++i) { 
    520                 Utils::putVal(tmp, m_hashList[i].first.getData(), 16); 
     520                Utils::putVal<std::string>(tmp, m_hashList[i].first.getData(), 16); 
    521521                if (m_sendSize) { 
    522522                        Utils::putVal<uint32_t>(tmp, m_hashList[i].second); 
     
    529529// ---------------------- 
    530530GlobFoundSources::GlobFoundSources(std::istream &i) { 
    531         m_hash = Utils::getVal<std::string>(i, 16)
     531        m_hash = Utils::getVal<std::string>(i, 16).value()
    532532        uint8_t cnt = Utils::getVal<uint8_t>(i); 
    533533        while (i && cnt--) { 
     
    612612                } 
    613613        } 
    614         m_hash = Utils::getVal<std::string>(i, 16)
     614        m_hash = Utils::getVal<std::string>(i, 16).value()
    615615        m_clientAddr.setAddr(Utils::getVal<uint32_t>(i)); 
    616616        m_clientAddr.setPort(Utils::getVal<uint16_t>(i)); 
     
    658658 
    659659        // Get our own userhash from ED2K class 
    660         Utils::putVal(tmp, ED2K::instance().getHash().getData(), 16); 
     660        Utils::putVal<std::string>(tmp, ED2K::instance().getHash().getData(), 16); 
    661661        // Get our own ip/port from ED2K class 
    662662        Utils::putVal<uint32_t>(tmp, ED2K::instance().getId()); 
     
    787787ReqFile::ReqFile(std::istream &i) { 
    788788        try { 
    789                 m_hash = Utils::getVal<std::string>(i, 16)
     789                m_hash = Utils::getVal<std::string>(i, 16).value()
    790790                readPartMap(i, &m_partMap); 
    791791                m_srcCnt = Utils::getVal<uint16_t>(i); 
     
    796796        std::ostringstream tmp; 
    797797        Utils::putVal<uint8_t>(tmp, OP_REQFILE); 
    798         Utils::putVal(tmp, m_hash.getData(), 16); 
     798        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    799799        writePartMap(tmp, m_partMap); 
    800800        Utils::putVal<uint16_t>(tmp, m_srcCnt); 
     
    807807: m_hash(h), m_name(filename) {} 
    808808FileName::FileName(std::istream &i) { 
    809         m_hash = Utils::getVal<std::string>(i, 16)
     809        m_hash = Utils::getVal<std::string>(i, 16).value()
    810810        uint16_t len = Utils::getVal<uint16_t>(i); 
    811811        m_name = Utils::getVal<std::string>(i, len); 
     
    814814        std::ostringstream tmp; 
    815815        Utils::putVal<uint8_t>(tmp, OP_FILENAME); 
    816         Utils::putVal(tmp, m_hash.getData(), 16); 
     816        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    817817        Utils::putVal<uint16_t>(tmp, m_name.size()); 
    818         Utils::putVal(tmp, m_name, m_name.size()); 
     818        Utils::putVal<std::string>(tmp, m_name, m_name.size()); 
    819819        return makePacket(tmp.str()); 
    820820} 
     
    825825: m_rating(rating), m_comment(comment) {} 
    826826FileDesc::FileDesc(std::istream &i) { 
    827         m_rating = static_cast<ED2KFile::Rating>(Utils::getVal<uint8_t>(i)); 
     827        m_rating = static_cast<ED2KFile::Rating>(Utils::getVal<uint8_t>(i).value()); 
    828828        uint32_t len = Utils::getVal<uint32_t>(i); 
    829829        m_comment = Utils::getVal<std::string>(i, len); 
     
    834834        Utils::putVal<uint8_t>(tmp, m_rating); 
    835835        Utils::putVal<uint32_t>(tmp, m_comment.size()); 
    836         Utils::putVal(tmp, m_comment, m_comment.size()); 
     836        Utils::putVal<std::string>(tmp, m_comment, m_comment.size()); 
    837837        return makePacket(tmp.str()); 
    838838} 
     
    842842SetReqFileId::SetReqFileId(const Hash<ED2KHash> &hash) : m_hash(hash) {} 
    843843SetReqFileId::SetReqFileId(std::istream &i) { 
    844          m_hash = Utils::getVal<std::string>(i, 16)
     844         m_hash = Utils::getVal<std::string>(i, 16).value()
    845845} 
    846846SetReqFileId::operator std::string() { 
    847847        std::ostringstream tmp; 
    848848        Utils::putVal<uint8_t>(tmp, OP_SETREQFILEID); 
    849         Utils::putVal(tmp, m_hash.getData(), 16); 
     849        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    850850        return makePacket(tmp.str()); 
    851851} 
     
    855855NoFile::NoFile(const Hash<ED2KHash> &hash) : m_hash(hash) {} 
    856856NoFile::NoFile(std::istream &i) { 
    857         m_hash = Utils::getVal<std::string>(i, 16)
     857        m_hash = Utils::getVal<std::string>(i, 16).value()
    858858} 
    859859NoFile::operator std::string() { 
    860860        std::ostringstream tmp; 
    861861        Utils::putVal<uint8_t>(tmp, OP_REQFILE_NOFILE); 
    862         Utils::putVal(tmp, m_hash.getData(), 16); 
     862        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    863863        return makePacket(tmp.str()); 
    864864} 
     
    875875        std::ostringstream tmp; 
    876876        Utils::putVal<uint8_t>(tmp, OP_REQFILE_STATUS); 
    877         Utils::putVal(tmp, m_hash.getData(), 16); 
     877        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    878878        writePartMap(tmp, m_partMap); 
    879879        return makePacket(tmp.str()); 
     
    881881 
    882882FileStatus::FileStatus(std::istream &i) { 
    883         m_hash = Utils::getVal<std::string>(i, 16)
     883        m_hash = Utils::getVal<std::string>(i, 16).value()
    884884        readPartMap(i, &m_partMap); 
    885885} 
     
    893893        std::ostringstream tmp; 
    894894        Utils::putVal<uint8_t>(tmp, OP_REQHASHSET); 
    895         Utils::putVal(tmp, m_hash.getData(), 16); 
     895        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    896896        return makePacket(tmp.str()); 
    897897} 
     
    902902HashSet::HashSet(std::istream &i) { 
    903903        m_hashSet.reset(new ED2KHashSet()); 
    904         m_hashSet->setFileHash(Utils::getVal<std::string>(i, 16)); 
     904        m_hashSet->setFileHash(Utils::getVal<std::string>(i, 16).value()); 
    905905        uint16_t count = Utils::getVal<uint16_t>(i); 
    906906        while (count--) { 
    907                 m_hashSet->addChunkHash(Utils::getVal<std::string>(i, 16)); 
     907                m_hashSet->addChunkHash(Utils::getVal<std::string>(i, 16).value()); 
    908908        } 
    909909} 
     
    912912        std::ostringstream tmp; 
    913913        Utils::putVal<uint8_t>(tmp, OP_HASHSET); 
    914         Utils::putVal(tmp, m_tmpSet->getFileHash().getData(), 16); 
     914        Utils::putVal<std::string>(tmp, m_tmpSet->getFileHash().getData(), 16); 
    915915        Utils::putVal<uint16_t>(tmp, m_tmpSet->getChunkCnt()); 
    916916        for (uint32_t i = 0; i < m_tmpSet->getChunkCnt(); ++i) { 
    917                 Utils::putVal(tmp, (*m_tmpSet)[i].getData(), 16); 
     917                Utils::putVal<std::string>(tmp, (*m_tmpSet)[i].getData(), 16); 
    918918        } 
    919919        return makePacket(tmp.str()); 
     
    927927        // Optional 
    928928        try { 
    929                 m_hash = Utils::getVal<std::string>(i, 16)
     929                m_hash = Utils::getVal<std::string>(i, 16).value()
    930930        } catch (Utils::ReadError&) {} 
    931931} 
     
    934934        Utils::putVal<uint8_t>(tmp, OP_STARTUPLOADREQ); 
    935935        if (!m_hash.isEmpty()) { 
    936                 Utils::putVal(tmp, m_hash.getData(), 16); 
     936                Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    937937        } 
    938938        return makePacket(tmp.str()); 
     
    989989} 
    990990ReqChunks::ReqChunks(std::istream &i) { 
    991         m_hash = Utils::getVal<std::string>(i, 16)
     991        m_hash = Utils::getVal<std::string>(i, 16).value()
    992992        boost::tuple<uint32_t, uint32_t, uint32_t> begins; 
    993993        boost::tuple<uint32_t, uint32_t, uint32_t> ends; 
     
    10141014        std::ostringstream tmp; 
    10151015        Utils::putVal<uint8_t>(tmp, OP_REQCHUNKS); 
    1016         Utils::putVal(tmp, m_hash.getData(), 16); 
     1016        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    10171017        CHECK_THROW(m_reqChunks.size()); 
    10181018        Utils::putVal<uint32_t>(tmp, m_reqChunks.at(0).begin()); 
     
    10521052} 
    10531053DataChunk::DataChunk(std::istream &i) { 
    1054         m_hash  = Utils::getVal<std::string>(i, 16)
     1054        m_hash  = Utils::getVal<std::string>(i, 16).value()
    10551055        m_begin = Utils::getVal<uint32_t>(i); 
    10561056        m_end   = Utils::getVal<uint32_t>(i); 
     
    10601060        std::ostringstream tmp; 
    10611061        Utils::putVal<uint8_t>(tmp, OP_SENDINGCHUNK); 
    1062         Utils::putVal(tmp, m_hash.getData(), 16); 
     1062        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    10631063        Utils::putVal<uint32_t>(tmp, m_begin); 
    10641064        Utils::putVal<uint32_t>(tmp, m_end); 
    1065         Utils::putVal(tmp, m_data, m_data.size()); 
     1065        Utils::putVal<std::string>(tmp, m_data, m_data.size()); 
    10661066        return makePacket(tmp.str()); 
    10671067} 
     
    10821082// in, but I see no other way - blame the protocol :( 
    10831083PackedChunk::PackedChunk(std::istream &i) : Packet(PR_EMULE) { 
    1084         m_hash  = Utils::getVal<std::string>(i, 16)
     1084        m_hash  = Utils::getVal<std::string>(i, 16).value()
    10851085        m_begin = Utils::getVal<uint32_t>(i); 
    10861086        m_size  = Utils::getVal<uint32_t>(i); 
     
    10911091        std::ostringstream tmp; 
    10921092        Utils::putVal<uint8_t>(tmp, OP_PACKEDCHUNK); 
    1093         Utils::putVal(tmp, m_hash.getData(), 16); 
     1093        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    10941094        Utils::putVal<uint32_t>(tmp, m_begin); 
    10951095        Utils::putVal<uint32_t>(tmp, m_size); 
    1096         Utils::putVal(tmp, m_data, m_data.size()); 
     1096        Utils::putVal<std::string>(tmp, m_data, m_data.size()); 
    10971097        return makePacket(tmp.str()); 
    10981098} 
     
    11151115} 
    11161116SourceExchReq::SourceExchReq(std::istream &i) { 
    1117         m_hash = Utils::getVal<std::string>(i, 16)
     1117        m_hash = Utils::getVal<std::string>(i, 16).value()
    11181118} 
    11191119SourceExchReq::operator std::string() { 
    11201120        std::ostringstream tmp; 
    11211121        Utils::putVal<uint8_t>(tmp, OP_REQSOURCES); 
    1122         Utils::putVal(tmp, m_hash.getData(), 16); 
     1122        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    11231123        return makePacket(tmp.str()); 
    11241124} 
     
    11311131} 
    11321132AnswerSources::AnswerSources(std::istringstream &i) { 
    1133         m_hash = Utils::getVal<std::string>(i, 16)
     1133        m_hash = Utils::getVal<std::string>(i, 16).value()
    11341134        uint16_t cnt = Utils::getVal<uint16_t>(i); 
    11351135        bool hashes = true; 
     
    11881188        std::ostringstream tmp; 
    11891189        Utils::putVal<uint16_t>(tmp, m_message.size()); 
    1190         Utils::putVal(tmp, m_message, m_message.size()); 
     1190        Utils::putVal<std::string>(tmp, m_message, m_message.size()); 
    11911191        return makePacket(tmp.str()); 
    11921192} 
     
    12441244        Utils::putVal<uint8_t>(tmp, OP_PUBLICKEY); 
    12451245        Utils::putVal<uint8_t>(tmp, m_pubKey.size()); 
    1246         Utils::putVal(tmp, m_pubKey.c_str(), m_pubKey.size()); 
     1246        Utils::putVal<std::string>(tmp, m_pubKey.c_str(), m_pubKey.size()); 
    12471247        return makePacket(tmp.str()); 
    12481248} 
     
    12681268        Utils::putVal<uint8_t>(tmp, OP_SIGNATURE); 
    12691269        Utils::putVal<uint8_t>(tmp, m_signature.size()); 
    1270         Utils::putVal(tmp, m_signature, m_signature.size()); 
     1270        Utils::putVal<std::string>(tmp, m_signature, m_signature.size()); 
    12711271        if (m_ipType) { 
    12721272                Utils::putVal<uint8_t>(tmp, m_ipType); 
     
    12871287m_udpVersion(udpVersion) {} 
    12881288ReaskFilePing::ReaskFilePing(std::istream &i) { 
    1289         m_hash = Utils::getVal<std::string>(i, 16)
     1289        m_hash = Utils::getVal<std::string>(i, 16).value()
    12901290        std::istringstream &tmp = dynamic_cast<std::istringstream&>(i); 
    12911291        if (tmp.str().size() >= 20) { // UDPv4 
     
    13001300        Utils::putVal<uint8_t>(tmp, PR_EMULE); 
    13011301        Utils::putVal<uint8_t>(tmp, OP_REASKFILEPING); 
    1302         Utils::putVal(tmp, m_hash.getData(), 16); 
     1302        Utils::putVal<std::string>(tmp, m_hash.getData(), 16); 
    13031303        if (m_udpVersion >= 4) { 
    13041304                writePartMap(tmp, m_partMap); 
  • hydranode/modules/ed2k/publickey.h

    r1001 r1426  
    4444        operator bool()  const { return m_key; } 
    4545 
     46        //! Assignment operator 
     47        PublicKey& operator=(const std::string &key) { 
     48