Skip to content

Commit fe9c293

Browse files
committed
comments
1 parent 5879ad7 commit fe9c293

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

include/prototls/Common.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <sstream>
55
namespace prototls {
6+
/** a convenience method to convert objects to strings */
67
template <class T> std::string toString(T t) {
78
std::stringstream s;
89
s << t;

include/prototls/Peer.hpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,66 @@
44
#include "prototls/Socket.hpp"
55
#include <boost/smart_ptr.hpp>
66
namespace prototls {
7+
/** Packet serializer on top of a Socket */
78
class Peer {
9+
/** socket operated and owned by peer */
810
boost::scoped_ptr<Socket> sock;
11+
12+
/** the length of the next protobuf message if nonzero */
913
size_t msgSize;
10-
std::string inBuf, outBuf;
14+
15+
/** buffer for incoming data */
16+
std::string inBuf;
17+
18+
/** buffer for outgoing data */
19+
std::string outBuf;
20+
21+
/** byte position in the incoming data buffer for parsing packets */
1122
int inBufPos;
1223

24+
/** reads the next protobuf message size from incoming data buffer
25+
and sets 'msgSize' */
1326
void readMessageSize();
1427
public:
28+
29+
/** initializes fields to zero */
1530
Peer();
1631

32+
/** sets the socket to use for transferring data */
1733
void setup(Socket* sock);
34+
35+
/** closes the socket */
1836
void close();
37+
38+
/** \return the socket descriptor */
1939
int getFd() const {
2040
return sock->getFd();
2141
}
42+
43+
/** \return true if the socket is set and alive */
2244
bool isActive() const {
2345
return sock.get() ? sock->isActive() : false;
2446
}
47+
48+
/** \return socket specific address description */
2549
const std::string& getInfo() const {
2650
return sock->getInfo();
2751
}
52+
53+
/** reads data from socket and tries to read the next message size */
2854
void onInput();
55+
56+
/** serializes protobuf message and stores the data in the
57+
outgoing data buffer */
2958
void send(const google::protobuf::MessageLite& m);
59+
60+
/** \return true, if a packet can be deserialized from the
61+
incoming data buffer */
3062
bool hasPacket() const {
3163
return msgSize && inBuf.size() - inBufPos >= msgSize;
3264
}
65+
/** deserializes a protobuf message of type T from the incoming
66+
data buffer */
3367
template <class T>
3468
void recv(T& m) {
3569
m.ParseFromArray(inBuf.c_str() + inBufPos, msgSize);
@@ -41,8 +75,9 @@ namespace prototls {
4175
}
4276
readMessageSize();
4377
}
44-
void flush();
4578

79+
/** sends the outgoing data buffer and empties it */
80+
void flush();
4681
};
4782
}
4883
#endif

include/prototls/Select.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,42 @@
22
#define _prototls_select_hpp_
33
#include "prototls/Socket.hpp"
44
namespace prototls {
5+
/** wrapper for select system call to perform asynchronous IO
6+
on multiple sockets */
57
class Select {
8+
/** file descriptor set */
69
fd_set rfds;
10+
11+
/** the highest socket descriptor 'rfds' */
712
Socket::Fd max;
813
public:
14+
/** performs reset */
915
Select() {
1016
reset();
1117
}
18+
19+
/** initializes the file descriptor set */
1220
void reset() {
1321
FD_ZERO(&rfds);
1422
max = 0;
1523
}
24+
25+
/** marks the socket to be watched for reading */
1626
void input(Socket::Fd fd) {
1727
FD_SET(fd, &rfds);
1828
if (fd > max)
1929
max = fd;
2030
}
31+
32+
/** \return true if the socket has data waiting to be read */
2133
bool canRead(Socket::Fd fd) const {
2234
return FD_ISSET(fd, &rfds);
2335
}
36+
37+
/** waits 'msecs' milliseconds for the sockets in the
38+
socket descriptor set for input
39+
\return -1 on error, 0 if no data, > 0 number of sockets
40+
that have data waiting */
2441
int select(int msecs) {
2542
struct timeval tv;
2643
tv.tv_usec = (msecs % 1000) * 1000;

include/prototls/Server.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,48 @@
1414
#include <vector>
1515
#include <deque>
1616
namespace prototls {
17+
/** Server class template for implementing
18+
asynchronous servers that send and receive protobuf messages
19+
with/without encrypted communication. TLS handshakes
20+
are performed in separate threads in order to avoid blocking
21+
for long time. */
1722
template <class PeerT>
1823
class Server {
24+
/** socket that accepts connections */
1925
boost::scoped_ptr<Socket> sock;
26+
27+
/** connected peers are stored in a vector holding
28+
boost shared pointers to simplify memory management */
2029
typedef std::vector< boost::shared_ptr<PeerT> > Peers;
30+
31+
/** connected peers */
2132
Peers peers;
33+
34+
/** a pool of threads for TLS handshakes */
2235
boost::threadpool::pool pool;
36+
37+
/** thread safe deque that holds fresh TLS sockets */
2338
TSDeque<Socket*> socketsReady;
39+
40+
/** this method is called when a peer can read a packet */
2441
virtual void onPacket(PeerT&p) = 0;
42+
43+
/** this method is called when a new peer joins (after TLS
44+
handshake if encrypted communication is used) */
2545
virtual void onJoin(PeerT&p) = 0;
46+
47+
/** this method is called when a peer leaves */
2648
virtual void onLeave(PeerT& p) = 0;
49+
50+
/** performs TLS handshake on the socket and
51+
adds it to the list of ready sockets */
2752
void handshake(Socket* sock) {
2853
if (sock->handshake()) {
2954
return;
3055
}
3156
socketsReady.push_back(sock);
3257
}
58+
/** flag marking that the server has been closed */
3359
bool closed;
3460
public:
3561
Server(int threads=16) : pool(threads), closed(false) {

0 commit comments

Comments
 (0)