IBR-DTNSuite  0.10
socketstream.h
Go to the documentation of this file.
1 /*
2  * socketstream.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef SOCKETSTREAM_H_
23 #define SOCKETSTREAM_H_
24 
25 #include "ibrcommon/net/socket.h"
26 #include "ibrcommon/net/vsocket.h"
27 #include <streambuf>
28 #include <iostream>
29 #include <vector>
30 
31 namespace ibrcommon
32 {
33  class stream_exception : public Exception
34  {
35  public:
36  stream_exception(string error) : Exception(error)
37  {};
38  };
39 
40  class socketstream : public std::basic_streambuf<char, std::char_traits<char> >, public std::iostream
41  {
42  public:
43  socketstream(clientsocket *sock, size_t buffer_size = 5120);
44  virtual ~socketstream();
45 
46  void setTimeout(const timeval &val);
47  void close();
48 
50 
51  protected:
52  virtual int sync();
53  virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
54  virtual std::char_traits<char>::int_type underflow();
55 
56  private:
57  vsocket _socket;
58  const size_t _bufsize;
59 
60  // Input buffer
61  std::vector<char> in_buf_;
62  // Output buffer
63  std::vector<char> out_buf_;
64 
65  timeval _timeout;
66  };
67 } /* namespace ibrcommon */
68 #endif /* SOCKETSTREAM_H_ */