File: connection_provider_win32.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 (193 lines) | stat: -rw-r--r-- 4,322 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
/*
	$Id: connection_provider_win32.cpp,v 1.2 2001/03/29 11:58:45 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 WIN32
#pragma warning (disable:4786)
#endif

#include "API/Core/System/mutex.h"
#include "API/Core/System/cl_assert.h" 
#include "API/Core/System/error.h"
#include "API/Core/System/system.h"
#include <Network/Generic/network_delivery_impl.h>
#include <Network/Generic/network_delivery_socket.h>
#include <Network/Win32/connection_provider_win32.h>

/*********************************************************************
				CL_Connections_Win32 Implementation
*********************************************************************/

CL_Connections_Win32::CL_Connections_Win32()
{
}

CL_Connections_Win32::~CL_Connections_Win32()
{
}

CL_UDPConnection *CL_Connections_Win32::create_udp_connection(unsigned int port)
{
	CL_UniformUDPConnection *ret = new CL_UniformUDPConnection;
										  
	bool res = ret->bind(port);

	if (res) 
	{
		udp_connections.push_back(ret);
		return ret;
	}
	else
	{
		delete ret;
		return NULL;
	}
}

CL_Connection *CL_Connections_Win32::create_tcp_connection(
	int ip_addr,
	int port)
{
	CL_UniformSocket *socket = new CL_UniformSocket(this);
	socket->init_socket();
	bool res = socket->try_connect(ip_addr, port);

	if (res) 
	{
		connections.push_back(socket);
		return socket;
	}
	else
	{
		delete socket;
		return NULL;
	}
}

CL_Connection *CL_Connections_Win32::accept()
{
	std::list<CL_UniformAcceptSocket *>::iterator counter=accepting_connections.begin();
	while (counter!=accepting_connections.end())
	{
		CL_UniformSocket *ret = (*counter)->accept();
		if (ret != NULL) 
		{
			connections.push_back(ret);
			return ret;
		}
		counter++;
	}
	return NULL;
}

void CL_Connections_Win32::start_accept_on_port(int port)
{
	CL_UniformAcceptSocket *new_accept_socket = new CL_UniformAcceptSocket(this);
	bool res = new_accept_socket->bind(port);

	if (res == false) throw CL_Error("Failed to bind socket to port");

	accepting_connections.push_back(new_accept_socket);
}

void CL_Connections_Win32::stop_accept_on_port(int port)
{
	std::list<CL_UniformAcceptSocket *>::iterator counter=accepting_connections.begin();
	while (counter!=accepting_connections.begin())
	{
		if ((*counter)->get_port() == port)
		{
			delete *counter;
			accepting_connections.erase(counter);
			return;
		}
	}
}

void CL_Connections_Win32::wait_for_connection_data(CL_Mutex *mutex)
{
	fd_set fdread;
	FD_ZERO(&fdread);

	fd_set fdwrite;
	FD_ZERO(&fdwrite);

	mutex->enter();

	bool added_connection = false;

	std::list<CL_UniformSocket *>::iterator counter=connections.begin();
	while (counter!=connections.end())
	{
		if ((*counter)->connection_lost() == false)
		{
			added_connection = true;

			FD_SET((*counter)->get_socket(), &fdread);
			if ((*counter)->send_avail() == false)
				FD_SET((*counter)->get_socket(), &fdwrite);
		}
		counter++;
	}

	std::list<CL_UniformUDPConnection *>::iterator counter2=udp_connections.begin();
	while (counter2!=udp_connections.end())
	{
		added_connection = true;

		FD_SET((*counter2)->get_socket(), &fdread);
//		if ((*counter2)->send_avail() == false)
//			FD_SET((*counter)->get_socket(), &fdwrite);
		counter2++;
	}

	std::list<CL_UniformAcceptSocket *>::iterator counter3=accepting_connections.begin();
	while (counter3!=accepting_connections.end())
	{
		added_connection = true;

		FD_SET((*counter3)->get_socket(), &fdread);
		if ((*counter3)->send_avail() == false)
			FD_SET((*counter)->get_socket(), &fdwrite);
		counter3++;
	}

	mutex->leave();

	if (added_connection)
	{
		timeval tv;
		tv.tv_sec = 0;
		tv.tv_usec = 1000;

		::select(0, &fdread, &fdwrite, NULL, &tv);
	}
	else
	{
		CL_System::sleep(1000);
	}
}

void CL_Connections_Win32::remove_connection(CL_Connection *removed_connection)
{
	std::list<CL_UniformSocket *>::iterator counter=connections.begin();
	while (counter!=connections.end())
	{
		if ((*counter) == removed_connection) 
		{
			connections.erase(counter);
			return;
		}
		counter++;
	}
}