]> git.sesse.net Git - cubemap/commitdiff
Start working on serialization.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 20:08:14 +0000 (22:08 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 20:08:14 +0000 (22:08 +0200)
.gitignore
Makefile
server.cpp
server.h
state.proto [new file with mode: 0644]

index 7212a665713e2e9d2bb1ad21c9f48d3fc48b78e4..aa8fde8410e66505a124ca726baa4cb7aa1edf49 100644 (file)
@@ -1,3 +1,5 @@
 cubemap
 *.o
 *.d
+*.pb.cc
+*.pb.h
index 78ec5a6e26edb10dd26191c9ccc46f6ff5447095..704ee7304ec7b9ffab9a2dfdcb5eced7c1e3e53a 100644 (file)
--- 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)
index 6dcddf0b4431e983909e012cb863636f6ccab635..9c4219a4789d263bc2d8b61a8d8e65143d717bad 100644 (file)
@@ -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()
 {
index 6311c68095c6ef9558bbb7518fb5e8d9a2caae02..d0e3d6a75a705b0ed9a2a16624155b0227800828 100644 (file)
--- a/server.h
+++ b/server.h
 #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 (file)
index 0000000..7c9ca7f
--- /dev/null
@@ -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;
+};