-
Notifications
You must be signed in to change notification settings - Fork 2
/
udpmanager.h
118 lines (96 loc) · 3.84 KB
/
udpmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This file is part of the Kapotah project.
Copyright (C) 2010 Shantanu Tushar <[email protected]>
Copyright (C) 2010 Sudhendu Kumar <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KAPOTAH_UDPMANAGER_H
#define KAPOTAH_UDPMANAGER_H
#include "singleton.h"
#include "peer.h"
#include <QObject>
#include <QUdpSocket>
namespace Kapotah
{
/**
* \brief Handles UDP datagrams
*
* This Singleton class handles low level UDP functions such as
* sending, broadcasts and unicasts, and receiving datagrams.
* Other library classes can connect to this to get the data.
*/
class UdpManager : public Singleton<UdpManager>
{
Q_OBJECT
public:
UdpManager();
/**
* Update brodcast addresses, localhost addreses.
* Typically should be called after the user indicates
* a change in network configurations
*/
void updateAddresses();
/**
* Used to check if an IP belongs to the local device
* @param ip IP address to be checked
* @return true if IP is locahost, false otherwise
*/
bool isLocalHostIp(const QHostAddress &ip);
protected:
virtual void timerEvent (QTimerEvent*);
private:
QList<QHostAddress> m_broadcastAddresses;
QList<QHostAddress> m_additionalBroadcastAddresses;
QList<QHostAddress> m_ipAddresses;
QUdpSocket m_broadcastSocket;
int m_timerId;
private slots:
void readBroadcast();
public slots:
/**
* Send a brodcast on the network, and additional addresses
*
* @param datagram the datagram to broadcast
*/
void sendBroadcast (const QByteArray &datagram);
/**
* Send a datagram to a particular host
*
* @param datagram the datagram to send
* @param host host to which the datagram should be sent
*/
void sendDatagram (const QByteArray &datagram, const QHostAddress &host);
/**
* Library call to add additional broadcast addresses
*
* @param broadcastAddress the address to broadcast to
*/
void addBroadcastAddress (const QHostAddress &broadcastAddress);
/**
* Library call to remove additional broadcast addresses
*
* @param broadcastAddress the address to stop broadcasting to
*/
void removeBroadcastAddress (const QHostAddress &broadcastAddress);
signals:
/**
* Emitted when a new datagram is received
* \note Datagram maybe broadcast or unicast
*
* @param datagram the datagram received
* @param host host which sent the datagram
* @param port sender port (generally not useful)
*/
void gotDatagram (const QByteArray &datagram, const QHostAddress &host, quint16 port);
};
}
#endif // KAPOTAH_UDPMANAGER_H