~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/broadcast_socket.hpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2007, Arvid Norberg
4
 
All rights reserved.
5
 
 
6
 
Redistribution and use in source and binary forms, with or without
7
 
modification, are permitted provided that the following conditions
8
 
are met:
9
 
 
10
 
    * Redistributions of source code must retain the above copyright
11
 
      notice, this list of conditions and the following disclaimer.
12
 
    * Redistributions in binary form must reproduce the above copyright
13
 
      notice, this list of conditions and the following disclaimer in
14
 
      the documentation and/or other materials provided with the distribution.
15
 
    * Neither the name of the author nor the names of its
16
 
      contributors may be used to endorse or promote products derived
17
 
      from this software without specific prior written permission.
18
 
 
19
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 
POSSIBILITY OF SUCH DAMAGE.
30
 
 
31
 
*/
32
 
 
33
 
#ifndef TORRENT_BROADCAST_SOCKET_HPP_INCLUDED
34
 
#define TORRENT_BROADCAST_SOCKET_HPP_INCLUDED
35
 
 
36
 
#include "libtorrent/config.hpp"
37
 
#include "libtorrent/socket.hpp"
38
 
#include <boost/shared_ptr.hpp>
39
 
#include <boost/function.hpp>
40
 
#include <list>
41
 
 
42
 
namespace libtorrent
43
 
{
44
 
 
45
 
        TORRENT_EXPORT bool is_local(address const& a);
46
 
        TORRENT_EXPORT bool is_loopback(address const& addr);
47
 
        TORRENT_EXPORT bool is_multicast(address const& addr);
48
 
        TORRENT_EXPORT bool is_any(address const& addr);
49
 
        TORRENT_EXPORT int cidr_distance(address const& a1, address const& a2);
50
 
 
51
 
        // determines if the operating system supports IPv6
52
 
        TORRENT_EXPORT bool supports_ipv6();
53
 
 
54
 
        int common_bits(unsigned char const* b1
55
 
                , unsigned char const* b2, int n);
56
 
 
57
 
        TORRENT_EXPORT address guess_local_address(io_service&);
58
 
 
59
 
        typedef boost::function<void(udp::endpoint const& from
60
 
                , char* buffer, int size)> receive_handler_t;
61
 
 
62
 
        class TORRENT_EXPORT broadcast_socket
63
 
        {
64
 
        public:
65
 
                broadcast_socket(io_service& ios, udp::endpoint const& multicast_endpoint
66
 
                        , receive_handler_t const& handler, bool loopback = true);
67
 
                ~broadcast_socket() { close(); }
68
 
 
69
 
                void send(char const* buffer, int size, error_code& ec);
70
 
                void close();
71
 
 
72
 
        private:
73
 
 
74
 
                struct socket_entry
75
 
                {
76
 
                        socket_entry(boost::shared_ptr<datagram_socket> const& s): socket(s) {}
77
 
                        boost::shared_ptr<datagram_socket> socket;
78
 
                        char buffer[1024];
79
 
                        udp::endpoint remote;
80
 
                        void close()
81
 
                        {
82
 
                                if (!socket) return;
83
 
                                error_code ec;
84
 
                                socket->close(ec);
85
 
                        }
86
 
                };
87
 
        
88
 
                void on_receive(socket_entry* s, error_code const& ec
89
 
                        , std::size_t bytes_transferred);
90
 
                void open_unicast_socket(io_service& ios, address const& addr);
91
 
                void open_multicast_socket(io_service& ios, address const& addr
92
 
                        , bool loopback);
93
 
 
94
 
                // these sockets are used to
95
 
                // join the multicast group (on each interface)
96
 
                // and receive multicast messages
97
 
                std::list<socket_entry> m_sockets;
98
 
                // these sockets are not bound to any
99
 
                // specific port and are used to
100
 
                // send messages to the multicast group
101
 
                // and receive unicast responses
102
 
                std::list<socket_entry> m_unicast_sockets;
103
 
                udp::endpoint m_multicast_endpoint;
104
 
                receive_handler_t m_on_receive;
105
 
                
106
 
        };
107
 
}
108
 
        
109
 
#endif
110