From 20e478b83e754fd9b19d58abe79732fa0dd20ffc Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 6 Apr 2013 22:08:14 +0200 Subject: [PATCH] Start working on serialization. --- .gitignore | 2 ++ Makefile | 8 ++++++-- server.cpp | 25 +++++++++++++++++++++++++ server.h | 6 ++++++ state.proto | 22 ++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 state.proto diff --git a/.gitignore b/.gitignore index 7212a66..aa8fde8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ cubemap *.o *.d +*.pb.cc +*.pb.h diff --git a/Makefile b/Makefile index 78ec5a6..704ee73 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,16 @@ CC=gcc CXX=g++ +PROTOC=protoc CXXFLAGS=-Wall -O2 -g -LDLIBS=-lcurl -lpthread +LDLIBS=-lcurl -lpthread -lprotobuf -OBJS=cubemap.o server.o mutexlock.o input.o +OBJS=cubemap.o server.o mutexlock.o input.o state.pb.o all: cubemap +%.pb.cc %.pb.h : %.proto + $(PROTOC) --cpp_out=. $< + %.o: %.cpp $(CXX) -MMD -MP $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $< cubemap: $(OBJS) diff --git a/server.cpp b/server.cpp index 6dcddf0..9c4219a 100644 --- a/server.cpp +++ b/server.cpp @@ -18,6 +18,7 @@ #include "metacube.h" #include "server.h" #include "mutexlock.h" +#include "state.pb.h" using namespace std; @@ -28,6 +29,30 @@ Client::Client(int sock) { request.reserve(1024); } + +Client::Client(const ClientProto &serialized) + : sock(serialized.sock()), + state(State(serialized.state())), + request(serialized.request()), + stream_id(serialized.stream_id()), + header(serialized.header()), + header_bytes_sent(serialized.header_bytes_sent()), + bytes_sent(serialized.bytes_sent()) +{ +} + +ClientProto Client::serialize() const +{ + ClientProto serialized; + serialized.set_sock(sock); + serialized.set_state(state); + serialized.set_request(request); + serialized.set_stream_id(stream_id); + serialized.set_header(header); + serialized.set_header_bytes_sent(serialized.header_bytes_sent()); + serialized.set_bytes_sent(bytes_sent); + return serialized; +} Server::Server() { diff --git a/server.h b/server.h index 6311c68..d0e3d6a 100644 --- a/server.h +++ b/server.h @@ -12,10 +12,16 @@ #define EPOLL_TIMEOUT_MS 20 #define MAX_CLIENT_REQUEST 16384 +class ClientProto; + struct Client { Client() {} Client(int sock); + // Serialization/deserialization. + Client(const ClientProto &serialized); + ClientProto serialize() const; + // The file descriptor associated with this socket. int sock; diff --git a/state.proto b/state.proto new file mode 100644 index 0000000..7c9ca7f --- /dev/null +++ b/state.proto @@ -0,0 +1,22 @@ +// Corresponds to struct Client. +message ClientProto { + optional int32 sock = 1; + optional int32 state = 2; + optional bytes request = 3; + optional string stream_id = 4; + optional bytes header = 5; + optional int64 header_bytes_sent = 6; + optional int64 bytes_sent = 7; +}; + +// Corresponds to struct Stream. +message StreamProto { + optional bytes header = 1; + optional bytes data = 2; + optional int64 data_size = 3; +}; + +message CubemapStateProto { + repeated ClientProto clients = 1; + repeated StreamProto streams = 2; +}; -- 2.39.2