# HG changeset patch # User terom # Date 1226010239 0 # Node ID 7710cce889b2a0787575a2c198568c42da89ff9a # Parent 22e3bfb6720dc643710bd9460a480671034ba25d simplistic packets diff -r 22e3bfb6720d -r 7710cce889b2 src/proto2/Network.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 -const std::string NETWORK_PORT_STR = "9338"; -const uint16_t NETWORK_PACKET_MAX = 1280; +#include "NetworkConfig.hh" class NetworkBase { protected: diff -r 22e3bfb6720d -r 7710cce889b2 src/proto2/NetworkConfig.hh --- /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 + #include + #include + #include + #include + #include +#else + #include + #include + typedef int socklen_t; +#endif + +const std::string NETWORK_PORT_STR = "9338"; +const uint16_t NETWORK_PACKET_MAX = 1280; + +#endif /* NETWORK_CONFIG_HH */ diff -r 22e3bfb6720d -r 7710cce889b2 src/proto2/NetworkServer.cc --- 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 #include @@ -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) { diff -r 22e3bfb6720d -r 7710cce889b2 src/proto2/Protocol.hh --- /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 + +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 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 T read_type (void) { + T val; + + read_into(&val); + + return val; + } + + uint8_t read_uint8 (void) { + return read_type(); + } + + uint16_t read_uint16 (void) { + return htons(read_type()); + } + + 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