2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
20 /***************************************************************************
21 insocketstream.hxx - C++ streambuffer wrapper
23 begin : Sun Nov 10 2002
24 copyright : (C) 2002 by Intra2net AG
25 ***************************************************************************/
27 #ifndef _INSOCKETSTREAM
28 #define _INSOCKETSTREAM
36 #include <sys/socket.h>
40 #include "exception.hxx"
42 // ATTENTION: A lot of mysterious STL bugs occured
43 // with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
44 // -> Keep it slow and working!
46 class insocketstream : public std::streambuf
53 insocketstream(std::string unixsocket)
55 sock=socket(AF_UNIX,SOCK_STREAM,0);
58 std::ostringstream os;
59 os << "can't open socket: " << strerror(errno);
60 throw EXCEPTION (insocketstream_error, os.str());
63 struct sockaddr_un server_adr;
64 server_adr.sun_family=AF_UNIX;
65 strncpy(server_adr.sun_path,unixsocket.c_str(),sizeof(server_adr.sun_path));
66 server_adr.sun_path[sizeof(server_adr.sun_path)-1]=0;
68 if(connect(sock,(struct sockaddr *) &server_adr, sizeof(server_adr)))
70 std::ostringstream os;
71 os << "can't connect to socket: " << strerror(errno);
72 throw EXCEPTION (insocketstream_error, os.str());
75 setg (&buffer, &buffer, &buffer); // force underflow
82 shutdown(sock,SHUT_RDWR);
90 virtual int_type underflow()
93 return std::streambuf::traits_type::to_int_type(*gptr());
95 int nbytes=::read(sock,&buffer,1);
100 return std::streambuf::traits_type::to_int_type(*gptr());
103 std::ostringstream os;
104 os << "error reading form socket: " << strerror(errno);
105 throw EXCEPTION (insocketstream_error, os.str());
111 setg (&buffer, &buffer, &buffer+sizeof(char));
112 return std::streambuf::traits_type::to_int_type(*gptr());