2 /* $Id: pipe.cpp,v 1.1 2005-11-07 12:32:01 adam Exp $
3 Copyright (c) 2005, Index Data.
16 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netinet/tcp.h>
23 #include <sys/socket.h>
26 #include <sys/select.h>
29 #include <boost/thread/thread.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
37 #include <yaz++/socket-observer.h>
43 class Pipe::Rep : public boost::noncopyable {
55 m_fd[0] = m_fd[1] = -1;
59 Pipe::Pipe(int port_to_use) : m_p(new Rep)
63 m_p->m_socket = socket(AF_INET, SOCK_STREAM, 0);
64 if (m_p->m_socket < 0)
65 throw Pipe::Error("could not create socket");
67 m_p->m_fd[1] = socket(AF_INET, SOCK_STREAM, 0);
69 throw Pipe::Error("could not create socket");
71 struct sockaddr_in add;
72 add.sin_family = AF_INET;
73 add.sin_port = htons(port_to_use);
74 add.sin_addr.s_addr = INADDR_ANY;
75 struct sockaddr *addr = ( struct sockaddr *) &add;
77 if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
78 throw Pipe::Error("could not bind on socket");
80 if (listen(m_p->m_socket, 3) < 0)
81 throw Pipe::Error("could not listen on socket");
83 struct sockaddr caddr;
84 socklen_t caddr_len = sizeof(caddr);
85 m_p->m_fd[0] = accept(m_p->m_socket, &caddr, &caddr_len);
87 throw Pipe::Error("could not accept on socket");
89 if (connect(m_p->m_fd[1], addr, sizeof(addr)) < 0)
90 throw Pipe::Error("could not connect to socket");
101 if (m_p->m_fd[0] != -1)
103 if (m_p->m_fd[1] != -1)
105 if (m_p->m_socket != -1)
106 close(m_p->m_socket);
109 int &Pipe::read_fd() const
114 int &Pipe::write_fd() const
122 * indent-tabs-mode: nil
123 * c-file-style: "stroustrup"
125 * vim: shiftwidth=4 tabstop=8 expandtab