void handle_client (ACE_LSOCK_Stream &stream) { char buf[BUFSIZ]; ACE_HANDLE handle; // Retrieve the socket descriptor passed from the client. if (stream.recv_handle (handle) == -1) ACE_ERROR ((LM_ERROR, "%p", "recv_handle")); ACE_DEBUG ((LM_DEBUG, "(%P|%t) ----------------------------------------\n")); // Read data from client (correctly handles incomplete reads due to // flow control). for (ssize_t n; (n = ACE_OS::read (handle, buf, sizeof buf)) > 0; ) ACE_DEBUG ((LM_DEBUG, "%*s", n, buf)); ACE_OS::sprintf (buf, "%d", static_cast<int> (ACE_OS::getpid ())); ACE_DEBUG ((LM_DEBUG, "(%s, %d) ----------------------------------------\n", buf, ACE_OS::strlen (buf))); // Tell the client to shut down. if (stream.send_n (buf, ACE_OS::strlen (buf)) == -1) ACE_ERROR ((LM_ERROR, "%p", "send")); // Close new endpoint (listening endpoint stays open). if (stream.close () == -1) ACE_ERROR ((LM_ERROR, "%p", "close")); }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { parse_args (argc, argv); ACE_LSOCK_Stream sc; ACE_LSOCK_Connector con; if (con.connect (sc, ACE_UNIX_Addr (rendezvous)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("connect")), -1); if (do_client_processing (sc) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("do_client_processing")), -1); if (sc.close () == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("close")), -1); return 0; }
static int do_client_processing (ACE_LSOCK_Stream &sc) { ACE_HANDLE fd_read[2]; ACE_HANDLE fd_write[2]; char buf[BUFSIZ]; int n; if (ACE_OS::pipe (fd_read) == -1 || ACE_OS::pipe (fd_write) == -1) return -1; if (sc.send_handle (fd_write[0]) == -1 || sc.send_handle (fd_read[1]) == -1) return -1; // Close off the ends we aren't interested in. if (ACE_OS::close (fd_read[1]) || ACE_OS::close (fd_write[0]) == -1) return -1; // Do a silly dup just for fun... ACE_HANDLE fd1 = ACE_OS::open (file_name, O_RDONLY); if (fd1 == ACE_INVALID_HANDLE) return -1; while ((n = ACE_OS::read (fd1, buf, sizeof buf)) > 0) { if (ACE_OS::write (fd_write[1], buf, n) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("write")), -1); if ((n = ACE_OS::read (fd_read[0], buf, sizeof buf)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("read")), -1); if (ACE_OS::write (ACE_STDOUT, buf, n) == -1) return -1; } if (ACE_OS::close (fd_read[0]) == -1 || ACE_OS::close (fd_write[1]) == -1 || ACE_OS::close (fd1) == -1) ACE_OS::exit (1); return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { parse_args (argc, argv); int fd; char buf[BUFSIZ]; int n; ACE_LSOCK_Stream sc; ACE_LSOCK_Connector con; if (con.connect (sc, ACE_UNIX_Addr (rendezvous)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("connect")), -1); if ((fd = ACE_OS::open (file_name, O_RDONLY)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), -1); // Send the open file descriptor to the server! if (sc.send_handle (fd) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("send_handle")), -1); if ((n = sc.recv_n (buf, sizeof buf)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv")), -1); else ACE_OS::write (ACE_STDOUT, buf, n); if (ACE_OS::close (fd) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("close")), -1); return 0; }
int ACE_LSOCK_Acceptor::accept (ACE_LSOCK_Stream &new_stream, ACE_Addr *remote_addr, ACE_Time_Value *timeout, int restart, int reset_new_handle) const { ACE_TRACE ("ACE_LSOCK_Acceptor::accept"); int in_blocking_mode = 0; if (this->shared_accept_start (timeout, restart, in_blocking_mode) == -1) return -1; else { sockaddr *addr = 0; int len = 0; if (remote_addr != 0) { len = remote_addr->get_size (); addr = (sockaddr *) remote_addr->get_addr (); } do new_stream.set_handle (ACE_OS::accept (this->get_handle (), addr, &len)); while (new_stream.get_handle () == ACE_INVALID_HANDLE && restart != 0 && errno == EINTR && timeout == 0); // Reset the size of the addr, which is only necessary for UNIX // domain sockets. if (new_stream.get_handle () != ACE_INVALID_HANDLE && remote_addr != 0) remote_addr->set_size (len); } return this->shared_accept_finish (new_stream, in_blocking_mode, reset_new_handle); }
// Establish a connection. ACE_LSOCK_Connector::ACE_LSOCK_Connector (ACE_LSOCK_Stream &new_stream, const ACE_UNIX_Addr &remote_sap, ACE_Time_Value *timeout, const ACE_Addr &local_sap, int reuse_addr, int flags, int perms) : ACE_SOCK_Connector (new_stream, remote_sap, timeout, local_sap, reuse_addr, flags, perms) { ACE_TRACE ("ACE_LSOCK_Connector::ACE_LSOCK_Connector"); // This is necessary due to the weird inheritance relationships of // ACE_LSOCK_Stream. new_stream.set_handle (new_stream.get_handle ()); }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { const ACE_TCHAR *rendezvous = argc > 1 ? argv[1] : ACE_DEFAULT_RENDEZVOUS; // Create a server. ACE_OS::unlink (rendezvous); ACE_UNIX_Addr addr (rendezvous); ACE_LSOCK_Acceptor peer_acceptor (addr); ACE_LSOCK_Stream stream; // Performs the concurrent server activities. for (;;) { // Create a new ACE_SOCK_Stream endpoint. if (peer_acceptor.accept (stream) == -1) ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "accept"), -1); ACE_DEBUG ((LM_DEBUG, "(%P|%t) accepted new connection\n")); #if defined (VXWORKS) handle_client (stream); #else switch (ACE_OS::fork (argv[0])) { case -1: ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "fork"), -1); /* NOTREACHED */ case 0: ACE_LOG_MSG->sync (argv[0]); handle_client (stream); ACE_OS::exit (0); /* NOTREACHED */ default: stream.close (); } #endif /* VXWORKS */ } ACE_NOTREACHED (return 0;) }