metaproxy  1.13.0
Classes | Public Member Functions | Private Attributes | List of all members
metaproxy_1::Pipe Class Reference

#include <pipe.hpp>

Collaboration diagram for metaproxy_1::Pipe:
Collaboration graph

Classes

class  Error
 
class  Rep
 

Public Member Functions

 Pipe (int port_to_use)
 
 ~Pipe ()
 
int & read_fd () const
 
int & write_fd () const
 

Private Attributes

boost::scoped_ptr< Repm_p
 

Detailed Description

Definition at line 29 of file pipe.hpp.

Constructor & Destructor Documentation

Pipe::Pipe ( int  port_to_use)

Definition at line 108 of file pipe.cpp.

References m_p.

Referenced by metaproxy_1::Pipe::Error::Error().

108  : m_p(new Rep)
109 {
110 #ifdef WIN32
111  WSADATA wsaData;
112  WORD wVersionRequested = MAKEWORD(2, 0);
113  if (WSAStartup( wVersionRequested, &wsaData ))
114  throw Pipe::Error("WSAStartup failed");
115 #else
116  port_to_use = 0; // we'll just use pipe on Unix
117 #endif
118  if (port_to_use)
119  {
120  // create server socket
121  m_p->m_socket = socket(AF_INET, SOCK_STREAM, 0);
122  if (m_p->m_socket < 0)
123  throw Pipe::Error("could not create socket");
124 #ifndef WIN32
125  unsigned long one = 1;
126  if (setsockopt(m_p->m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)
127  &one, sizeof(one)) < 0)
128  throw Pipe::Error("setsockopt error");
129 #endif
130  // bind server socket
131  struct sockaddr_in add;
132  add.sin_family = AF_INET;
133  add.sin_port = htons(port_to_use);
134  add.sin_addr.s_addr = INADDR_ANY;
135  struct sockaddr *addr = ( struct sockaddr *) &add;
136 
137  if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
138  throw Pipe::Error("could not bind on socket");
139 
140  if (listen(m_p->m_socket, 3) < 0)
141  throw Pipe::Error("could not listen on socket");
142 
143  // client socket
144  unsigned int tmpadd;
145  tmpadd = (unsigned) inet_addr("127.0.0.1");
146  if (tmpadd)
147  memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
148  else
149  throw Pipe::Error("inet_addr failed");
150 
151  m_p->m_fd[1] = socket(AF_INET, SOCK_STREAM, 0);
152  if (m_p->m_fd[1] < 0)
153  throw Pipe::Error("could not create socket");
154 
155  m_p->nonblock(m_p->m_fd[1]);
156 
157  if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0)
158  {
159 #ifdef WIN32
160  if (WSAGetLastError() != WSAEWOULDBLOCK)
161  throw Pipe::Error("could not connect to socket");
162 #else
163  if (errno != EINPROGRESS)
164  throw Pipe::Error("could not connect to socket");
165 #endif
166  }
167 
168  // server accept
169  struct sockaddr caddr;
170 #ifdef WIN32
171  int caddr_len = sizeof(caddr);
172 #else
173  socklen_t caddr_len = sizeof(caddr);
174 #endif
175  m_p->m_fd[0] = accept(m_p->m_socket, &caddr, &caddr_len);
176  if (m_p->m_fd[0] < 0)
177  throw Pipe::Error("could not accept on socket");
178 
179  // complete connect
180  fd_set write_set;
181  FD_ZERO(&write_set);
182  FD_SET(m_p->m_fd[1], &write_set);
183  int r = select(m_p->m_fd[1]+1, 0, &write_set, 0, 0);
184  if (r != 1)
185  throw Pipe::Error("could not complete connect");
186 
187  m_p->close(m_p->m_socket);
188  }
189  else
190  {
191 #ifndef WIN32
192  if (pipe(m_p->m_fd))
193  throw Pipe::Error("pipe failed");
194  else
195  {
196  assert(m_p->m_fd[0] >= 0);
197  assert(m_p->m_fd[1] >= 0);
198  }
199 #endif
200  }
201 }
boost::scoped_ptr< Rep > m_p
Definition: pipe.hpp:42
Pipe::~Pipe ( )

Definition at line 203 of file pipe.cpp.

References m_p.

Referenced by metaproxy_1::Pipe::Error::Error().

204 {
205  m_p->close(m_p->m_fd[0]);
206  m_p->close(m_p->m_fd[1]);
207  m_p->close(m_p->m_socket);
208 #ifdef WIN32
209  WSACleanup();
210 #endif
211 }
boost::scoped_ptr< Rep > m_p
Definition: pipe.hpp:42

Member Function Documentation

int & Pipe::read_fd ( ) const

Definition at line 213 of file pipe.cpp.

References m_p.

Referenced by metaproxy_1::Pipe::Error::Error().

214 {
215  return m_p->m_fd[0];
216 }
boost::scoped_ptr< Rep > m_p
Definition: pipe.hpp:42
int & Pipe::write_fd ( ) const

Definition at line 218 of file pipe.cpp.

References m_p.

Referenced by metaproxy_1::Pipe::Error::Error().

219 {
220  return m_p->m_fd[1];
221 }
boost::scoped_ptr< Rep > m_p
Definition: pipe.hpp:42

Member Data Documentation

boost::scoped_ptr<Rep> metaproxy_1::Pipe::m_p
private

Definition at line 42 of file pipe.hpp.

Referenced by Pipe(), read_fd(), write_fd(), and ~Pipe().


The documentation for this class was generated from the following files: