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 | /**
* @file SocketStream.h
*
* @author <a href="mailto:xu@informatik.hu-berlin.de">Xu Yuan</a>
* @breief encapsulate the socket I/O as a stream
*
*/
#ifndef SOCKET_STREAM_H
#define SOCKET_STREAM_H
#include <gio/gio.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <cstring>
#include <stdexcept>
const int default_recv_buffer_size = 1024;
class SocketStream<--- class 'SocketStream' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.
{
public:
SocketStream();
~SocketStream();
void init(GSocket* s);
void send(const std::string& msg) throw(std::runtime_error);
SocketStream& send();
int recv(std::string& msg) throw(std::runtime_error);
template <class T>
SocketStream & operator <<(const T& msg)
{
mSendStr << msg;
return *this;
}
SocketStream & operator <<(SocketStream& (*pf) (SocketStream&))
{
return pf(*this);
}
SocketStream & operator >>(std::string& msg)
{
recv(msg);<--- Unhandled exception specification when calling function recv(). [+]Unhandled exception specification when calling function recv(). Either use a try/catch around the function call, or add a exception specification for operator>>() also.
return *this;
}
/////////////////
protected:
void prefixedSend();
bool isFixedLengthDataAvailable(unsigned int len) throw(std::runtime_error);
unsigned int prefixedRecv(std::string& msg);
void reallocRecvBuffer(unsigned int size);
template <class T>
void addSendMsg(const T& msg)
{
mSendStr << msg;
}
private:
std::stringstream mSendStr;
char* mRecvBuf;
unsigned int mRecvBufSize;
unsigned int mRecvdLen;
GSocket* socket;
};
template<class T>
T& send(T& o)
{
return o.send();
}
class PrefixedSocketStream : public SocketStream
{
public:
PrefixedSocketStream()
{
}
~PrefixedSocketStream()
{
}
PrefixedSocketStream& send()
{
prefixedSend();
return *this;
}
template <class T>
PrefixedSocketStream & operator <<(const T& msg)
{
addSendMsg(msg);
return *this;
}
PrefixedSocketStream & operator <<(PrefixedSocketStream& (*pf) (PrefixedSocketStream&))
{
return pf(*this);
}
PrefixedSocketStream & operator >>(std::string& msg)
{
prefixedRecv(msg);
return *this;
}
};
#endif /* SOCKET_STREAM_HPP */
|