Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / examples / IPC_SAP / SPIPE_SAP / server.cpp
blob43b65be91f2487c9791dcbfcae2b305940be2007
1 #include "ace/OS_main.h"
2 #include "ace/SPIPE_Addr.h"
3 #include "ace/SPIPE_Acceptor.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/OS_NS_poll.h"
7 #include "ace/OS_NS_unistd.h"
10 #if defined (ACE_HAS_STREAM_PIPES)
12 #include "shared.h"
14 // Maximum per-process open I/O descriptors.
15 const int MAX_HANDLES = 200;
17 int
18 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
20 ACE_SPIPE_Acceptor peer_acceptor;
21 ACE_SPIPE_Stream new_stream;
22 struct pollfd poll_array[MAX_HANDLES];
23 ACE_HANDLE handle;
25 for (handle = 0; handle < MAX_HANDLES; handle++)
27 poll_array[handle].fd = -1;
28 poll_array[handle].events = POLLIN;
31 if (argc > 1)
32 rendezvous = argv[1];
34 ACE_OS::fdetach (ACE_TEXT_ALWAYS_CHAR (rendezvous));
35 ACE_SPIPE_Addr addr (rendezvous);
37 ACE_HANDLE s_handle = peer_acceptor.open (addr);
39 if (s_handle == -1)
40 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "peer_acceptor.open"), -1);
42 poll_array[0].fd = s_handle;
44 for (int width = 1;;)
46 // Block waiting for client I/O events (handle interrupts).
47 while (ACE_OS::poll (poll_array, width) == -1 && errno == EINTR)
48 continue;
50 // Handle pending logging messages first (s_handle + 1 is
51 // guaranteed to be lowest client descriptor).
53 for (handle = s_handle + 1; handle < width; handle++)
54 if (ACE_BIT_ENABLED (poll_array[handle].revents, POLLIN)
55 || ACE_BIT_ENABLED (poll_array[handle].revents, POLLHUP))
57 char buf[BUFSIZ];
58 ssize_t n = ACE_OS::read (handle, buf, sizeof buf);
60 // recv will not block in this case!
61 if (n == -1)
62 ACE_DEBUG ((LM_DEBUG, "%p\n", "read failed"));
63 else if (n == 0)
65 // Handle client connection shutdown.
66 if (ACE_OS::close (poll_array[handle].fd) == -1)
67 ACE_DEBUG ((LM_DEBUG, "%p\n", "close"));
68 poll_array[handle].fd = -1;
70 if (handle + 1 == width)
72 while (poll_array[handle].fd == -1)
73 handle--;
74 width = handle + 1;
77 else
78 ACE_DEBUG ((LM_DEBUG, "%*s\n", n, buf));
81 if (ACE_BIT_ENABLED (poll_array[0].revents, POLLIN))
83 if (peer_acceptor.accept (new_stream) == -1)
84 ACE_DEBUG ((LM_DEBUG, "%p\n", "accept failed"));
86 ACE_SPIPE_Addr client;
87 ACE_HANDLE n_handle = new_stream.get_handle ();
89 if (new_stream.get_remote_addr (client) == -1)
90 ACE_DEBUG ((LM_DEBUG, "%p\n",
91 "get_remote_addr failed"));
93 ACE_DEBUG ((LM_DEBUG,
94 "n_handle = %d, uid = %d, gid = %d\n",
95 n_handle,
96 client.user_id (),
97 client.group_id ()));
99 int arg = RMSGN | RPROTDAT;
101 if (ACE_OS::ioctl (n_handle,
102 I_SRDOPT, (void *) arg) == -1)
103 ACE_DEBUG ((LM_DEBUG, "%p\n", "ioctl failed"));
105 poll_array[n_handle].fd = n_handle;
107 if (n_handle >= width)
108 width = n_handle + 1;
112 ACE_NOTREACHED (return 0;)
114 #else
115 #include <stdio.h>
116 int ACE_TMAIN (int, ACE_TCHAR *[])
118 ACE_OS::fprintf (stderr, "This feature is not supported\n");
119 return 0;
121 #endif /* ACE_HAS_STREAM_PIPES */