metaproxy  1.21.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::Pipe ( int  port_to_use)

Definition at line 109 of file pipe.cpp.

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

References m_p.

◆ ~Pipe()

Pipe::~Pipe ( )

Definition at line 204 of file pipe.cpp.

205 {
206  m_p->close(m_p->m_fd[0]);
207  m_p->close(m_p->m_fd[1]);
208  m_p->close(m_p->m_socket);
209 #ifdef WIN32
210  WSACleanup();
211 #endif
212 }

References m_p.

Member Function Documentation

◆ read_fd()

int & Pipe::read_fd ( ) const

Definition at line 214 of file pipe.cpp.

215 {
216  return m_p->m_fd[0];
217 }

References m_p.

Referenced by metaproxy_1::filter::FrontendNet::My_Timer_Thread::My_Timer_Thread().

◆ write_fd()

int & Pipe::write_fd ( ) const

Definition at line 219 of file pipe.cpp.

220 {
221  return m_p->m_fd[1];
222 }

References m_p.

Member Data Documentation

◆ m_p

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: