00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifdef OBSOLUTE_STUFF
00016
00017 #ifdef WIN32
00018 #pragma warning (disable:4786)
00019 #endif
00020
00021 #include <API/Core/System/error.h>
00022 #include <API/Core/System/system.h>
00023 #include <API/Network/netsession.h>
00024 #include <API/Network/network.h>
00025 #include <Network/Generic/network_delivery_impl.h>
00026 #include <Network/Generic/netsession_client.h>
00027 #include <Network/Generic/netsession_server.h>
00028 #include <Network/Generic/network_generic.h>
00029
00030 #ifndef WIN32
00031 #include <sys/socket.h>
00032 #include <netinet/in.h>
00033 #include <arpa/inet.h>
00034 #include <netdb.h>
00035 #else
00036 #include <windows.h>
00037 #endif
00038
00039 CL_Network_Generic *CL_Network_Generic::self = NULL;
00040
00041 CL_Network_Generic::CL_Network_Generic(CL_ConnectionProvider *provider)
00042 {
00043 this->provider = provider;
00044 self = this;
00045 udp_connection = provider->create_udp_connection(0);
00046 }
00047
00048 CL_Network_Generic::~CL_Network_Generic()
00049 {
00050 delete udp_connection;
00051 delete provider;
00052 self = NULL;
00053 }
00054
00055 void CL_Network_Generic::find_sessions_broadcast(
00056 std::string game_id,
00057 int port)
00058 {
00059 CL_OutputSource_MemoryGeneric msg;
00060 msg.write_int32(0);
00061 msg.write_int32(game_id.length());
00062 msg.write(game_id.c_str(), game_id.length());
00063
00064 CL_UDPConnectionPacket netmsg;
00065 netmsg.ip_addr = 0;
00066 netmsg.port = port;
00067 netmsg.size = msg.size();
00068 netmsg.data = msg.get_data();
00069
00070 udp_connection->broadcast(netmsg);
00071 }
00072
00073 void CL_Network_Generic::find_session_at(
00074 std::string app_id,
00075 const char *host_address,
00076 int port)
00077 {
00078
00079 unsigned int addr = 0;
00080
00081 addr = inet_addr(host_address);
00082 if (addr == INADDR_NONE)
00083 {
00084 hostent *host = gethostbyname(host_address);
00085 if (host == NULL) throw CL_Error("Could not lookup DNS name");
00086
00087 addr = *((unsigned int*) host->h_addr_list[0]);
00088 }
00089
00090 queue.push(
00091 new CL_NetSession_Client(
00092 addr,
00093 port,
00094 app_id,
00095 this));
00096 }
00097
00098 bool CL_Network_Generic::peek_game_found()
00099 {
00100 if (udp_connection->peek())
00101 {
00102 CL_UDPConnectionPacket netmsg = udp_connection->receive();
00103 CL_InputSource_MemoryGeneric input(netmsg.data, netmsg.size);
00104
00105 if (input.read_int32() == 1)
00106 {
00107 std::string game_id = input.read_string();
00108 queue.push(
00109 new CL_NetSession_Client(
00110 netmsg.ip_addr,
00111 netmsg.port,
00112 game_id,
00113 this));
00114 }
00115 }
00116
00117 return !queue.empty();
00118 }
00119
00120 CL_NetSession *CL_Network_Generic::receive_session_found(int timeout_millis)
00121 {
00122 if (timeout_millis > 0) CL_System::sleep(timeout_millis);
00123 peek_game_found();
00124
00125 if (queue.empty()) throw CL_Error("Game not found");
00126 CL_NetSession *session = queue.front();
00127 queue.pop();
00128 return session;
00129 }
00130
00131 void CL_Network_Generic::clear_games_found()
00132 {
00133 while (queue.empty() == false)
00134 {
00135 delete queue.front();
00136 queue.pop();
00137 }
00138 }
00139
00140 CL_NetSession *CL_Network_Generic::create_session(std::string game_id, int port)
00141 {
00142 return new CL_NetSession_Server(
00143 this,
00144 game_id.c_str(),
00145 port);
00146 }
00147
00148 void CL_Network_Generic::keep_alive()
00149 {
00150 }
00151
00152
00153
00154
00155
00156
00157 void CL_Network::find_sessions_broadcast(
00158 std::string game_id,
00159 int port)
00160 {
00161 if (CL_Network_Generic::self == NULL)
00162 throw CL_Error("Network not initialized!");
00163
00164 CL_Network_Generic::self->find_sessions_broadcast(
00165 game_id,
00166 port);
00167 }
00168
00169 void CL_Network::find_session_at(
00170 std::string app_id,
00171 const char *host_address,
00172 int port)
00173 {
00174 if (CL_Network_Generic::self == NULL)
00175 throw CL_Error("Network not initialized!");
00176
00177 CL_Network_Generic::self->find_session_at(
00178 app_id,
00179 host_address,
00180 port);
00181 }
00182
00183 bool CL_Network::peek_session_found()
00184 {
00185 if (CL_Network_Generic::self == NULL)
00186 throw CL_Error("Network not initialized!");
00187
00188 return CL_Network_Generic::self->peek_game_found();
00189 }
00190
00191 CL_NetSession *CL_Network::receive_session_found(int timeout)
00192 {
00193 if (CL_Network_Generic::self == NULL)
00194 throw CL_Error("Network not initialized!");
00195
00196 return CL_Network_Generic::self->receive_session_found(timeout);
00197 }
00198
00199 void CL_Network::clear_sessions_found()
00200 {
00201 if (CL_Network_Generic::self == NULL)
00202 throw CL_Error("Network not initialized!");
00203
00204 CL_Network_Generic::self->clear_sessions_found();
00205 }
00206
00207 CL_NetSession *CL_Network::create_session(
00208 std::string session_id,
00209 int port)
00210 {
00211 if (CL_Network_Generic::self == NULL)
00212 throw CL_Error("Network not initialized!");
00213
00214 return CL_Network_Generic::self->create_session(
00215 session_id,
00216 port);
00217 }
00218
00219
00220
00221 CL_NetGroup::CL_NetGroup()
00222 {
00223 }
00224
00225 CL_NetGroup::CL_NetGroup(CL_NetComputer *comp)
00226 {
00227 computers.push_back(comp);
00228 }
00229
00230 #endif