front_idill/extern/fajran-npTuioClient/TuioClient/oscpack/ip/UdpSocket.h
author bastiena
Thu, 12 Apr 2012 15:33:25 +0200
changeset 28 9ccef81f02ab
parent 21 e4e5f02787a1
permissions -rw-r--r--
Charset set to UTF-8 without bom tab replaced by 4 spaces \r\n replaced by \n in non cs files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     1
/*
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     2
    oscpack -- Open Sound Control packet manipulation library
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     3
    http://www.audiomulch.com/~rossb/oscpack
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     4
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     5
    Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
     6
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     7
    Permission is hereby granted, free of charge, to any person obtaining
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     8
    a copy of this software and associated documentation files
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
     9
    (the "Software"), to deal in the Software without restriction,
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    10
    including without limitation the rights to use, copy, modify, merge,
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    11
    publish, distribute, sublicense, and/or sell copies of the Software,
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    12
    and to permit persons to whom the Software is furnished to do so,
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    13
    subject to the following conditions:
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    14
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    15
    The above copyright notice and this permission notice shall be
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    16
    included in all copies or substantial portions of the Software.
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    17
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    18
    Any person wishing to distribute modifications to the Software is
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    19
    requested to send the modifications to the original developer so that
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    20
    they can be incorporated into the canonical version.
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    21
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    22
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    23
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    24
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    25
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    26
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    27
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    28
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    29
*/
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    30
#ifndef INCLUDED_UDPSOCKET_H
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    31
#define INCLUDED_UDPSOCKET_H
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    32
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    33
#ifndef INCLUDED_NETWORKINGUTILITIES_H
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    34
#include "NetworkingUtils.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    35
#endif /* INCLUDED_NETWORKINGUTILITIES_H */
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    36
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    37
#ifndef INCLUDED_IPENDPOINTNAME_H
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    38
#include "IpEndpointName.h"
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    39
#endif /* INCLUDED_IPENDPOINTNAME_H */
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    40
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    41
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    42
class PacketListener;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    43
class TimerListener;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    44
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    45
class UdpSocket;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    46
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    47
class SocketReceiveMultiplexer{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    48
    class Implementation;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    49
    Implementation *impl_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    50
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    51
    friend class UdpSocket;
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    52
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    53
public:
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    54
    SocketReceiveMultiplexer();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    55
    ~SocketReceiveMultiplexer();
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    56
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    57
    // only call the attach/detach methods _before_ calling Run
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    58
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    59
    // only one listener per socket, each socket at most once
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    60
    void AttachSocketListener( UdpSocket *socket, PacketListener *listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    61
    void DetachSocketListener( UdpSocket *socket, PacketListener *listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    62
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    63
    void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener );
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    64
    void AttachPeriodicTimerListener(
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    65
            int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    66
    void DetachPeriodicTimerListener( TimerListener *listener );  
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    67
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    68
    void Run();      // loop and block processing messages indefinitely
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    69
    void RunUntilSigInt();
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    70
    void Break();    // call this from a listener to exit once the listener returns
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    71
    void AsynchronousBreak(); // call this from another thread or signal handler to exit the Run() state
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    72
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    73
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    74
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    75
class UdpSocket{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    76
    class Implementation;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    77
    Implementation *impl_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    78
    
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    79
    friend class SocketReceiveMultiplexer::Implementation;
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    80
    
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    81
public:
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    82
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    83
    // ctor throws std::runtime_error if there's a problem
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    84
    // initializing the socket.
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    85
    UdpSocket();
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    86
    virtual ~UdpSocket();
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    87
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    88
    // the socket is created in an unbound, unconnected state
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    89
    // such a socket can only be used to send to an arbitrary
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    90
    // address using SendTo(). To use Send() you need to first
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    91
    // connect to a remote endpoint using Connect(). To use
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    92
    // ReceiveFrom you need to first bind to a local endpoint
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    93
    // using Bind().
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    94
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    95
    // retrieve the local endpoint name when sending to 'to'
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    96
    IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
    97
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    98
    // Connect to a remote endpoint which is used as the target
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
    99
    // for calls to Send()
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   100
    void Connect( const IpEndpointName& remoteEndpoint );    
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   101
    void Send( const char *data, int size );
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   102
    void SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   103
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   104
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   105
    // Bind a local endpoint to receive incoming data. Endpoint
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   106
    // can be 'any' for the system to choose an endpoint
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   107
    void Bind( const IpEndpointName& localEndpoint );
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   108
    bool IsBound() const;
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   109
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   110
    int ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size );
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   111
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   112
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   113
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   114
// convenience classes for transmitting and receiving
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   115
// they just call Connect and/or Bind in the ctor.
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   116
// note that you can still use a receive socket
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   117
// for transmitting etc
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   118
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   119
class UdpTransmitSocket : public UdpSocket{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   120
public:
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   121
    UdpTransmitSocket( const IpEndpointName& remoteEndpoint )
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   122
        { Connect( remoteEndpoint ); }
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   123
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   124
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   125
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   126
class UdpReceiveSocket : public UdpSocket{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   127
public:
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   128
    UdpReceiveSocket( const IpEndpointName& localEndpoint )
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   129
        { Bind( localEndpoint ); }
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   130
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   131
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   132
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   133
// UdpListeningReceiveSocket provides a simple way to bind one listener
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   134
// to a single socket without having to manually set up a SocketReceiveMultiplexer
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   135
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   136
class UdpListeningReceiveSocket : public UdpSocket{
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   137
    SocketReceiveMultiplexer mux_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   138
    PacketListener *listener_;
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   139
public:
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   140
    UdpListeningReceiveSocket( const IpEndpointName& localEndpoint, PacketListener *listener )
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   141
        : listener_( listener )
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   142
    {
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   143
        Bind( localEndpoint );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   144
        mux_.AttachSocketListener( this, listener_ );
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   145
    }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   146
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   147
    ~UdpListeningReceiveSocket()
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   148
        { mux_.DetachSocketListener( this, listener_ ); }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   149
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   150
    // see SocketReceiveMultiplexer above for the behaviour of these methods...
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   151
    void Run() { mux_.Run(); }
28
9ccef81f02ab Charset set to UTF-8 without bom
bastiena
parents: 21
diff changeset
   152
    void RunUntilSigInt() { mux_.RunUntilSigInt(); }
21
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   153
    void Break() { mux_.Break(); }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   154
    void AsynchronousBreak() { mux_.AsynchronousBreak(); }
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   155
};
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   156
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   157
e4e5f02787a1 Front IDILL :
bastiena
parents:
diff changeset
   158
#endif /* INCLUDED_UDPSOCKET_H */