switching structures to use new type serialization, removing dead code

This commit is contained in:
Alex Yatskov 2011-09-04 10:57:04 -07:00
parent 2457458aca
commit 30a41a4315
9 changed files with 82 additions and 178 deletions

View File

@ -98,5 +98,26 @@ int Buffer::bytes() const {
return data_.size(); return data_.size();
} }
bool Buffer::serialize(Serializer* serializer) const {
serializer->write(bytes());
serializer->writeRaw(data(), bytes());
return true;
}
bool Buffer::deserialize(Deserializer* deserializer) {
int count = 0;
if (!deserializer->read(&count)) {
return false;
}
const byte* const data = deserializer->readRaw(count);
if (data == NULL) {
return false;
}
setData(data, count);
return true;
}
} }

View File

@ -48,6 +48,9 @@ public:
void* data (); void* data ();
int bytes () const; int bytes () const;
bool serialize (Serializer* serializer) const;
bool deserialize (Deserializer* deserializer);
private: private:
std::vector<byte> data_; std::vector<byte> data_;
}; };

View File

@ -53,4 +53,12 @@ namespace metacall {
#endif #endif
//
// Forward declarations
//
class Serializer;
class Deserializer;
} }

View File

@ -32,7 +32,7 @@ namespace metacall {
// Meta // Meta
// //
qword serialize(class Serializer*, ...); qword serialize(Serializer*, ...);
template <typename T> template <typename T>
struct HasGlobalSerializer { struct HasGlobalSerializer {
@ -46,7 +46,7 @@ struct HasGlobalSerializer {
}; };
}; };
qword deserialize(class Deserializer*, ...); qword deserialize(Deserializer*, ...);
template <typename T> template <typename T>
struct HasGlobalDeserializer { struct HasGlobalDeserializer {
@ -62,11 +62,11 @@ struct HasGlobalDeserializer {
template <typename T> template <typename T>
struct HasLocalSerializer { struct HasLocalSerializer {
template <typename U, bool (U::*)(Serializer*)> template <typename U, bool (U::*)(Serializer*) const>
struct Signature { }; struct Signature { };
template <typename U> template <typename U>
static dword Test(Signature<U, U::serialize>*); static dword Test(Signature<U, &U::serialize>*);
template <typename U> template <typename U>
static qword Test(...); static qword Test(...);
@ -82,7 +82,7 @@ struct HasLocalDeserializer {
struct Signature { }; struct Signature { };
template <typename U> template <typename U>
static dword Test(Signature<U, U::deserialize>*); static dword Test(Signature<U, &U::deserialize>*);
template <typename U> template <typename U>
static qword Test(...); static qword Test(...);

View File

@ -56,6 +56,20 @@ struct PacketHeader {
{ {
} }
bool serialize(Serializer* serializer) const {
return
serializer->write(magic) &&
serializer->write(id) &&
serializer->write(size);
}
bool deserialize(Deserializer* deserializer) {
return
deserializer->read(&magic) &&
deserializer->read(&id) &&
deserializer->read(&size);
}
dword magic; dword magic;
byte id; byte id;
dword size; dword size;
@ -77,6 +91,20 @@ struct PacketInvokeRequest {
Id = PACKET_ID_FUNCTION_CALL_REQUEST Id = PACKET_ID_FUNCTION_CALL_REQUEST
}; };
bool serialize(Serializer* serializer) const {
return
serializer->write(function) &&
serializer->write(taskId) &&
serializer->write(data);
}
bool deserialize(Deserializer* deserializer) {
return
deserializer->read(&function) &&
deserializer->read(&taskId) &&
deserializer->read(&data);
}
dword function; dword function;
dword taskId; dword taskId;
Buffer data; Buffer data;
@ -103,6 +131,20 @@ struct PacketInvokeReply {
FLAG_INVALID_ARGS = 1 << 1, FLAG_INVALID_ARGS = 1 << 1,
}; };
bool serialize(Serializer* serializer) const {
return
serializer->write(flags) &&
serializer->write(taskId) &&
serializer->write(data);
}
bool deserialize(Deserializer* deserializer) {
return
deserializer->read(&flags) &&
deserializer->read(&taskId) &&
deserializer->read(&data);
}
word flags; word flags;
dword taskId; dword taskId;
Buffer data; Buffer data;

View File

@ -104,40 +104,4 @@ bool Serializer::write(const T& data) {
} }
//
// Serializers
//
template <typename T>
bool serialize(Serializer* serializer, const ArrayWriter<T>& writer) {
serializer->write(writer.count);
for (int i = 0; i < writer.count; ++i) {
serializer->write(writer.data[i]);
}
return true;
}
template <typename T>
bool deserialize(Deserializer* deserializer, ArrayReader<T>* reader) {
reader->clear();
if (!deserializer->read(&reader->count)) {
return false;
}
reader->data = new T[reader->count];
for (int i = 0; i < reader->count; ++i) {
if (!deserializer->read(reader->data + i)) {
reader->clear();
return false;
}
}
return true;
}
} }

View File

@ -101,73 +101,4 @@ int Serializer::offset() const {
} }
//
// Serializers
//
bool serialize(Serializer* serializer, const Buffer& buffer) {
serializer->write(buffer.bytes());
serializer->writeRaw(buffer.data(), buffer.bytes());
return true;
}
bool deserialize(Deserializer* deserializer, Buffer* buffer) {
int count = 0;
if (!deserializer->read(&count)) {
return false;
}
const byte* const data = deserializer->readRaw(count);
if (data == NULL)
{
return false;
}
buffer->setData(data, count);
return true;
}
bool serialize(Serializer* serializer, const PacketHeader& packet) {
return
serializer->write(packet.magic) &&
serializer->write(packet.id) &&
serializer->write(packet.size);
}
bool deserialize(Deserializer* deserializer, PacketHeader* packet) {
return
deserializer->read(&packet->magic) &&
deserializer->read(&packet->id) &&
deserializer->read(&packet->size);
}
bool serialize(Serializer* serializer, const PacketInvokeRequest& packet) {
return
serializer->write(packet.function) &&
serializer->write(packet.taskId) &&
serializer->write(packet.data);
}
bool deserialize(Deserializer* deserializer, PacketInvokeRequest* packet) {
return
deserializer->read(&packet->function) &&
deserializer->read(&packet->taskId) &&
deserializer->read(&packet->data);
}
bool serialize(Serializer* serializer, const PacketInvokeReply& packet) {
return
serializer->write(packet.flags) &&
serializer->write(packet.taskId) &&
serializer->write(packet.data);
}
bool deserialize(Deserializer* deserializer, PacketInvokeReply* packet) {
return
deserializer->read(&packet->flags) &&
deserializer->read(&packet->taskId) &&
deserializer->read(&packet->data);
}
} }

View File

@ -68,69 +68,4 @@ private:
}; };
//
// ArrayWriter
//
template <typename T>
class ArrayWriter {
public:
ArrayWriter(const T* data, int count) :
data(data),
count(count)
{
}
const T* data;
int count;
};
//
// ArrayReader
//
template <typename T>
class ArrayReader {
public:
ArrayReader() :
data(NULL),
count(0)
{
}
~ArrayReader() {
delete[] data;
data = NULL;
count = 0;
}
const T* data;
int count;
};
//
// Standard serializers/deserializers
//
template <typename T>
bool serialize (Serializer* serializer, const ArrayWriter<T>& writer);
template <typename T>
bool deserialize (Deserializer* deserializer, ArrayReader<T>* reader);
bool serialize (Serializer* serializer, const Buffer& buffer);
bool deserialize (Deserializer* deserializer, Buffer* buffer);
bool serialize (Serializer* serializer, const PacketHeader& packet);
bool deserialize (Deserializer* deserializer, PacketHeader* packet);
bool serialize (Serializer* serializer, const PacketInvokeRequest& packet);
bool deserialize (Deserializer* deserializer, PacketInvokeRequest* packet);
bool serialize (Serializer* serializer, const PacketInvokeReply& packet);
bool deserialize (Deserializer* deserializer, PacketInvokeReply* packet);
} }

View File

@ -45,10 +45,10 @@
#include "mc_token.hpp" #include "mc_token.hpp"
#include "mc_buffer.hpp" #include "mc_buffer.hpp"
#include "mc_socket.hpp" #include "mc_socket.hpp"
#include "mc_packet.hpp"
#include "mc_meta.hpp" #include "mc_meta.hpp"
#include "mc_serial.hpp" #include "mc_serial.hpp"
#include "mc_serial-inl.hpp" #include "mc_serial-inl.hpp"
#include "mc_packet.hpp"
#include "mc_functor.hpp" #include "mc_functor.hpp"
#include "mc_binding.hpp" #include "mc_binding.hpp"
#include "mc_binding-inl.hpp" #include "mc_binding-inl.hpp"