00001 /* 00002 * tcpstream.h 00003 * 00004 * Created on: 29.07.2009 00005 * Author: morgenro 00006 */ 00007 00008 #ifndef IBRCOMMON_TCPSTREAM_H_ 00009 #define IBRCOMMON_TCPSTREAM_H_ 00010 00011 #include "ibrcommon/Exceptions.h" 00012 #include <streambuf> 00013 #include <sys/types.h> 00014 #include <sys/socket.h> 00015 #include <iostream> 00016 00017 namespace ibrcommon 00018 { 00019 class SocketException : public Exception 00020 { 00021 public: 00022 SocketException(string error) : Exception(error) 00023 {}; 00024 }; 00025 00026 class ConnectionClosedException : public SocketException 00027 { 00028 public: 00029 ConnectionClosedException(string what = "The connection has been closed.") throw() : SocketException(what) 00030 { 00031 }; 00032 }; 00033 00034 class tcpstream : public std::basic_streambuf<char, std::char_traits<char> >, public std::iostream 00035 { 00036 public: 00037 enum stream_error 00038 { 00039 ERROR_NONE = 0, 00040 ERROR_EPIPE = 1, 00041 ERROR_CLOSED = 2, 00042 ERROR_WRITE = 3, 00043 ERROR_READ = 4, 00044 ERROR_RESET = 5 00045 }; 00046 00047 // The size of the input and output buffers. 00048 static const size_t BUFF_SIZE = 5120; 00049 00050 tcpstream(int socket = -1); 00051 virtual ~tcpstream(); 00052 00053 string getAddress() const; 00054 int getPort() const; 00055 00056 void close(bool errorcheck = false); 00057 00058 stream_error errmsg; 00059 00060 void enableKeepalive(); 00061 void enableLinger(int linger); 00062 void enableNoDelay(); 00063 00064 protected: 00065 virtual int sync(); 00066 virtual int overflow(int = std::char_traits<char>::eof()); 00067 virtual int underflow(); 00068 00069 int _socket; 00070 00071 private: 00072 // Input buffer 00073 char *in_buf_; 00074 // Output buffer 00075 char *out_buf_; 00076 }; 00077 } 00078 00079 #endif /* TCPSTREAM_H_ */
1.7.1