00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef header_netsession_server
00016 #define header_netsession_server
00017
00018 #include <list>
00019 #include <queue>
00020 #include <string>
00021 #include <map>
00022 #include "API/Core/System/thread.h"
00023 #include "API/Network/netgroup.h"
00024 #include "netsession_generic.h"
00025 #include "netcomputer_generic.h"
00026
00027 class CL_NetComputer_Host;
00028 class CL_NetSession_Server;
00029 class CL_Connection;
00030 class CL_UDPConnection;
00031 class CL_Mutex;
00032
00033 class CL_NetComputer_Host : public CL_NetComputer_Generic
00034 {
00035 public:
00036 CL_NetComputer_Host(CL_NetSession_Server *session, CL_Connection *connection, int id);
00037 virtual ~CL_NetComputer_Host();
00038
00039 virtual unsigned long get_address() const;
00040 virtual void disconnect();
00041
00042 int id;
00043 CL_Connection *connection;
00044 };
00045
00046 class CL_NetChannelQueue : public std::queue<CL_NetMessage>
00047 {
00048 public:
00049 std::map<int, int> access;
00050 };
00051
00052
00053 class CL_NetSession_Server : public CL_NetSession_Generic, CL_Runnable
00054 {
00055
00056 public:
00057 CL_NetSession_Server(
00058 CL_ConnectionProvider *provider,
00059 const char *game_id,
00060 int port);
00061
00062 virtual ~CL_NetSession_Server();
00063
00064
00065 public:
00066 virtual CL_NetComputer &get_server();
00067
00068 virtual CL_NetGroup &get_all();
00069
00070 virtual bool peek(int channel) const;
00071
00072 virtual int access_status(int channel) const;
00073
00074 virtual bool is_writable(int channel) const;
00075
00076 virtual bool is_readable(int channel) const;
00077
00078
00079 public:
00080 virtual void keep_alive();
00081
00082 virtual CL_NetMessage receive(
00083 int channel,
00084 int timeout=-1);
00085
00086 void send(
00087 const int dest_channel,
00088 const CL_NetComputer &dest,
00089 const CL_NetMessage &message,
00090 bool reliable = true);
00091
00092 virtual void send(
00093 const int dest_channel,
00094 const CL_NetGroup &dest,
00095 const CL_NetMessage &message,
00096 bool reliable = true);
00097
00098 virtual CL_NetComputer receive_computer_leave();
00099
00100 virtual CL_NetComputer receive_computer_join();
00101
00102 virtual CL_NetComputer receive_computer_rejoin();
00103
00104 virtual bool receive_session_closed();
00105
00106
00107 virtual int receive_access_changed();
00108
00109
00110 virtual void set_access(
00111 int channel,
00112 const CL_NetComputer &computer,
00113 int access_rights=ACCESS_CHANNEL_READ|ACCESS_CHANNEL_WRITE);
00114
00115 virtual void set_access(
00116 int channel,
00117 const CL_NetGroup &group,
00118 int access_rights=ACCESS_CHANNEL_READ|ACCESS_CHANNEL_WRITE);
00119
00120 private:
00121 CL_NetComputer_Host *get_computer_host(const CL_NetComputer &comp)
00122 { return static_cast<CL_NetComputer_Host *>(comp.impl); }
00123
00124
00125 CL_NetChannelQueue *find_queue(int netchannel) const;
00126
00127
00128 CL_NetChannelQueue *create_queue(int netchannel);
00129
00130
00131 bool has_read_access(
00132 CL_NetChannelQueue *channel,
00133 CL_NetComputer_Host *host);
00134
00135 void check_trigger();
00136
00137
00138 private:
00139 CL_NetGroup all;
00140 std::list<CL_NetComputer_Host*> prejoin;
00141 std::list<CL_NetComputer_Host*> computers;
00142
00143 std::map<int ,CL_NetChannelQueue> netchannels;
00144
00145 CL_UDPConnection *udp_connection;
00146
00147 int id_counter;
00148 std::string game_id;
00149 int port;
00150
00151 std::queue<CL_NetComputer_Host*> join_queue;
00152 std::queue<CL_NetComputer_Host*> rejoin_queue;
00153 std::queue<CL_NetComputer_Host*> leave_queue;
00154
00155 private:
00156 CL_Mutex *mutex;
00157 CL_Thread *thread;
00158 volatile bool exit_thread;
00159
00160 virtual void run();
00161
00162 friend CL_NetComputer_Host;
00163 };
00164
00165
00166 #endif