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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/* -*- C++ -*-
* Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef KDATAGRAMSOCKET_H
#define KDATAGRAMSOCKET_H
#include <qcstring.h>
#include "ksocketaddress.h"
#include "kclientsocketbase.h"
namespace KNetwork {
class KResolverEntry;
/**
* @class KDatagramPacket kdatagramsocket.h kdatagramsocket.h
* @brief one datagram
*
* This object represents one datagram of data sent or received through
* a datagram socket (as @ref KDatagramSocket or derived classes). A datagram
* consists of data as well as a network address associated (whither to send
* the data or whence it came).
*
* This is a lightweight class. Data is stored in a @ref QByteArray, which means
* that it is explicitly shared.
*
* @author Thiago Macieira <thiago.macieira@kdemail.net>
*/
class KDECORE_EXPORT KDatagramPacket
{
QByteArray m_data;
KSocketAddress m_address;
public:
/**
* Default constructor.
*/
KDatagramPacket()
{ }
/**
* Constructs the datagram with the specified content.
*/
KDatagramPacket(const QByteArray& content)
: m_data(content)
{ }
/**
* Constructs the datagram with the specified content.
*
* @see setData for information on data sharing.
*/
KDatagramPacket(const char* content, uint length)
{ setData(content, length); }
/**
* Constructs the datagram with the specified content and address.
*/
KDatagramPacket(const QByteArray& content, const KSocketAddress& addr)
: m_data(content), m_address(addr)
{ }
/**
* Constructs the datagram with the specified content and address.
*/
KDatagramPacket(const char *content, uint length, const KSocketAddress& addr)
: m_address(addr)
{ setData(content, length); }
/**
* Copy constructor. Note that data is explicitly shared.
*/
KDatagramPacket(const KDatagramPacket& other)
{ *this = other; }
/**
* Destructor. Non-virtual.
*/
~KDatagramPacket()
{ }
/**
* Returns the data.
*/
const QByteArray& data() const
{ return m_data; }
/**
* Returns the data length.
*/
uint length() const
{ return m_data.size(); }
/**
* Returns the data length.
*/
uint size() const
{ return m_data.size(); }
/**
* Returns true if this object is empty.
*/
bool isEmpty() const
{ return m_data.isEmpty(); }
/**
* Returns true if this object is null.
*/
bool isNull() const
{ return m_data.isNull(); }
/**
* Returns the socket address
*/
const KSocketAddress& address() const
{ return m_address; }
/**
* Sets the address stored to the given value.
*/
void setAddress(const KSocketAddress& addr)
{ m_address = addr; }
/**
* Detaches our data from a shared pool.
* @see QByteArray::detach
*/
void detach()
{ m_data.detach(); }
/**
* Sets the data to the given value. Data is explicitly shared.
*/
void setData(const QByteArray& data)
{ m_data = data; }
/**
* Sets the data to the given buffer and size.
*/
void setData(const char* data, uint length)
{ m_data.duplicate(data, length); }
};
class KDatagramSocketPrivate;
/**
* @class KDatagramSocket kdatagramsocket.h kdatagramsocket.h
* @brief A socket that operates on datagrams.
*
* Unlike @ref KStreamSocket, which operates on a connection-based stream
* socket (generally TCP), this class and its descendants operates on datagrams,
* which are normally connectionless.
*
* This class in specific provides easy access to the system's connectionless
* SOCK_DGRAM sockets.
*
* @author Thiago Macieira <thiago.macieira@kdemail.net>
*/
class KDECORE_EXPORT KDatagramSocket: public KClientSocketBase
{
Q_OBJECT
public:
/**
* Default constructor.
*/
KDatagramSocket(QObject* parent = 0L, const char *name = 0L);
/**
* Destructor. This closes the socket.
*/
virtual ~KDatagramSocket();
/**
* Performs host lookups.
*/
// virtual bool lookup();
/**
* Binds this socket to the given address. If the socket is blocking,
* the socket will be bound when this function returns.
*
* Note that binding a socket is not necessary to be able to send datagrams.
* Some protocol families will use anonymous source addresses, while others
* will allocate an address automatically.
*/
virtual bool bind(const QString& node = QString::null,
const QString& service = QString::null);
/**
* @overload
* Binds this socket to the given address.
*/
virtual bool bind(const KResolverEntry& entry)
{ return KClientSocketBase::bind(entry); }
/**
* "Connects" this socket to the given address. Note that connecting
* a datagram socket normally does not establish a permanent connection
* with the peer nor normally returns an error in case of failure.
*
* Connecting means only to designate the given address as the default
* destination address for datagrams sent without destination addresses
* ( writeBlock(const char *, Q_ULONG) ).
*
* @note Calling connect will not cause the socket to be bound. You have
* to call @ref bind explicitly.
*/
virtual bool connect(const QString& node = QString::null,
const QString& service = QString::null);
/**
* @overload
* "Connects" this socket to the given address.
*/
virtual bool connect(const KResolverEntry& entry)
{ return KClientSocketBase::connect(entry); }
/**
* Writes data to the socket. Reimplemented from KClientSocketBase.
*/
virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to);
/**
* Receives one datagram from the stream. The reading process is guaranteed
* to be atomical and not lose data from the packet.
*
* If nothing could be read, a null object will be returned.
*/
virtual KDatagramPacket receive();
/**
* Sends one datagram into the stream. The destination address must be
* set if this socket has not been connected (see @ref connect).
*
* The data in this packet will be sent only in one single datagram. If the
* system cannot send it like that, this function will fail. So, please take
* into consideration the datagram size limits.
*
* @returns the number of bytes written or -1 in case of error.
*/
virtual Q_LONG send(const KDatagramPacket& packet);
private slots:
void lookupFinishedLocal();
void lookupFinishedPeer();
private:
bool doBind();
void setupSignals();
KDatagramSocketPrivate *d;
};
} // namespace KNetwork
#endif
|