front_idill/extern/fajran-npTuioClient/TuioClient/oscpack/ip/win32/UdpSocket.cpp
author bastiena
Fri, 06 Apr 2012 10:44:54 +0200
changeset 21 e4e5f02787a1
permissions -rw-r--r--
Front IDILL : Added Communication extern named fajran-npTuioClient It contains the project generating a dll used as a browser plugin.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     1
/*
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     2
	oscpack -- Open Sound Control packet manipulation library
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     3
	http://www.audiomulch.com/~rossb/oscpack
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     4
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     5
	Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     6
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     7
	Permission is hereby granted, free of charge, to any person obtaining
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     8
	a copy of this software and associated documentation files
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     9
	(the "Software"), to deal in the Software without restriction,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    10
	including without limitation the rights to use, copy, modify, merge,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    11
	publish, distribute, sublicense, and/or sell copies of the Software,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    12
	and to permit persons to whom the Software is furnished to do so,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    13
	subject to the following conditions:
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    14
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    15
	The above copyright notice and this permission notice shall be
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    16
	included in all copies or substantial portions of the Software.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    17
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    18
	Any person wishing to distribute modifications to the Software is
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    19
	requested to send the modifications to the original developer so that
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    20
	they can be incorporated into the canonical version.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    22
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    23
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    24
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    25
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    26
	ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    27
	CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    28
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    29
*/
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    30
#include "ip/UdpSocket.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    31
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    32
#include <winsock2.h>   // this must come first to prevent errors with MSVC7
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    33
#include <windows.h>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    34
#include <mmsystem.h>   // for timeGetTime()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    35
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    36
#include <vector>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    37
#include <algorithm>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    38
#include <stdexcept>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    39
#include <assert.h>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    40
#include <signal.h>
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    41
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    42
#include "ip/NetworkingUtils.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    43
#include "ip/PacketListener.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    44
#include "ip/TimerListener.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    45
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    46
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    47
typedef int socklen_t;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    48
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    49
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    50
static void SockaddrFromIpEndpointName( struct sockaddr_in& sockAddr, const IpEndpointName& endpoint )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    51
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    52
    memset( (char *)&sockAddr, 0, sizeof(sockAddr ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    53
    sockAddr.sin_family = AF_INET;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    54
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    55
	sockAddr.sin_addr.s_addr = 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    56
		(endpoint.address == IpEndpointName::ANY_ADDRESS)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    57
		? INADDR_ANY
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    58
		: htonl( endpoint.address );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    59
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    60
	sockAddr.sin_port =
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    61
		(endpoint.port == IpEndpointName::ANY_PORT)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    62
		? (short)0
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    63
		: htons( (short)endpoint.port );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    64
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    65
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    66
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    67
static IpEndpointName IpEndpointNameFromSockaddr( const struct sockaddr_in& sockAddr )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    68
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    69
	return IpEndpointName( 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    70
		(sockAddr.sin_addr.s_addr == INADDR_ANY) 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    71
			? IpEndpointName::ANY_ADDRESS 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    72
			: ntohl( sockAddr.sin_addr.s_addr ),
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    73
		(sockAddr.sin_port == 0)
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    74
			? IpEndpointName::ANY_PORT
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    75
			: ntohs( sockAddr.sin_port )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    76
		);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    77
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    78
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    79
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    80
class UdpSocket::Implementation{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    81
    NetworkInitializer networkInitializer_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    82
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    83
	bool isBound_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    84
	bool isConnected_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    85
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    86
	SOCKET socket_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    87
	struct sockaddr_in connectedAddr_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    88
	struct sockaddr_in sendToAddr_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    89
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    90
public:
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    91
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    92
	Implementation()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    93
		: isBound_( false )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    94
		, isConnected_( false )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    95
		, socket_( INVALID_SOCKET )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    96
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    97
		if( (socket_ = socket( AF_INET, SOCK_DGRAM, 0 )) == INVALID_SOCKET ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    98
            throw std::runtime_error("unable to create udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    99
        }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   100
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   101
		memset( &sendToAddr_, 0, sizeof(sendToAddr_) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   102
        sendToAddr_.sin_family = AF_INET;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   103
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   104
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   105
	~Implementation()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   106
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   107
		if (socket_ != INVALID_SOCKET) closesocket(socket_);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   108
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   109
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   110
	IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   111
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   112
		assert( isBound_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   113
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   114
		// first connect the socket to the remote server
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   115
        
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   116
        struct sockaddr_in connectSockAddr;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   117
		SockaddrFromIpEndpointName( connectSockAddr, remoteEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   118
       
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   119
        if (connect(socket_, (struct sockaddr *)&connectSockAddr, sizeof(connectSockAddr)) < 0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   120
            throw std::runtime_error("unable to connect udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   121
        }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   122
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   123
        // get the address
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   124
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   125
        struct sockaddr_in sockAddr;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   126
        memset( (char *)&sockAddr, 0, sizeof(sockAddr ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   127
        socklen_t length = sizeof(sockAddr);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   128
        if (getsockname(socket_, (struct sockaddr *)&sockAddr, &length) < 0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   129
            throw std::runtime_error("unable to getsockname\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   130
        }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   131
        
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   132
		if( isConnected_ ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   133
			// reconnect to the connected address
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   134
			
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   135
			if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   136
				throw std::runtime_error("unable to connect udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   137
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   138
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   139
		}else{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   140
			// unconnect from the remote address
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   141
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   142
			struct sockaddr_in unconnectSockAddr;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   143
			SockaddrFromIpEndpointName( unconnectSockAddr, IpEndpointName() );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   144
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   145
			if( connect(socket_, (struct sockaddr *)&unconnectSockAddr, sizeof(unconnectSockAddr)) < 0 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   146
					&& WSAGetLastError() != WSAEADDRNOTAVAIL ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   147
				throw std::runtime_error("unable to un-connect udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   148
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   149
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   150
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   151
		return IpEndpointNameFromSockaddr( sockAddr );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   152
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   153
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   154
	void Connect( const IpEndpointName& remoteEndpoint )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   155
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   156
		SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   157
       
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   158
        if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   159
            throw std::runtime_error("unable to connect udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   160
        }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   161
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   162
		isConnected_ = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   163
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   164
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   165
	void Send( const char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   166
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   167
		assert( isConnected_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   168
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   169
        send( socket_, data, size, 0 );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   170
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   171
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   172
    void SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   173
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   174
		sendToAddr_.sin_addr.s_addr = htonl( remoteEndpoint.address );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   175
        sendToAddr_.sin_port = htons( (short)remoteEndpoint.port );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   176
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   177
        sendto( socket_, data, size, 0, (sockaddr*)&sendToAddr_, sizeof(sendToAddr_) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   178
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   179
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   180
	void Bind( const IpEndpointName& localEndpoint )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   181
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   182
		struct sockaddr_in bindSockAddr;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   183
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   184
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   185
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   186
            throw std::runtime_error("unable to bind udp socket\n");
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   187
        }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   188
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   189
		isBound_ = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   190
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   191
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   192
	bool IsBound() const { return isBound_; }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   193
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   194
    int ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   195
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   196
		assert( isBound_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   197
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   198
		struct sockaddr_in fromAddr;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   199
        socklen_t fromAddrLen = sizeof(fromAddr);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   200
             	 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   201
        int result = recvfrom(socket_, data, size, 0,
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   202
                    (struct sockaddr *) &fromAddr, (socklen_t*)&fromAddrLen);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   203
		if( result < 0 )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   204
			return 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   205
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   206
		remoteEndpoint.address = ntohl(fromAddr.sin_addr.s_addr);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   207
		remoteEndpoint.port = ntohs(fromAddr.sin_port);
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   208
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   209
		return result;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   210
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   211
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   212
	SOCKET& Socket() { return socket_; }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   213
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   214
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   215
UdpSocket::UdpSocket()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   216
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   217
	impl_ = new Implementation();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   218
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   219
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   220
UdpSocket::~UdpSocket()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   221
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   222
	delete impl_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   223
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   224
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   225
IpEndpointName UdpSocket::LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   226
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   227
	return impl_->LocalEndpointFor( remoteEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   228
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   229
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   230
void UdpSocket::Connect( const IpEndpointName& remoteEndpoint )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   231
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   232
	impl_->Connect( remoteEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   233
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   234
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   235
void UdpSocket::Send( const char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   236
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   237
	impl_->Send( data, size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   238
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   239
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   240
void UdpSocket::SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   241
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   242
	impl_->SendTo( remoteEndpoint, data, size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   243
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   244
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   245
void UdpSocket::Bind( const IpEndpointName& localEndpoint )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   246
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   247
	impl_->Bind( localEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   248
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   249
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   250
bool UdpSocket::IsBound() const
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   251
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   252
	return impl_->IsBound();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   253
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   254
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   255
int UdpSocket::ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   256
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   257
	return impl_->ReceiveFrom( remoteEndpoint, data, size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   258
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   259
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   260
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   261
struct AttachedTimerListener{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   262
	AttachedTimerListener( int id, int p, TimerListener *tl )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   263
		: initialDelayMs( id )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   264
		, periodMs( p )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   265
		, listener( tl ) {}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   266
	int initialDelayMs;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   267
	int periodMs;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   268
	TimerListener *listener;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   269
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   270
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   271
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   272
static bool CompareScheduledTimerCalls( 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   273
		const std::pair< double, AttachedTimerListener > & lhs, const std::pair< double, AttachedTimerListener > & rhs )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   274
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   275
	return lhs.first < rhs.first;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   276
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   277
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   278
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   279
SocketReceiveMultiplexer *multiplexerInstanceToAbortWithSigInt_ = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   280
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   281
extern "C" /*static*/ void InterruptSignalHandler( int );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   282
/*static*/ void InterruptSignalHandler( int )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   283
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   284
	multiplexerInstanceToAbortWithSigInt_->AsynchronousBreak();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   285
	signal( SIGINT, SIG_DFL );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   286
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   287
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   288
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   289
class SocketReceiveMultiplexer::Implementation{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   290
    NetworkInitializer networkInitializer_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   291
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   292
	std::vector< std::pair< PacketListener*, UdpSocket* > > socketListeners_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   293
	std::vector< AttachedTimerListener > timerListeners_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   294
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   295
	volatile bool break_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   296
	HANDLE breakEvent_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   297
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   298
	double GetCurrentTimeMs() const
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   299
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   300
		return timeGetTime(); // FIXME: bad choice if you want to run for more than 40 days
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   301
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   302
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   303
public:
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   304
    Implementation()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   305
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   306
		breakEvent_ = CreateEvent( NULL, FALSE, FALSE, NULL );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   307
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   308
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   309
    ~Implementation()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   310
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   311
		CloseHandle( breakEvent_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   312
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   313
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   314
    void AttachSocketListener( UdpSocket *socket, PacketListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   315
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   316
		assert( std::find( socketListeners_.begin(), socketListeners_.end(), std::make_pair(listener, socket) ) == socketListeners_.end() );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   317
		// we don't check that the same socket has been added multiple times, even though this is an error
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   318
		socketListeners_.push_back( std::make_pair( listener, socket ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   319
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   320
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   321
    void DetachSocketListener( UdpSocket *socket, PacketListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   322
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   323
		std::vector< std::pair< PacketListener*, UdpSocket* > >::iterator i = 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   324
				std::find( socketListeners_.begin(), socketListeners_.end(), std::make_pair(listener, socket) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   325
		assert( i != socketListeners_.end() );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   326
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   327
		socketListeners_.erase( i );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   328
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   329
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   330
    void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   331
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   332
		timerListeners_.push_back( AttachedTimerListener( periodMilliseconds, periodMilliseconds, listener ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   333
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   334
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   335
	void AttachPeriodicTimerListener( int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   336
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   337
		timerListeners_.push_back( AttachedTimerListener( initialDelayMilliseconds, periodMilliseconds, listener ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   338
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   339
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   340
    void DetachPeriodicTimerListener( TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   341
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   342
		std::vector< AttachedTimerListener >::iterator i = timerListeners_.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   343
		while( i != timerListeners_.end() ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   344
			if( i->listener == listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   345
				break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   346
			++i;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   347
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   348
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   349
		assert( i != timerListeners_.end() );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   350
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   351
		timerListeners_.erase( i );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   352
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   353
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   354
    void Run()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   355
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   356
		break_ = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   357
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   358
		// prepare the window events which we use to wake up on incoming data
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   359
		// we use this instead of select() primarily to support the AsyncBreak() 
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   360
		// mechanism.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   361
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   362
		std::vector<HANDLE> events( socketListeners_.size() + 1, 0 );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   363
		int j=0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   364
		for( std::vector< std::pair< PacketListener*, UdpSocket* > >::iterator i = socketListeners_.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   365
				i != socketListeners_.end(); ++i, ++j ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   366
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   367
			HANDLE event = CreateEvent( NULL, FALSE, FALSE, NULL );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   368
			WSAEventSelect( i->second->impl_->Socket(), event, FD_READ ); // note that this makes the socket non-blocking which is why we can safely call RecieveFrom() on all sockets below
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   369
			events[j] = event;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   370
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   371
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   372
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   373
		events[ socketListeners_.size() ] = breakEvent_; // last event in the collection is the break event
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   374
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   375
		
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   376
		// configure the timer queue
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   377
		double currentTimeMs = GetCurrentTimeMs();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   378
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   379
		// expiry time ms, listener
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   380
		std::vector< std::pair< double, AttachedTimerListener > > timerQueue_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   381
		for( std::vector< AttachedTimerListener >::iterator i = timerListeners_.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   382
				i != timerListeners_.end(); ++i )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   383
			timerQueue_.push_back( std::make_pair( currentTimeMs + i->initialDelayMs, *i ) );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   384
		std::sort( timerQueue_.begin(), timerQueue_.end(), CompareScheduledTimerCalls );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   385
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   386
		const int MAX_BUFFER_SIZE = 4098;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   387
		char *data = new char[ MAX_BUFFER_SIZE ];
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   388
		IpEndpointName remoteEndpoint;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   389
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   390
		while( !break_ ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   391
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   392
			double currentTimeMs = GetCurrentTimeMs();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   393
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   394
            DWORD waitTime = INFINITE;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   395
            if( !timerQueue_.empty() ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   396
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   397
                waitTime = (DWORD)( timerQueue_.front().first >= currentTimeMs
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   398
                            ? timerQueue_.front().first - currentTimeMs
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   399
                            : 0 );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   400
            }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   401
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   402
			DWORD waitResult = WaitForMultipleObjects( (DWORD)socketListeners_.size() + 1, &events[0], FALSE, waitTime );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   403
			if( break_ )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   404
				break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   405
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   406
			if( waitResult != WAIT_TIMEOUT ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   407
				for( int i = waitResult - WAIT_OBJECT_0; i < (int)socketListeners_.size(); ++i ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   408
					int size = socketListeners_[i].second->ReceiveFrom( remoteEndpoint, data, MAX_BUFFER_SIZE );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   409
					if( size > 0 ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   410
						socketListeners_[i].first->ProcessPacket( data, size, remoteEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   411
						if( break_ )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   412
							break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   413
					}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   414
				}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   415
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   416
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   417
			// execute any expired timers
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   418
			currentTimeMs = GetCurrentTimeMs();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   419
			bool resort = false;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   420
			for( std::vector< std::pair< double, AttachedTimerListener > >::iterator i = timerQueue_.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   421
					i != timerQueue_.end() && i->first <= currentTimeMs; ++i ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   422
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   423
				i->second.listener->TimerExpired();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   424
				if( break_ )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   425
					break;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   426
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   427
				i->first += i->second.periodMs;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   428
				resort = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   429
			}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   430
			if( resort )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   431
				std::sort( timerQueue_.begin(), timerQueue_.end(), CompareScheduledTimerCalls );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   432
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   433
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   434
		delete [] data;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   435
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   436
		// free events
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   437
		j = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   438
		for( std::vector< std::pair< PacketListener*, UdpSocket* > >::iterator i = socketListeners_.begin();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   439
				i != socketListeners_.end(); ++i, ++j ){
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   440
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   441
			WSAEventSelect( i->second->impl_->Socket(), events[j], 0 ); // remove association between socket and event
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   442
			CloseHandle( events[j] );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   443
			unsigned long enableNonblocking = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   444
			ioctlsocket( i->second->impl_->Socket(), FIONBIO, &enableNonblocking );  // make the socket blocking again
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   445
		}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   446
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   447
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   448
    void Break()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   449
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   450
		break_ = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   451
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   452
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   453
    void AsynchronousBreak()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   454
	{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   455
		break_ = true;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   456
		SetEvent( breakEvent_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   457
	}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   458
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   459
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   460
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   461
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   462
SocketReceiveMultiplexer::SocketReceiveMultiplexer()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   463
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   464
	impl_ = new Implementation();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   465
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   466
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   467
SocketReceiveMultiplexer::~SocketReceiveMultiplexer()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   468
{	
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   469
	delete impl_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   470
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   471
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   472
void SocketReceiveMultiplexer::AttachSocketListener( UdpSocket *socket, PacketListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   473
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   474
	impl_->AttachSocketListener( socket, listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   475
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   476
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   477
void SocketReceiveMultiplexer::DetachSocketListener( UdpSocket *socket, PacketListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   478
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   479
	impl_->DetachSocketListener( socket, listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   480
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   481
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   482
void SocketReceiveMultiplexer::AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   483
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   484
	impl_->AttachPeriodicTimerListener( periodMilliseconds, listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   485
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   486
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   487
void SocketReceiveMultiplexer::AttachPeriodicTimerListener( int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   488
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   489
	impl_->AttachPeriodicTimerListener( initialDelayMilliseconds, periodMilliseconds, listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   490
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   491
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   492
void SocketReceiveMultiplexer::DetachPeriodicTimerListener( TimerListener *listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   493
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   494
	impl_->DetachPeriodicTimerListener( listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   495
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   496
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   497
void SocketReceiveMultiplexer::Run()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   498
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   499
	impl_->Run();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   500
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   501
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   502
void SocketReceiveMultiplexer::RunUntilSigInt()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   503
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   504
	assert( multiplexerInstanceToAbortWithSigInt_ == 0 ); /* at present we support only one multiplexer instance running until sig int */
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   505
	multiplexerInstanceToAbortWithSigInt_ = this;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   506
	signal( SIGINT, InterruptSignalHandler );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   507
	impl_->Run();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   508
	signal( SIGINT, SIG_DFL );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   509
	multiplexerInstanceToAbortWithSigInt_ = 0;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   510
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   511
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   512
void SocketReceiveMultiplexer::Break()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   513
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   514
	impl_->Break();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   515
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   516
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   517
void SocketReceiveMultiplexer::AsynchronousBreak()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   518
{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   519
	impl_->AsynchronousBreak();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   520
}
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   521