Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

connection_provider_win32.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: connection_provider_win32.cpp,v 1.1 2001/02/15 13:18:52 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #ifdef WIN32
00016 #pragma warning (disable:4786)
00017 #endif
00018 
00019 #include "API/Core/System/mutex.h"
00020 #include "API/Core/System/cl_assert.h" 
00021 #include "API/Core/System/error.h"
00022 #include <Network/Generic/network_delivery_impl.h>
00023 #include <Network/Generic/network_delivery_socket.h>
00024 #include <Network/Win32/connection_provider_win32.h>
00025 
00026 /*********************************************************************
00027                                 CL_Connections_Win32 Implementation
00028 *********************************************************************/
00029 
00030 CL_Connections_Win32::CL_Connections_Win32()
00031 {
00032 }
00033 
00034 CL_Connections_Win32::~CL_Connections_Win32()
00035 {
00036 }
00037 
00038 CL_UDPConnection *CL_Connections_Win32::create_udp_connection(unsigned int port)
00039 {
00040         CL_UniformUDPConnection *ret = new CL_UniformUDPConnection;
00041                                                                                   
00042         bool res = ret->bind(port);
00043 
00044         if (res) 
00045         {
00046                 udp_connections.push_back(ret);
00047                 return ret;
00048         }
00049         else
00050         {
00051                 delete ret;
00052                 return NULL;
00053         }
00054 }
00055 
00056 CL_Connection *CL_Connections_Win32::create_tcp_connection(
00057         int ip_addr,
00058         int port)
00059 {
00060         CL_UniformSocket *socket = new CL_UniformSocket(this);
00061         socket->init_socket();
00062         bool res = socket->try_connect(ip_addr, port);
00063 
00064         if (res) 
00065         {
00066                 connections.push_back(socket);
00067                 return socket;
00068         }
00069         else
00070         {
00071                 delete socket;
00072                 return NULL;
00073         }
00074 }
00075 
00076 CL_Connection *CL_Connections_Win32::accept()
00077 {
00078         std::list<CL_UniformAcceptSocket *>::iterator counter=accepting_connections.begin();
00079         while (counter!=accepting_connections.end())
00080         {
00081                 CL_UniformSocket *ret = (*counter)->accept();
00082                 if (ret != NULL) 
00083                 {
00084                         connections.push_back(ret);
00085                         return ret;
00086                 }
00087                 counter++;
00088         }
00089         return NULL;
00090 }
00091 
00092 void CL_Connections_Win32::start_accept_on_port(int port)
00093 {
00094         CL_UniformAcceptSocket *new_accept_socket = new CL_UniformAcceptSocket(this);
00095         bool res = new_accept_socket->bind(port);
00096 
00097         if (res == false) throw CL_Error("Failed to bind socket to port");
00098 
00099         accepting_connections.push_back(new_accept_socket);
00100 }
00101 
00102 void CL_Connections_Win32::stop_accept_on_port(int port)
00103 {
00104         std::list<CL_UniformAcceptSocket *>::iterator counter=accepting_connections.begin();
00105         while (counter!=accepting_connections.begin())
00106         {
00107                 if ((*counter)->get_port() == port)
00108                 {
00109                         delete *counter;
00110                         accepting_connections.erase(counter);
00111                         return;
00112                 }
00113         }
00114 }
00115 
00116 void CL_Connections_Win32::wait_for_connection_data(CL_Mutex *mutex)
00117 {
00118         fd_set fdread;
00119         FD_ZERO(&fdread);
00120 
00121         fd_set fdwrite;
00122         FD_ZERO(&fdwrite);
00123 
00124         mutex->enter();
00125 
00126         std::list<CL_UniformSocket *>::iterator counter=connections.begin();
00127         while (counter!=connections.end())
00128         {
00129                 if ((*counter)->connection_lost() == false)
00130                 {
00131                         FD_SET((*counter)->get_socket(), &fdread);
00132                         if ((*counter)->send_avail() == false)
00133                                 FD_SET((*counter)->get_socket(), &fdwrite);
00134                 }
00135                 counter++;
00136         }
00137 
00138         std::list<CL_UniformUDPConnection *>::iterator counter2=udp_connections.begin();
00139         while (counter2!=udp_connections.end())
00140         {
00141                 FD_SET((*counter2)->get_socket(), &fdread);
00142 //              if ((*counter2)->send_avail() == false)
00143 //                      FD_SET((*counter)->get_socket(), &fdwrite);
00144                 counter2++;
00145         }
00146 
00147         std::list<CL_UniformAcceptSocket *>::iterator counter3=accepting_connections.begin();
00148         while (counter3!=accepting_connections.end())
00149         {
00150                 FD_SET((*counter3)->get_socket(), &fdread);
00151                 if ((*counter3)->send_avail() == false)
00152                         FD_SET((*counter)->get_socket(), &fdwrite);
00153                 counter3++;
00154         }
00155 
00156         mutex->leave();
00157 
00158         timeval tv;
00159         tv.tv_sec = 0;
00160         tv.tv_usec = 1000;
00161 
00162         ::select(0, &fdread, &fdwrite, NULL, &tv);
00163 }
00164 
00165 void CL_Connections_Win32::remove_connection(CL_Connection *removed_connection)
00166 {
00167         std::list<CL_UniformSocket *>::iterator counter=connections.begin();
00168         while (counter!=connections.end())
00169         {
00170                 if ((*counter) == removed_connection) 
00171                 {
00172                         connections.erase(counter);
00173                         return;
00174                 }
00175                 counter++;
00176         }
00177 }

Generated at Wed Apr 4 19:53:59 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001