3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
15 Buffered::Buffered(Base &b, unsigned s):
18 in_buf(new char[buf_size]),
21 out_buf(new char[buf_size]),
24 mode=below.get_mode();
25 below.signal_closing.connect(sigc::mem_fun(this, &Buffered::below_closing));
28 unsigned Buffered::put(char c)
32 out_buf[out_used++]=c;
36 return do_write(&c, 1);
39 void Buffered::flush()
44 unsigned len=below.write(out_buf, out_used);
47 memmove(out_buf, out_buf+len, out_used-len);
49 throw Exception("Couldn't flush all data");
54 bool Buffered::getline(std::string &line)
56 for(unsigned i=0; i<in_avail; ++i)
59 line.assign(in_ptr, i);
65 return Base::getline(line);
79 return static_cast<unsigned char>(c);
82 Handle Buffered::get_event_handle()
84 throw Exception("Buffered doesn't support events");
100 void Buffered::below_closing()
105 unsigned Buffered::do_write(const char *buf, unsigned size)
107 if(out_used+size<buf_size)
109 // All data fits in the buffer
110 memcpy(out_buf+out_used, buf, size);
122 // XXX sub-obtimal - should try to write directly from input first
123 // Fill the buffer and pass it on
124 unsigned head=min(buf_size-out_used, size);
126 memcpy(out_buf+out_used, buf, head);
135 unsigned len=below.write(out_buf, out_used);
142 memmove(out_buf, out_buf+len, buf_size-len);
143 out_used=buf_size-len;
151 unsigned Buffered::do_read(char *buf, unsigned size)
155 // The request can be served from the buffer
156 memcpy(buf, in_ptr, size);
164 // Use whatever is left in the buffer
165 memcpy(buf, in_ptr, in_avail);
175 ret+=below.read(buf, size);
178 // Read more data into the buffer
181 in_avail=below.read(in_buf, buf_size);
188 unsigned head=min(size, in_avail);
189 memcpy(buf, in_buf, head);