libcsdbg  1.28
C++ exception (and generic) stack trace debug library
tcpsockbuf & csdbg::tcpsockbuf::open ( )
virtual

Connect the socket to its peer.

Returns
*this
Exceptions
csdbg::exception
Note
If the socket is already connected, it is closed and re-connected to the new address/port

Implements csdbg::streambuf.

Definition at line 170 of file tcpsockbuf.cpp.

References csdbg::streambuf::close(), m_address, csdbg::streambuf::m_handle, m_port, csdbg::util::memset(), and unlikely.

171 {
172  if ( unlikely(m_handle >= 0) )
173  close();
174 
175  /* Create the stream socket */
176  m_handle = socket(AF_INET, SOCK_STREAM, 0);
177  if ( unlikely(m_handle < 0) )
178  throw exception(
179  "failed to create stream socket (errno %d - %s)",
180  errno,
181  strerror(errno)
182  );
183 
184  tcp_addr_t addr;
185  util::memset(&addr, 0, sizeof(tcp_addr_t));
186  addr.sin_family = AF_INET;
187  addr.sin_port = htons(m_port);
188  addr.sin_addr.s_addr = inet_addr(m_address);
189 
190  /* Connect the socket to its peer */
191  ip_addr_t *ip = reinterpret_cast<ip_addr_t*> (&addr);
192  i32 retval;
193  do {
194  retval = connect(m_handle, ip, sizeof(tcp_addr_t));
195  }
196  while ( unlikely(retval < 0 && errno == EINTR) );
197 
198  if ( unlikely(retval < 0) ) {
199  close();
200  throw exception(
201  "failed to connect TCP/IP socket @ %s:%d (errno %d - %s)",
202  m_address,
203  m_port,
204  errno,
205  strerror(errno)
206  );
207  }
208 
209  return *this;
210 }
static void * memset(void *, u8, u32)
Fill a memory block with a constant byte.
Definition: util.cpp:320
i8 * m_address
Peer IP address (numerical, IPv4)
Definition: tcpsockbuf.hpp:40
i32 m_port
Peer TCP port.
Definition: tcpsockbuf.hpp:42
i32 m_handle
Stream handle (descriptor)
Definition: streambuf.hpp:43
struct sockaddr_in tcp_addr_t
TCP IPv4 address.
Definition: config.hpp:137
int i32
32-bit signed integer
Definition: config.hpp:82
struct sockaddr ip_addr_t
IP address.
Definition: config.hpp:142
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
virtual streambuf & close()
Close the stream.
Definition: streambuf.cpp:130

+ Here is the call graph for this function: