-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
socket.h
214 lines (175 loc) · 5.41 KB
/
socket.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
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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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 2 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 LIBDENG2_SOCKET_H
#define LIBDENG2_SOCKET_H
/**
* @defgroup net Network
*
* Classes responsible for network communications.
*/
#include "../libdeng2.h"
#include "../IByteArray"
#include "../Address"
#include "../Transmitter"
#include <QTcpSocket>
#include <QList>
#include <QFlags>
namespace de {
class Message;
/**
* TCP/IP network socket.
*
* ListenSocket constructs Socket instances for incoming connections.
*
* @ingroup net
*/
class DENG2_PUBLIC Socket : public QObject, public Transmitter
{
Q_OBJECT
public:
/// Creating the TCP/IP connection failed. @ingroup errors
DENG2_ERROR(ConnectionError)
/// Error subclass for all situations where the socket is left unusable. @ingroup errors
DENG2_ERROR(BrokenError)
/// The TCP/IP connection was disconnected. @ingroup errors
DENG2_SUB_ERROR(BrokenError, DisconnectedError)
/// Encountered a problem related to the messaging protocol. @ingroup errors
DENG2_SUB_ERROR(BrokenError, ProtocolError)
/// There is no peer connected. @ingroup errors
DENG2_SUB_ERROR(BrokenError, PeerError)
/// These flags are written to the sent headers.
enum HeaderFlag {
Huffman = 0x1,
Channel1 = 0x2
};
Q_DECLARE_FLAGS(HeaderFlags, HeaderFlag)
public:
/**
* Opens a socket to @a address and waits (blocks) until the connection has
* been formed. The socket is ready to be used after the constructor
* returns. If the connection can be formed within the specified @a timeOut
* threshold, an exception is thrown.
*
* @param address Address to connect to.
* @param timeOut Maximum time to wait for connection.
*/
Socket(const Address& address, const Time::Delta& timeOut = 5);
virtual ~Socket();
/**
* Returns the currently active channel.
*/
duint channel() const;
/**
* Sets the active channel.
*
* @param number Channel number.
*/
void setChannel(duint number);
// Implements Transmitter.
/**
* Sends the given data over the socket. Copies the data into
* a temporary buffer before sending. The data is sent on the current
* sending channel.
*
* @param data Data to send.
*/
void send(const IByteArray &packet);
/**
* Sends the given data over the socket. Copies the data into
* a temporary buffer before sending. The data is sent on the current
* sending channel.
*
* @param data Data to send.
*
* @return Reference to this socket.
*/
Socket& operator << (const IByteArray& data);
/**
* Returns the next received message. If nothing has been received,
* returns @c NULL.
*
* @return Received bytes. Caller gets ownership of the message.
*/
Message* receive();
/**
* Returns a pointer to the next received message, if one is available.
*
* @return Message. Socket retains ownership.
*/
Message* peek();
/**
* Determines the IP address and port of the remote end of a connected socket.
*
* @return Address of the peer.
*/
Address peerAddress() const;
/**
* Determines if the socket is open for communications.
*/
bool isOpen() const;
/**
* Determines whether there are any incoming messages waiting.
*/
bool hasIncoming() const;
/**
* Determines the amount of data waiting to be sent out.
*/
dsize bytesBuffered() const;
/**
* Blocks until all outgoing data has been written to the socket.
*/
void flush();
/**
* Close the socket.
*/
void close();
signals:
void messagesReady();
void connectionFailure();
void disconnected();
public slots:
void socketDisconnected();
void socketError(QAbstractSocket::SocketError socketError);
void readIncomingBytes();
private slots:
void bytesWereWritten(qint64 bytes);
protected:
/// Create a Socket object for a previously opened socket.
Socket(QTcpSocket* existingSocket);
void initialize();
/**
* Receives a specific number of bytes from the socket. Blocks
* until all the data has been read correctly. An exception is
* thrown if the connection fails.
*/
void receiveBytes(duint count, dbyte* buffer);
void send(const IByteArray& packet, duint channel);
private:
struct Instance;
Instance* d;
/**
* ListenSocket creates instances of Socket so it needs to use
* the special private constructor that takes an existing
* socket data pointer as a parameter.
*/
friend class ListenSocket;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Socket::HeaderFlags)
} // namespace de
#endif // LIBDENG2_SOCKET_H