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 7212a66..aa8fde8 100644 (file)
@@ -1,3 +1,5 @@
 cubemap
 *.o
 *.d
+*.pb.cc
+*.pb.h
index 78ec5a6..704ee73 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 6dcddf0..9c4219a 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 6311c68..d0e3d6a 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;
+};