Packet.hh

00001 /*
00002 ** TowBowlTactics, a turn-based strategy football game.
00003 **
00004 ** Copyright (C) 2006-2010 The TBT Team.
00005 **
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 **
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 **
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef PACKET_HH_
00018 # define PACKET_HH_
00019 
00020 # include <sstream>
00021 
00022 const unsigned CX_TOKEN_START = 0x8000;
00023 
00030 enum eCxToken {
00031   CX_INIT = CX_TOKEN_START,
00032   CX_ACCEPT,
00033   CX_DENY,
00034   CX_QUERYLIST,
00035   CX_LIST,
00036   CX_JOIN,
00037   CX_READY,
00038   CX_ABORT,
00039   CX_ERROR,
00040   CX_TOKEN_LAST,
00041 };
00042 
00043 const unsigned PACKET_SIZE_MAX  = 2048;
00044 
00051 struct Packet
00052 {
00054   unsigned      token;
00058   int           client_id;
00060   unsigned      data_size;
00061 
00062   Packet(int token = 0, int client_id = -1);
00063   friend std::ostream& operator<< (std::ostream& os, const Packet& pkt);
00064 };
00065 
00066 
00067 inline Packet::Packet(int token, int client_id)
00068   : token(token), client_id(client_id)
00069 {
00070   data_size = sizeof(*this);
00071 }
00072 
00073 inline std::ostream& operator<< (std::ostream& os, const Packet& pkt)
00074 {
00075   os << "*** Packet dump ***" << std::endl;
00076   os << "  - token : " << pkt.token << std::endl;
00077   os << "  - player_id : " << pkt.client_id << std::endl;
00078   os << "  - data_size : " << pkt.data_size << std::endl;
00079   return os;
00080 }
00081 
00084 
00089 void          stringToPacket(int* dst, const std::string& src, unsigned max_size);
00090 
00092 std::string   packetToString(const int* src);
00093 
00095 
00096 
00097 //
00098 // Cx packets, managed by clients and server, not by rules.
00099 //
00100 
00102 struct CxInit : public Packet
00103 {
00104   CxInit()
00105    : Packet(CX_INIT, -1) { data_size = sizeof(*this); }
00106   int binary_version;
00107 };
00108 
00110 struct CxDeny : public Packet
00111 {
00112   CxDeny()
00113    : Packet(CX_DENY, -1) { data_size = sizeof(*this); }
00114   int reason[16];
00115 };
00116 
00118 struct CxError : public Packet
00119 {
00120   CxError(int player_id = -1)
00121     : Packet(CX_ERROR, player_id) { data_size = sizeof(*this); }
00122   int msg[16];
00123 };
00124 
00125 
00126 #endif /* !PACKET_HH_ */
Generated on Mon Apr 5 21:17:12 2010 for Stechec/TBT by  doxygen 1.6.3