-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtcpstream.hpp
More file actions
195 lines (163 loc) · 4.84 KB
/
tcpstream.hpp
File metadata and controls
195 lines (163 loc) · 4.84 KB
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
#ifndef _TCPSTREAM_HPP
#define _TCPSTREAM_HPP
#include <string>
#include <streambuf>
#include <iostream>
#include "socket.hpp"
template <class Socket,
class charT, class traits = std::char_traits<charT> >
class SocketStreamBuffer : public std::basic_streambuf<charT, traits>
{
typedef std::basic_streambuf<charT, traits> sbuftype;
typedef typename sbuftype::int_type int_type;
typedef charT char_type;
public:
// the buffer will take ownership of the socket (ie. it will close it
// in the destructor) if takeowner == true
explicit SocketStreamBuffer(Socket &sock,
bool takeowner = false, std::streamsize bufsize = 512)
: rsocket_(sock), ownsocket_(takeowner),
inbuf_(NULL), outbuf_(NULL), bufsize_(bufsize),
remained_(0), ownbuffers_(false)
{
}
~SocketStreamBuffer()
{
try
{
_flush();
if (ownsocket_ == true)
{
rsocket_.close();
}
}
catch (...)
{
// we don't want exceptions to fly out of here
// and there is not much we can do with errors
// in this context anyway
}
if (ownbuffers_)
{
delete [] inbuf_;
delete [] outbuf_;
}
}
protected:
sbuftype * setbuf(char_type *s, std::streamsize n)
{
if (this->gptr() == NULL)
{
setg(s, s + n, s + n);
setp(s, s + n);
inbuf_ = s;
outbuf_ = s;
bufsize_ = n;
ownbuffers_ = false;
}
return this;
}
void _flush()
{
rsocket_.write(outbuf_,
(this->pptr() - outbuf_) * sizeof(char_type));
}
int_type overflow(int_type c = traits::eof())
{
// this method is supposed to flush the put area of the buffer
// to the I/O device
// if the buffer was not already allocated nor set by user,
// do it just now
if (this->pptr() == NULL)
{
outbuf_ = new char_type[bufsize_];
ownbuffers_ = true;
}
else
{
_flush();
}
setp(outbuf_, outbuf_ + bufsize_);
if (c != traits::eof())
{
sputc(traits::to_char_type(c));
}
return 0;
}
int sync()
{
// just flush the put area
_flush();
setp(outbuf_, outbuf_ + bufsize_);
return 0;
}
int_type underflow()
{
// this method is supposed to read some bytes from the I/O device
// if the buffer was not already allocated nor set by user,
// do it just now
if (this->gptr() == NULL)
{
inbuf_ = new char_type[bufsize_];
ownbuffers_ = true;
}
if (remained_ != 0)
{
inbuf_[0] = remainedchar_;
}
size_t readn = rsocket_.read(static_cast<char*>(inbuf_) + remained_,
bufsize_ * sizeof(char_type) - remained_);
// if (readn == 0 && remained_ != 0)
// error - there is not enough bytes for completing
// the last character before the end of the stream
// - this can mean error on the remote end
if (readn == 0)
{
return traits::eof();
}
size_t totalbytes = readn + remained_;
setg(inbuf_, inbuf_,
inbuf_ + totalbytes / sizeof(char_type));
remained_ = totalbytes % sizeof(char_type);
if (remained_ != 0)
{
remainedchar_ = inbuf_[totalbytes / sizeof(char_type)];
}
return this->sgetc();
}
private:
// not for use
SocketStreamBuffer(const SocketStreamBuffer&);
void operator=(const SocketStreamBuffer&);
Socket &rsocket_;
bool ownsocket_;
char_type *inbuf_;
char_type *outbuf_;
std::streamsize bufsize_;
size_t remained_;
char_type remainedchar_;
bool ownbuffers_;
};
// this class is an ultimate stream associated with a socket
template <class SocketWrapper,
class charT, class traits = std::char_traits<charT> >
class SocketGenericStream :
private SocketStreamBuffer<SocketWrapper, charT, traits>,
public std::basic_iostream<charT, traits>
{
public:
// this constructor takes 'ownership' of the socket wrapper if btakeowner == true,
// so that the socket will be closed in the destructor of the
// TCPStreamBuffer object
explicit SocketGenericStream(SocketWrapper &sock, bool takeowner = false)
: SocketStreamBuffer<SocketWrapper, charT, traits>(sock, takeowner),
std::basic_iostream<charT, traits>(this)
{
}
private:
// not for use
SocketGenericStream(const SocketGenericStream&);
void operator=(const SocketGenericStream&);
};
typedef SocketGenericStream<Socket, char> TCPStream;
#endif