Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / examples / IPC_SAP / SOCK_SAP / CPP-unserver.cpp
blob2dd15f63352395126b812195631b65903f73c39a
1 // This example tests the features of the ACE_LSOCK_Acceptor and
2 // ACE_LSOCK_Stream classes. If the platform supports threads it uses
3 // a thread-per-request concurrency model.
5 #include "ace/LSOCK_Acceptor.h"
6 #include "ace/Thread_Manager.h"
7 #include "ace/OS_main.h"
8 #include "ace/OS_NS_unistd.h"
11 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
13 // Are we running verbosely?
14 static int verbose = 1;
16 // Entry point into the server task.
18 static void *
19 server (void *arg)
21 ACE_UNIX_Addr cli_addr;
22 ACE_LSOCK_Stream new_stream;
23 ACE_HANDLE handle = (ACE_HANDLE) (long) arg;
25 new_stream.set_handle (handle);
27 // Make sure we're not in non-blocking mode.
28 if (new_stream.disable (ACE_NONBLOCK) == -1)
29 ACE_ERROR_RETURN ((LM_ERROR,
30 ACE_TEXT ("%p\n"),
31 ACE_TEXT ("disable")),
32 0);
34 if (new_stream.get_remote_addr (cli_addr) == -1)
35 ACE_ERROR ((LM_ERROR,
36 ACE_TEXT ("%p\n"),
37 ACE_TEXT ("get_remote_addr")));
39 ACE_DEBUG ((LM_DEBUG,
40 ACE_TEXT ("(%P|%t) client connected from %C\n"),
41 cli_addr.get_path_name ()));
43 // Read data from client (terminate on error).
45 for (;;)
47 char buf[BUFSIZ];
49 ssize_t r_bytes = new_stream.recv (buf, sizeof buf);
51 if (r_bytes == -1)
53 ACE_ERROR ((LM_ERROR,
54 ACE_TEXT ("%p\n"),
55 ACE_TEXT ("recv")));
56 break;
58 else if (r_bytes == 0)
60 ACE_DEBUG ((LM_DEBUG,
61 ACE_TEXT ("(%P|%t) reached end of input, connection closed by client\n")));
62 break;
64 else if (verbose && ACE::write_n (ACE_STDOUT, buf, r_bytes) != r_bytes)
65 ACE_ERROR ((LM_ERROR,
66 ACE_TEXT ("%p\n"),
67 ACE_TEXT ("ACE::write_n")));
68 else if (new_stream.send_n (buf, r_bytes) != r_bytes)
69 ACE_ERROR ((LM_ERROR,
70 ACE_TEXT ("%p\n"),
71 ACE_TEXT ("send_n")));
74 // Close new endpoint (listening endpoint stays open).
75 if (new_stream.close () == -1)
76 ACE_ERROR ((LM_ERROR,
77 ACE_TEXT ("%p\n"),
78 ACE_TEXT ("close")));
80 return 0;
83 static int
84 run_event_loop (const ACE_TCHAR rendezvous[])
86 ACE_LSOCK_Acceptor peer_acceptor;
88 // Create a server address.
89 ACE_UNIX_Addr server_addr (rendezvous);
91 ACE_OS::unlink (rendezvous);
93 // Create a server.
95 if (peer_acceptor.open (server_addr) == -1)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 ACE_TEXT ("%p\n"),
98 ACE_TEXT ("open")),
99 1);
100 else if (peer_acceptor.get_local_addr (server_addr) == -1)
101 ACE_ERROR_RETURN ((LM_ERROR,
102 ACE_TEXT ("%p\n"),
103 ACE_TEXT ("get_local_addr")),
104 -1);
106 ACE_DEBUG ((LM_DEBUG,
107 ACE_TEXT ("starting server %C\n"),
108 server_addr.get_path_name ()));
110 // Keep these guys out here to prevent excessive constructor
111 // calls...
112 ACE_LSOCK_Stream new_stream;
114 // Performs the iterative server activities.
116 for (;;)
118 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
120 if (peer_acceptor.accept (new_stream, 0, &timeout) == -1)
122 ACE_ERROR ((LM_ERROR,
123 ACE_TEXT ("%p\n"),
124 ACE_TEXT ("accept")));
125 continue;
128 #if defined (ACE_HAS_THREADS)
129 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) server,
130 reinterpret_cast<void *> (new_stream.get_handle ()),
131 THR_DETACHED) == -1)
132 ACE_ERROR_RETURN ((LM_ERROR,
133 ACE_TEXT ("(%P|%t) %p\n"),
134 ACE_TEXT ("spawn")),
136 #else
137 server (reinterpret_cast<void *> (new_stream.get_handle ()));
138 #endif /* ACE_HAS_THREADS */
141 ACE_NOTREACHED (return 0;)
145 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
147 return run_event_loop (argc > 1 ? argv[1] : ACE_DEFAULT_RENDEZVOUS);
149 #else
150 int ACE_TMAIN (int, ACE_TCHAR *[])
152 ACE_ERROR_RETURN ((LM_ERROR,
153 "this platform does not support UNIX-domain sockets\n"), -1);
155 #endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */