libt2n: (gerd) add lots of error handling code, unit tests for this error handling...
[libt2n] / src / socket_handler.hxx
CommitLineData
a11e19b7
GE
1/***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19#ifndef __LIBT2N_SOCKET_HANDLER
20#define __LIBT2N_SOCKET_HANDLER
21
a7170401
GE
22#include <iostream>
23
a11e19b7
GE
24#include "types.hxx"
25
26namespace libt2n
27{
28
94247295
GE
29/** @brief handles socket based communication.
30 Don't use directly, use socket_server or socket_client_connection instead.
31*/
a11e19b7
GE
32class socket_handler
33{
34 private:
517d1214
RP
35 static const unsigned int default_recv_buffer_size=2048;
36 static const unsigned int default_write_block_size=4096;
c7857475 37 static const long long default_write_timeout=30000000;
a11e19b7
GE
38
39 socket_type_value socket_type;
40
45a2ebc9 41 bool data_waiting(long long usec_timeout,long long *timeout_remaining=NULL);
c7857475 42 void wait_ready_to_write(int socket, long long write_block_timeout);
a11e19b7
GE
43
44 protected:
45 int sock;
517d1214
RP
46 unsigned int recv_buffer_size;
47 unsigned int write_block_size;
c7857475 48 long long write_timeout;
a11e19b7 49
517d1214 50 socket_handler(int _sock, socket_type_value _socket_type);
a11e19b7
GE
51
52 void set_socket_options(int sock);
53
a7170401
GE
54 virtual std::ostream* get_logstream(log_level_values level)
55 { return NULL; }
a11e19b7 56
94247295
GE
57 void socket_write(const std::string& data);
58
59 virtual void close();
a11e19b7 60
45a2ebc9 61 bool fill_buffer(std::string& buffer, long long usec_timeout, long long*timeout_remaining=NULL);
a11e19b7
GE
62 bool fill_buffer(std::string& buffer);
63
94247295 64 public:
517d1214 65 /// is this a tcp or unix socket connection
94247295
GE
66 socket_type_value get_type()
67 { return socket_type; }
a11e19b7 68
644c4d26 69 bool is_closed();
517d1214
RP
70
71 void set_recv_buffer_size(unsigned int new_recv_buffer_size);
72 void set_write_block_size(unsigned int new_write_block_size);
c7857475 73 void set_write_timeout(long long new_write_timeout);
517d1214
RP
74
75 unsigned int get_recv_buffer_size() const { return recv_buffer_size; }
76 unsigned int get_write_block_size() const { return write_block_size; }
c7857475 77 long long get_write_timeout() const { return write_timeout; }
a11e19b7
GE
78};
79
80}
81
82#endif