44#include " prototls/Socket.hpp"
55#include < boost/smart_ptr.hpp>
66namespace 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
0 commit comments