IBR-DTNSuite  0.8
ibrcommon/ibrcommon/net/tcpstream.h
Go to the documentation of this file.
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                 void setTimeout(unsigned int value);
00059 
00060         protected:
00061                 virtual int sync();
00062                 virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
00063                 virtual std::char_traits<char>::int_type underflow();
00064                 void interrupt();
00065 
00066                 int _socket;
00067 
00068                 class select_exception : public ibrcommon::Exception
00069                 {
00070                 public:
00071                         enum EXCEPTION_TYPE
00072                         {
00073                                 SELECT_UNKNOWN = 0,
00074                                 SELECT_TIMEOUT = 1,
00075                                 SELECT_CLOSED = 2,
00076                                 SELECT_ERROR = 3
00077                         };
00078 
00079                         select_exception(EXCEPTION_TYPE) throw() : ibrcommon::Exception("select exception")
00080                         {
00081                         };
00082                 };
00083 
00084                 int select(int int_pipe, bool &read, bool &write, bool &error, int timeout = 0) throw (select_exception);
00085 
00086                 int _interrupt_pipe_read[2];
00087                 int _interrupt_pipe_write[2];
00088 
00089         private:
00090                 // Input buffer
00091                 char *in_buf_;
00092                 // Output buffer
00093                 char *out_buf_;
00094 
00095                 // flag for non-blocking mode
00096                 bool _nonblocking;
00097 
00098                 unsigned int _timeout;
00099         };
00100 }
00101 
00102 #endif /* TCPSTREAM_H_ */