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
|
/*
* Connection.h
*
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
*
* 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 MAD_NET_CONNECTION_H_
#define MAD_NET_CONNECTION_H_
#include <config.h>
#include "Packet.h"
#include <queue>
#include <string>
#include <gnutls/gnutls.h>
#include <poll.h>
#include <boost/signal.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <iostream>
namespace Mad {
namespace Net {
class IPAddress;
class Packet;
class Connection {
private:
class StaticInit {
public:
StaticInit() {
gnutls_global_init();
}
~StaticInit() {
gnutls_global_deinit();
}
};
static StaticInit staticInit;
struct Transmission {
unsigned long length;
unsigned long transmitted;
uint8_t *data;
boost::function2<void,const void*,unsigned long> notify;
};
boost::mutex receiveLock;
Transmission transR;
boost::mutex sendLock;
std::queue<Transmission> transS;
Packet::Data header;
boost::signal1<void,const Packet&> receiveSignal;
boost::signal0<void> connectedSignal;
boost::signal0<void> disconnectedSignal;
void doHandshake();
void packetHeaderReceiveHandler(const void *data, unsigned long length);
void packetDataReceiveHandler(const void *data, unsigned long length);
void doReceive();
void doSend();
void doBye();
void doDisconnect();
bool _receiveComplete() const {
return (transR.length == transR.transmitted);
}
bool _sendQueueEmpty() const {return transS.empty();}
void bye();
// Prevent shallow copy
Connection(const Connection &o);
Connection& operator=(const Connection &o);
protected:
struct ConnectionHeader {
uint8_t m;
uint8_t a;
uint8_t d;
uint8_t type;
uint8_t versionMajor;
uint8_t versionMinor;
uint8_t protVerMin;
uint8_t protVerMax;
};
boost::shared_mutex stateLock;
enum State {
DISCONNECTED, CONNECT, HANDSHAKE, CONNECTION_HEADER, PACKET_HEADER, PACKET_DATA, DISCONNECT, BYE
} state;
int sock;
gnutls_session_t session;
gnutls_certificate_credentials_t x509_cred;
IPAddress *peer;
void handshake();
virtual void connectionHeader() = 0;
bool rawReceive(unsigned long length, const boost::function2<void,const void*,unsigned long> ¬ify);
bool rawSend(const uint8_t *data, unsigned long length);
void enterReceiveLoop();
void sendReceive(short events);
bool _isConnected() const {return (state != DISCONNECTED);}
bool _isConnecting() const {
return (state == CONNECT || state == HANDSHAKE || state == CONNECTION_HEADER);
}
bool _isDisconnecting() const {
return (state == DISCONNECT || state == BYE);
}
void updateEvents();
public:
Connection() : state(DISCONNECTED), peer(0) {
transR.length = transR.transmitted = 0;
transR.data = 0;
gnutls_certificate_allocate_credentials(&x509_cred);
}
virtual ~Connection();
bool isConnected() {
boost::shared_lock<boost::shared_mutex> lock(stateLock);
return _isConnected();
}
bool isConnecting() {
boost::shared_lock<boost::shared_mutex> lock(stateLock);
return _isConnecting();
}
bool isDisconnecting() {
boost::shared_lock<boost::shared_mutex> lock(stateLock);
return _isDisconnecting();
}
const gnutls_datum_t* getCertificate() const {
// TODO Thread-safeness
return gnutls_certificate_get_ours(session);
}
const gnutls_datum_t* getPeerCertificate() const {
// TODO Thread-safeness
unsigned int n;
return gnutls_certificate_get_peers(session, &n);
}
// TODO Thread-safeness
const IPAddress* getPeer() const {return peer;}
void disconnect();
bool send(const Packet &packet);
boost::signal1<void,const Packet&>& signalReceive() {return receiveSignal;}
boost::signal0<void>& signalConnected() {return connectedSignal;}
boost::signal0<void>& signalDisconnected() {return disconnectedSignal;}
};
}
}
#endif /*MAD_NET_CONNECTION_H_*/
|