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 #include "ibrcommon/net/vsocket.h" 00017 00018 namespace ibrcommon 00019 { 00020 class ConnectionClosedException : public vsocket_exception 00021 { 00022 public: 00023 ConnectionClosedException(string what = "The connection has been closed.") throw() : ibrcommon::vsocket_exception(what) 00024 { 00025 }; 00026 }; 00027 00028 class tcpstream : public std::basic_streambuf<char, std::char_traits<char> >, public std::iostream 00029 { 00030 public: 00031 enum stream_error 00032 { 00033 ERROR_NONE = 0, 00034 ERROR_EPIPE = 1, 00035 ERROR_CLOSED = 2, 00036 ERROR_WRITE = 3, 00037 ERROR_READ = 4, 00038 ERROR_RESET = 5 00039 }; 00040 00041 // The size of the input and output buffers. 00042 static const size_t BUFF_SIZE = 5120; 00043 00044 tcpstream(int socket = -1); 00045 virtual ~tcpstream(); 00046 00047 string getAddress() const; 00048 int getPort() const; 00049 00050 void close(bool errorcheck = false); 00051 00052 stream_error errmsg; 00053 00054 void enableKeepalive(); 00055 void enableLinger(int linger); 00056 void enableNoDelay(); 00057 00058 protected: 00059 virtual int sync(); 00060 virtual int overflow(int = std::char_traits<char>::eof()); 00061 virtual int underflow(); 00062 00063 int _socket; 00064 00065 private: 00066 // Input buffer 00067 char *in_buf_; 00068 // Output buffer 00069 char *out_buf_; 00070 }; 00071 } 00072 00073 #endif /* TCPSTREAM_H_ */
1.7.1