[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / ACE.cpp
blob4980b2d73441651d7f15900e0a56b2e7abd10084
1 // $Id: ACE.cpp 82581 2008-08-11 08:58:24Z johnnyw $
3 #include "ace/ACE.h"
5 #include "ace/Basic_Types.h"
6 #include "ace/Handle_Set.h"
7 #include "ace/Auto_Ptr.h"
8 #include "ace/SString.h"
9 #include "ace/Version.h"
10 #include "ace/Message_Block.h"
11 #include "ace/Log_Msg.h"
12 #include "ace/OS_NS_sys_select.h"
13 #include "ace/OS_NS_string.h"
14 #include "ace/OS_NS_strings.h"
15 #include "ace/OS_NS_signal.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/OS_NS_sys_resource.h"
18 #include "ace/OS_NS_sys_wait.h"
19 #include "ace/OS_NS_sys_time.h"
20 #include "ace/OS_NS_time.h"
21 #include "ace/OS_NS_sys_uio.h"
22 #include "ace/OS_NS_sys_stat.h"
23 #include "ace/OS_NS_ctype.h"
24 #include "ace/OS_NS_fcntl.h"
25 #include "ace/OS_TLI.h"
26 #include "ace/Truncate.h"
28 #if defined (ACE_VXWORKS) && (ACE_VXWORKS < 0x620)
29 extern "C" int maxFiles;
30 #endif /* ACE_VXWORKS */
32 #if !defined (__ACE_INLINE__)
33 #include "ace/ACE.inl"
34 #endif /* __ACE_INLINE__ */
36 #if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
37 # include "ace/OS_NS_poll.h"
38 #endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
41 ACE_RCSID (ace,
42 ACE,
43 "$Id: ACE.cpp 82581 2008-08-11 08:58:24Z johnnyw $")
46 // Open versioned namespace, if enabled by the user.
47 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
49 namespace ACE
51 // private:
52 // Used internally so not exported.
54 // Size of allocation granularity.
55 size_t allocation_granularity_ = 0;
57 // Size of a VM page.
58 size_t pagesize_ = 0;
60 // Are we debugging ACE?
61 // Keeps track of whether we're in some global debug mode.
62 char debug_;
66 int
67 ACE::out_of_handles (int error)
69 // EMFILE is common to all platforms.
70 if (error == EMFILE ||
71 #if defined (ACE_WIN32)
72 // On Win32, we need to check for ENOBUFS also.
73 error == ENOBUFS ||
74 #elif defined (HPUX)
75 // On HPUX, we need to check for EADDRNOTAVAIL also.
76 error == EADDRNOTAVAIL ||
77 #elif defined (linux)
78 // On linux, we need to check for ENOENT also.
79 error == ENOENT ||
80 // For RedHat5.2, need to check for EINVAL too.
81 error == EINVAL ||
82 // Without threads check for EOPNOTSUPP
83 error == EOPNOTSUPP ||
84 #elif defined (sun)
85 // On sun, we need to check for ENOSR also.
86 error == ENOSR ||
87 // Without threads check for ENOTSUP
88 error == ENOTSUP ||
89 #elif defined (__FreeBSD__)
90 // On FreeBSD we need to check for EOPNOTSUPP (LinuxThreads) or
91 // ENOSYS (libc_r threads) also.
92 error == EOPNOTSUPP ||
93 error == ENOSYS ||
94 #elif defined (__OpenBSD__)
95 // OpenBSD appears to return EBADF.
96 error == EBADF ||
97 #elif defined (__sgi) // irix
98 error == ENOTSUP ||
99 #elif defined (DIGITAL_UNIX) // osf1
100 error == ENOTSUP ||
101 #endif /* ACE_WIN32 */
102 error == ENFILE)
103 return 1;
104 else
105 return 0;
108 u_int
109 ACE::major_version (void)
111 return ACE_MAJOR_VERSION;
114 u_int
115 ACE::minor_version (void)
117 return ACE_MINOR_VERSION;
120 u_int
121 ACE::beta_version (void)
123 return ACE_BETA_VERSION;
126 const ACE_TCHAR *
127 ACE::compiler_name (void)
129 #ifdef ACE_CC_NAME
130 return ACE_CC_NAME;
131 #else
132 return ACE_TEXT ("");
133 #endif
136 u_int
137 ACE::compiler_major_version (void)
139 #ifdef ACE_CC_MAJOR_VERSION
140 return ACE_CC_MAJOR_VERSION;
141 #else
142 return 0;
143 #endif
146 u_int
147 ACE::compiler_minor_version (void)
149 #ifdef ACE_CC_MINOR_VERSION
150 return ACE_CC_MINOR_VERSION;
151 #else
152 return 0;
153 #endif
156 u_int
157 ACE::compiler_beta_version (void)
159 #ifdef ACE_CC_BETA_VERSION
160 return ACE_CC_BETA_VERSION;
161 #else
162 return 0;
163 #endif
166 bool
167 ACE::debug (void)
169 static const char* debug = ACE_OS::getenv ("ACE_DEBUG");
170 return (ACE::debug_ != 0) ? ACE::debug_ : (debug != 0 ? (*debug != '0'): false);
173 void
174 ACE::debug (bool onoff)
176 ACE::debug_ = onoff;
180 ACE::select (int width,
181 ACE_Handle_Set *readfds,
182 ACE_Handle_Set *writefds,
183 ACE_Handle_Set *exceptfds,
184 const ACE_Time_Value *timeout)
186 int result = ACE_OS::select (width,
187 readfds ? readfds->fdset () : 0,
188 writefds ? writefds->fdset () : 0,
189 exceptfds ? exceptfds->fdset () : 0,
190 timeout);
191 if (result > 0)
193 # if !defined (ACE_WIN32)
194 // This isn't needed for Windows... it's a no-op anyway.
195 if (readfds)
196 readfds->sync ((ACE_HANDLE) width);
197 if (writefds)
198 writefds->sync ((ACE_HANDLE) width);
199 if (exceptfds)
200 exceptfds->sync ((ACE_HANDLE) width);
201 #endif /* ACE_WIN32 */
203 return result;
207 ACE::select (int width,
208 ACE_Handle_Set &readfds,
209 const ACE_Time_Value *timeout)
211 int result = ACE_OS::select (width,
212 readfds.fdset (),
215 timeout);
217 #if !defined (ACE_WIN32)
218 if (result > 0)
219 readfds.sync ((ACE_HANDLE) width);
220 #endif /* ACE_WIN32 */
221 return result;
225 ACE::terminate_process (pid_t pid)
227 #if defined (ACE_HAS_PHARLAP)
228 ACE_UNUSED_ARG (pid);
229 ACE_NOTSUP_RETURN (-1);
230 #elif defined (ACE_WIN32)
231 // Create a handle for the given process id.
232 ACE_HANDLE process_handle =
233 ::OpenProcess (PROCESS_TERMINATE,
234 FALSE, // New handle is not inheritable.
235 pid);
237 if (process_handle == ACE_INVALID_HANDLE
238 || process_handle == 0)
239 return -1;
240 else
242 // Kill the process associated with process_handle.
243 BOOL terminate_result =
244 ::TerminateProcess (process_handle, 0);
245 // Free up the kernel resources.
246 ACE_OS::close (process_handle);
247 return terminate_result ? 0 : -1;
249 #else
250 return ACE_OS::kill (pid, 9);
251 #endif /* ACE_HAS_PHARLAP */
255 ACE::process_active (pid_t pid)
257 #if !defined(ACE_WIN32)
258 if (ACE_OS::kill (pid, 0) == 0)
259 return 1;
260 else if (errno == ESRCH)
261 return 0;
262 else
263 return -1;
264 #else
265 // Create a handle for the given process id.
266 ACE_HANDLE process_handle =
267 ::OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, pid);
268 if (process_handle == ACE_INVALID_HANDLE || process_handle == 0)
269 return 0;
270 else
272 DWORD status;
273 int result = 1;
274 if (::GetExitCodeProcess (process_handle,
275 &status) == 0
276 || status != STILL_ACTIVE)
277 result = 0;
279 ::CloseHandle (process_handle);
280 return result;
282 #endif /* !ACE_WIN32 */
285 const ACE_TCHAR *
286 ACE::execname (const ACE_TCHAR *old_name)
288 #if defined (ACE_WIN32)
289 const ACE_TCHAR *suffix = ACE_OS::strrchr (old_name, ACE_TEXT ('.'));
290 if (suffix == 0 || ACE_OS::strcasecmp (suffix, ACE_TEXT (".exe")) != 0)
292 ACE_TCHAR *new_name = 0;
294 size_t size =
295 ACE_OS::strlen (old_name)
296 + ACE_OS::strlen (ACE_TEXT (".exe"))
297 + 1;
299 ACE_NEW_RETURN (new_name,
300 ACE_TCHAR[size],
302 ACE_TCHAR *end = new_name;
304 end = ACE_OS::strecpy (new_name, old_name);
306 // Concatenate the .exe suffix onto the end of the executable.
307 // end points _after_ the terminating nul.
308 ACE_OS::strcpy (end - 1, ACE_TEXT (".exe"));
310 return new_name;
312 #endif /* ACE_WIN32 */
313 return old_name;
316 u_long
317 ACE::hash_pjw (const char *str, size_t len)
319 u_long hash = 0;
321 for (size_t i = 0; i < len; i++)
323 const char temp = str[i];
324 hash = (hash << 4) + (temp * 13);
326 u_long g = hash & 0xf0000000;
328 if (g)
330 hash ^= (g >> 24);
331 hash ^= g;
335 return hash;
338 u_long
339 ACE::hash_pjw (const char *str)
341 return ACE::hash_pjw (str, ACE_OS::strlen (str));
344 #if defined (ACE_HAS_WCHAR)
345 u_long
346 ACE::hash_pjw (const wchar_t *str, size_t len)
348 u_long hash = 0;
350 for (size_t i = 0; i < len; i++)
352 // @@ UNICODE: Does this function do the correct thing with wchar's?
354 const wchar_t temp = str[i];
355 hash = (hash << 4) + (temp * 13);
357 u_long g = hash & 0xf0000000;
359 if (g)
361 hash ^= (g >> 24);
362 hash ^= g;
366 return hash;
369 u_long
370 ACE::hash_pjw (const wchar_t *str)
372 return ACE::hash_pjw (str, ACE_OS::strlen (str));
374 #endif /* ACE_HAS_WCHAR */
376 #if !defined (ACE_HAS_WINCE)
377 ACE_TCHAR *
378 ACE::strenvdup (const ACE_TCHAR *str)
380 ACE_TRACE ("ACE::strenvdup");
382 return ACE_OS::strenvdup (str);
384 #endif /* ACE_HAS_WINCE */
388 Examples:
390 Source NT UNIX
391 ==================================================================
392 netsvc netsvc.dll libnetsvc.so
393 (PATH will be (LD_LIBRARY_PATH
394 evaluated) evaluated)
396 libnetsvc.dll libnetsvc.dll libnetsvc.dll + warning
397 netsvc.so netsvc.so + warning libnetsvc.so
399 ..\../libs/netsvc ..\..\libs\netsvc.dll ../../libs/netsvc.so
400 (absolute path used) (absolute path used)
404 const ACE_TCHAR *
405 ACE::basename (const ACE_TCHAR *pathname, ACE_TCHAR delim)
407 ACE_TRACE ("ACE::basename");
408 const ACE_TCHAR *temp = ACE_OS::strrchr (pathname, delim);
410 if (temp == 0)
411 return pathname;
412 else
413 return temp + 1;
416 const ACE_TCHAR *
417 ACE::dirname (const ACE_TCHAR *pathname, ACE_TCHAR delim)
419 ACE_TRACE ("ACE::dirname");
420 static ACE_TCHAR return_dirname[MAXPATHLEN + 1];
422 const ACE_TCHAR *temp = ACE_OS::strrchr (pathname, delim);
424 if (temp == 0)
426 return_dirname[0] = '.';
427 return_dirname[1] = '\0';
429 return return_dirname;
431 else
433 // When the len is truncated, there are problems! This should
434 // not happen in normal circomstances
435 size_t len = temp - pathname + 1;
436 if (len > (sizeof return_dirname / sizeof (ACE_TCHAR)))
437 len = sizeof return_dirname / sizeof (ACE_TCHAR);
439 ACE_OS::strsncpy (return_dirname,
440 pathname,
441 len);
442 return return_dirname;
446 ssize_t
447 ACE::recv (ACE_HANDLE handle,
448 void *buf,
449 size_t len,
450 int flags,
451 const ACE_Time_Value *timeout)
453 if (timeout == 0)
454 return ACE_OS::recv (handle, (char *) buf, len, flags);
455 else
457 int val = 0;
458 if (ACE::enter_recv_timedwait (handle, timeout, val) ==-1)
459 return -1;
460 else
462 ssize_t bytes_transferred =
463 ACE_OS::recv (handle, (char *) buf, len, flags);
464 ACE::restore_non_blocking_mode (handle, val);
465 return bytes_transferred;
470 #if defined (ACE_HAS_TLI)
472 ssize_t
473 ACE::t_rcv (ACE_HANDLE handle,
474 void *buf,
475 size_t len,
476 int *flags,
477 const ACE_Time_Value *timeout)
479 if (timeout == 0)
480 return ACE_OS::t_rcv (handle, (char *) buf, len, flags);
481 else
483 int val = 0;
484 if (ACE::enter_recv_timedwait (handle, timeout, val) ==-1)
485 return -1;
486 else
488 ssize_t bytes_transferred =
489 ACE_OS::t_rcv (handle, (char *) buf, len, flags);
490 ACE::restore_non_blocking_mode (handle, val);
491 return bytes_transferred;
496 #endif /* ACE_HAS_TLI */
498 ssize_t
499 ACE::recv (ACE_HANDLE handle,
500 void *buf,
501 size_t n,
502 const ACE_Time_Value *timeout)
504 if (timeout == 0)
505 return ACE::recv_i (handle, buf, n);
506 else
508 int val = 0;
509 if (ACE::enter_recv_timedwait (handle, timeout, val) == -1)
510 return -1;
511 else
513 ssize_t bytes_transferred = ACE::recv_i (handle, buf, n);
514 ACE::restore_non_blocking_mode (handle, val);
515 return bytes_transferred;
520 ssize_t
521 ACE::recvmsg (ACE_HANDLE handle,
522 struct msghdr *msg,
523 int flags,
524 const ACE_Time_Value *timeout)
526 if (timeout == 0)
527 return ACE_OS::recvmsg (handle, msg, flags);
528 else
530 int val = 0;
531 if (ACE::enter_recv_timedwait (handle, timeout, val) == -1)
532 return -1;
533 else
535 ssize_t bytes_transferred = ACE_OS::recvmsg (handle, msg, flags);
536 ACE::restore_non_blocking_mode (handle, val);
537 return bytes_transferred;
542 ssize_t
543 ACE::recvfrom (ACE_HANDLE handle,
544 char *buf,
545 int len,
546 int flags,
547 struct sockaddr *addr,
548 int *addrlen,
549 const ACE_Time_Value *timeout)
551 if (timeout == 0)
552 return ACE_OS::recvfrom (handle, buf, len, flags, addr, addrlen);
553 else
555 int val = 0;
556 if (ACE::enter_recv_timedwait (handle, timeout, val) == -1)
557 return -1;
558 else
560 ssize_t bytes_transferred =
561 ACE_OS::recvfrom (handle, buf, len, flags, addr, addrlen);
562 ACE::restore_non_blocking_mode (handle, val);
563 return bytes_transferred;
568 ssize_t
569 ACE::recv_n_i (ACE_HANDLE handle,
570 void *buf,
571 size_t len,
572 int flags,
573 size_t *bt)
575 size_t temp;
576 size_t &bytes_transferred = bt == 0 ? temp : *bt;
577 ssize_t n;
579 for (bytes_transferred = 0;
580 bytes_transferred < len;
581 bytes_transferred += n)
583 // Try to transfer as much of the remaining data as possible.
584 n = ACE_OS::recv (handle,
585 static_cast <char *> (buf) + bytes_transferred,
586 len - bytes_transferred,
587 flags);
588 // Check EOF.
589 if (n == 0)
590 return 0;
592 // Check for other errors.
593 if (n == -1)
595 // Check for possible blocking.
596 if (errno == EWOULDBLOCK)
598 // Wait for the blocking to subside.
599 int const result = ACE::handle_read_ready (handle, 0);
601 // Did select() succeed?
602 if (result != -1)
604 // Blocking subsided. Continue data transfer.
605 n = 0;
606 continue;
610 // Other data transfer or select() failures.
611 return -1;
615 return static_cast<ssize_t> (bytes_transferred);
618 ssize_t
619 ACE::recv_n_i (ACE_HANDLE handle,
620 void *buf,
621 size_t len,
622 int flags,
623 const ACE_Time_Value *timeout,
624 size_t *bt)
626 size_t temp;
627 size_t &bytes_transferred = bt == 0 ? temp : *bt;
628 ssize_t n;
629 ssize_t result = 0;
630 int error = 0;
632 int val = 0;
633 ACE::record_and_set_non_blocking_mode (handle, val);
635 for (bytes_transferred = 0;
636 bytes_transferred < len;
637 bytes_transferred += n)
639 // Try to transfer as much of the remaining data as possible.
640 // Since the socket is in non-blocking mode, this call will not
641 // block.
642 n = ACE_OS::recv (handle,
643 static_cast <char *> (buf) + bytes_transferred,
644 len - bytes_transferred,
645 flags);
647 // Check for errors.
648 if (n == 0 ||
649 n == -1)
651 // Check for possible blocking.
652 if (n == -1 &&
653 errno == EWOULDBLOCK)
655 // Wait upto <timeout> for the blocking to subside.
656 int rtn = ACE::handle_read_ready (handle,
657 timeout);
659 // Did select() succeed?
660 if (rtn != -1)
662 // Blocking subsided in <timeout> period. Continue
663 // data transfer.
664 n = 0;
665 continue;
669 // Wait in select() timed out or other data transfer or
670 // select() failures.
671 error = 1;
672 result = n;
673 break;
677 ACE::restore_non_blocking_mode (handle, val);
679 if (error)
680 return result;
681 else
682 return static_cast<ssize_t> (bytes_transferred);
685 #if defined (ACE_HAS_TLI)
687 ssize_t
688 ACE::t_rcv_n_i (ACE_HANDLE handle,
689 void *buf,
690 size_t len,
691 int *flags,
692 size_t *bt)
694 size_t temp;
695 size_t &bytes_transferred = bt == 0 ? temp : *bt;
696 ssize_t n;
698 for (bytes_transferred = 0;
699 bytes_transferred < len;
700 bytes_transferred += n)
702 // Try to transfer as much of the remaining data as possible.
703 n = ACE_OS::t_rcv (handle,
704 (char *) buf + bytes_transferred,
705 len - bytes_transferred,
706 flags);
707 // Check EOF.
708 if (n == 0)
709 return 0;
711 // Check for other errors.
712 if (n == -1)
714 // Check for possible blocking.
715 if (errno == EWOULDBLOCK)
717 // Wait for the blocking to subside.
718 int result = ACE::handle_read_ready (handle,
721 // Did select() succeed?
722 if (result != -1)
724 // Blocking subsided. Continue data transfer.
725 n = 0;
726 continue;
730 // Other data transfer or select() failures.
731 return -1;
735 return bytes_transferred;
738 ssize_t
739 ACE::t_rcv_n_i (ACE_HANDLE handle,
740 void *buf,
741 size_t len,
742 int *flags,
743 const ACE_Time_Value *timeout,
744 size_t *bt)
746 size_t temp;
747 size_t &bytes_transferred = bt == 0 ? temp : *bt;
748 ssize_t n;
749 ssize_t result = 0;
750 int error = 0;
752 int val = 0;
753 ACE::record_and_set_non_blocking_mode (handle, val);
755 for (bytes_transferred = 0;
756 bytes_transferred < len;
757 bytes_transferred += n)
759 // Try to transfer as much of the remaining data as possible.
760 // Since the socket is in non-blocking mode, this call will not
761 // block.
762 n = ACE_OS::t_rcv (handle,
763 (char *) buf + bytes_transferred,
764 len - bytes_transferred,
765 flags);
767 // Check for errors.
768 if (n == 0 ||
769 n == -1)
771 // Check for possible blocking.
772 if (n == -1 &&
773 errno == EWOULDBLOCK)
775 // Wait upto <timeout> for the blocking to subside.
776 int rtn = ACE::handle_read_ready (handle,
777 timeout);
779 // Did select() succeed?
780 if (rtn != -1)
782 // Blocking subsided in <timeout> period. Continue
783 // data transfer.
784 n = 0;
785 continue;
789 // Wait in select() timed out or other data transfer or
790 // select() failures.
791 error = 1;
792 result = n;
793 break;
797 ACE::restore_non_blocking_mode (handle, val);
799 if (error)
800 return result;
801 else
802 return bytes_transferred;
805 #endif /* ACE_HAS_TLI */
807 ssize_t
808 ACE::recv_n_i (ACE_HANDLE handle,
809 void *buf,
810 size_t len,
811 size_t *bt)
813 size_t temp;
814 size_t &bytes_transferred = bt == 0 ? temp : *bt;
815 ssize_t n;
817 for (bytes_transferred = 0;
818 bytes_transferred < len;
819 bytes_transferred += n)
821 // Try to transfer as much of the remaining data as possible.
822 n = ACE::recv_i (handle,
823 static_cast <char *> (buf) + bytes_transferred,
824 len - bytes_transferred);
825 // Check EOF.
826 if (n == 0)
828 return 0;
830 // Check for other errors.
831 if (n == -1)
833 // Check for possible blocking.
834 if (errno == EWOULDBLOCK)
836 // Wait for the blocking to subside.
837 int result = ACE::handle_read_ready (handle,
840 // Did select() succeed?
841 if (result != -1)
843 // Blocking subsided. Continue data transfer.
844 n = 0;
845 continue;
849 // Other data transfer or select() failures.
850 return -1;
854 return static_cast<ssize_t> (bytes_transferred);
857 ssize_t
858 ACE::recv_n_i (ACE_HANDLE handle,
859 void *buf,
860 size_t len,
861 const ACE_Time_Value *timeout,
862 size_t *bt)
864 size_t temp;
865 size_t &bytes_transferred = bt == 0 ? temp : *bt;
866 ssize_t n;
867 ssize_t result = 0;
868 int error = 0;
870 int val = 0;
871 ACE::record_and_set_non_blocking_mode (handle, val);
873 for (bytes_transferred = 0;
874 bytes_transferred < len;
875 bytes_transferred += n)
877 // Try to transfer as much of the remaining data as possible.
878 // Since the socket is in non-blocking mode, this call will not
879 // block.
880 n = ACE::recv_i (handle,
881 static_cast <char *> (buf) + bytes_transferred,
882 len - bytes_transferred);
884 // Check for errors.
885 if (n == 0 ||
886 n == -1)
888 // Check for possible blocking.
889 if (n == -1 &&
890 errno == EWOULDBLOCK)
892 // Wait upto <timeout> for the blocking to subside.
893 int rtn = ACE::handle_read_ready (handle,
894 timeout);
896 // Did select() succeed?
897 if (rtn != -1)
899 // Blocking subsided in <timeout> period. Continue
900 // data transfer.
901 n = 0;
902 continue;
906 // Wait in select() timed out or other data transfer or
907 // select() failures.
908 error = 1;
909 result = n;
910 break;
914 ACE::restore_non_blocking_mode (handle, val);
916 if (error)
917 return result;
918 else
919 return static_cast<ssize_t> (bytes_transferred);
922 // This is basically an interface to ACE_OS::readv, that doesn't use
923 // the struct iovec explicitly. The ... can be passed as an arbitrary
924 // number of (char *ptr, int len) tuples. However, the count N is the
925 // *total* number of trailing arguments, *not* a couple of the number
926 // of tuple pairs!
928 ssize_t
929 ACE::recv (ACE_HANDLE handle, size_t n, ...)
931 va_list argp;
932 int total_tuples = static_cast<int> (n / 2);
933 iovec *iovp;
934 #if defined (ACE_HAS_ALLOCA)
935 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
936 #else
937 ACE_NEW_RETURN (iovp,
938 iovec[total_tuples],
939 -1);
940 #endif /* !defined (ACE_HAS_ALLOCA) */
942 va_start (argp, n);
944 for (int i = 0; i < total_tuples; i++)
946 iovp[i].iov_base = va_arg (argp, char *);
947 iovp[i].iov_len = va_arg (argp, int);
950 ssize_t result = ACE_OS::recvv (handle, iovp, total_tuples);
951 #if !defined (ACE_HAS_ALLOCA)
952 delete [] iovp;
953 #endif /* !defined (ACE_HAS_ALLOCA) */
954 va_end (argp);
955 return result;
958 ssize_t
959 ACE::recvv (ACE_HANDLE handle,
960 iovec *iov,
961 int iovcnt,
962 const ACE_Time_Value *timeout)
964 if (timeout == 0)
965 return ACE_OS::recvv (handle, iov, iovcnt);
966 else
968 int val = 0;
969 if (ACE::enter_recv_timedwait (handle, timeout, val) == -1)
970 return -1;
971 else
973 ssize_t bytes_transferred = ACE_OS::recvv (handle, iov, iovcnt);
974 ACE::restore_non_blocking_mode (handle, val);
975 return bytes_transferred;
980 ssize_t
981 ACE::recvv_n_i (ACE_HANDLE handle,
982 iovec *iov,
983 int iovcnt,
984 size_t *bt)
986 size_t temp;
987 size_t &bytes_transferred = bt == 0 ? temp : *bt;
988 bytes_transferred = 0;
990 for (int s = 0;
991 s < iovcnt;
994 // Try to transfer as much of the remaining data as possible.
995 ssize_t n = ACE_OS::recvv (handle,
996 iov + s,
997 iovcnt - s);
998 // Check EOF.
999 if (n == 0)
1000 return 0;
1002 // Check for other errors.
1003 if (n == -1)
1005 // Check for possible blocking.
1006 if (errno == EWOULDBLOCK)
1008 // Wait for the blocking to subside.
1009 int result = ACE::handle_read_ready (handle,
1012 // Did select() succeed?
1013 if (result != -1)
1015 // Blocking subsided. Continue data transfer.
1016 n = 0;
1017 continue;
1021 // Other data transfer or select() failures.
1022 return -1;
1025 for (bytes_transferred += n;
1026 s < iovcnt
1027 && n >= static_cast<ssize_t> (iov[s].iov_len);
1028 s++)
1029 n -= iov[s].iov_len;
1031 if (n != 0)
1033 char *base = static_cast<char *> (iov[s].iov_base);
1034 iov[s].iov_base = base + n;
1035 iov[s].iov_len = iov[s].iov_len - n;
1039 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1042 ssize_t
1043 ACE::recvv_n_i (ACE_HANDLE handle,
1044 iovec *iov,
1045 int iovcnt,
1046 const ACE_Time_Value *timeout,
1047 size_t *bt)
1049 size_t temp;
1050 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1051 bytes_transferred = 0;
1052 ssize_t result = 0;
1053 int error = 0;
1055 int val = 0;
1056 ACE::record_and_set_non_blocking_mode (handle, val);
1058 for (int s = 0;
1059 s < iovcnt;
1062 // Try to transfer as much of the remaining data as possible.
1063 // Since the socket is in non-blocking mode, this call will not
1064 // block.
1065 ssize_t n = ACE_OS::recvv (handle,
1066 iov + s,
1067 iovcnt - s);
1069 // Check for errors.
1070 if (n == 0 ||
1071 n == -1)
1073 // Check for possible blocking.
1074 if (n == -1 &&
1075 errno == EWOULDBLOCK)
1077 // Wait upto <timeout> for the blocking to subside.
1078 int rtn = ACE::handle_read_ready (handle,
1079 timeout);
1081 // Did select() succeed?
1082 if (rtn != -1)
1084 // Blocking subsided in <timeout> period. Continue
1085 // data transfer.
1086 n = 0;
1087 continue;
1091 // Wait in select() timed out or other data transfer or
1092 // select() failures.
1093 error = 1;
1094 result = n;
1095 break;
1098 for (bytes_transferred += n;
1099 s < iovcnt
1100 && n >= static_cast<ssize_t> (iov[s].iov_len);
1101 s++)
1102 n -= iov[s].iov_len;
1104 if (n != 0)
1106 char *base = reinterpret_cast<char *> (iov[s].iov_base);
1107 iov[s].iov_base = base + n;
1108 iov[s].iov_len = iov[s].iov_len - n;
1112 ACE::restore_non_blocking_mode (handle, val);
1114 if (error)
1116 return result;
1118 else
1120 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1124 ssize_t
1125 ACE::recv_n (ACE_HANDLE handle,
1126 ACE_Message_Block *message_block,
1127 const ACE_Time_Value *timeout,
1128 size_t *bt)
1130 size_t temp;
1131 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1132 bytes_transferred = 0;
1134 iovec iov[ACE_IOV_MAX];
1135 int iovcnt = 0;
1137 while (message_block != 0)
1139 // Our current message block chain.
1140 const ACE_Message_Block *current_message_block = message_block;
1142 while (current_message_block != 0)
1144 size_t current_message_block_length =
1145 current_message_block->length ();
1146 char *this_rd_ptr = current_message_block->rd_ptr ();
1148 // Check if this block has any space for incoming data.
1149 while (current_message_block_length > 0)
1151 u_long const this_chunk_length =
1152 ACE_Utils::truncate_cast<u_long> (
1153 current_message_block_length);
1155 // Collect the data in the iovec.
1156 iov[iovcnt].iov_base = this_rd_ptr;
1157 iov[iovcnt].iov_len = this_chunk_length;
1158 current_message_block_length -= this_chunk_length;
1159 this_rd_ptr += this_chunk_length;
1161 // Increment iovec counter.
1162 ++iovcnt;
1164 // The buffer is full make a OS call. @@ TODO find a way to
1165 // find ACE_IOV_MAX for platforms that do not define it rather
1166 // than simply setting ACE_IOV_MAX to some arbitrary value such
1167 // as 16.
1168 if (iovcnt == ACE_IOV_MAX)
1170 size_t current_transfer = 0;
1172 ssize_t const result = ACE::recvv_n (handle,
1173 iov,
1174 iovcnt,
1175 timeout,
1176 &current_transfer);
1178 // Add to total bytes transferred.
1179 bytes_transferred += current_transfer;
1181 // Errors.
1182 if (result == -1 || result == 0)
1183 return result;
1185 // Reset iovec counter.
1186 iovcnt = 0;
1190 // Select the next message block in the chain.
1191 current_message_block = current_message_block->cont ();
1194 // Selection of the next message block chain.
1195 message_block = message_block->next ();
1198 // Check for remaining buffers to be sent. This will happen when
1199 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1200 if (iovcnt != 0)
1202 size_t current_transfer = 0;
1204 ssize_t const result = ACE::recvv_n (handle,
1205 iov,
1206 iovcnt,
1207 timeout,
1208 &current_transfer);
1210 // Add to total bytes transferred.
1211 bytes_transferred += current_transfer;
1213 // Errors.
1214 if (result == -1 || result == 0)
1216 return result;
1220 // Return total bytes transferred.
1221 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1224 ssize_t
1225 ACE::send (ACE_HANDLE handle,
1226 const void *buf,
1227 size_t n,
1228 int flags,
1229 const ACE_Time_Value *timeout)
1231 if (timeout == 0)
1232 return ACE_OS::send (handle, (const char *) buf, n, flags);
1233 else
1235 int val = 0;
1236 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1237 return -1;
1238 else
1240 ssize_t bytes_transferred = ACE_OS::send (handle, (const char *) buf, n, flags);
1241 ACE::restore_non_blocking_mode (handle, val);
1242 return bytes_transferred;
1247 #if defined (ACE_HAS_TLI)
1249 ssize_t
1250 ACE::t_snd (ACE_HANDLE handle,
1251 const void *buf,
1252 size_t n,
1253 int flags,
1254 const ACE_Time_Value *timeout)
1256 if (timeout == 0)
1257 return ACE_OS::t_snd (handle, (const char *) buf, n, flags);
1258 else
1260 int val = 0;
1261 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1262 return -1;
1263 else
1265 ssize_t bytes_transferred = ACE_OS::t_snd (handle, (const char *) buf, n, flags);
1266 ACE::restore_non_blocking_mode (handle, val);
1267 return bytes_transferred;
1272 #endif /* ACE_HAS_TLI */
1274 ssize_t
1275 ACE::send (ACE_HANDLE handle,
1276 const void *buf,
1277 size_t n,
1278 const ACE_Time_Value *timeout)
1280 if (timeout == 0)
1281 return ACE::send_i (handle, buf, n);
1282 else
1284 int val = 0;
1285 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1286 return -1;
1287 else
1289 ssize_t bytes_transferred = ACE::send_i (handle, buf, n);
1290 ACE::restore_non_blocking_mode (handle, val);
1291 return bytes_transferred;
1296 ssize_t
1297 ACE::sendmsg (ACE_HANDLE handle,
1298 const struct msghdr *msg,
1299 int flags,
1300 const ACE_Time_Value *timeout)
1302 if (timeout == 0)
1303 return ACE_OS::sendmsg (handle, msg, flags);
1304 else
1306 int val = 0;
1307 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1308 return -1;
1309 else
1311 ssize_t bytes_transferred = ACE_OS::sendmsg (handle, msg, flags);
1312 ACE::restore_non_blocking_mode (handle, val);
1313 return bytes_transferred;
1318 ssize_t
1319 ACE::sendto (ACE_HANDLE handle,
1320 const char *buf,
1321 int len,
1322 int flags,
1323 const struct sockaddr *addr,
1324 int addrlen,
1325 const ACE_Time_Value *timeout)
1327 if (timeout == 0)
1328 return ACE_OS::sendto (handle, buf, len, flags, addr, addrlen);
1329 else
1331 int val = 0;
1332 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1333 return -1;
1334 else
1336 ssize_t bytes_transferred =
1337 ACE_OS::sendto (handle, buf, len, flags, addr, addrlen);
1338 ACE::restore_non_blocking_mode (handle, val);
1339 return bytes_transferred;
1344 ssize_t
1345 ACE::send_n_i (ACE_HANDLE handle,
1346 const void *buf,
1347 size_t len,
1348 int flags,
1349 size_t *bt)
1351 size_t temp;
1352 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1353 ssize_t n;
1355 for (bytes_transferred = 0;
1356 bytes_transferred < len;
1357 bytes_transferred += n)
1359 // Try to transfer as much of the remaining data as possible.
1360 n = ACE_OS::send (handle,
1361 (char *) buf + bytes_transferred,
1362 len - bytes_transferred,
1363 flags);
1364 // Check EOF.
1365 if (n == 0)
1366 return 0;
1368 // Check for other errors.
1369 if (n == -1)
1371 // Check for possible blocking.
1372 #if defined (ACE_WIN32)
1373 if (errno == EWOULDBLOCK) // If enobufs no need to loop
1374 #else
1375 if (errno == EWOULDBLOCK || errno == ENOBUFS)
1376 #endif /* ACE_WIN32 */
1378 // Wait for the blocking to subside.
1379 int result = ACE::handle_write_ready (handle,
1382 // Did select() succeed?
1383 if (result != -1)
1385 // Blocking subsided. Continue data transfer.
1386 n = 0;
1387 continue;
1391 // Other data transfer or select() failures.
1392 return -1;
1396 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1399 ssize_t
1400 ACE::send_n_i (ACE_HANDLE handle,
1401 const void *buf,
1402 size_t len,
1403 int flags,
1404 const ACE_Time_Value *timeout,
1405 size_t *bt)
1407 size_t temp;
1408 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1409 ssize_t n;
1410 ssize_t result = 0;
1411 int error = 0;
1413 int val = 0;
1414 ACE::record_and_set_non_blocking_mode (handle, val);
1416 for (bytes_transferred = 0;
1417 bytes_transferred < len;
1418 bytes_transferred += n)
1420 // Try to transfer as much of the remaining data as possible.
1421 // Since the socket is in non-blocking mode, this call will not
1422 // block.
1423 n = ACE_OS::send (handle,
1424 (char *) buf + bytes_transferred,
1425 len - bytes_transferred,
1426 flags);
1428 // Check for errors.
1429 if (n == 0 ||
1430 n == -1)
1432 // Check for possible blocking.
1433 if (n == -1 &&
1434 (errno == EWOULDBLOCK || errno == ENOBUFS))
1436 // Wait upto <timeout> for the blocking to subside.
1437 int rtn = ACE::handle_write_ready (handle,
1438 timeout);
1440 // Did select() succeed?
1441 if (rtn != -1)
1443 // Blocking subsided in <timeout> period. Continue
1444 // data transfer.
1445 n = 0;
1446 continue;
1450 // Wait in select() timed out or other data transfer or
1451 // select() failures.
1452 error = 1;
1453 result = n;
1454 break;
1458 ACE::restore_non_blocking_mode (handle, val);
1460 if (error)
1462 return result;
1464 else
1466 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1470 #if defined (ACE_HAS_TLI)
1472 ssize_t
1473 ACE::t_snd_n_i (ACE_HANDLE handle,
1474 const void *buf,
1475 size_t len,
1476 int flags,
1477 size_t *bt)
1479 size_t temp;
1480 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1481 ssize_t n;
1483 for (bytes_transferred = 0;
1484 bytes_transferred < len;
1485 bytes_transferred += n)
1487 // Try to transfer as much of the remaining data as possible.
1488 n = ACE_OS::t_snd (handle,
1489 (char *) buf + bytes_transferred,
1490 len - bytes_transferred,
1491 flags);
1492 // Check EOF.
1493 if (n == 0)
1494 return 0;
1496 // Check for other errors.
1497 if (n == -1)
1499 // Check for possible blocking.
1500 if (errno == EWOULDBLOCK || errno == ENOBUFS)
1502 // Wait for the blocking to subside.
1503 int result = ACE::handle_write_ready (handle,
1506 // Did select() succeed?
1507 if (result != -1)
1509 // Blocking subsided. Continue data transfer.
1510 n = 0;
1511 continue;
1515 // Other data transfer or select() failures.
1516 return -1;
1520 return bytes_transferred;
1523 ssize_t
1524 ACE::t_snd_n_i (ACE_HANDLE handle,
1525 const void *buf,
1526 size_t len,
1527 int flags,
1528 const ACE_Time_Value *timeout,
1529 size_t *bt)
1531 size_t temp;
1532 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1533 ssize_t n;
1534 ssize_t result = 0;
1535 int error = 0;
1537 int val = 0;
1538 ACE::record_and_set_non_blocking_mode (handle, val);
1540 for (bytes_transferred = 0;
1541 bytes_transferred < len;
1542 bytes_transferred += n)
1544 // Try to transfer as much of the remaining data as possible.
1545 // Since the socket is in non-blocking mode, this call will not
1546 // block.
1547 n = ACE_OS::t_snd (handle,
1548 (char *) buf + bytes_transferred,
1549 len - bytes_transferred,
1550 flags);
1552 // Check for errors.
1553 if (n == 0 ||
1554 n == -1)
1556 // Check for possible blocking.
1557 if (n == -1 &&
1558 errno == EWOULDBLOCK || errno == ENOBUFS)
1560 // Wait upto <timeout> for the blocking to subside.
1561 int rtn = ACE::handle_write_ready (handle,
1562 timeout);
1564 // Did select() succeed?
1565 if (rtn != -1)
1567 // Blocking subsided in <timeout> period. Continue
1568 // data transfer.
1569 n = 0;
1570 continue;
1574 // Wait in select() timed out or other data transfer or
1575 // select() failures.
1576 error = 1;
1577 result = n;
1578 break;
1582 ACE::restore_non_blocking_mode (handle, val);
1584 if (error)
1585 return result;
1586 else
1587 return bytes_transferred;
1590 #endif /* ACE_HAS_TLI */
1592 ssize_t
1593 ACE::send_n_i (ACE_HANDLE handle,
1594 const void *buf,
1595 size_t len,
1596 size_t *bt)
1598 size_t temp;
1599 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1600 ssize_t n;
1602 for (bytes_transferred = 0;
1603 bytes_transferred < len;
1604 bytes_transferred += n)
1606 // Try to transfer as much of the remaining data as possible.
1607 n = ACE::send_i (handle,
1608 (char *) buf + bytes_transferred,
1609 len - bytes_transferred);
1610 // Check EOF.
1611 if (n == 0)
1613 return 0;
1616 // Check for other errors.
1617 if (n == -1)
1619 // Check for possible blocking.
1620 if (errno == EWOULDBLOCK || errno == ENOBUFS)
1622 // Wait for the blocking to subside.
1623 int result = ACE::handle_write_ready (handle,
1626 // Did select() succeed?
1627 if (result != -1)
1629 // Blocking subsided. Continue data transfer.
1630 n = 0;
1631 continue;
1635 // Other data transfer or select() failures.
1636 return -1;
1640 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1643 ssize_t
1644 ACE::send_n_i (ACE_HANDLE handle,
1645 const void *buf,
1646 size_t len,
1647 const ACE_Time_Value *timeout,
1648 size_t *bt)
1650 size_t temp;
1651 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1652 ssize_t n;
1653 ssize_t result = 0;
1654 int error = 0;
1656 int val = 0;
1657 ACE::record_and_set_non_blocking_mode (handle, val);
1659 for (bytes_transferred = 0;
1660 bytes_transferred < len;
1661 bytes_transferred += n)
1663 // Try to transfer as much of the remaining data as possible.
1664 // Since the socket is in non-blocking mode, this call will not
1665 // block.
1666 n = ACE::send_i (handle,
1667 (char *) buf + bytes_transferred,
1668 len - bytes_transferred);
1670 // Check for errors.
1671 if (n == 0 ||
1672 n == -1)
1674 // Check for possible blocking.
1675 if (n == -1 &&
1676 (errno == EWOULDBLOCK || errno == ENOBUFS))
1678 // Wait upto <timeout> for the blocking to subside.
1679 int rtn = ACE::handle_write_ready (handle,
1680 timeout);
1682 // Did select() succeed?
1683 if (rtn != -1)
1685 // Blocking subsided in <timeout> period. Continue
1686 // data transfer.
1687 n = 0;
1688 continue;
1692 // Wait in select() timed out or other data transfer or
1693 // select() failures.
1694 error = 1;
1695 result = n;
1696 break;
1700 ACE::restore_non_blocking_mode (handle, val);
1702 if (error)
1704 return result;
1706 else
1708 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1712 // Send N char *ptrs and int lengths. Note that the char *'s precede
1713 // the ints (basically, an varargs version of writev). The count N is
1714 // the *total* number of trailing arguments, *not* a couple of the
1715 // number of tuple pairs!
1717 ssize_t
1718 ACE::send (ACE_HANDLE handle, size_t n, ...)
1720 va_list argp;
1721 int total_tuples = static_cast<int> (n / 2);
1722 iovec *iovp;
1723 #if defined (ACE_HAS_ALLOCA)
1724 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
1725 #else
1726 ACE_NEW_RETURN (iovp,
1727 iovec[total_tuples],
1728 -1);
1729 #endif /* !defined (ACE_HAS_ALLOCA) */
1731 va_start (argp, n);
1733 for (int i = 0; i < total_tuples; i++)
1735 iovp[i].iov_base = va_arg (argp, char *);
1736 iovp[i].iov_len = va_arg (argp, int);
1739 ssize_t result = ACE_OS::sendv (handle, iovp, total_tuples);
1740 #if !defined (ACE_HAS_ALLOCA)
1741 delete [] iovp;
1742 #endif /* !defined (ACE_HAS_ALLOCA) */
1743 va_end (argp);
1744 return result;
1747 ssize_t
1748 ACE::sendv (ACE_HANDLE handle,
1749 const iovec *iov,
1750 int iovcnt,
1751 const ACE_Time_Value *timeout)
1753 if (timeout == 0)
1754 return ACE_OS::sendv (handle, iov, iovcnt);
1755 else
1757 int val = 0;
1758 if (ACE::enter_send_timedwait (handle, timeout, val) == -1)
1759 return -1;
1760 else
1762 ssize_t bytes_transferred = ACE_OS::sendv (handle, iov, iovcnt);
1763 ACE::restore_non_blocking_mode (handle, val);
1764 return bytes_transferred;
1769 ssize_t
1770 ACE::sendv_n_i (ACE_HANDLE handle,
1771 const iovec *i,
1772 int iovcnt,
1773 size_t *bt)
1775 size_t temp;
1776 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1777 bytes_transferred = 0;
1779 iovec *iov = const_cast<iovec *> (i);
1781 for (int s = 0;
1782 s < iovcnt;
1785 // Try to transfer as much of the remaining data as possible.
1786 ssize_t n = ACE_OS::sendv (handle,
1787 iov + s,
1788 iovcnt - s);
1789 // Check EOF.
1790 if (n == 0)
1791 return 0;
1793 // Check for other errors.
1794 if (n == -1)
1796 // Check for possible blocking.
1797 if (errno == EWOULDBLOCK || errno == ENOBUFS)
1799 // Wait for the blocking to subside.
1800 int result = ACE::handle_write_ready (handle,
1803 // Did select() succeed?
1804 if (result != -1)
1806 // Blocking subsided. Continue data transfer.
1807 n = 0;
1808 continue;
1812 // Other data transfer or select() failures.
1813 return -1;
1816 for (bytes_transferred += n;
1817 s < iovcnt
1818 && n >= static_cast<ssize_t> (iov[s].iov_len);
1819 s++)
1820 n -= iov[s].iov_len;
1822 if (n != 0)
1824 char *base = reinterpret_cast<char *> (iov[s].iov_base);
1825 iov[s].iov_base = base + n;
1826 iov[s].iov_len = iov[s].iov_len - n;
1830 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1833 ssize_t
1834 ACE::sendv_n_i (ACE_HANDLE handle,
1835 const iovec *i,
1836 int iovcnt,
1837 const ACE_Time_Value *timeout,
1838 size_t *bt)
1840 size_t temp;
1841 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1842 bytes_transferred = 0;
1843 ssize_t result = 0;
1844 int error = 0;
1846 int val = 0;
1847 ACE::record_and_set_non_blocking_mode (handle, val);
1849 iovec *iov = const_cast<iovec *> (i);
1851 for (int s = 0;
1852 s < iovcnt;
1855 // Try to transfer as much of the remaining data as possible.
1856 // Since the socket is in non-blocking mode, this call will not
1857 // block.
1858 ssize_t n = ACE_OS::sendv (handle,
1859 iov + s,
1860 iovcnt - s);
1862 // Check for errors.
1863 if (n == 0 ||
1864 n == -1)
1866 // Check for possible blocking.
1867 if (n == -1 &&
1868 (errno == EWOULDBLOCK || errno == ENOBUFS))
1870 // Wait upto <timeout> for the blocking to subside.
1871 int rtn = ACE::handle_write_ready (handle,
1872 timeout);
1874 // Did select() succeed?
1875 if (rtn != -1)
1877 // Blocking subsided in <timeout> period. Continue
1878 // data transfer.
1879 n = 0;
1880 continue;
1884 // Wait in select() timed out or other data transfer or
1885 // select() failures.
1886 error = 1;
1887 result = n;
1888 break;
1891 for (bytes_transferred += n;
1892 s < iovcnt
1893 && n >= static_cast<ssize_t> (iov[s].iov_len);
1894 s++)
1895 n -= iov[s].iov_len;
1897 if (n != 0)
1899 char *base = reinterpret_cast<char *> (iov[s].iov_base);
1900 iov[s].iov_base = base + n;
1901 iov[s].iov_len = iov[s].iov_len - n;
1905 ACE::restore_non_blocking_mode (handle, val);
1907 if (error)
1909 return result;
1911 else
1913 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
1917 ssize_t
1918 ACE::write_n (ACE_HANDLE handle,
1919 const ACE_Message_Block *message_block,
1920 size_t *bt)
1922 size_t temp;
1923 size_t &bytes_transferred = bt == 0 ? temp : *bt;
1924 bytes_transferred = 0;
1926 iovec iov[ACE_IOV_MAX];
1927 int iovcnt = 0;
1929 while (message_block != 0)
1931 // Our current message block chain.
1932 const ACE_Message_Block *current_message_block = message_block;
1934 while (current_message_block != 0)
1936 size_t current_message_block_length =
1937 current_message_block->length ();
1938 char *this_block_ptr = current_message_block->rd_ptr ();
1940 // Check if this block has any data to be sent.
1941 while (current_message_block_length > 0)
1943 u_long const this_chunk_length =
1944 ACE_Utils::truncate_cast<u_long> (
1945 current_message_block_length);
1947 // Collect the data in the iovec.
1948 iov[iovcnt].iov_base = this_block_ptr;
1949 iov[iovcnt].iov_len = this_chunk_length;
1950 current_message_block_length -= this_chunk_length;
1951 this_block_ptr += this_chunk_length;
1953 // Increment iovec counter.
1954 ++iovcnt;
1956 // The buffer is full make a OS call. @@ TODO find a way to
1957 // find ACE_IOV_MAX for platforms that do not define it rather
1958 // than simply setting ACE_IOV_MAX to some arbitrary value such
1959 // as 16.
1960 if (iovcnt == ACE_IOV_MAX)
1962 size_t current_transfer = 0;
1964 ssize_t const result = ACE::writev_n (handle,
1965 iov,
1966 iovcnt,
1967 &current_transfer);
1969 // Add to total bytes transferred.
1970 bytes_transferred += current_transfer;
1972 // Errors.
1973 if (result == -1 || result == 0)
1974 return result;
1976 // Reset iovec counter.
1977 iovcnt = 0;
1981 // Select the next message block in the chain.
1982 current_message_block = current_message_block->cont ();
1985 // Selection of the next message block chain.
1986 message_block = message_block->next ();
1989 // Check for remaining buffers to be sent. This will happen when
1990 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1991 if (iovcnt != 0)
1993 size_t current_transfer = 0;
1995 ssize_t const result = ACE::writev_n (handle,
1996 iov,
1997 iovcnt,
1998 &current_transfer);
2000 // Add to total bytes transferred.
2001 bytes_transferred += current_transfer;
2003 // Errors.
2004 if (result == -1 || result == 0)
2005 return result;
2008 // Return total bytes transferred.
2009 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
2012 ssize_t
2013 ACE::send_n (ACE_HANDLE handle,
2014 const ACE_Message_Block *message_block,
2015 const ACE_Time_Value *timeout,
2016 size_t *bt)
2018 size_t temp;
2019 size_t &bytes_transferred = bt == 0 ? temp : *bt;
2020 bytes_transferred = 0;
2022 iovec iov[ACE_IOV_MAX];
2023 int iovcnt = 0;
2025 while (message_block != 0)
2027 // Our current message block chain.
2028 const ACE_Message_Block *current_message_block = message_block;
2030 while (current_message_block != 0)
2032 char *this_block_ptr = current_message_block->rd_ptr ();
2033 size_t current_message_block_length =
2034 current_message_block->length ();
2036 // Check if this block has any data to be sent.
2037 while (current_message_block_length > 0)
2039 u_long const this_chunk_length =
2040 ACE_Utils::truncate_cast<u_long> (
2041 current_message_block_length);
2043 // Collect the data in the iovec.
2044 iov[iovcnt].iov_base = this_block_ptr;
2045 iov[iovcnt].iov_len = this_chunk_length;
2046 current_message_block_length -= this_chunk_length;
2047 this_block_ptr += this_chunk_length;
2049 // Increment iovec counter.
2050 ++iovcnt;
2052 // The buffer is full make a OS call. @@ TODO find a way to
2053 // find ACE_IOV_MAX for platforms that do not define it rather
2054 // than simply setting ACE_IOV_MAX to some arbitrary value such
2055 // as 16.
2056 if (iovcnt == ACE_IOV_MAX)
2058 size_t current_transfer = 0;
2060 ssize_t const result = ACE::sendv_n (handle,
2061 iov,
2062 iovcnt,
2063 timeout,
2064 &current_transfer);
2066 // Add to total bytes transferred.
2067 bytes_transferred += current_transfer;
2069 // Errors.
2070 if (result == -1 || result == 0)
2071 return result;
2073 // Reset iovec counter.
2074 iovcnt = 0;
2078 // Select the next message block in the chain.
2079 current_message_block = current_message_block->cont ();
2082 // Selection of the next message block chain.
2083 message_block = message_block->next ();
2086 // Check for remaining buffers to be sent. This will happen when
2087 // ACE_IOV_MAX is not a multiple of the number of message blocks.
2088 if (iovcnt != 0)
2090 size_t current_transfer = 0;
2092 ssize_t const result = ACE::sendv_n (handle,
2093 iov,
2094 iovcnt,
2095 timeout,
2096 &current_transfer);
2098 // Add to total bytes transferred.
2099 bytes_transferred += current_transfer;
2101 // Errors.
2102 if (result == -1 || result == 0)
2104 return result;
2108 // Return total bytes transferred.
2109 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
2112 ssize_t
2113 ACE::readv_n (ACE_HANDLE handle,
2114 iovec *iov,
2115 int iovcnt,
2116 size_t *bt)
2118 size_t temp;
2119 size_t &bytes_transferred = bt == 0 ? temp : *bt;
2120 bytes_transferred = 0;
2122 for (int s = 0;
2123 s < iovcnt;
2126 ssize_t n = ACE_OS::readv (handle,
2127 iov + s,
2128 iovcnt - s);
2130 if (n == -1 || n == 0)
2131 return n;
2133 for (bytes_transferred += n;
2134 s < iovcnt
2135 && n >= static_cast<ssize_t> (iov[s].iov_len);
2136 s++)
2137 n -= iov[s].iov_len;
2139 if (n != 0)
2141 char *base = reinterpret_cast<char *> (iov[s].iov_base);
2142 iov[s].iov_base = base + n;
2143 iov[s].iov_len = iov[s].iov_len - n;
2147 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
2150 ssize_t
2151 ACE::writev_n (ACE_HANDLE handle,
2152 const iovec *i,
2153 int iovcnt,
2154 size_t *bt)
2156 size_t temp;
2157 size_t &bytes_transferred = bt == 0 ? temp : *bt;
2158 bytes_transferred = 0;
2160 iovec *iov = const_cast<iovec *> (i);
2162 for (int s = 0;
2163 s < iovcnt;
2166 ssize_t n = ACE_OS::writev (handle,
2167 iov + s,
2168 iovcnt - s);
2170 if (n == -1 || n == 0)
2172 return n;
2175 for (bytes_transferred += n;
2176 s < iovcnt
2177 && n >= static_cast<ssize_t> (iov[s].iov_len);
2178 s++)
2179 n -= iov[s].iov_len;
2181 if (n != 0)
2183 char *base = reinterpret_cast<char *> (iov[s].iov_base);
2184 iov[s].iov_base = base + n;
2185 iov[s].iov_len = iov[s].iov_len - n;
2189 return ACE_Utils::truncate_cast<ssize_t> (bytes_transferred);
2193 ACE::handle_ready (ACE_HANDLE handle,
2194 const ACE_Time_Value *timeout,
2195 int read_ready,
2196 int write_ready,
2197 int exception_ready)
2199 #if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2200 ACE_UNUSED_ARG (write_ready);
2201 ACE_UNUSED_ARG (exception_ready);
2203 struct pollfd fds;
2205 fds.fd = handle;
2206 fds.events = read_ready ? POLLIN : POLLOUT;
2207 fds.revents = 0;
2209 int result = ACE_OS::poll (&fds, 1, timeout);
2210 #else
2211 ACE_Handle_Set handle_set;
2212 handle_set.set_bit (handle);
2214 // Wait for data or for the timeout to elapse.
2215 int select_width;
2216 # if defined (ACE_WIN32)
2217 // This arg is ignored on Windows and causes pointer truncation
2218 // warnings on 64-bit compiles.
2219 select_width = 0;
2220 # else
2221 select_width = int (handle) + 1;
2222 # endif /* ACE_WIN64 */
2223 int result = ACE_OS::select (select_width,
2224 read_ready ? handle_set.fdset () : 0, // read_fds.
2225 write_ready ? handle_set.fdset () : 0, // write_fds.
2226 exception_ready ? handle_set.fdset () : 0, // exception_fds.
2227 timeout);
2229 #endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2231 switch (result)
2233 case 0: // Timer expired.
2234 errno = ETIME;
2235 /* FALLTHRU */
2236 case -1: // we got here directly - select() returned -1.
2237 return -1;
2238 case 1: // Handle has data.
2239 /* FALLTHRU */
2240 default: // default is case result > 0; return a
2241 // ACE_ASSERT (result == 1);
2242 return result;
2247 ACE::enter_recv_timedwait (ACE_HANDLE handle,
2248 const ACE_Time_Value *timeout,
2249 int &val)
2251 int result = ACE::handle_read_ready (handle,
2252 timeout);
2254 if (result == -1)
2255 return -1;
2257 ACE::record_and_set_non_blocking_mode (handle,
2258 val);
2260 return result;
2264 ACE::enter_send_timedwait (ACE_HANDLE handle,
2265 const ACE_Time_Value *timeout,
2266 int &val)
2268 int result = ACE::handle_write_ready (handle,
2269 timeout);
2271 if (result == -1)
2272 return -1;
2274 ACE::record_and_set_non_blocking_mode (handle,
2275 val);
2277 return result;
2280 void
2281 ACE::record_and_set_non_blocking_mode (ACE_HANDLE handle,
2282 int &val)
2284 // We need to record whether we are already *in* nonblocking mode,
2285 // so that we can correctly reset the state when we're done.
2286 val = ACE::get_flags (handle);
2288 if (ACE_BIT_DISABLED (val, ACE_NONBLOCK))
2289 // Set the handle into non-blocking mode if it's not already in
2290 // it.
2291 ACE::set_flags (handle, ACE_NONBLOCK);
2294 void
2295 ACE::restore_non_blocking_mode (ACE_HANDLE handle,
2296 int val)
2298 if (ACE_BIT_DISABLED (val,
2299 ACE_NONBLOCK))
2301 // Save/restore errno.
2302 ACE_Errno_Guard error (errno);
2303 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
2304 // originally.
2305 ACE::clr_flags (handle, ACE_NONBLOCK);
2310 // Format buffer into printable format. This is useful for debugging.
2311 // Portions taken from mdump by J.P. Knight (J.P.Knight@lut.ac.uk)
2312 // Modifications by Todd Montgomery.
2314 size_t
2315 ACE::format_hexdump (const char *buffer,
2316 size_t size,
2317 ACE_TCHAR *obuf,
2318 size_t obuf_sz)
2320 ACE_TRACE ("ACE::format_hexdump");
2322 u_char c;
2323 ACE_TCHAR textver[16 + 1];
2325 // We can fit 16 bytes output in text mode per line, 4 chars per byte.
2326 size_t maxlen = (obuf_sz / 68) * 16;
2328 if (size > maxlen)
2329 size = maxlen;
2331 size_t i;
2333 size_t lines = size / 16;
2334 for (i = 0; i < lines; i++)
2336 size_t j;
2338 for (j = 0 ; j < 16; j++)
2340 c = (u_char) buffer[(i << 4) + j]; // or, buffer[i*16+j]
2341 ACE_OS::sprintf (obuf,
2342 ACE_TEXT ("%02x "),
2344 obuf += 3;
2345 if (j == 7)
2347 ACE_OS::sprintf (obuf,
2348 ACE_TEXT (" "));
2349 ++obuf;
2351 textver[j] = ACE_OS::ace_isprint (c) ? c : '.';
2354 textver[j] = 0;
2356 ACE_OS::sprintf (obuf,
2357 ACE_TEXT (" %s\n"),
2358 textver);
2360 while (*obuf != '\0')
2361 ++obuf;
2364 if (size % 16)
2366 for (i = 0 ; i < size % 16; i++)
2368 c = (u_char) buffer[size - size % 16 + i];
2369 ACE_OS::sprintf (obuf,
2370 ACE_TEXT ("%02x "),
2372 obuf += 3;
2373 if (i == 7)
2375 ACE_OS::sprintf (obuf,
2376 ACE_TEXT (" "));
2377 ++obuf;
2379 textver[i] = ACE_OS::ace_isprint (c) ? c : '.';
2382 for (i = size % 16; i < 16; i++)
2384 ACE_OS::sprintf (obuf,
2385 ACE_TEXT (" "));
2386 obuf += 3;
2387 if (i == 7)
2389 ACE_OS::sprintf (obuf,
2390 ACE_TEXT (" "));
2391 ++obuf;
2393 textver[i] = ' ';
2396 textver[i] = 0;
2397 ACE_OS::sprintf (obuf,
2398 ACE_TEXT (" %s\n"),
2399 textver);
2401 return size;
2404 // Returns the current timestamp in the form
2405 // "hour:minute:second:microsecond." The month, day, and year are
2406 // also stored in the beginning of the date_and_time array.
2408 ACE_TCHAR *
2409 ACE::timestamp (ACE_TCHAR date_and_time[],
2410 size_t date_and_timelen,
2411 bool return_pointer_to_first_digit)
2413 //ACE_TRACE ("ACE::timestamp");
2415 if (date_and_timelen < 35)
2417 errno = EINVAL;
2418 return 0;
2421 #if defined (WIN32)
2422 // Emulate Unix. Win32 does NOT support all the UNIX versions
2423 // below, so DO we need this ifdef.
2424 static const ACE_TCHAR *day_of_week_name[] =
2426 ACE_TEXT ("Sun"),
2427 ACE_TEXT ("Mon"),
2428 ACE_TEXT ("Tue"),
2429 ACE_TEXT ("Wed"),
2430 ACE_TEXT ("Thu"),
2431 ACE_TEXT ("Fri"),
2432 ACE_TEXT ("Sat")
2435 static const ACE_TCHAR *month_name[] =
2437 ACE_TEXT ("Jan"),
2438 ACE_TEXT ("Feb"),
2439 ACE_TEXT ("Mar"),
2440 ACE_TEXT ("Apr"),
2441 ACE_TEXT ("May"),
2442 ACE_TEXT ("Jun"),
2443 ACE_TEXT ("Jul"),
2444 ACE_TEXT ("Aug"),
2445 ACE_TEXT ("Sep"),
2446 ACE_TEXT ("Oct"),
2447 ACE_TEXT ("Nov"),
2448 ACE_TEXT ("Dec")
2451 SYSTEMTIME local;
2452 ::GetLocalTime (&local);
2454 ACE_OS::sprintf (date_and_time,
2455 ACE_TEXT ("%3s %3s %2d %04d %02d:%02d:%02d.%06d"),
2456 day_of_week_name[local.wDayOfWeek],
2457 month_name[local.wMonth - 1],
2458 (int) local.wDay,
2459 (int) local.wYear,
2460 (int) local.wHour,
2461 (int) local.wMinute,
2462 (int) local.wSecond,
2463 (int) (local.wMilliseconds * 1000));
2464 return &date_and_time[15 + (return_pointer_to_first_digit != 0)];
2465 #else /* UNIX */
2466 ACE_TCHAR timebuf[26]; // This magic number is based on the ctime(3c) man page.
2467 ACE_Time_Value cur_time = ACE_OS::gettimeofday ();
2468 time_t secs = cur_time.sec ();
2470 ACE_OS::ctime_r (&secs,
2471 timebuf,
2472 sizeof timebuf / sizeof (ACE_TCHAR));
2473 // date_and_timelen > sizeof timebuf!
2474 ACE_OS::strsncpy (date_and_time,
2475 timebuf,
2476 date_and_timelen);
2477 ACE_TCHAR yeartmp[5];
2478 ACE_OS::strsncpy (yeartmp,
2479 &date_and_time[20],
2481 ACE_TCHAR timetmp[9];
2482 ACE_OS::strsncpy (timetmp,
2483 &date_and_time[11],
2485 ACE_OS::sprintf (&date_and_time[11],
2486 # if defined (ACE_USES_WCHAR)
2487 ACE_TEXT ("%ls %ls.%06ld"),
2488 # else
2489 ACE_TEXT ("%s %s.%06ld"),
2490 # endif /* ACE_USES_WCHAR */
2491 yeartmp,
2492 timetmp,
2493 cur_time.usec ());
2494 date_and_time[33] = '\0';
2495 return &date_and_time[15 + (return_pointer_to_first_digit != 0)];
2496 #endif /* WIN32 */
2499 // This function rounds the request to a multiple of the page size.
2501 size_t
2502 ACE::round_to_pagesize (size_t len)
2504 ACE_TRACE ("ACE::round_to_pagesize");
2506 if (ACE::pagesize_ == 0)
2507 ACE::pagesize_ = ACE_OS::getpagesize ();
2509 return (len + (ACE::pagesize_ - 1)) & ~(ACE::pagesize_ - 1);
2512 size_t
2513 ACE::round_to_allocation_granularity (size_t len)
2515 ACE_TRACE ("ACE::round_to_allocation_granularity");
2517 if (ACE::allocation_granularity_ == 0)
2518 ACE::allocation_granularity_ = ACE_OS::allocation_granularity ();
2520 return (len + (ACE::allocation_granularity_ - 1)) & ~(ACE::allocation_granularity_ - 1);
2523 ACE_HANDLE
2524 ACE::handle_timed_complete (ACE_HANDLE h,
2525 const ACE_Time_Value *timeout,
2526 int is_tli)
2528 ACE_TRACE ("ACE::handle_timed_complete");
2530 #if !defined (ACE_WIN32) && defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2532 struct pollfd fds;
2534 fds.fd = h;
2535 fds.events = POLLIN | POLLOUT;
2536 fds.revents = 0;
2538 #else
2539 ACE_Handle_Set rd_handles;
2540 ACE_Handle_Set wr_handles;
2542 rd_handles.set_bit (h);
2543 wr_handles.set_bit (h);
2544 #endif /* !ACE_WIN32 && ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2546 #if defined (ACE_WIN32)
2547 // Winsock is different - it sets the exception bit for failed connect,
2548 // unlike other platforms, where the read bit is set.
2549 ACE_Handle_Set ex_handles;
2550 ex_handles.set_bit (h);
2551 #endif /* ACE_WIN32 */
2553 bool need_to_check = false;
2554 bool known_failure = false;
2556 #if defined (ACE_WIN32)
2557 int n = ACE_OS::select (0, // Ignored on Windows: int (h) + 1,
2559 wr_handles,
2560 ex_handles,
2561 timeout);
2562 #else
2563 # if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2565 int n = ACE_OS::poll (&fds, 1, timeout);
2567 # else
2568 int n = ACE_OS::select (int (h) + 1,
2569 rd_handles,
2570 wr_handles,
2572 timeout);
2573 # endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2574 #endif /* ACE_WIN32 */
2576 // If we failed to connect within the time period allocated by the
2577 // caller, then we fail (e.g., the remote host might have been too
2578 // busy to accept our call).
2579 if (n <= 0)
2581 if (n == 0 && timeout != 0)
2582 errno = ETIME;
2583 return ACE_INVALID_HANDLE;
2586 // Usually, a ready-for-write handle is successfully connected, and
2587 // ready-for-read (exception on Win32) is a failure. On fails, we
2588 // need to grab the error code via getsockopt. On possible success for
2589 // any platform where we can't tell just from select() (e.g. AIX),
2590 // we also need to check for success/fail.
2591 #if defined (ACE_WIN32)
2592 ACE_UNUSED_ARG (is_tli);
2594 // On Win32, ex_handle set indicates a failure. We'll do the check
2595 // to try and get an errno value, but the connect failed regardless of
2596 // what getsockopt says about the error.
2597 if (ex_handles.is_set (h))
2599 need_to_check = true;
2600 known_failure = true;
2602 #elif defined (ACE_VXWORKS)
2603 ACE_UNUSED_ARG (is_tli);
2605 // Force the check on VxWorks. The read handle for "h" is not set,
2606 // so "need_to_check" is false at this point. The write handle is
2607 // set, for what it's worth.
2608 need_to_check = true;
2609 #else
2610 if (is_tli)
2612 # if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2613 need_to_check = (fds.revents & POLLIN) && !(fds.revents & POLLOUT);
2614 # else
2615 need_to_check = rd_handles.is_set (h) && !wr_handles.is_set (h);
2616 # endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2618 else
2619 #if defined(AIX)
2620 // AIX is broken... both success and failed connect will set the
2621 // write handle only, so always check.
2622 need_to_check = true;
2623 #else
2624 # if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2625 need_to_check = (fds.revents & POLLIN);
2626 # else
2627 need_to_check = rd_handles.is_set (h);
2628 # endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2629 #endif /* AIX */
2630 #endif /* ACE_WIN32 */
2632 if (need_to_check)
2634 #if defined (SOL_SOCKET) && defined (SO_ERROR)
2635 int sock_err = 0;
2636 int sock_err_len = sizeof (sock_err);
2637 int sockopt_ret = ACE_OS::getsockopt (h, SOL_SOCKET, SO_ERROR,
2638 (char *)&sock_err, &sock_err_len);
2639 if (sockopt_ret < 0)
2641 h = ACE_INVALID_HANDLE;
2644 if (sock_err != 0 || known_failure)
2646 h = ACE_INVALID_HANDLE;
2647 errno = sock_err;
2649 #else
2650 char dummy;
2652 // The following recv() won't block provided that the
2653 // ACE_NONBLOCK flag has not been turned off .
2654 n = ACE::recv (h, &dummy, 1, MSG_PEEK);
2656 // If no data was read/peeked at, check to see if it's because
2657 // of a non-connected socket (and therefore an error) or there's
2658 // just no data yet.
2659 if (n <= 0)
2661 if (n == 0)
2663 errno = ECONNREFUSED;
2664 h = ACE_INVALID_HANDLE;
2666 else if (errno != EWOULDBLOCK && errno != EAGAIN)
2667 h = ACE_INVALID_HANDLE;
2669 #endif
2672 // 1. The HANDLE is ready for writing and doesn't need to be checked or
2673 // 2. recv() returned an indication of the state of the socket - if there is
2674 // either data present, or a recv is legit but there's no data yet,
2675 // the connection was successfully established.
2676 return h;
2679 // Wait up to <timeout> amount of time to accept a connection.
2682 ACE::handle_timed_accept (ACE_HANDLE listener,
2683 ACE_Time_Value *timeout,
2684 int restart)
2686 ACE_TRACE ("ACE::handle_timed_accept");
2687 // Make sure we don't bomb out on erroneous values.
2688 if (listener == ACE_INVALID_HANDLE)
2689 return -1;
2691 #if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2693 struct pollfd fds;
2695 fds.fd = listener;
2696 fds.events = POLLIN;
2697 fds.revents = 0;
2699 #else
2700 // Use the select() implementation rather than poll().
2701 ACE_Handle_Set rd_handle;
2702 rd_handle.set_bit (listener);
2703 #endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2705 // We need a loop here if <restart> is enabled.
2707 for (;;)
2709 #if defined (ACE_HAS_POLL) && defined (ACE_HAS_LIMITED_SELECT)
2711 int n = ACE_OS::poll (&fds, 1, timeout);
2713 #else
2714 int select_width;
2715 # if defined (ACE_WIN32)
2716 // This arg is ignored on Windows and causes pointer truncation
2717 // warnings on 64-bit compiles.
2718 select_width = 0;
2719 # else
2720 select_width = int (listener) + 1;
2721 # endif /* ACE_WIN32 */
2722 int n = ACE_OS::select (select_width,
2723 rd_handle, 0, 0,
2724 timeout);
2725 #endif /* ACE_HAS_POLL && ACE_HAS_LIMITED_SELECT */
2727 switch (n)
2729 case -1:
2730 if (errno == EINTR && restart)
2731 continue;
2732 else
2733 return -1;
2734 /* NOTREACHED */
2735 case 0:
2736 if (timeout != 0
2737 && timeout->sec () == 0
2738 && timeout->usec () == 0)
2739 errno = EWOULDBLOCK;
2740 else
2741 errno = ETIMEDOUT;
2742 return -1;
2743 /* NOTREACHED */
2744 case 1:
2745 return 0;
2746 /* NOTREACHED */
2747 default:
2748 errno = EINVAL;
2749 return -1;
2750 /* NOTREACHED */
2755 // Make the current process a UNIX daemon. This is based on Stevens
2756 // code from APUE.
2759 ACE::daemonize (const ACE_TCHAR pathname[],
2760 bool close_all_handles,
2761 const ACE_TCHAR program_name[])
2763 ACE_TRACE ("ACE::daemonize");
2764 #if !defined (ACE_LACKS_FORK)
2765 pid_t pid = ACE_OS::fork ();
2767 if (pid == -1)
2768 return -1;
2769 else if (pid != 0)
2770 ACE_OS::exit (0); // Parent exits.
2772 // 1st child continues.
2773 ACE_OS::setsid (); // Become session leader.
2775 ACE_OS::signal (SIGHUP, SIG_IGN);
2777 pid = ACE_OS::fork (program_name);
2779 if (pid != 0)
2780 ACE_OS::exit (0); // First child terminates.
2782 // Second child continues.
2784 if (pathname != 0)
2785 // change working directory.
2786 ACE_OS::chdir (pathname);
2788 ACE_OS::umask (0); // clear our file mode creation mask.
2790 // Close down the I/O handles.
2791 if (close_all_handles)
2793 for (int i = ACE::max_handles () - 1; i >= 0; i--)
2794 ACE_OS::close (i);
2796 int fd = ACE_OS::open ("/dev/null", O_RDWR, 0);
2797 if (fd != -1)
2799 ACE_OS::dup2 (fd, ACE_STDIN);
2800 ACE_OS::dup2 (fd, ACE_STDOUT);
2801 ACE_OS::dup2 (fd, ACE_STDERR);
2803 if (fd > ACE_STDERR)
2804 ACE_OS::close (fd);
2808 return 0;
2809 #else
2810 ACE_UNUSED_ARG (pathname);
2811 ACE_UNUSED_ARG (close_all_handles);
2812 ACE_UNUSED_ARG (program_name);
2814 ACE_NOTSUP_RETURN (-1);
2815 #endif /* ACE_LACKS_FORK */
2818 pid_t
2819 ACE::fork (const ACE_TCHAR *program_name,
2820 int avoid_zombies)
2822 if (avoid_zombies == 0)
2823 return ACE_OS::fork (program_name);
2824 else
2826 // This algorithm is adapted from an example in the Stevens book
2827 // "Advanced Programming in the Unix Environment" and an item in
2828 // Andrew Gierth's Unix Programming FAQ. It creates an orphan
2829 // process that's inherited by the init process; init cleans up
2830 // when the orphan process terminates.
2832 // Another way to avoid zombies is to ignore or catch the
2833 // SIGCHLD signal; we don't use that approach here.
2835 pid_t pid = ACE_OS::fork ();
2836 if (pid == 0)
2838 // The child process forks again to create a grandchild.
2839 switch (ACE_OS::fork (program_name))
2841 case 0: // grandchild returns 0.
2842 return 0;
2843 case -1: // assumes all errnos are < 256
2844 ACE_OS::_exit (errno);
2845 default: // child terminates, orphaning grandchild
2846 ACE_OS::_exit (0);
2850 // Parent process waits for child to terminate.
2851 ACE_exitcode status;
2852 if (pid < 0 || ACE_OS::waitpid (pid, &status, 0) < 0)
2853 return -1;
2855 // child terminated by calling exit()?
2856 if (WIFEXITED ((status)))
2858 // child terminated normally?
2859 if (WEXITSTATUS ((status)) == 0)
2860 return 1;
2861 else
2862 errno = WEXITSTATUS ((status));
2864 else
2865 // child didn't call exit(); perhaps it received a signal?
2866 errno = EINTR;
2868 return -1;
2873 ACE::max_handles (void)
2875 ACE_TRACE ("ACE::max_handles");
2876 #if defined (RLIMIT_NOFILE) && !defined (ACE_LACKS_RLIMIT)
2877 rlimit rl;
2878 int const r = ACE_OS::getrlimit (RLIMIT_NOFILE, &rl);
2879 # if !defined (RLIM_INFINITY)
2880 if (r == 0)
2881 return rl.rlim_cur;
2882 # else
2883 if (r == 0 && rl.rlim_cur != RLIM_INFINITY)
2884 return rl.rlim_cur;
2885 // If == RLIM_INFINITY, fall through to the ACE_LACKS_RLIMIT sections
2886 # endif /* RLIM_INFINITY */
2887 #endif /* RLIMIT_NOFILE && !ACE_LACKS_RLIMIT */
2889 #if defined (_SC_OPEN_MAX)
2890 return ACE_OS::sysconf (_SC_OPEN_MAX);
2891 #elif defined (ACE_VXWORKS) && (ACE_VXWORKS < 0x620)
2892 return maxFiles;
2893 #elif defined (FD_SETSIZE)
2894 return FD_SETSIZE;
2895 #else
2896 ACE_NOTSUP_RETURN (-1);
2897 #endif /* _SC_OPEN_MAX */
2900 // Set the number of currently open handles in the process.
2902 // If NEW_LIMIT == -1 set the limit to the maximum allowable.
2903 // Otherwise, set it to be the value of NEW_LIMIT.
2906 ACE::set_handle_limit (int new_limit,
2907 int increase_limit_only)
2909 ACE_TRACE ("ACE::set_handle_limit");
2910 int cur_limit = ACE::max_handles ();
2911 int max_limit = cur_limit;
2913 if (cur_limit == -1)
2914 return -1;
2916 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2917 struct rlimit rl;
2919 ACE_OS::memset ((void *) &rl, 0, sizeof rl);
2920 int r = ACE_OS::getrlimit (RLIMIT_NOFILE, &rl);
2921 if (r == 0)
2922 max_limit = rl.rlim_max;
2923 #endif /* ACE_LACKS_RLIMIT */
2925 if (new_limit == -1)
2926 new_limit = max_limit;
2928 if (new_limit < 0)
2930 errno = EINVAL;
2931 return -1;
2933 else if (new_limit > cur_limit)
2935 // Increase the limit.
2936 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2937 rl.rlim_cur = new_limit;
2938 return ACE_OS::setrlimit (RLIMIT_NOFILE, &rl);
2939 #elif defined (ACE_LACKS_RLIMIT_NOFILE)
2940 return 0;
2941 #else
2942 // Must return EINVAL errno.
2943 ACE_NOTSUP_RETURN (-1);
2944 #endif /* ACE_LACKS_RLIMIT */
2946 else if (increase_limit_only == 0)
2948 // Decrease the limit.
2949 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2950 rl.rlim_cur = new_limit;
2951 return ACE_OS::setrlimit (RLIMIT_NOFILE, &rl);
2952 #else
2953 // We give a chance to platforms without RLIMIT to work.
2954 // Instead of ACE_NOTSUP_RETURN (0), just return 0 because
2955 // new_limit is <= cur_limit, so it's a no-op.
2956 return 0;
2957 #endif /* ACE_LACKS_RLIMIT */
2960 return 0;
2963 // Euclid's greatest common divisor algorithm.
2964 u_long
2965 ACE::gcd (u_long x, u_long y)
2967 while (y != 0)
2969 u_long r = x % y;
2970 x = y;
2971 y = r;
2974 return x;
2978 // Calculates the minimum enclosing frame size for the given values.
2979 u_long
2980 ACE::minimum_frame_size (u_long period1, u_long period2)
2982 // if one of the periods is zero, treat it as though it as
2983 // uninitialized and return the other period as the frame size
2984 if (0 == period1)
2986 return period2;
2988 if (0 == period2)
2990 return period1;
2993 // if neither is zero, find the greatest common divisor of the two periods
2994 u_long greatest_common_divisor = ACE::gcd (period1, period2);
2996 // explicitly consider cases to reduce risk of possible overflow errors
2997 if (greatest_common_divisor == 1)
2999 // periods are relative primes: just multiply them together
3000 return period1 * period2;
3002 else if (greatest_common_divisor == period1)
3004 // the first period divides the second: return the second
3005 return period2;
3007 else if (greatest_common_divisor == period2)
3009 // the second period divides the first: return the first
3010 return period1;
3012 else
3014 // the current frame size and the entry's effective period
3015 // have a non-trivial greatest common divisor: return the
3016 // product of factors divided by those in their gcd.
3017 return (period1 * period2) / greatest_common_divisor;
3022 u_long
3023 ACE::is_prime (const u_long n,
3024 const u_long min_factor,
3025 const u_long max_factor)
3027 if (n > 3)
3028 for (u_long factor = min_factor;
3029 factor <= max_factor;
3030 ++factor)
3031 if (n / factor * factor == n)
3032 return factor;
3034 return 0;
3037 const ACE_TCHAR *
3038 ACE::sock_error (int error)
3040 #if defined (ACE_WIN32)
3041 static ACE_TCHAR unknown_msg[64];
3043 switch (error)
3045 case WSAVERNOTSUPPORTED:
3046 return ACE_TEXT ("version of WinSock not supported");
3047 /* NOTREACHED */
3048 case WSASYSNOTREADY:
3049 return ACE_TEXT ("WinSock not present or not responding");
3050 /* NOTREACHED */
3051 case WSAEINVAL:
3052 return ACE_TEXT ("app version not supported by DLL");
3053 /* NOTREACHED */
3054 case WSAHOST_NOT_FOUND:
3055 return ACE_TEXT ("Authoritive: Host not found");
3056 /* NOTREACHED */
3057 case WSATRY_AGAIN:
3058 return ACE_TEXT ("Non-authoritive: host not found or server failure");
3059 /* NOTREACHED */
3060 case WSANO_RECOVERY:
3061 return ACE_TEXT ("Non-recoverable: refused or not implemented");
3062 /* NOTREACHED */
3063 case WSANO_DATA:
3064 return ACE_TEXT ("Valid name, no data record for type");
3065 /* NOTREACHED */
3067 case WSANO_ADDRESS:
3068 return "Valid name, no MX record";
3070 case WSANOTINITIALISED:
3071 return ACE_TEXT ("WSA Startup not initialized");
3072 /* NOTREACHED */
3073 case WSAENETDOWN:
3074 return ACE_TEXT ("Network subsystem failed");
3075 /* NOTREACHED */
3076 case WSAEINPROGRESS:
3077 return ACE_TEXT ("Blocking operation in progress");
3078 /* NOTREACHED */
3079 case WSAEINTR:
3080 return ACE_TEXT ("Blocking call cancelled");
3081 /* NOTREACHED */
3082 case WSAEAFNOSUPPORT:
3083 return ACE_TEXT ("address family not supported");
3084 /* NOTREACHED */
3085 case WSAEMFILE:
3086 return ACE_TEXT ("no file handles available");
3087 /* NOTREACHED */
3088 case WSAENOBUFS:
3089 return ACE_TEXT ("no buffer space available");
3090 /* NOTREACHED */
3091 case WSAEPROTONOSUPPORT:
3092 return ACE_TEXT ("specified protocol not supported");
3093 /* NOTREACHED */
3094 case WSAEPROTOTYPE:
3095 return ACE_TEXT ("protocol wrong type for this socket");
3096 /* NOTREACHED */
3097 case WSAESOCKTNOSUPPORT:
3098 return ACE_TEXT ("socket type not supported for address family");
3099 /* NOTREACHED */
3100 case WSAENOTSOCK:
3101 return ACE_TEXT ("handle is not a socket");
3102 /* NOTREACHED */
3103 case WSAEWOULDBLOCK:
3104 return ACE_TEXT ("resource temporarily unavailable");
3105 /* NOTREACHED */
3106 case WSAEADDRINUSE:
3107 return ACE_TEXT ("address already in use");
3108 /* NOTREACHED */
3109 case WSAECONNABORTED:
3110 return ACE_TEXT ("connection aborted");
3111 /* NOTREACHED */
3112 case WSAECONNRESET:
3113 return ACE_TEXT ("connection reset");
3114 /* NOTREACHED */
3115 case WSAENOTCONN:
3116 return ACE_TEXT ("not connected");
3117 /* NOTREACHED */
3118 case WSAETIMEDOUT:
3119 return ACE_TEXT ("connection timed out");
3120 /* NOTREACHED */
3121 case WSAECONNREFUSED:
3122 return ACE_TEXT ("connection refused");
3123 /* NOTREACHED */
3124 case WSAEHOSTDOWN:
3125 return ACE_TEXT ("host down");
3126 /* NOTREACHED */
3127 case WSAEHOSTUNREACH:
3128 return ACE_TEXT ("host unreachable");
3129 /* NOTREACHED */
3130 case WSAEADDRNOTAVAIL:
3131 return ACE_TEXT ("address not available");
3132 /* NOTREACHED */
3133 case WSAEISCONN:
3134 return ACE_TEXT ("socket is already connected");
3135 /* NOTREACHED */
3136 case WSAENETRESET:
3137 return ACE_TEXT ("network dropped connection on reset");
3138 /* NOTREACHED */
3139 case WSAEMSGSIZE:
3140 return ACE_TEXT ("message too long");
3141 /* NOTREACHED */
3142 case WSAENETUNREACH:
3143 return ACE_TEXT ("network is unreachable");
3144 /* NOTREACHED */
3145 case WSAEFAULT:
3146 return ACE_TEXT ("bad address");
3147 /* NOTREACHED */
3148 case WSAEDISCON:
3149 return ACE_TEXT ("graceful shutdown in progress");
3150 /* NOTREACHED */
3151 case WSAEACCES:
3152 return ACE_TEXT ("permission denied");
3153 /* NOTREACHED */
3154 case WSAESHUTDOWN:
3155 return ACE_TEXT ("cannot send after socket shutdown");
3156 /* NOTREACHED */
3157 case WSAEPROCLIM:
3158 return ACE_TEXT ("too many processes");
3159 /* NOTREACHED */
3160 case WSAEALREADY:
3161 return ACE_TEXT ("operation already in progress");
3162 /* NOTREACHED */
3163 case WSAEPFNOSUPPORT:
3164 return ACE_TEXT ("protocol family not supported");
3165 /* NOTREACHED */
3166 case WSAENOPROTOOPT:
3167 return ACE_TEXT ("bad protocol option");
3168 /* NOTREACHED */
3169 case WSATYPE_NOT_FOUND:
3170 return ACE_TEXT ("class type not found");
3171 /* NOTREACHED */
3172 case WSAEOPNOTSUPP:
3173 return ACE_TEXT ("operation not supported");
3174 /* NOTREACHED */
3175 case WSAEDESTADDRREQ:
3176 return ACE_TEXT ("destination address required");
3177 /* NOTREACHED */
3178 default:
3179 ACE_OS::sprintf (unknown_msg, ACE_TEXT ("unknown error: %d"), error);
3180 return unknown_msg;
3181 /* NOTREACHED */
3183 #else
3184 ACE_UNUSED_ARG (error);
3185 ACE_NOTSUP_RETURN (0);
3186 #endif /* ACE_WIN32 */
3189 bool
3190 ACE::is_sock_error (int error)
3192 #if defined (ACE_WIN32)
3193 switch (error)
3195 case WSAVERNOTSUPPORTED:
3196 case WSASYSNOTREADY:
3197 case WSAEINVAL:
3198 case WSAHOST_NOT_FOUND:
3199 case WSATRY_AGAIN:
3200 case WSANO_RECOVERY:
3201 case WSANO_DATA:
3203 case WSANO_ADDRESS:
3205 case WSANOTINITIALISED:
3206 case WSAENETDOWN:
3207 case WSAEINPROGRESS:
3208 case WSAEINTR:
3209 case WSAEAFNOSUPPORT:
3210 case WSAEMFILE:
3211 case WSAENOBUFS:
3212 case WSAEPROTONOSUPPORT:
3213 case WSAEPROTOTYPE:
3214 case WSAESOCKTNOSUPPORT:
3215 case WSAENOTSOCK:
3216 case WSAEWOULDBLOCK:
3217 case WSAEADDRINUSE:
3218 case WSAECONNABORTED:
3219 case WSAECONNRESET:
3220 case WSAENOTCONN:
3221 case WSAETIMEDOUT:
3222 case WSAECONNREFUSED:
3223 case WSAEHOSTDOWN:
3224 case WSAEHOSTUNREACH:
3225 case WSAEADDRNOTAVAIL:
3226 case WSAEISCONN:
3227 case WSAENETRESET:
3228 case WSAEMSGSIZE:
3229 case WSAENETUNREACH:
3230 case WSAEFAULT:
3231 case WSAEDISCON:
3232 case WSAEACCES:
3233 case WSAESHUTDOWN:
3234 case WSAEPROCLIM:
3235 case WSAEALREADY:
3236 case WSAEPFNOSUPPORT:
3237 case WSAENOPROTOOPT:
3238 case WSATYPE_NOT_FOUND:
3239 case WSAEOPNOTSUPP:
3240 return true;
3242 #else
3243 ACE_UNUSED_ARG (error);
3244 #endif /* ACE_WIN32 */
3245 return false;
3248 char *
3249 ACE::strndup (const char *str, size_t n)
3251 const char *t = str;
3252 size_t len;
3254 // Figure out how long this string is (remember, it might not be
3255 // NUL-terminated).
3257 for (len = 0;
3258 len < n && *t++ != '\0';
3259 len++)
3260 continue;
3262 char *s;
3263 ACE_ALLOCATOR_RETURN (s,
3264 (char *) ACE_OS::malloc (len + 1),
3266 return ACE_OS::strsncpy (s, str, len + 1);
3269 #if defined (ACE_HAS_WCHAR)
3270 wchar_t *
3271 ACE::strndup (const wchar_t *str, size_t n)
3273 const wchar_t *t = str;
3274 size_t len;
3276 // Figure out how long this string is (remember, it might not be
3277 // NUL-terminated).
3279 for (len = 0;
3280 len < n && *t++ != '\0';
3281 len++)
3282 continue;
3284 wchar_t *s;
3285 ACE_ALLOCATOR_RETURN (s,
3286 static_cast<wchar_t *> (
3287 ACE_OS::malloc ((len + 1) * sizeof (wchar_t))),
3289 return ACE_OS::strsncpy (s, str, len + 1);
3291 #endif /* ACE_HAS_WCHAR */
3293 char *
3294 ACE::strnnew (const char *str, size_t n)
3296 const char *t = str;
3297 size_t len;
3299 // Figure out how long this string is (remember, it might not be
3300 // NUL-terminated).
3302 for (len = 0;
3303 len < n && *t++ != L'\0';
3304 len++)
3305 continue;
3307 char *s;
3308 ACE_NEW_RETURN (s,
3309 char[len + 1],
3311 return ACE_OS::strsncpy (s, str, len + 1);
3314 #if defined (ACE_HAS_WCHAR)
3315 wchar_t *
3316 ACE::strnnew (const wchar_t *str, size_t n)
3318 const wchar_t *t = str;
3319 size_t len;
3321 // Figure out how long this string is (remember, it might not be
3322 // NUL-terminated).
3324 for (len = 0;
3325 len < n && *t++ != ACE_TEXT_WIDE ('\0');
3326 len++)
3327 continue;
3329 wchar_t *s;
3330 ACE_NEW_RETURN (s,
3331 wchar_t[len + 1],
3333 return ACE_OS::strsncpy (s, str, len + 1);
3335 #endif /* ACE_HAS_WCHAR */
3337 const char *
3338 ACE::strend (const char *s)
3340 while (*s++ != '\0')
3341 continue;
3343 return s;
3346 #if defined ACE_HAS_WCHAR
3347 const wchar_t *
3348 ACE::strend (const wchar_t *s)
3350 while (*s++ != ACE_TEXT_WIDE ('\0'))
3351 continue;
3353 return s;
3355 #endif
3357 char *
3358 ACE::strnew (const char *s)
3360 if (s == 0)
3361 return 0;
3362 char *t = 0;
3363 ACE_NEW_RETURN (t,
3364 char [ACE_OS::strlen (s) + 1],
3366 if (t == 0)
3367 return 0;
3368 else
3369 return ACE_OS::strcpy (t, s);
3372 #if defined (ACE_HAS_WCHAR)
3373 wchar_t *
3374 ACE::strnew (const wchar_t *s)
3376 if (s == 0)
3377 return 0;
3378 wchar_t *t = 0;
3379 ACE_NEW_RETURN (t,
3380 wchar_t[ACE_OS::strlen (s) + 1],
3382 if (t == 0)
3383 return 0;
3384 else
3385 return ACE_OS::strcpy (t, s);
3387 #endif /* ACE_HAS_WCHAR */
3389 inline static bool equal_char(char a, char b, bool case_sensitive)
3391 if (case_sensitive)
3392 return a == b;
3393 return ACE_OS::ace_tolower(a) == ACE_OS::ace_tolower(b);
3396 bool
3397 ACE::wild_match(const char* str, const char* pat, bool case_sensitive)
3399 if (str == pat)
3400 return true;
3401 if (pat == 0 || str == 0)
3402 return false;
3404 bool star = false;
3405 const char* s = str;
3406 const char* p = pat;
3407 while (*s != '\0')
3409 if (*p == '*')
3411 star = true;
3412 pat = p;
3413 while (*++pat == '*') {}
3415 if (*pat == '\0')
3416 return true;
3417 p = pat;
3419 else if (*p == '?')
3421 ++s;
3422 ++p;
3424 else if (! equal_char(*s, *p, case_sensitive))
3426 if (!star)
3427 return false;
3428 s = ++str;
3429 p = pat;
3431 else
3433 ++s;
3434 ++p;
3437 if (*p == '*')
3438 while (*++p == '*') {}
3440 return *p == '\0';
3443 // Close versioned namespace, if enabled by the user.
3444 ACE_END_VERSIONED_NAMESPACE_DECL