Changeset 507
- Timestamp:
- 04/10/06 15:04:33 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
library/trunk/modules/service-gotek2/GotekPacket.cpp
r506 r507 1 1 /* $Id$ */ 2 #include <netinet/in.h> 2 3 #include "GotekException.hpp" 3 4 #include "GotekPacket.hpp" … … 11 12 } 12 13 13 GotekPacket::GotekPacket(GotekPacketType t, string const& data, int options) 14 { 15 GotekPacket(t, options); 14 15 GotekPacket::GotekPacket(string const& data, int options) 16 { 16 17 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 25 GotekPacket *GotekPacket::createReadPacket(string const& data) 26 { 27 return new GotekPacket(data, GotekRead); 28 } 29 23 30 24 31 GotekPacket *GotekPacket::createWritePacket(GotekPacketType t) … … 27 34 } 28 35 36 29 37 GotekPacketType GotekPacket::type() const 30 38 { … … 32 40 } 33 41 42 34 43 string GotekPacket::buffer() const 35 44 { 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 38 58 39 59 inline void GotekPacket::checkBadness() const … … 43 63 } 44 64 45 inline void GotekPacket::assumeWrite() const 65 66 inline void GotekPacket::assumeWrite() 46 67 { 47 68 if( !(m_options & GotekWrite) ) 69 { 70 m_options |= GotekBad; 48 71 throw GotekException(string("Attempted to perform write operation on non-write packet!")); 49 } 50 51 inline void GotekPacket::assumeRead() const 72 } 73 } 74 75 76 inline void GotekPacket::assumeRead() 52 77 { 53 78 if( !(m_options & GotekRead) ) 79 { 80 m_options |= GotekBad; 54 81 throw GotekException(string("Attempted to perform read operation on non-read packet!")); 82 } 83 } 84 85 86 inline 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 } 55 95 } 56 96 … … 64 104 } 65 105 106 66 107 void GotekPacket::writeString(string const& s) 67 108 { … … 70 111 71 112 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 } 73 117 74 118 m_data.push_back((unsigned char)s.size()); … … 76 120 } 77 121 78 void GotekPacket::writeUnsignedInt8(uint8_t i) 79 { 80 } 122 123 void GotekPacket::writeUInt8(uint8_t i) 124 { 125 checkBadness(); 126 assumeWrite(); 127 128 m_data.push_back((unsigned char)i); 129 } 130 131 132 void 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 144 void 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 158 string 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 172 string GotekPacket::readString() 173 { 174 checkBadness(); 175 assumeRead(); 176 177 return readRaw(readUInt8()); 178 } 179 180 181 uint8_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 195 uint16_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 209 uint32_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 11 11 * reading data 12 12 * 13 * GotekPacket *p = createReadPacket(netcode_ type, netcode_data);13 * GotekPacket *p = createReadPacket(netcode_data); 14 14 * 15 15 * try … … 42 42 * } 43 43 * 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()); 47 45 * 48 46 * delete p; … … 55 53 56 54 // FIXME 57 enum GotekPacketType { foobar };55 enum GotekPacketType { foobar = 0x33}; 58 56 59 57 namespace library … … 64 62 { 65 63 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); 68 66 69 string readRaw(u nsigned int size);67 string readRaw(uint32_t size); 70 68 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(); 77 72 78 73 void writeRaw(string const& r); 79 74 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); 86 78 87 79 GotekPacketType type() const; … … 90 82 private: 91 83 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); 96 89 97 90 GotekPacketType m_type;
