simplistic packets
authorterom
Thu, 06 Nov 2008 22:23:59 +0000
changeset 15 7710cce889b2
parent 14 22e3bfb6720d
child 16 f54587940861
simplistic packets
src/proto2/Network.hh
src/proto2/NetworkConfig.hh
src/proto2/NetworkServer.cc
src/proto2/Protocol.hh
--- a/src/proto2/Network.hh	Wed Nov 05 22:29:31 2008 +0000
+++ b/src/proto2/Network.hh	Thu Nov 06 22:23:59 2008 +0000
@@ -3,8 +3,7 @@
 
 #include <ClanLib/network.h>
 
-const std::string NETWORK_PORT_STR = "9338";
-const uint16_t NETWORK_PACKET_MAX = 1280;
+#include "NetworkConfig.hh"
 
 class NetworkBase {
 	protected:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/NetworkConfig.hh	Thu Nov 06 22:23:59 2008 +0000
@@ -0,0 +1,20 @@
+#ifndef NETWORK_CONFIG_HH
+#define NETWORK_CONFIG_HH
+
+#ifndef WIN32
+        #include <sys/socket.h>
+        #include <netinet/in.h>
+        #include <arpa/inet.h>
+        #include <netdb.h>
+        #include <sys/time.h>
+        #include <unistd.h> 
+#else
+        #include <winsock2.h>
+        #include <windows.h>
+        typedef int socklen_t;
+#endif
+
+const std::string NETWORK_PORT_STR = "9338";
+const uint16_t NETWORK_PACKET_MAX = 1280;
+
+#endif /* NETWORK_CONFIG_HH */
--- a/src/proto2/NetworkServer.cc	Wed Nov 05 22:29:31 2008 +0000
+++ b/src/proto2/NetworkServer.cc	Thu Nov 06 22:23:59 2008 +0000
@@ -1,4 +1,5 @@
 #include "NetworkServer.hh"
+#include "Protocol.hh"
 
 #include <iostream>
 #include <cassert>
@@ -32,17 +33,28 @@
 }
 
 void NetworkServer::_onRecv (void) {
-	char buf[NETWORK_PACKET_MAX];
-	CL_IPAddress src;
-	int ret;
+    NetworkPacket pkt;
 
-	std::cout << "NetworkServer::_onRecv: recv" << std::endl;
+    pkt.recvFromSocket(socket);
 
-	ret = socket.recv((void*) buf, NETWORK_PACKET_MAX, src);
+	std::cout << pkt.src.get_address() << ":" << pkt.src.get_port() << " <- ";
+    
+    enum packet_type type = pkt.read_pkt_type();
 
-	assert(ret > 0);
+    switch (type) {
+        case PKT_HELLO: 
+        {
+            pkt_Hello hello(pkt);
 
-	std::cout << src.get_address() << ":" << src.get_port() << " <- " << std::string(buf, ret) << std::endl;
+            std::cout << "\tPKT_HELLO: " << hello.player_id << std::endl;
+        } break;
+
+        default:
+        {
+            std::cout << "\t???:" << type << std::endl;
+
+        } break;
+    }    
 }
 		
 NetworkServerClient::NetworkServerClient (NetworkServer &server) : server(server) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Protocol.hh	Thu Nov 06 22:23:59 2008 +0000
@@ -0,0 +1,74 @@
+#ifndef PROTOCOL_HH
+#define PROTOCOL_HH
+
+#include "NetworkConfig.hh"
+#include "Network.hh"
+
+#include <stdexcept>
+
+enum packet_type {
+    PKT_INVALID,
+    PKT_HELLO
+};
+
+class NetworkPacket {
+    private:
+        char buf[NETWORK_PACKET_MAX];
+        size_t size;
+        size_t offset;
+    public:
+        CL_IPAddress src;
+
+    public:
+        NetworkPacket () : size(0), offset(0) { }
+
+        void recvFromSocket (CL_Socket &sock) {
+            size = sock.recv((void *) buf, NETWORK_PACKET_MAX, src);
+
+            offset = 0;
+        }
+    
+        template <typename T> void read_into (T* val_ptr) {
+            if (offset + sizeof(T) > size)
+                throw std::logic_error("short packet");
+
+            *val_ptr = *((T*) (buf + offset));
+
+            offset += sizeof(T);
+        }
+
+        template <typename T> T read_type (void) {
+            T val;
+
+            read_into(&val);
+
+            return val;
+        }
+        
+        uint8_t read_uint8 (void) {
+            return read_type<uint8_t>();
+        }
+
+        uint16_t read_uint16 (void) {
+            return htons(read_type<uint16_t>());
+        }
+
+        enum packet_type read_pkt_type (void) {
+            return (enum packet_type) read_uint8();
+        }
+};
+
+struct pkt_base {
+    NetworkPacket &packet;
+    
+    pkt_base (NetworkPacket &packet) : packet(packet) { }
+};
+
+struct pkt_Hello : public pkt_base {
+    uint16_t player_id;
+
+    pkt_Hello (NetworkPacket &packet) : pkt_base(packet), 
+        player_id(packet.read_uint16()) { }
+};
+
+#endif