Changeset 507

Show
Ignore:
Timestamp:
04/10/06 15:04:33 (2 years ago)
Author:
dp
Message:

service-gotek2

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • library/trunk/modules/service-gotek2/GotekPacket.cpp

    r506 r507  
    11/* $Id$ */ 
     2#include <netinet/in.h> 
    23#include "GotekException.hpp" 
    34#include "GotekPacket.hpp" 
     
    1112} 
    1213 
    13 GotekPacket::GotekPacket(GotekPacketType t, string const& data, int options) 
    14 
    15         GotekPacket(t, options); 
     14 
     15GotekPacket::GotekPacket(string const& data, int options) 
     16
    1617        m_data = data; 
    17 
    18  
    19 GotekPacket *GotekPacket::createReadPacket(GotekPacketType t, string const& data) 
    20 
    21         return new GotekPacket(t, data, GotekRead); 
    22 
     18        m_options = options; 
     19 
     20        m_type = (GotekPacketType)readUInt8(); 
     21        readUInt16(); 
     22
     23 
     24 
     25GotekPacket *GotekPacket::createReadPacket(string const& data) 
     26
     27        return new GotekPacket(data, GotekRead); 
     28
     29 
    2330 
    2431GotekPacket *GotekPacket::createWritePacket(GotekPacketType t) 
     
    2734} 
    2835 
     36 
    2937GotekPacketType GotekPacket::type() const 
    3038{ 
     
    3240} 
    3341 
     42 
    3443string GotekPacket::buffer() const 
    3544{ 
    36         return m_data; 
    37 
     45        string packet; 
     46         
     47        uint16_t nsize = htons(m_data.size()); 
     48         
     49        packet.push_back((unsigned char)type()); 
     50        packet.push_back((unsigned char)(nsize >> 8)); 
     51        packet.push_back((unsigned char)(nsize & 0xff)); 
     52 
     53        packet.append(m_data); 
     54         
     55        return packet; 
     56
     57 
    3858 
    3959inline void GotekPacket::checkBadness() const 
     
    4363} 
    4464 
    45 inline void GotekPacket::assumeWrite() const 
     65 
     66inline void GotekPacket::assumeWrite() 
    4667{ 
    4768        if( !(m_options & GotekWrite) ) 
     69        { 
     70                m_options |= GotekBad; 
    4871                throw GotekException(string("Attempted to perform write operation on non-write packet!")); 
    49 
    50  
    51 inline void GotekPacket::assumeRead() const 
     72        } 
     73
     74 
     75 
     76inline void GotekPacket::assumeRead() 
    5277{ 
    5378        if( !(m_options & GotekRead) ) 
     79        { 
     80                m_options |= GotekBad; 
    5481                throw GotekException(string("Attempted to perform read operation on non-read packet!")); 
     82        } 
     83} 
     84 
     85 
     86inline void GotekPacket::expectLength(uint32_t i) 
     87{ 
     88        if( m_data.size() < i ) 
     89        { 
     90                m_options |= GotekBad; 
     91 
     92                // TODO: inherit exception 
     93                throw GotekException(string("Premature end of packet!")); 
     94        } 
    5595} 
    5696 
     
    64104} 
    65105 
     106 
    66107void GotekPacket::writeString(string const& s) 
    67108{ 
     
    70111 
    71112        if( s.size() > 0xff ) 
    72                 throw GotekException(string("Attempted to write a string longer than 256 bytes!")); 
     113        { 
     114                m_options |= GotekBad; 
     115                throw GotekException(string("Attempted to write a string longer than 255 bytes!")); 
     116        } 
    73117 
    74118        m_data.push_back((unsigned char)s.size()); 
     
    76120} 
    77121 
    78 void GotekPacket::writeUnsignedInt8(uint8_t i) 
    79 
    80 
     122 
     123void GotekPacket::writeUInt8(uint8_t i) 
     124
     125        checkBadness(); 
     126        assumeWrite(); 
     127 
     128        m_data.push_back((unsigned char)i); 
     129
     130 
     131 
     132void GotekPacket::writeUInt16(uint16_t i) 
     133
     134        checkBadness(); 
     135        assumeWrite(); 
     136 
     137        uint16_t ni = htons(i); 
     138         
     139        m_data.push_back((unsigned char)(ni >> 8)); 
     140        m_data.push_back((unsigned char)(ni & 0xff)); 
     141
     142 
     143 
     144void GotekPacket::writeUInt32(uint32_t i) 
     145
     146        checkBadness(); 
     147        assumeWrite(); 
     148 
     149        uint32_t ni = htonl(i); 
     150 
     151        m_data.push_back((unsigned char)(ni >> 24)); 
     152        m_data.push_back((unsigned char)((ni >> 16) & 0xff)); 
     153        m_data.push_back((unsigned char)((ni >> 8) & 0xff)); 
     154        m_data.push_back((unsigned char)(ni & 0xff)); 
     155
     156 
     157 
     158string GotekPacket::readRaw(uint32_t size) 
     159
     160        checkBadness(); 
     161        assumeRead(); 
     162 
     163        expectLength(size); 
     164 
     165        string raw = m_data.substr(0, size); 
     166        m_data.erase(0, size); 
     167 
     168        return raw; 
     169
     170 
     171 
     172string GotekPacket::readString() 
     173
     174        checkBadness(); 
     175        assumeRead(); 
     176 
     177        return readRaw(readUInt8()); 
     178
     179 
     180 
     181uint8_t GotekPacket::readUInt8() 
     182
     183        checkBadness(); 
     184        assumeRead(); 
     185 
     186        expectLength(1); 
     187 
     188        uint8_t i = m_data[0]; 
     189        m_data.erase(0, 1); 
     190 
     191        return i; 
     192
     193 
     194 
     195uint16_t GotekPacket::readUInt16() 
     196
     197        checkBadness(); 
     198        assumeRead(); 
     199 
     200        expectLength(2); 
     201 
     202        uint16_t ni = (uint8_t)m_data[0] << 8 | (uint8_t)m_data[1]; 
     203        m_data.erase(0, 2); 
     204 
     205        return ntohs(ni); 
     206
     207 
     208 
     209uint32_t GotekPacket::readUInt32() 
     210
     211        checkBadness(); 
     212        assumeRead(); 
     213 
     214        expectLength(4); 
     215 
     216        uint32_t ni = (uint8_t)m_data[0] << 24 | (uint8_t)m_data[1] << 16 | 
     217                (uint8_t)m_data[2] << 8 | (uint8_t)m_data[3]; 
     218        m_data.erase(0, 4); 
     219 
     220        return ntohl(ni); 
     221
  • library/trunk/modules/service-gotek2/GotekPacket.hpp

    r506 r507  
    1111 * reading data 
    1212 * 
    13  * GotekPacket *p = createReadPacket(netcode_type, netcode_data); 
     13 * GotekPacket *p = createReadPacket(netcode_data); 
    1414 * 
    1515 * try 
     
    4242 * } 
    4343 * 
    44  * netcode_send(p->getType()); 
    45  * netcode_send(p->getBuffer().size()); 
    46  * netcode_send(p->getBuffer().c_str, len ..); 
     44 * netcode_send(p->buffer(), p->size()); 
    4745 * 
    4846 * delete p; 
     
    5553 
    5654// FIXME 
    57 enum GotekPacketType { foobar }; 
     55enum GotekPacketType { foobar = 0x33}; 
    5856 
    5957namespace library 
     
    6462        { 
    6563        public: 
    66                 GotekPacket *                  createReadPacket(GotekPacketType t, string const& data); 
    67                 GotekPacket *                 createWritePacket(GotekPacketType t); 
     64                static GotekPacket *   createReadPacket(string const& data); 
     65                static GotekPacket *  createWritePacket(GotekPacketType t); 
    6866 
    69                 string                                  readRaw(unsigned int size); 
     67                string                                  readRaw(uint32_t size); 
    7068                string                                  readString(); 
    71                 uint8_t                                 readUnsignedInt8(); 
    72                 uint16_t                                readUnsignedInt16(); 
    73                 uint32_t                                readUnsignedInt32(); 
    74                 int8_t                                  readInt8(); 
    75                 int16_t                                 readInt16(); 
    76                 int32_t                                 readInt32(); 
     69                uint8_t                                 readUInt8(); 
     70                uint16_t                                readUInt16(); 
     71                uint32_t                                readUInt32(); 
    7772 
    7873                void                                    writeRaw(string const& r); 
    7974                void                                    writeString(string const& s); 
    80                 void                                    writeUnsignedInt8(uint8_t i); 
    81                 void                                    writeUnsignedInt16(uint16_t i); 
    82                 void                                    writeUnsignedInt32(uint32_t i); 
    83                 void                                    writeInt8(int8_t i); 
    84                 void                                    writeInt16(int16_t i); 
    85                 void                                    writeInt32(int32_t i); 
     75                void                                    writeUInt8(uint8_t i); 
     76                void                                    writeUInt16(uint16_t i); 
     77                void                                    writeUInt32(uint32_t i); 
    8678 
    8779                GotekPacketType                 type() const; 
     
    9082        private: 
    9183                                                                GotekPacket(GotekPacketType t, int options); 
    92                                                                 GotekPacket(GotekPacketType t, string const& data, int options); 
    93                 inline void                             checkBadness() const; 
    94                 inline void                             assumeRead() const; 
    95                 inline void                             assumeWrite() const; 
     84                                                                GotekPacket(string const& data, int options); 
     85                void                                    checkBadness() const; 
     86                void                                    assumeRead(); 
     87                void                                    assumeWrite(); 
     88                void                                    expectLength(uint32_t l); 
    9689                 
    9790                GotekPacketType                 m_type;