libt2n: (gerd) add lots of error handling code, unit tests for this error handling...
[libt2n] / src / socket_handler.hxx
index 832fa3a..623f31c 100644 (file)
 #ifndef __LIBT2N_SOCKET_HANDLER
 #define __LIBT2N_SOCKET_HANDLER
 
+#include <iostream>
+
 #include "types.hxx"
 
 namespace libt2n
 {
 
+/** @brief handles socket based communication.
+    Don't use directly, use socket_server or socket_client_connection instead.
+*/
 class socket_handler
 {
     private:
-        static const unsigned int recv_buffer_size=2048;
-        static const unsigned int write_block_size=4096;
+        static const unsigned int default_recv_buffer_size=2048;
+        static const unsigned int default_write_block_size=4096;
+        static const long long default_write_timeout=30000000;
 
         socket_type_value socket_type;
 
-        bool data_waiting(long long usec_timeout=-1);
+        bool data_waiting(long long usec_timeout,long long *timeout_remaining=NULL);
+        void wait_ready_to_write(int socket, long long write_block_timeout);
 
     protected:
         int sock;
+        unsigned int recv_buffer_size;
+        unsigned int write_block_size;
+        long long write_timeout;
 
-        socket_handler(int _sock, socket_type_value _socket_type)
-            { sock=_sock; socket_type=_socket_type; }
+        socket_handler(int _sock, socket_type_value _socket_type);
 
         void set_socket_options(int sock);
 
-        void log(log_level_values level, const std::string& message)
-            { log(level,message.c_str()); }
-        virtual void log(log_level_values level, const char* message)
-            { return; }
+        virtual std::ostream* get_logstream(log_level_values level)
+            { return NULL; }
 
-    public:
-        socket_type_value get_type()
-            { return socket_type; }
+        void socket_write(const std::string& data);
+
+        virtual void close();
 
-        bool fill_buffer(std::string& buffer, long long usec_timeout);
+        bool fill_buffer(std::string& buffer, long long usec_timeout, long long*timeout_remaining=NULL);
         bool fill_buffer(std::string& buffer);
 
-        virtual void close();
+    public:
+        /// is this a tcp or unix socket connection
+        socket_type_value get_type()
+            { return socket_type; }
 
         bool is_closed();
 
-        void socket_write(const std::string& data);
+        void set_recv_buffer_size(unsigned int new_recv_buffer_size);
+        void set_write_block_size(unsigned int new_write_block_size);
+        void set_write_timeout(long long new_write_timeout);
+
+        unsigned int get_recv_buffer_size() const { return recv_buffer_size; }
+        unsigned int get_write_block_size() const { return write_block_size; }
+        long long get_write_timeout() const { return write_timeout; }
 };
 
 }