2009-01-29 00:18:34 +08:00
|
|
|
/*
|
2010-01-07 04:37:29 +08:00
|
|
|
* DSFML - SFML Library wrapper for the D programming language.
|
|
|
|
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
|
|
|
|
* Copyright (C) 2010 Andreas Hollandt
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* This software is provided 'as-is', without any express or
|
|
|
|
* implied warranty. In no event will the authors be held
|
|
|
|
* liable for any damages arising from the use of this software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute
|
|
|
|
* it freely, subject to the following restrictions:
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 1. The origin of this software must not be misrepresented;
|
|
|
|
* you must not claim that you wrote the original software.
|
|
|
|
* If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but
|
|
|
|
* is not required.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 2. Altered source versions must be plainly marked as such,
|
|
|
|
* and must not be misrepresented as being the original software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 3. This notice may not be removed or altered from any
|
|
|
|
* source distribution.
|
2009-01-29 00:18:34 +08:00
|
|
|
*/
|
|
|
|
|
2010-04-21 03:51:48 +08:00
|
|
|
module dsfml.network.socketselector;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-04-21 03:51:48 +08:00
|
|
|
import dsfml.network.tcpsocket;
|
|
|
|
import dsfml.network.udpsocket;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
import dsfml.system.common;
|
|
|
|
|
|
|
|
/**
|
2010-04-21 03:51:48 +08:00
|
|
|
* Multiplexer that allows to read from multiple sockets
|
2010-03-16 07:35:53 +08:00
|
|
|
*/
|
2010-04-21 03:51:48 +08:00
|
|
|
class SocketSelector : DSFMLObject
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
this()
|
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
super(sfSocketSelector_Create());
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
override void dispose()
|
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
sfSocketSelector_Destroy(m_ptr);
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a socket to watch
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* socket = A tcp or udp socket
|
|
|
|
*/
|
|
|
|
void add(T socket)
|
|
|
|
{
|
2010-03-16 07:35:53 +08:00
|
|
|
if (!(socket.nativePointer in m_watchedSockets))
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
sfSocketSelector_Add(m_ptr, socket.nativePointer);
|
2010-03-16 07:35:53 +08:00
|
|
|
m_watchedSockets[socket.nativePointer] = socket;
|
2010-01-07 04:37:29 +08:00
|
|
|
m_numSocketsWatched++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a previously added socket
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* socket = A tcp or udp socket
|
|
|
|
*/
|
|
|
|
void remove(T socket)
|
|
|
|
{
|
2010-03-16 07:35:53 +08:00
|
|
|
if (socket.nativePointer in m_watchedSockets)
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
sfSocketSelector_Remove(m_ptr, socket.nativePointer);
|
2010-03-16 07:35:53 +08:00
|
|
|
m_watchedSockets.remove(socket.nativePointer);
|
2010-01-07 04:37:29 +08:00
|
|
|
m_numSocketsWatched--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all sockets being watched
|
|
|
|
*/
|
|
|
|
void clear()
|
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
sfSocketSelector_Clear(m_ptr);
|
2010-01-07 04:37:29 +08:00
|
|
|
foreach(key; m_watchedSockets.keys)
|
|
|
|
m_watchedSockets.remove(key);
|
|
|
|
m_numSocketsWatched = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait and collect sockets which are ready for reading.
|
|
|
|
* This functions will return either when at least one socket
|
|
|
|
* is ready, or when the given time is out
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* timeout = Maximum time to wait, in seconds (0 to disable timeout)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Number of sockets ready
|
|
|
|
*/
|
|
|
|
uint wait(float timeout = 0.f)
|
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
return sfSocketSelector_Wait(m_ptr, timeout);
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After a call to Wait(), get the Index-th socket which is
|
|
|
|
* ready for reading. The total number of sockets ready
|
|
|
|
* is the integer returned by the previous call to Wait()
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* index = Index of the socket to get
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* The Index-th socket
|
|
|
|
*/
|
|
|
|
T GetSocketsReady(uint index)
|
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
return m_watchedSockets[sfSocketSelector_GetSocketReady(m_ptr, index)];
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
private:
|
2010-04-21 03:51:48 +08:00
|
|
|
// size_t m_numSocketsWatched;
|
|
|
|
// T[void*] m_watchedSockets;
|
2010-01-07 04:37:29 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// External ====================================================================
|
2010-04-21 03:51:48 +08:00
|
|
|
static extern(C)
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
2010-04-21 03:51:48 +08:00
|
|
|
SFMLClass function() sfSocketSelector_Create;
|
|
|
|
void function(SFMLClass) sfSocketSelector_Destroy;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_AddTcpListener;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_AddTcpSocket;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_AddUdpSocket;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_RemoveTcpListener;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_RemoveTcpSocket;
|
|
|
|
void function(SFMLClass, SFMLClass) sfSocketSelector_RemoveUdpSocket;
|
|
|
|
void function(SFMLClass) sfSocketSelector_Clear;
|
|
|
|
bool function(SFMLClass, float) sfSocketSelector_Wait;
|
|
|
|
bool function(SFMLClass, SFMLClass) sfSocketSelector_IsTcpListenerReady;
|
|
|
|
bool function(SFMLClass, SFMLClass) sfSocketSelector_IsTcpSocketReady;
|
|
|
|
bool function(SFMLClass, SFMLClass) sfSocketSelector_IsUdpSocketReady;
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-04-21 03:51:48 +08:00
|
|
|
mixin(loadFromSharedLib2("csfml-network", "sfSocketSelector",
|
|
|
|
"Create", "Destroy", "AddTcpListener", "AddTcpSocket", "AddUdpSocket", "RemoveTcpListener", "RemoveTcpSocket", "RemoveUdpSocket",
|
|
|
|
"Clear", "Wait", "IsTcpListenerReady", "IsTcpSocketReady", "IsUdpSocketReady"));
|
|
|
|
}
|