00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef CX_HH_
00014 # define CX_HH_
00015
00016 # include "datatfs/Packet.hh"
00017 # include "misc/Exception.hh"
00018
00020 const unsigned CX_TOKEN_START = 0x8000;
00021
00027 enum eCxToken {
00028 CX_INIT = CX_TOKEN_START,
00029 CX_ACCEPT,
00030 CX_DENY,
00031 CX_QUERYLIST,
00032 CX_LIST,
00033 CX_JOIN,
00034 CX_READY,
00035 CX_ABORT,
00036 CX_ERROR,
00037 CX_TOKEN_LAST,
00038 };
00039
00044 class NetError : public Exception
00045 {
00046 public:
00049 NetError(const std::string& msg);
00050 };
00051
00056 class NetSysError : public NetError
00057 {
00058 public:
00061 NetSysError(const std::string& msg);
00062 };
00063
00066
00067 const unsigned PACKET_SIZE_MAX = 2048;
00068 const unsigned BUF_SEND_SIZE = PACKET_SIZE_MAX * 4;
00069 const unsigned BUF_RECV_SIZE = PACKET_SIZE_MAX * 2;
00070
00071 const int CX_CLOSED = 0x00;
00072 const int CX_READ = 0x01;
00073 const int CX_WRITE = 0x02;
00074 const int CX_RW = 0x03;
00075 const int CX_CONNECTING = 0x04;
00076 const int CX_LISTENING = 0x08;
00077
00079
00097 class Cx
00098 {
00099 template<typename> friend class CxPool;
00100
00101 public:
00102 Cx();
00103 virtual ~Cx();
00104
00109 bool poll(int timeout = 0);
00110
00112 void begin();
00114 void commit();
00119 void send(const Packet& pkt);
00120
00124 Packet* receive();
00125
00127 friend std::ostream& operator<< (std::ostream& os, const Cx& cx);
00128
00129 protected:
00130 virtual bool pollInternal(int* timeout) = 0;
00131 virtual int recvData(bool use_exception) = 0;
00132 virtual void sendData(unsigned char* data, unsigned size) = 0;
00133
00135 virtual void print(std::ostream& os) const;
00136
00137 int fd_;
00138 int state_;
00139 char last_error_[64];
00140
00141
00142 unsigned char buff_send_[BUF_SEND_SIZE];
00143 bool batch_send_;
00144 unsigned buff_send_tail_;
00145
00146
00147 unsigned char buff_recv_[BUF_RECV_SIZE];
00148 unsigned buff_recv_head_;
00149 unsigned buff_recv_tail_;
00150
00151 private:
00152 bool arrangeReadBuffer();
00153 };
00154
00155
00156 inline void Cx::print(std::ostream&) const
00157 {
00158 }
00159
00160 inline std::ostream& operator<< (std::ostream& os, const Cx& cx)
00161 {
00162 cx.print(os);
00163 return os;
00164 }
00165
00166
00167
00168
00169
00171 struct CxInit : public Packet
00172 {
00173 CxInit()
00174 : Packet(CX_INIT, -1) { data_size = sizeof(*this); }
00175 int binary_version;
00176 };
00177
00179 struct CxDeny : public Packet
00180 {
00181 CxDeny()
00182 : Packet(CX_DENY, -1) { data_size = sizeof(*this); }
00183 int reason[16];
00184 };
00185
00187 struct CxList : public Packet
00188 {
00189 CxList()
00190 : Packet(CX_LIST, -1) { data_size = sizeof(*this); }
00191 int server_name[8];
00192 int game_uid;
00193 int nb_coach;
00194 int nb_viewer;
00195 };
00196
00198 struct CxJoin : public Packet
00199 {
00200 CxJoin(int player_id = -1)
00201 : Packet(CX_JOIN, player_id) { data_size = sizeof(*this); }
00202 int rules_name[8];
00203 int server_lib[16];
00204 int rules_major;
00205 int rules_minor;
00206 int game_uid;
00207 int is_coach;
00208 int client_extid;
00209 };
00210
00212 struct CxError : public Packet
00213 {
00214 CxError(int player_id = -1)
00215 : Packet(CX_ERROR, player_id) { data_size = sizeof(*this); }
00216 int msg[16];
00217 };
00218
00219 #endif