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
|
/*
$Id: connection_provider_unix.cpp,v 1.3 2001/04/23 21:46:43 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.
------------------------------------------------------------------------
*/
#include "Core/precomp.h"
#ifdef USE_NETWORK
#include <API/Core/System/cl_assert.h>
#include <API/Core/System/error.h>
#include <API/Core/System/mutex.h>
#include <Network/Generic/network_delivery_impl.h>
#include <Network/Generic/network_delivery_socket.h>
#include <Network/Unix/network_unix.h>
#include <Network/Unix/connection_provider_unix.h>
/*********************************************************************
CL_Connections_Unix Implementation
*********************************************************************/
CL_Connections_Unix::CL_Connections_Unix()
{
}
CL_Connections_Unix::~CL_Connections_Unix()
{
}
CL_UDPConnection *CL_Connections_Unix::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_Unix::get_client()
{
return NULL;
}
CL_Connection *CL_Connections_Unix::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_Unix::accept()
{
for (
std::list<CL_UniformAcceptSocket*>::iterator it = accepting_connections.begin();
it != accepting_connections.end();
it++)
{
CL_UniformSocket *ret = (*it)->accept();
if (ret != NULL)
{
connections.push_back(ret);
return ret;
}
}
return NULL;
}
void CL_Connections_Unix::start_accept_on_port(int port)
{
CL_UniformAcceptSocket *new_accept_socket = new CL_UniformAcceptSocket(this);
bool res = new_accept_socket->bind(port);
// Argh - we need some robustness bad!
if (res == false) throw CL_Error("Port already in use an other application!");
accepting_connections.push_back(new_accept_socket);
}
void CL_Connections_Unix::stop_accept_on_port(int port)
{
for (
std::list<CL_UniformAcceptSocket *>::iterator it = accepting_connections.begin();
it != accepting_connections.end();
it++)
{
if ((*it)->get_port() == port)
{
delete (*it);
it = accepting_connections.erase(it);
return;
}
}
}
void CL_Connections_Unix::wait_for_connection_data(CL_Mutex *mutex)
{
mutex->enter();
fd_set fdread;
FD_ZERO(&fdread);
fd_set fdwrite;
FD_ZERO(&fdwrite);
int highest_fd = -1;
/*
if (client_connection != NULL)
{
highest_fd = client_connection->get_socket();
FD_SET(client_connection->get_socket(), &fdread);
}
*/
for (
std::list<CL_UniformSocket *>::iterator counter = connections.begin();
counter != connections.end();
counter++)
{
if ((*counter)->connection_lost() == false)
{
if ((*counter)->get_socket() > highest_fd)
highest_fd = (*counter)->get_socket();
FD_SET((*counter)->get_socket(), &fdread);
if ((*counter)->send_avail() == false)
FD_SET((*counter)->get_socket(), &fdwrite);
}
}
for (
std::list<CL_UniformUDPConnection *>::iterator counter2 = udp_connections.begin();
counter2 != udp_connections.end();
counter2++)
{
if ((*counter2)->get_socket() > highest_fd)
highest_fd = (*counter2)->get_socket();
FD_SET((*counter2)->get_socket(), &fdread);
// if ((*counter2)->send_avail() == false)
// FD_SET((*counter)->get_socket(), &fdwrite);
}
for (
std::list<CL_UniformAcceptSocket *>::iterator counter3 = accepting_connections.begin();
counter3 != accepting_connections.end();
counter3++)
{
if ((*counter3)->get_socket() > highest_fd)
highest_fd = (*counter3)->get_socket();
if ((*counter3)->send_avail() == false)
FD_SET((*counter3)->get_socket(), &fdwrite);
FD_SET((*counter3)->get_socket(), &fdread);
}
mutex->leave();
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000;
::select(highest_fd+1, &fdread, &fdwrite, NULL, &tv);
// don't assert if select() fails. It can happen because we select
// outside the mutex section, and the game thread may close a socket in
// the mean time.
}
void CL_Connections_Unix::remove_connection(CL_Connection *removed_connection)
{
connections.remove((CL_UniformSocket *) removed_connection);
}
#endif // USE_NETWORK
|