File: network_generic.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (230 lines) | stat: -rw-r--r-- 4,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
	$Id: network_generic.cpp,v 1.4 2001/12/11 20:44:23 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#ifdef OBSOLUTE_STUFF

#ifdef WIN32
#pragma warning (disable:4786)
#endif

#include <API/Core/System/error.h>
#include <API/Core/System/system.h>
#include <API/Network/netsession.h>
#include <API/Network/network.h>
#include <Network/Generic/network_delivery_impl.h>
#include <Network/Generic/netsession_client.h>
#include <Network/Generic/netsession_server.h>
#include <Network/Generic/network_generic.h>

#ifndef WIN32
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <arpa/inet.h>
	#include <netdb.h>
#else
	#include <windows.h>
#endif

CL_Network_Generic *CL_Network_Generic::self = NULL;

CL_Network_Generic::CL_Network_Generic(CL_ConnectionProvider *provider)
{
	this->provider = provider;
	self = this;
	udp_connection = provider->create_udp_connection(0);
}

CL_Network_Generic::~CL_Network_Generic()
{
	delete udp_connection;
	delete provider;
	self = NULL;
}

void CL_Network_Generic::find_sessions_broadcast(
	std::string game_id,
	int port)
{
	CL_OutputSource_MemoryGeneric msg;
	msg.write_int32(0); // ping message.
	msg.write_int32(game_id.length());
	msg.write(game_id.c_str(), game_id.length());

	CL_UDPConnectionPacket netmsg;
	netmsg.ip_addr = 0;
	netmsg.port = port;
	netmsg.size = msg.size();
	netmsg.data = msg.get_data();

	udp_connection->broadcast(netmsg);
}

void CL_Network_Generic::find_session_at(
	std::string app_id,
	const std::string &host_address,
	int port)
{
	// lookup dns name, if needed.
	unsigned int addr = 0;
	
	addr = inet_addr(host_address);
	if (addr == INADDR_NONE)
	{
		hostent *host = gethostbyname(host_address);
		if (host == NULL) throw CL_Error("Could not lookup DNS name");

		addr = *((unsigned int*) host->h_addr_list[0]);
	}

	queue.push(
		new CL_NetSession_Client(
			addr,
			port,
			app_id,
			this));
}

bool CL_Network_Generic::peek_game_found()
{
	if (udp_connection->peek())
	{
		CL_UDPConnectionPacket netmsg = udp_connection->receive();
		CL_InputSource_MemoryGeneric input(netmsg.data, netmsg.size);

		if (input.read_int32() == 1)
		{
			std::string game_id = input.read_string();
			queue.push(
				new CL_NetSession_Client(
					netmsg.ip_addr,
					netmsg.port,
					game_id,
					this));
		}
	}

	return !queue.empty();
}

CL_NetSession *CL_Network_Generic::receive_session_found(int timeout_millis)
{
	if (timeout_millis > 0) CL_System::sleep(timeout_millis);
	peek_game_found();

	if (queue.empty()) throw CL_Error("Game not found");
	CL_NetSession *session = queue.front();
	queue.pop();
	return session;
}

void CL_Network_Generic::clear_games_found()
{
	while (queue.empty() == false)
	{
		delete queue.front();
		queue.pop();
	}
}

CL_NetSession *CL_Network_Generic::create_session(std::string game_id, int port)
{
	return new CL_NetSession_Server(
		this,
		game_id.c_str(),
		port);
}

void CL_Network_Generic::keep_alive()
{
}

/*
	CL_Network API static function calls:
	-------------------------------------
*/

void CL_Network::find_sessions_broadcast(
	std::string game_id,
	int port)
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	CL_Network_Generic::self->find_sessions_broadcast(
		game_id,
		port);
}
	
void CL_Network::find_session_at(
	std::string app_id,
	const std::string &host_address,
	int port)
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	CL_Network_Generic::self->find_session_at(
		app_id,
		host_address,
		port);
}
	
bool CL_Network::peek_session_found()
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	return CL_Network_Generic::self->peek_game_found();
}

CL_NetSession *CL_Network::receive_session_found(int timeout)
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	return CL_Network_Generic::self->receive_session_found(timeout);
}

void CL_Network::clear_sessions_found()
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	CL_Network_Generic::self->clear_sessions_found();
}

CL_NetSession *CL_Network::create_session(
	std::string session_id,
	int port)
{
	if (CL_Network_Generic::self == NULL)
		throw CL_Error("Network not initialized!");

	return CL_Network_Generic::self->create_session(
		session_id,
		port);
}

// --- netgroup: ---

CL_NetGroup::CL_NetGroup()
{
}

CL_NetGroup::CL_NetGroup(CL_NetComputer *comp)
{
	computers.push_back(comp);
}

#endif