src/proto2/NetworkPacket.hh
changeset 89 825c4613e087
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/NetworkPacket.hh	Thu Nov 20 23:51:46 2008 +0000
@@ -0,0 +1,66 @@
+#ifndef NETWORK_PACKET_HH
+#define NETWORK_PACKET_HH
+
+#include "NetworkConfig.hh"
+#include "Vector.hh"
+#include "Error.hh"
+
+class NetworkPacketError : public Error {
+    public:
+        NetworkPacketError (const std::string &message) : Error(message) { }
+};
+
+class NetworkPacket {
+    private:
+        char buf[NETWORK_PACKET_SIZE];
+        size_t buf_size, data_size, offset;
+        
+        void check_write_size (size_t item_size);
+        void check_read_size (size_t item_size);
+
+        template <typename T> T read_val (void);
+        template <typename T> void write_val (const T &val);
+ 
+    public:
+        NetworkPacket (void);
+        
+        char* get_buf (void) { return buf; }
+        const char* get_buf (void) const { return buf; }
+        size_t get_data_size (void) const { return data_size; }
+        size_t get_buf_size (void) const { return buf_size; }
+
+        void set_data_size (size_t size) { offset = 0; data_size = size; }
+
+        // raw
+        void write (const void *ptr, size_t len);
+        void read (void *ptr, size_t len);
+       
+        // type-reads, handle network-endianlness
+        uint32_t read_uint32 (void);
+        uint16_t read_uint16 (void);
+        uint8_t read_uint8 (void);
+
+        int32_t read_int32 (void);
+        int16_t read_int16 (void);
+        int8_t read_int8 (void);
+        
+        float read_float32 (void);
+
+
+        void write_uint32 (uint32_t val);
+        void write_uint16 (uint16_t val);
+        void write_uint8 (uint8_t val);
+
+        void write_int32 (int32_t val);
+        void write_int16 (int16_t val);
+        void write_int8 (int8_t val);
+        
+        void write_float32 (float val);
+
+        // complex
+        Vector read_vector (void);
+        void write_vector (const Vector &vec);
+        void write_packet (const NetworkPacket &pkt);
+};
+
+#endif /* NETWORK_PACKET_HH */