3 #include "ace/Basic_Types.h"
4 #include "ace/Handle_Set.h"
5 #include "ace/Auto_Ptr.h"
6 #include "ace/SString.h"
7 #include "ace/Version.h"
8 #include "ace/Message_Block.h"
9 #include "ace/Log_Category.h"
10 #include "ace/Flag_Manip.h"
11 #include "ace/OS_NS_sys_select.h"
12 #include "ace/OS_NS_string.h"
13 #include "ace/OS_NS_strings.h"
14 #include "ace/OS_NS_signal.h"
15 #include "ace/OS_NS_stdio.h"
16 #include "ace/OS_NS_sys_resource.h"
17 #include "ace/OS_NS_sys_wait.h"
18 #include "ace/OS_NS_sys_time.h"
19 #include "ace/OS_NS_time.h"
20 #include "ace/OS_NS_sys_uio.h"
21 #include "ace/OS_NS_sys_stat.h"
22 #include "ace/OS_NS_ctype.h"
23 #include "ace/OS_NS_fcntl.h"
24 #include "ace/OS_TLI.h"
25 #include "ace/Truncate.h"
27 #if !defined (__ACE_INLINE__)
28 #include "ace/ACE.inl"
29 #endif /* __ACE_INLINE__ */
31 #if defined (ACE_HAS_POLL)
32 # include "ace/OS_NS_poll.h"
33 #endif /* ACE_HAS_POLL */
35 // Open versioned namespace, if enabled by the user.
36 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
41 // Used internally so not exported.
43 // Size of allocation granularity.
44 size_t allocation_granularity_
= 0;
49 // Are we debugging ACE?
50 // Keeps track of whether we're in some global debug mode.
55 ACE::out_of_handles (int error
)
57 // EMFILE is common to all platforms.
58 if (error
== EMFILE
||
59 #if defined (ACE_WIN32)
60 // On Win32, we need to check for ENOBUFS also.
62 #elif defined (ACE_LINUX)
63 // On linux, we need to check for ENOENT also.
65 // For RedHat5.2, need to check for EINVAL too.
67 // Without threads check for EOPNOTSUPP
68 error
== EOPNOTSUPP
||
69 #elif defined (__FreeBSD__)
70 // On FreeBSD we need to check for EOPNOTSUPP (LinuxThreads) or
71 // ENOSYS (libc_r threads) also.
72 error
== EOPNOTSUPP
||
74 #elif defined (__OpenBSD__)
75 // OpenBSD appears to return EBADF.
77 #endif /* ACE_WIN32 */
87 return ACE_MAJOR_VERSION
;
93 return ACE_MINOR_VERSION
;
99 return ACE_MICRO_VERSION
;
103 ACE::micro_version ()
105 return ACE_MICRO_VERSION
;
109 ACE::compiler_name ()
114 return ACE_TEXT ("");
119 ACE::compiler_major_version ()
121 #ifdef ACE_CC_MAJOR_VERSION
122 return ACE_CC_MAJOR_VERSION
;
129 ACE::compiler_minor_version ()
131 #ifdef ACE_CC_MINOR_VERSION
132 return ACE_CC_MINOR_VERSION
;
139 ACE::compiler_beta_version ()
141 #ifdef ACE_CC_BETA_VERSION
142 return ACE_CC_BETA_VERSION
;
149 ACE::nibble2hex (u_int n
)
151 // Yes, this works for UNICODE
152 return ACE_TEXT ("0123456789abcdef")[n
& 0x0f];
158 //FUZZ: disable check_for_ace_log_categories
159 static const char *debug
= ACE_OS::getenv ("ACE_DEBUG");
160 //FUZZ: enable check_for_ace_log_categories
161 return (ACE::debug_
) ? ACE::debug_
: (debug
!= 0 ? (*debug
!= '0') : false);
165 ACE::debug (bool onoff
)
171 ACE::select (int width
,
172 ACE_Handle_Set
*readfds
,
173 ACE_Handle_Set
*writefds
,
174 ACE_Handle_Set
*exceptfds
,
175 const ACE_Time_Value
*timeout
)
177 int result
= ACE_OS::select (width
,
178 readfds
? readfds
->fdset () : 0,
179 writefds
? writefds
->fdset () : 0,
180 exceptfds
? exceptfds
->fdset () : 0,
184 # if !defined (ACE_WIN32)
185 // This isn't needed for Windows... it's a no-op anyway.
187 readfds
->sync ((ACE_HANDLE
) width
);
189 writefds
->sync ((ACE_HANDLE
) width
);
191 exceptfds
->sync ((ACE_HANDLE
) width
);
192 #endif /* ACE_WIN32 */
198 ACE::select (int width
,
199 ACE_Handle_Set
&readfds
,
200 const ACE_Time_Value
*timeout
)
202 int result
= ACE_OS::select (width
,
208 #if !defined (ACE_WIN32)
210 readfds
.sync ((ACE_HANDLE
) width
);
211 #endif /* ACE_WIN32 */
216 ACE::terminate_process (pid_t pid
)
218 #if defined (ACE_WIN32)
219 // Create a handle for the given process id.
220 ACE_HANDLE process_handle
=
221 ::OpenProcess (PROCESS_TERMINATE
,
222 FALSE
, // New handle is not inheritable.
225 if (process_handle
== ACE_INVALID_HANDLE
226 || process_handle
== 0)
230 // Kill the process associated with process_handle.
231 BOOL terminate_result
=
232 ::TerminateProcess (process_handle
, 0);
233 // Free up the kernel resources.
234 ACE_OS::close (process_handle
);
235 return terminate_result
? 0 : -1;
238 return ACE_OS::kill (pid
, 9);
239 #endif /* ACE_WIN32 */
243 ACE::process_active (pid_t pid
)
245 #if !defined(ACE_WIN32)
246 if (ACE_OS::kill (pid
, 0) == 0)
248 else if (errno
== ESRCH
)
253 // Create a handle for the given process id.
254 ACE_HANDLE process_handle
=
255 ::OpenProcess (PROCESS_QUERY_INFORMATION
, FALSE
, pid
);
256 if (process_handle
== ACE_INVALID_HANDLE
|| process_handle
== 0)
262 if (::GetExitCodeProcess (process_handle
,
264 || status
!= STILL_ACTIVE
)
267 ::CloseHandle (process_handle
);
270 #endif /* !ACE_WIN32 */
274 ACE::execname (const ACE_TCHAR
*old_name
)
276 #if defined (ACE_WIN32)
277 const ACE_TCHAR
*suffix
= ACE_OS::strrchr (old_name
, ACE_TEXT ('.'));
278 if (suffix
== 0 || ACE_OS::strcasecmp (suffix
, ACE_TEXT (".exe")) != 0)
280 ACE_TCHAR
*new_name
= 0;
283 ACE_OS::strlen (old_name
)
284 + ACE_OS::strlen (ACE_TEXT (".exe"))
287 ACE_NEW_RETURN (new_name
,
290 ACE_TCHAR
*end
= new_name
;
292 end
= ACE_OS::strecpy (new_name
, old_name
);
294 // Concatenate the .exe suffix onto the end of the executable.
295 // end points _after_ the terminating nul.
296 ACE_OS::strcpy (end
- 1, ACE_TEXT (".exe"));
300 #endif /* ACE_WIN32 */
305 ACE::hash_pjw (const char *str
, size_t len
)
309 for (size_t i
= 0; i
< len
; i
++)
311 const char temp
= str
[i
];
312 hash
= (hash
<< 4) + (temp
* 13);
314 u_long g
= hash
& 0xf0000000;
327 ACE::hash_pjw (const char *str
)
329 return ACE::hash_pjw (str
, ACE_OS::strlen (str
));
332 #if defined (ACE_HAS_WCHAR)
334 ACE::hash_pjw (const wchar_t *str
, size_t len
)
338 for (size_t i
= 0; i
< len
; i
++)
340 // @@ UNICODE: Does this function do the correct thing with wchar's?
342 const wchar_t temp
= str
[i
];
343 hash
= (hash
<< 4) + (temp
* 13);
345 u_long g
= hash
& 0xf0000000;
358 ACE::hash_pjw (const wchar_t *str
)
360 return ACE::hash_pjw (str
, ACE_OS::strlen (str
));
362 #endif /* ACE_HAS_WCHAR */
365 ACE::strenvdup (const ACE_TCHAR
*str
)
367 ACE_TRACE ("ACE::strenvdup");
369 return ACE_OS::strenvdup (str
);
377 ==================================================================
378 netsvc netsvc.dll libnetsvc.so
379 (PATH will be (LD_LIBRARY_PATH
380 evaluated) evaluated)
382 libnetsvc.dll libnetsvc.dll libnetsvc.dll + warning
383 netsvc.so netsvc.so + warning libnetsvc.so
385 ..\../libs/netsvc ..\..\libs\netsvc.dll ../../libs/netsvc.so
386 (absolute path used) (absolute path used)
391 ACE::basename (const ACE_TCHAR
*pathname
, ACE_TCHAR delim
)
393 ACE_TRACE ("ACE::basename");
394 const ACE_TCHAR
*temp
= ACE_OS::strrchr (pathname
, delim
);
403 ACE::dirname (const ACE_TCHAR
*pathname
, ACE_TCHAR delim
)
405 ACE_TRACE ("ACE::dirname");
406 static ACE_TCHAR return_dirname
[MAXPATHLEN
+ 1];
408 const ACE_TCHAR
*temp
= ACE_OS::strrchr (pathname
, delim
);
412 return_dirname
[0] = '.';
413 return_dirname
[1] = '\0';
415 return return_dirname
;
419 // When the len is truncated, there are problems! This should
420 // not happen in normal circomstances
421 size_t len
= temp
- pathname
+ 1;
422 if (len
> (sizeof return_dirname
/ sizeof (ACE_TCHAR
)))
423 len
= sizeof return_dirname
/ sizeof (ACE_TCHAR
);
425 ACE_OS::strsncpy (return_dirname
,
428 return return_dirname
;
433 ACE::recv (ACE_HANDLE handle
,
437 const ACE_Time_Value
*timeout
)
440 return ACE_OS::recv (handle
, (char *) buf
, len
, flags
);
444 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) ==-1)
448 ssize_t bytes_transferred
=
449 ACE_OS::recv (handle
, (char *) buf
, len
, flags
);
450 ACE::restore_non_blocking_mode (handle
, val
);
451 return bytes_transferred
;
456 #if defined (ACE_HAS_TLI)
459 ACE::t_rcv (ACE_HANDLE handle
,
463 const ACE_Time_Value
*timeout
)
466 return ACE_OS::t_rcv (handle
, (char *) buf
, len
, flags
);
470 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) ==-1)
474 ssize_t bytes_transferred
=
475 ACE_OS::t_rcv (handle
, (char *) buf
, len
, flags
);
476 ACE::restore_non_blocking_mode (handle
, val
);
477 return bytes_transferred
;
482 #endif /* ACE_HAS_TLI */
485 ACE::recv (ACE_HANDLE handle
,
488 const ACE_Time_Value
*timeout
)
491 return ACE::recv_i (handle
, buf
, n
);
495 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
499 ssize_t bytes_transferred
= ACE::recv_i (handle
, buf
, n
);
500 ACE::restore_non_blocking_mode (handle
, val
);
501 return bytes_transferred
;
507 ACE::recvmsg (ACE_HANDLE handle
,
510 const ACE_Time_Value
*timeout
)
513 return ACE_OS::recvmsg (handle
, msg
, flags
);
517 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
521 ssize_t bytes_transferred
= ACE_OS::recvmsg (handle
, msg
, flags
);
522 ACE::restore_non_blocking_mode (handle
, val
);
523 return bytes_transferred
;
529 ACE::recvfrom (ACE_HANDLE handle
,
533 struct sockaddr
*addr
,
535 const ACE_Time_Value
*timeout
)
538 return ACE_OS::recvfrom (handle
, buf
, len
, flags
, addr
, addrlen
);
542 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
546 ssize_t bytes_transferred
=
547 ACE_OS::recvfrom (handle
, buf
, len
, flags
, addr
, addrlen
);
548 ACE::restore_non_blocking_mode (handle
, val
);
549 return bytes_transferred
;
555 ACE::recv_n_i (ACE_HANDLE handle
,
562 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
565 for (bytes_transferred
= 0;
566 bytes_transferred
< len
;
567 bytes_transferred
+= n
)
569 // Try to transfer as much of the remaining data as possible.
570 n
= ACE_OS::recv (handle
,
571 static_cast <char *> (buf
) + bytes_transferred
,
572 len
- bytes_transferred
,
578 // Check for other errors.
581 // Check for possible blocking.
582 if (errno
== EWOULDBLOCK
)
584 // Wait for the blocking to subside.
585 int const result
= ACE::handle_read_ready (handle
, 0);
587 // Did select() succeed?
590 // Blocking subsided. Continue data transfer.
596 // Other data transfer or select() failures.
601 return static_cast<ssize_t
> (bytes_transferred
);
605 ACE::recv_n_i (ACE_HANDLE handle
,
609 const ACE_Time_Value
*timeout
,
613 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
619 ACE::record_and_set_non_blocking_mode (handle
, val
);
621 for (bytes_transferred
= 0;
622 bytes_transferred
< len
;
623 bytes_transferred
+= n
)
625 // Try to transfer as much of the remaining data as possible.
626 // Since the socket is in non-blocking mode, this call will not
628 n
= ACE_OS::recv (handle
,
629 static_cast <char *> (buf
) + bytes_transferred
,
630 len
- bytes_transferred
,
637 // Check for possible blocking.
639 errno
== EWOULDBLOCK
)
641 // Wait upto <timeout> for the blocking to subside.
642 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
644 // Did select() succeed?
647 // Blocking subsided in <timeout> period. Continue
654 // Wait in select() timed out or other data transfer or
655 // select() failures.
662 ACE::restore_non_blocking_mode (handle
, val
);
667 return static_cast<ssize_t
> (bytes_transferred
);
670 #if defined (ACE_HAS_TLI)
673 ACE::t_rcv_n_i (ACE_HANDLE handle
,
680 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
683 for (bytes_transferred
= 0;
684 bytes_transferred
< len
;
685 bytes_transferred
+= n
)
687 // Try to transfer as much of the remaining data as possible.
688 n
= ACE_OS::t_rcv (handle
,
689 (char *) buf
+ bytes_transferred
,
690 len
- bytes_transferred
,
696 // Check for other errors.
699 // Check for possible blocking.
700 if (errno
== EWOULDBLOCK
)
702 // Wait for the blocking to subside.
703 int const result
= ACE::handle_read_ready (handle
, 0);
705 // Did select() succeed?
708 // Blocking subsided. Continue data transfer.
714 // Other data transfer or select() failures.
719 return bytes_transferred
;
723 ACE::t_rcv_n_i (ACE_HANDLE handle
,
727 const ACE_Time_Value
*timeout
,
731 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
737 ACE::record_and_set_non_blocking_mode (handle
, val
);
739 for (bytes_transferred
= 0;
740 bytes_transferred
< len
;
741 bytes_transferred
+= n
)
743 // Try to transfer as much of the remaining data as possible.
744 // Since the socket is in non-blocking mode, this call will not
746 n
= ACE_OS::t_rcv (handle
,
747 (char *) buf
+ bytes_transferred
,
748 len
- bytes_transferred
,
755 // Check for possible blocking.
757 errno
== EWOULDBLOCK
)
759 // Wait upto <timeout> for the blocking to subside.
760 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
762 // Did select() succeed?
765 // Blocking subsided in <timeout> period. Continue
772 // Wait in select() timed out or other data transfer or
773 // select() failures.
780 ACE::restore_non_blocking_mode (handle
, val
);
785 return bytes_transferred
;
788 #endif /* ACE_HAS_TLI */
791 ACE::recv_n_i (ACE_HANDLE handle
,
797 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
800 for (bytes_transferred
= 0;
801 bytes_transferred
< len
;
802 bytes_transferred
+= n
)
804 // Try to transfer as much of the remaining data as possible.
805 n
= ACE::recv_i (handle
,
806 static_cast <char *> (buf
) + bytes_transferred
,
807 len
- bytes_transferred
);
813 // Check for other errors.
816 // Check for possible blocking.
817 if (errno
== EWOULDBLOCK
)
819 // Wait for the blocking to subside.
820 int const result
= ACE::handle_read_ready (handle
, 0);
822 // Did select() succeed?
825 // Blocking subsided. Continue data transfer.
831 // Other data transfer or select() failures.
836 return static_cast<ssize_t
> (bytes_transferred
);
840 ACE::recv_n_i (ACE_HANDLE handle
,
843 const ACE_Time_Value
*timeout
,
847 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
853 ACE::record_and_set_non_blocking_mode (handle
, val
);
855 for (bytes_transferred
= 0;
856 bytes_transferred
< len
;
857 bytes_transferred
+= n
)
859 // Try to transfer as much of the remaining data as possible.
860 // Since the socket is in non-blocking mode, this call will not
862 n
= ACE::recv_i (handle
,
863 static_cast <char *> (buf
) + bytes_transferred
,
864 len
- bytes_transferred
);
870 // Check for possible blocking.
872 errno
== EWOULDBLOCK
)
874 // Wait upto <timeout> for the blocking to subside.
875 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
877 // Did select() succeed?
880 // Blocking subsided in <timeout> period. Continue
887 // Wait in select() timed out or other data transfer or
888 // select() failures.
895 ACE::restore_non_blocking_mode (handle
, val
);
900 return static_cast<ssize_t
> (bytes_transferred
);
903 // This is basically an interface to ACE_OS::readv, that doesn't use
904 // the struct iovec explicitly. The ... can be passed as an arbitrary
905 // number of (char *ptr, int len) tuples. However, the count N is the
906 // *total* number of trailing arguments, *not* a couple of the number
908 #if !defined (ACE_LACKS_VA_FUNCTIONS)
910 ACE::recv (ACE_HANDLE handle
, size_t n
, ...)
913 int const total_tuples
= static_cast<int> (n
/ 2);
915 #if defined (ACE_HAS_ALLOCA)
916 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
918 # ifdef ACE_HAS_ALLOC_HOOKS
919 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
920 ACE_Allocator::instance ()->malloc (total_tuples
*
924 ACE_NEW_RETURN (iovp
,
927 # endif /* ACE_HAS_ALLOC_HOOKS */
928 #endif /* !defined (ACE_HAS_ALLOCA) */
932 for (int i
= 0; i
< total_tuples
; i
++)
934 iovp
[i
].iov_base
= va_arg (argp
, char *);
935 iovp
[i
].iov_len
= va_arg (argp
, int);
938 ssize_t
const result
= ACE_OS::recvv (handle
, iovp
, total_tuples
);
939 #if !defined (ACE_HAS_ALLOCA)
940 # ifdef ACE_HAS_ALLOC_HOOKS
941 ACE_Allocator::instance ()->free (iovp
);
944 # endif /* ACE_HAS_ALLOC_HOOKS */
945 #endif /* !defined (ACE_HAS_ALLOCA) */
949 #endif /* ACE_LACKS_VA_FUNCTIONS */
953 ACE::recvv (ACE_HANDLE handle
,
956 const ACE_Time_Value
*timeout
)
959 return ACE_OS::recvv (handle
, iov
, iovcnt
);
963 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
967 ssize_t bytes_transferred
= ACE_OS::recvv (handle
, iov
, iovcnt
);
968 ACE::restore_non_blocking_mode (handle
, val
);
969 return bytes_transferred
;
975 ACE::recvv_n_i (ACE_HANDLE handle
,
981 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
982 bytes_transferred
= 0;
984 for (int s
= 0; s
< iovcnt
; )
986 // Try to transfer as much of the remaining data as possible.
987 ssize_t n
= ACE_OS::recvv (handle
, iov
+ s
, iovcnt
- s
);
992 // Check for other errors.
995 // Check for possible blocking.
996 if (errno
== EWOULDBLOCK
)
998 // Wait for the blocking to subside.
999 int const result
= ACE::handle_read_ready (handle
, 0);
1001 // Did select() succeed?
1004 // Blocking subsided. Continue data transfer.
1009 // Other data transfer or select() failures.
1013 for (bytes_transferred
+= n
;
1015 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1017 n
-= iov
[s
].iov_len
;
1021 char *base
= static_cast<char *> (iov
[s
].iov_base
);
1022 iov
[s
].iov_base
= base
+ n
;
1023 // This blind cast is safe because n < iov_len, after above loop.
1024 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1028 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1032 ACE::recvv_n_i (ACE_HANDLE handle
,
1035 const ACE_Time_Value
*timeout
,
1039 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1040 bytes_transferred
= 0;
1045 ACE::record_and_set_non_blocking_mode (handle
, val
);
1047 for (int s
= 0; s
< iovcnt
; )
1049 // Try to transfer as much of the remaining data as possible.
1050 // Since the socket is in non-blocking mode, this call will not
1052 ssize_t n
= ACE_OS::recvv (handle
, iov
+ s
, iovcnt
- s
);
1054 // Check for errors.
1055 if (n
== 0 || n
== -1)
1057 // Check for possible blocking.
1058 if (n
== -1 && errno
== EWOULDBLOCK
)
1060 // Wait upto <timeout> for the blocking to subside.
1061 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
1063 // Did select() succeed?
1066 // Blocking subsided in <timeout> period. Continue
1072 // Wait in select() timed out or other data transfer or
1073 // select() failures.
1079 for (bytes_transferred
+= n
;
1081 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1083 n
-= iov
[s
].iov_len
;
1087 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1088 iov
[s
].iov_base
= base
+ n
;
1089 // This blind cast is safe because n < iov_len, after above loop.
1090 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1094 ACE::restore_non_blocking_mode (handle
, val
);
1102 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1107 ACE::recv_n (ACE_HANDLE handle
,
1108 ACE_Message_Block
*message_block
,
1109 const ACE_Time_Value
*timeout
,
1113 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1114 bytes_transferred
= 0;
1116 iovec iov
[ACE_IOV_MAX
];
1119 while (message_block
!= 0)
1121 // Our current message block chain.
1122 const ACE_Message_Block
*current_message_block
= message_block
;
1124 while (current_message_block
!= 0)
1126 size_t current_message_block_length
=
1127 current_message_block
->length ();
1128 char *this_rd_ptr
= current_message_block
->rd_ptr ();
1130 // Check if this block has any space for incoming data.
1131 while (current_message_block_length
> 0)
1133 u_long
const this_chunk_length
=
1134 ACE_Utils::truncate_cast
<u_long
> (
1135 current_message_block_length
);
1137 // Collect the data in the iovec.
1138 iov
[iovcnt
].iov_base
= this_rd_ptr
;
1139 iov
[iovcnt
].iov_len
= this_chunk_length
;
1140 current_message_block_length
-= this_chunk_length
;
1141 this_rd_ptr
+= this_chunk_length
;
1143 // Increment iovec counter.
1146 // The buffer is full make a OS call. @@ TODO find a way to
1147 // find ACE_IOV_MAX for platforms that do not define it rather
1148 // than simply setting ACE_IOV_MAX to some arbitrary value such
1150 if (iovcnt
== ACE_IOV_MAX
)
1152 size_t current_transfer
= 0;
1154 ssize_t
const result
= ACE::recvv_n (handle
,
1160 // Add to total bytes transferred.
1161 bytes_transferred
+= current_transfer
;
1164 if (result
== -1 || result
== 0)
1167 // Reset iovec counter.
1172 // Select the next message block in the chain.
1173 current_message_block
= current_message_block
->cont ();
1176 // Selection of the next message block chain.
1177 message_block
= message_block
->next ();
1180 // Check for remaining buffers to be sent. This will happen when
1181 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1184 size_t current_transfer
= 0;
1186 ssize_t
const result
= ACE::recvv_n (handle
,
1192 // Add to total bytes transferred.
1193 bytes_transferred
+= current_transfer
;
1196 if (result
== -1 || result
== 0)
1202 // Return total bytes transferred.
1203 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1207 ACE::send (ACE_HANDLE handle
,
1211 const ACE_Time_Value
*timeout
)
1214 return ACE_OS::send (handle
, (const char *) buf
, n
, flags
);
1218 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1222 ssize_t
const bytes_transferred
=
1223 ACE_OS::send (handle
, (const char *) buf
, n
, flags
);
1224 ACE::restore_non_blocking_mode (handle
, val
);
1225 return bytes_transferred
;
1230 #if defined (ACE_HAS_TLI)
1233 ACE::t_snd (ACE_HANDLE handle
,
1237 const ACE_Time_Value
*timeout
)
1240 return ACE_OS::t_snd (handle
, (const char *) buf
, n
, flags
);
1244 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1248 ssize_t
const bytes_transferred
=
1249 ACE_OS::t_snd (handle
, (const char *) buf
, n
, flags
);
1250 ACE::restore_non_blocking_mode (handle
, val
);
1251 return bytes_transferred
;
1256 #endif /* ACE_HAS_TLI */
1259 ACE::send (ACE_HANDLE handle
,
1262 const ACE_Time_Value
*timeout
)
1265 return ACE::send_i (handle
, buf
, n
);
1269 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1273 ssize_t
const bytes_transferred
= ACE::send_i (handle
, buf
, n
);
1274 ACE::restore_non_blocking_mode (handle
, val
);
1275 return bytes_transferred
;
1281 ACE::sendmsg (ACE_HANDLE handle
,
1282 const struct msghdr
*msg
,
1284 const ACE_Time_Value
*timeout
)
1287 return ACE_OS::sendmsg (handle
, msg
, flags
);
1291 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1295 ssize_t
const bytes_transferred
=
1296 ACE_OS::sendmsg (handle
, msg
, flags
);
1297 ACE::restore_non_blocking_mode (handle
, val
);
1298 return bytes_transferred
;
1304 ACE::sendto (ACE_HANDLE handle
,
1308 const struct sockaddr
*addr
,
1310 const ACE_Time_Value
*timeout
)
1313 return ACE_OS::sendto (handle
, buf
, len
, flags
, addr
, addrlen
);
1317 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1321 ssize_t
const bytes_transferred
=
1322 ACE_OS::sendto (handle
, buf
, len
, flags
, addr
, addrlen
);
1323 ACE::restore_non_blocking_mode (handle
, val
);
1324 return bytes_transferred
;
1330 ACE::send_n_i (ACE_HANDLE handle
,
1337 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1340 for (bytes_transferred
= 0;
1341 bytes_transferred
< len
;
1342 bytes_transferred
+= n
)
1344 // Try to transfer as much of the remaining data as possible.
1345 n
= ACE_OS::send (handle
,
1346 (char *) buf
+ bytes_transferred
,
1347 len
- bytes_transferred
,
1353 // Check for other errors.
1356 // Check for possible blocking.
1357 #if defined (ACE_WIN32)
1358 if (errno
== EWOULDBLOCK
) // If enobufs no need to loop
1360 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1361 #endif /* ACE_WIN32 */
1363 // Wait for the blocking to subside.
1364 int const result
= ACE::handle_write_ready (handle
, 0);
1366 // Did select() succeed?
1369 // Blocking subsided. Continue data transfer.
1375 // Other data transfer or select() failures.
1380 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1384 ACE::send_n_i (ACE_HANDLE handle
,
1388 const ACE_Time_Value
*timeout
,
1392 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1398 ACE::record_and_set_non_blocking_mode (handle
, val
);
1400 for (bytes_transferred
= 0;
1401 bytes_transferred
< len
;
1402 bytes_transferred
+= n
)
1404 // Try to transfer as much of the remaining data as possible.
1405 // Since the socket is in non-blocking mode, this call will not
1407 n
= ACE_OS::send (handle
,
1408 (char *) buf
+ bytes_transferred
,
1409 len
- bytes_transferred
,
1412 // Check for errors.
1416 // Check for possible blocking.
1417 if (n
== -1 && (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1419 // Wait upto <timeout> for the blocking to subside.
1420 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1422 // Did select() succeed?
1425 // Blocking subsided in <timeout> period. Continue
1432 // Wait in select() timed out or other data transfer or
1433 // select() failures.
1440 ACE::restore_non_blocking_mode (handle
, val
);
1448 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1452 #if defined (ACE_HAS_TLI)
1455 ACE::t_snd_n_i (ACE_HANDLE handle
,
1462 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1465 for (bytes_transferred
= 0;
1466 bytes_transferred
< len
;
1467 bytes_transferred
+= n
)
1469 // Try to transfer as much of the remaining data as possible.
1470 n
= ACE_OS::t_snd (handle
,
1471 (char *) buf
+ bytes_transferred
,
1472 len
- bytes_transferred
,
1478 // Check for other errors.
1481 // Check for possible blocking.
1482 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1484 // Wait for the blocking to subside.
1485 int const result
= ACE::handle_write_ready (handle
, 0);
1487 // Did select() succeed?
1490 // Blocking subsided. Continue data transfer.
1496 // Other data transfer or select() failures.
1501 return bytes_transferred
;
1505 ACE::t_snd_n_i (ACE_HANDLE handle
,
1509 const ACE_Time_Value
*timeout
,
1513 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1519 ACE::record_and_set_non_blocking_mode (handle
, val
);
1521 for (bytes_transferred
= 0;
1522 bytes_transferred
< len
;
1523 bytes_transferred
+= n
)
1525 // Try to transfer as much of the remaining data as possible.
1526 // Since the socket is in non-blocking mode, this call will not
1528 n
= ACE_OS::t_snd (handle
,
1529 (char *) buf
+ bytes_transferred
,
1530 len
- bytes_transferred
,
1533 // Check for errors.
1537 // Check for possible blocking.
1539 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1541 // Wait upto <timeout> for the blocking to subside.
1542 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1544 // Did select() succeed?
1547 // Blocking subsided in <timeout> period. Continue
1554 // Wait in select() timed out or other data transfer or
1555 // select() failures.
1562 ACE::restore_non_blocking_mode (handle
, val
);
1567 return bytes_transferred
;
1570 #endif /* ACE_HAS_TLI */
1573 ACE::send_n_i (ACE_HANDLE handle
,
1579 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1582 for (bytes_transferred
= 0;
1583 bytes_transferred
< len
;
1584 bytes_transferred
+= n
)
1586 // Try to transfer as much of the remaining data as possible.
1587 n
= ACE::send_i (handle
,
1588 (char *) buf
+ bytes_transferred
,
1589 len
- bytes_transferred
);
1596 // Check for other errors.
1599 // Check for possible blocking.
1600 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1602 // Wait for the blocking to subside.
1603 int const result
= ACE::handle_write_ready (handle
, 0);
1605 // Did select() succeed?
1608 // Blocking subsided. Continue data transfer.
1614 // Other data transfer or select() failures.
1619 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1623 ACE::send_n_i (ACE_HANDLE handle
,
1626 const ACE_Time_Value
*timeout
,
1630 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1636 ACE::record_and_set_non_blocking_mode (handle
, val
);
1638 for (bytes_transferred
= 0;
1639 bytes_transferred
< len
;
1640 bytes_transferred
+= n
)
1642 // Try to transfer as much of the remaining data as possible.
1643 // Since the socket is in non-blocking mode, this call will not
1645 n
= ACE::send_i (handle
,
1646 (char *) buf
+ bytes_transferred
,
1647 len
- bytes_transferred
);
1649 // Check for errors.
1653 // Check for possible blocking.
1655 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1657 // Wait upto <timeout> for the blocking to subside.
1658 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1660 // Did select() succeed?
1663 // Blocking subsided in <timeout> period. Continue
1670 // Wait in select() timed out or other data transfer or
1671 // select() failures.
1678 ACE::restore_non_blocking_mode (handle
, val
);
1686 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1690 // Send N char *ptrs and int lengths. Note that the char *'s precede
1691 // the ints (basically, an varargs version of writev). The count N is
1692 // the *total* number of trailing arguments, *not* a couple of the
1693 // number of tuple pairs!
1694 #if !defined (ACE_LACKS_VA_FUNCTIONS)
1696 ACE::send (ACE_HANDLE handle
, size_t n
, ...)
1699 int total_tuples
= static_cast<int> (n
/ 2);
1701 #if defined (ACE_HAS_ALLOCA)
1702 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
1704 # ifdef ACE_HAS_ALLOC_HOOKS
1705 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
1706 ACE_Allocator::instance ()->malloc (total_tuples
*
1710 ACE_NEW_RETURN (iovp
,
1711 iovec
[total_tuples
],
1713 # endif /* ACE_HAS_ALLOC_HOOKS */
1714 #endif /* !defined (ACE_HAS_ALLOCA) */
1718 for (int i
= 0; i
< total_tuples
; i
++)
1720 iovp
[i
].iov_base
= va_arg (argp
, char *);
1721 iovp
[i
].iov_len
= va_arg (argp
, int);
1724 ssize_t result
= ACE_OS::sendv (handle
, iovp
, total_tuples
);
1725 #if !defined (ACE_HAS_ALLOCA)
1726 # ifdef ACE_HAS_ALLOC_HOOKS
1727 ACE_Allocator::instance ()->free (iovp
);
1730 # endif /* ACE_HAS_ALLOC_HOOKS */
1731 #endif /* !defined (ACE_HAS_ALLOCA) */
1735 #endif /* ACE_LACKS_VA_FUNCTIONS */
1738 ACE::sendv (ACE_HANDLE handle
,
1741 const ACE_Time_Value
*timeout
)
1744 return ACE_OS::sendv (handle
, iov
, iovcnt
);
1748 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1752 ssize_t bytes_transferred
= ACE_OS::sendv (handle
, iov
, iovcnt
);
1753 ACE::restore_non_blocking_mode (handle
, val
);
1754 return bytes_transferred
;
1760 ACE::sendv_n_i (ACE_HANDLE handle
,
1766 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1767 bytes_transferred
= 0;
1769 iovec
*iov
= const_cast<iovec
*> (i
);
1775 // Try to transfer as much of the remaining data as possible.
1776 ssize_t n
= ACE_OS::sendv (handle
, iov
+ s
, iovcnt
- s
);
1782 // Check for other errors.
1785 // Check for possible blocking.
1786 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1788 // Wait for the blocking to subside.
1789 int const result
= ACE::handle_write_ready (handle
, 0);
1791 // Did select() succeed?
1794 // Blocking subsided. Continue data transfer.
1799 // Other data transfer or select() failures.
1803 for (bytes_transferred
+= n
;
1805 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1807 n
-= iov
[s
].iov_len
;
1811 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1812 iov
[s
].iov_base
= base
+ n
;
1813 // This blind cast is safe because n < iov_len, after above loop.
1814 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1818 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1822 ACE::sendv_n_i (ACE_HANDLE handle
,
1825 const ACE_Time_Value
*timeout
,
1829 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1830 bytes_transferred
= 0;
1835 ACE::record_and_set_non_blocking_mode (handle
, val
);
1837 iovec
*iov
= const_cast<iovec
*> (i
);
1843 // Try to transfer as much of the remaining data as possible.
1844 // Since the socket is in non-blocking mode, this call will not
1846 ssize_t n
= ACE_OS::sendv (handle
, iov
+ s
, iovcnt
- s
);
1848 // Check for errors.
1852 // Check for possible blocking.
1854 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1856 // Wait upto <timeout> for the blocking to subside.
1857 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1859 // Did select() succeed?
1862 // Blocking subsided in <timeout> period. Continue
1868 // Wait in select() timed out or other data transfer or
1869 // select() failures.
1875 for (bytes_transferred
+= n
;
1877 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1879 n
-= iov
[s
].iov_len
;
1883 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1884 iov
[s
].iov_base
= base
+ n
;
1885 // This blind cast is safe because n < iov_len, after above loop.
1886 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1890 ACE::restore_non_blocking_mode (handle
, val
);
1898 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1903 ACE::write_n (ACE_HANDLE handle
,
1904 const ACE_Message_Block
*message_block
,
1908 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1909 bytes_transferred
= 0;
1911 iovec iov
[ACE_IOV_MAX
];
1914 while (message_block
!= 0)
1916 // Our current message block chain.
1917 const ACE_Message_Block
*current_message_block
= message_block
;
1919 while (current_message_block
!= 0)
1921 size_t current_message_block_length
=
1922 current_message_block
->length ();
1923 char *this_block_ptr
= current_message_block
->rd_ptr ();
1925 // Check if this block has any data to be sent.
1926 while (current_message_block_length
> 0)
1928 u_long
const this_chunk_length
=
1929 ACE_Utils::truncate_cast
<u_long
> (
1930 current_message_block_length
);
1932 // Collect the data in the iovec.
1933 iov
[iovcnt
].iov_base
= this_block_ptr
;
1934 iov
[iovcnt
].iov_len
= this_chunk_length
;
1935 current_message_block_length
-= this_chunk_length
;
1936 this_block_ptr
+= this_chunk_length
;
1938 // Increment iovec counter.
1941 // The buffer is full make a OS call. @@ TODO find a way to
1942 // find ACE_IOV_MAX for platforms that do not define it rather
1943 // than simply setting ACE_IOV_MAX to some arbitrary value such
1945 if (iovcnt
== ACE_IOV_MAX
)
1947 size_t current_transfer
= 0;
1949 ssize_t
const result
= ACE::writev_n (handle
,
1954 // Add to total bytes transferred.
1955 bytes_transferred
+= current_transfer
;
1958 if (result
== -1 || result
== 0)
1961 // Reset iovec counter.
1966 // Select the next message block in the chain.
1967 current_message_block
= current_message_block
->cont ();
1970 // Selection of the next message block chain.
1971 message_block
= message_block
->next ();
1974 // Check for remaining buffers to be sent. This will happen when
1975 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1978 size_t current_transfer
= 0;
1980 ssize_t
const result
= ACE::writev_n (handle
,
1985 // Add to total bytes transferred.
1986 bytes_transferred
+= current_transfer
;
1989 if (result
== -1 || result
== 0)
1993 // Return total bytes transferred.
1994 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1998 ACE::send_n (ACE_HANDLE handle
,
1999 const ACE_Message_Block
*message_block
,
2000 const ACE_Time_Value
*timeout
,
2004 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2005 bytes_transferred
= 0;
2007 iovec iov
[ACE_IOV_MAX
];
2010 while (message_block
!= 0)
2012 // Our current message block chain.
2013 const ACE_Message_Block
*current_message_block
= message_block
;
2015 while (current_message_block
!= 0)
2017 char *this_block_ptr
= current_message_block
->rd_ptr ();
2018 size_t current_message_block_length
=
2019 current_message_block
->length ();
2021 // Check if this block has any data to be sent.
2022 while (current_message_block_length
> 0)
2024 u_long
const this_chunk_length
=
2025 ACE_Utils::truncate_cast
<u_long
> (
2026 current_message_block_length
);
2028 // Collect the data in the iovec.
2029 iov
[iovcnt
].iov_base
= this_block_ptr
;
2030 iov
[iovcnt
].iov_len
= this_chunk_length
;
2031 current_message_block_length
-= this_chunk_length
;
2032 this_block_ptr
+= this_chunk_length
;
2034 // Increment iovec counter.
2037 // The buffer is full make a OS call. @@ TODO find a way to
2038 // find ACE_IOV_MAX for platforms that do not define it rather
2039 // than simply setting ACE_IOV_MAX to some arbitrary value such
2041 if (iovcnt
== ACE_IOV_MAX
)
2043 size_t current_transfer
= 0;
2045 ssize_t
const result
= ACE::sendv_n (handle
,
2051 // Add to total bytes transferred.
2052 bytes_transferred
+= current_transfer
;
2055 if (result
== -1 || result
== 0)
2058 // Reset iovec counter.
2063 // Select the next message block in the chain.
2064 current_message_block
= current_message_block
->cont ();
2067 // Selection of the next message block chain.
2068 message_block
= message_block
->next ();
2071 // Check for remaining buffers to be sent. This will happen when
2072 // ACE_IOV_MAX is not a multiple of the number of message blocks.
2075 size_t current_transfer
= 0;
2077 ssize_t
const result
= ACE::sendv_n (handle
,
2083 // Add to total bytes transferred.
2084 bytes_transferred
+= current_transfer
;
2087 if (result
== -1 || result
== 0)
2093 // Return total bytes transferred.
2094 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2098 ACE::readv_n (ACE_HANDLE handle
,
2104 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2105 bytes_transferred
= 0;
2111 ssize_t n
= ACE_OS::readv (handle
,
2115 if (n
== -1 || n
== 0)
2118 for (bytes_transferred
+= n
;
2120 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
2122 n
-= iov
[s
].iov_len
;
2126 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
2127 iov
[s
].iov_base
= base
+ n
;
2128 // This blind cast is safe because n < iov_len, after above loop.
2129 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
2133 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2137 ACE::writev_n (ACE_HANDLE handle
,
2143 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2144 bytes_transferred
= 0;
2146 iovec
*iov
= const_cast<iovec
*> (i
);
2152 ssize_t n
= ACE_OS::writev (handle
,
2156 if (n
== -1 || n
== 0)
2161 for (bytes_transferred
+= n
;
2163 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
2165 n
-= iov
[s
].iov_len
;
2169 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
2170 iov
[s
].iov_base
= base
+ n
;
2171 // This blind cast is safe because n < iov_len, after above loop.
2172 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
2176 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2180 ACE::handle_ready (ACE_HANDLE handle
,
2181 const ACE_Time_Value
*timeout
,
2184 bool exception_ready
)
2186 #if defined (ACE_HAS_POLL)
2187 ACE_UNUSED_ARG (exception_ready
);
2192 fds
.events
= read_ready
? POLLIN
: 0;
2196 fds
.events
|= POLLOUT
;
2201 int const result
= ACE_OS::poll (&fds
, 1, timeout
);
2203 ACE_Handle_Set handle_set
;
2204 handle_set
.set_bit (handle
);
2206 // Wait for data or for the timeout to elapse.
2207 int select_width
= 0;
2208 #if !defined (ACE_WIN32)
2209 select_width
= int (handle
) + 1;
2210 # endif /* ACE_WIN64 */
2211 int result
= ACE_OS::select (select_width
,
2212 read_ready
? handle_set
.fdset () : 0, // read_fds.
2213 write_ready
? handle_set
.fdset () : 0, // write_fds.
2214 exception_ready
? handle_set
.fdset () : 0, // exception_fds.
2217 #endif /* ACE_HAS_POLL */
2220 case 0: // Timer expired.
2223 case -1: // we got here directly - select() returned -1.
2225 case 1: // Handle has data.
2227 default: // default is case result > 0; return a
2228 // ACE_ASSERT (result == 1);
2234 ACE::enter_recv_timedwait (ACE_HANDLE handle
,
2235 const ACE_Time_Value
*timeout
,
2238 int const result
= ACE::handle_read_ready (handle
, timeout
);
2243 ACE::record_and_set_non_blocking_mode (handle
, val
);
2249 ACE::enter_send_timedwait (ACE_HANDLE handle
,
2250 const ACE_Time_Value
*timeout
,
2253 int const result
= ACE::handle_write_ready (handle
, timeout
);
2258 ACE::record_and_set_non_blocking_mode (handle
, val
);
2264 ACE::record_and_set_non_blocking_mode (ACE_HANDLE handle
, int &val
)
2266 // We need to record whether we are already *in* nonblocking mode,
2267 // so that we can correctly reset the state when we're done.
2268 val
= ACE::get_flags (handle
);
2270 if (ACE_BIT_DISABLED (val
, ACE_NONBLOCK
))
2271 // Set the handle into non-blocking mode if it's not already in
2273 ACE::set_flags (handle
, ACE_NONBLOCK
);
2277 ACE::restore_non_blocking_mode (ACE_HANDLE handle
, int val
)
2279 if (ACE_BIT_DISABLED (val
, ACE_NONBLOCK
))
2281 // Save/restore errno.
2282 ACE_Errno_Guard
error (errno
);
2283 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
2285 ACE::clr_flags (handle
, ACE_NONBLOCK
);
2289 /// Format buffer into printable format. This is useful for debugging.
2290 /// Portions taken from mdump by J.P. Knight (J.P.Knight@lut.ac.uk)
2291 /// Modifications by Todd Montgomery.
2293 ACE::format_hexdump (const char *buffer
,
2298 ACE_TRACE ("ACE::format_hexdump");
2301 ACE_TCHAR textver
[16 + 1];
2303 // We can fit 16 bytes output in text mode per line, 4 chars per byte.
2304 size_t maxlen
= (obuf_sz
/ 68) * 16;
2305 const ACE_TCHAR
*const obuf_start
= obuf
;
2312 size_t const lines
= size
/ 16;
2313 for (i
= 0; i
< lines
; i
++)
2317 for (j
= 0 ; j
< 16; j
++)
2319 c
= (u_char
) buffer
[(i
<< 4) + j
]; // or, buffer[i*16+j]
2320 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2326 *obuf
++ = ACE_TEXT (' ');
2328 textver
[j
] = ACE_OS::ace_isprint (c
) ? c
: u_char ('.');
2333 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2334 ACE_TEXT (" %") ACE_TEXT_PRIs
ACE_TEXT ("\n"),
2337 while (*obuf
!= '\0')
2343 for (i
= 0 ; i
< size
% 16; i
++)
2345 c
= (u_char
) buffer
[size
- size
% 16 + i
];
2346 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2352 *obuf
++ = ACE_TEXT (' ');
2354 textver
[i
] = ACE_OS::ace_isprint (c
) ? c
: u_char ('.');
2357 for (i
= size
% 16; i
< 16; i
++)
2359 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2364 *obuf
++ = ACE_TEXT (' ');
2370 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2371 ACE_TEXT (" %") ACE_TEXT_PRIs
ACE_TEXT ("\n"),
2377 /// Returns the current timestamp in the form
2378 /// "hour:minute:second:microsecond." The month, day, and year are
2379 /// also stored in the beginning of the date_and_time array
2380 /// using ISO-8601 format.
2382 ACE::timestamp (ACE_TCHAR date_and_time
[],
2383 size_t date_and_timelen
,
2384 bool return_pointer_to_first_digit
)
2386 return ACE::timestamp (ACE_Time_Value::zero
,
2389 return_pointer_to_first_digit
);
2392 /// Returns the given timestamp in the form
2393 /// "hour:minute:second:microsecond." The month, day, and year are
2394 /// also stored in the beginning of the date_and_time array
2395 /// using ISO-8601 format.
2396 /// 012345678901234567890123456
2397 /// 2010-12-02 12:56:00.123456<nul>
2399 ACE::timestamp (const ACE_Time_Value
& time_value
,
2400 ACE_TCHAR date_and_time
[],
2401 size_t date_and_timelen
,
2402 bool return_pointer_to_first_digit
)
2404 //ACE_TRACE ("ACE::timestamp");
2406 // This magic number is from the formatting statement
2407 // farther down this routine.
2408 if (date_and_timelen
< 27)
2414 ACE_Time_Value cur_time
=
2415 (time_value
== ACE_Time_Value::zero
) ?
2416 ACE_Time_Value (ACE_OS::gettimeofday ()) : time_value
;
2417 time_t secs
= cur_time
.sec ();
2419 ACE_OS::localtime_r (&secs
, &tms
);
2420 ACE_OS::snprintf (date_and_time
,
2422 ACE_TEXT ("%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d.%06ld"),
2429 static_cast<long> (cur_time
.usec()));
2430 date_and_time
[date_and_timelen
- 1] = '\0';
2431 return &date_and_time
[10 + (return_pointer_to_first_digit
!= 0)];
2434 /// This function rounds the request to a multiple of the page size.
2436 ACE::round_to_pagesize (size_t len
)
2438 ACE_TRACE ("ACE::round_to_pagesize");
2440 if (ACE::pagesize_
== 0)
2441 ACE::pagesize_
= ACE_OS::getpagesize ();
2443 return (len
+ (ACE::pagesize_
- 1)) & ~(ACE::pagesize_
- 1);
2447 ACE::round_to_allocation_granularity (size_t len
)
2449 ACE_TRACE ("ACE::round_to_allocation_granularity");
2451 if (ACE::allocation_granularity_
== 0)
2452 ACE::allocation_granularity_
= ACE_OS::allocation_granularity ();
2454 return (len
+ (ACE::allocation_granularity_
- 1)) & ~(ACE::allocation_granularity_
- 1);
2458 ACE::handle_timed_complete (ACE_HANDLE h
,
2459 const ACE_Time_Value
*timeout
,
2462 ACE_TRACE ("ACE::handle_timed_complete");
2464 #if !defined (ACE_WIN32) && defined (ACE_HAS_POLL)
2469 fds
.events
= POLLIN
| POLLOUT
;
2473 ACE_Handle_Set rd_handles
;
2474 ACE_Handle_Set wr_handles
;
2475 rd_handles
.set_bit (h
);
2476 wr_handles
.set_bit (h
);
2477 #endif /* !ACE_WIN32 && ACE_HAS_POLL */
2479 #if defined (ACE_WIN32)
2480 // Winsock is different - it sets the exception bit for failed connect,
2481 // unlike other platforms, where the write bit is set for both success
2483 ACE_Handle_Set ex_handles
;
2484 ex_handles
.set_bit (h
);
2485 #endif /* ACE_WIN32 */
2487 bool need_to_check
= false;
2488 bool known_failure
= false;
2490 #if defined (ACE_WIN32)
2491 int n
= ACE_OS::select (0, // Ignored on Windows: int (h) + 1,
2497 # if defined (ACE_HAS_POLL)
2499 int n
= ACE_OS::poll (&fds
, 1, timeout
);
2504 n
= ACE_OS::select (int (h
) + 1,
2510 n
= ACE_OS::select (int (h
) + 1,
2515 # endif /* ACE_HAS_POLL */
2516 #endif /* ACE_WIN32 */
2518 // If we failed to connect within the time period allocated by the
2519 // caller, then we fail (e.g., the remote host might have been too
2520 // busy to accept our call).
2523 if (n
== 0 && timeout
!= 0)
2525 return ACE_INVALID_HANDLE
;
2528 // On Windows, a ready-for-write handle is successfully connected, and
2529 // ready-for-exception is a failure. On fails, we need to grab the error
2530 // code via getsockopt.
2531 // On BSD sockets using select(), the handle becomes writable on
2532 // completion either success or fail, so if the select() does not time
2533 // out, we need to check for success/fail.
2534 // It is believed that TLI sockets use the readable=fail, writeable=success
2535 // but that hasn't been as well tested.
2536 #if defined (ACE_WIN32)
2537 ACE_UNUSED_ARG (is_tli
);
2539 // On Win32, ex_handle set indicates a failure. We'll do the check
2540 // to try and get an errno value, but the connect failed regardless of
2541 // what getsockopt says about the error.
2542 if (ex_handles
.is_set (h
))
2544 need_to_check
= true;
2545 known_failure
= true;
2549 # if defined (ACE_HAS_POLL)
2550 need_to_check
= (fds
.revents
& POLLIN
) && !(fds
.revents
& POLLOUT
);
2552 need_to_check
= rd_handles
.is_set (h
) && !wr_handles
.is_set (h
);
2553 # endif /* ACE_HAS_POLL */
2556 # if defined (ACE_HAS_POLL)
2558 // The "official" bit for failed connect is POLLIN. However, POLLERR
2559 // is often set and there are occasional cases seen with some kernels
2560 // where only POLLERR is set on a failed connect.
2561 need_to_check
= (fds
.revents
& POLLIN
) || (fds
.revents
& POLLERR
);
2562 known_failure
= (fds
.revents
& POLLERR
);
2565 need_to_check
= true;
2566 # endif /* ACE_HAS_POLL */
2567 #endif /* ACE_WIN32 */
2571 #if defined (SOL_SOCKET) && defined (SO_ERROR)
2573 int sock_err_len
= sizeof (sock_err
);
2574 int sockopt_ret
= ACE_OS::getsockopt (h
, SOL_SOCKET
, SO_ERROR
,
2575 (char *)&sock_err
, &sock_err_len
);
2576 if (sockopt_ret
< 0)
2578 h
= ACE_INVALID_HANDLE
;
2581 if (sock_err
!= 0 || known_failure
)
2583 h
= ACE_INVALID_HANDLE
;
2589 // The following recv() won't block provided that the
2590 // ACE_NONBLOCK flag has not been turned off .
2591 n
= ACE::recv (h
, &dummy
, 1, MSG_PEEK
);
2593 // If no data was read/peeked at, check to see if it's because
2594 // of a non-connected socket (and therefore an error) or there's
2595 // just no data yet.
2600 errno
= ECONNREFUSED
;
2601 h
= ACE_INVALID_HANDLE
;
2603 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
2604 h
= ACE_INVALID_HANDLE
;
2609 // 1. The HANDLE is ready for writing and doesn't need to be checked or
2610 // 2. recv() returned an indication of the state of the socket - if there is
2611 // either data present, or a recv is legit but there's no data yet,
2612 // the connection was successfully established.
2616 /// Wait up to @a timeout amount of time to accept a connection.
2618 ACE::handle_timed_accept (ACE_HANDLE listener
,
2619 ACE_Time_Value
*timeout
,
2622 ACE_TRACE ("ACE::handle_timed_accept");
2623 // Make sure we don't bomb out on erroneous values.
2624 if (listener
== ACE_INVALID_HANDLE
)
2627 #if defined (ACE_HAS_POLL)
2632 fds
.events
= POLLIN
;
2636 // Use the select() implementation rather than poll().
2637 ACE_Handle_Set rd_handle
;
2638 rd_handle
.set_bit (listener
);
2639 #endif /* ACE_HAS_POLL */
2641 // We need a loop here if <restart> is enabled.
2645 #if defined (ACE_HAS_POLL)
2647 int n
= ACE_OS::poll (&fds
, 1, timeout
);
2650 int select_width
= 0;
2651 # if !defined (ACE_WIN32)
2652 select_width
= int (listener
) + 1;
2653 # endif /* ACE_WIN32 */
2654 int n
= ACE_OS::select (select_width
,
2657 #endif /* ACE_HAS_POLL */
2662 if (errno
== EINTR
&& restart
)
2668 if (timeout
!= 0 && *timeout
== ACE_Time_Value::zero
)
2669 errno
= EWOULDBLOCK
;
2685 /// Make the current process a UNIX daemon. This is based on Stevens
2688 ACE::daemonize (const ACE_TCHAR pathname
[],
2689 bool close_all_handles
,
2690 const ACE_TCHAR program_name
[])
2692 ACE_TRACE ("ACE::daemonize");
2693 #if !defined (ACE_LACKS_FORK)
2694 pid_t pid
= ACE_OS::fork ();
2699 ACE_OS::exit (0); // Parent exits.
2701 // 1st child continues.
2702 ACE_OS::setsid (); // Become session leader.
2704 ACE_OS::signal (SIGHUP
, SIG_IGN
);
2706 pid
= ACE_OS::fork (program_name
);
2709 ACE_OS::exit (0); // First child terminates.
2711 // Second child continues.
2714 // change working directory.
2715 ACE_OS::chdir (pathname
);
2717 ACE_OS::umask (0); // clear our file mode creation mask.
2719 // Close down the I/O handles.
2720 if (close_all_handles
)
2722 for (int i
= ACE::max_handles () - 1; i
>= 0; i
--)
2725 int fd
= ACE_OS::open ("/dev/null", O_RDWR
, 0);
2728 ACE_OS::dup2 (fd
, ACE_STDIN
);
2729 ACE_OS::dup2 (fd
, ACE_STDOUT
);
2730 ACE_OS::dup2 (fd
, ACE_STDERR
);
2732 if (fd
> ACE_STDERR
)
2739 ACE_UNUSED_ARG (pathname
);
2740 ACE_UNUSED_ARG (close_all_handles
);
2741 ACE_UNUSED_ARG (program_name
);
2743 ACE_NOTSUP_RETURN (-1);
2744 #endif /* ACE_LACKS_FORK */
2748 ACE::fork (const ACE_TCHAR
*program_name
,
2751 if (avoid_zombies
== 0)
2752 return ACE_OS::fork (program_name
);
2755 // This algorithm is adapted from an example in the Stevens book
2756 // "Advanced Programming in the Unix Environment" and an item in
2757 // Andrew Gierth's Unix Programming FAQ. It creates an orphan
2758 // process that's inherited by the init process; init cleans up
2759 // when the orphan process terminates.
2761 // Another way to avoid zombies is to ignore or catch the
2762 // SIGCHLD signal; we don't use that approach here.
2764 pid_t pid
= ACE_OS::fork ();
2767 // The child process forks again to create a grandchild.
2768 switch (ACE_OS::fork (program_name
))
2770 case 0: // grandchild returns 0.
2772 case static_cast<pid_t
>(-1): // assumes all errnos are < 256
2773 ACE_OS::_exit (errno
);
2774 ACE_FALLTHROUGH
; // gcc sees this as a fallthrough
2775 default: // child terminates, orphaning grandchild
2780 // Parent process waits for child to terminate.
2781 ACE_exitcode status
;
2782 if (pid
< 0 || ACE_OS::waitpid (pid
, &status
, 0) < 0)
2785 // child terminated by calling exit()?
2786 if (WIFEXITED ((status
)))
2788 // child terminated normally?
2789 if (WEXITSTATUS ((status
)) == 0)
2792 errno
= WEXITSTATUS ((status
));
2795 // child didn't call exit(); perhaps it received a signal?
2805 ACE_TRACE ("ACE::max_handles");
2806 #if defined (RLIMIT_NOFILE) && !defined (ACE_LACKS_RLIMIT)
2808 int const r
= ACE_OS::getrlimit (RLIMIT_NOFILE
, &rl
);
2809 # if !defined (RLIM_INFINITY)
2813 if (r
== 0 && rl
.rlim_cur
!= RLIM_INFINITY
)
2815 // If == RLIM_INFINITY, fall through to the ACE_LACKS_RLIMIT sections
2816 # endif /* RLIM_INFINITY */
2817 #endif /* RLIMIT_NOFILE && !ACE_LACKS_RLIMIT */
2819 #if defined (_SC_OPEN_MAX) && !defined (ACE_LACKS_SYSCONF)
2820 return static_cast<int> (ACE_OS::sysconf (_SC_OPEN_MAX
));
2821 #elif defined (FD_SETSIZE)
2824 ACE_NOTSUP_RETURN (-1);
2825 #endif /* _SC_OPEN_MAX */
2828 // Set the number of currently open handles in the process.
2830 // If NEW_LIMIT == -1 set the limit to the maximum allowable.
2831 // Otherwise, set it to be the value of NEW_LIMIT.
2834 ACE::set_handle_limit (int new_limit
,
2835 int increase_limit_only
)
2837 ACE_TRACE ("ACE::set_handle_limit");
2838 int cur_limit
= ACE::max_handles ();
2839 int max_limit
= cur_limit
;
2841 if (cur_limit
== -1)
2844 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2847 ACE_OS::memset ((void *) &rl
, 0, sizeof rl
);
2848 int r
= ACE_OS::getrlimit (RLIMIT_NOFILE
, &rl
);
2850 max_limit
= rl
.rlim_max
;
2851 #endif /* ACE_LACKS_RLIMIT */
2853 if (new_limit
== -1)
2854 new_limit
= max_limit
;
2861 else if (new_limit
> cur_limit
)
2863 // Increase the limit.
2864 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2865 rl
.rlim_cur
= new_limit
;
2866 return ACE_OS::setrlimit (RLIMIT_NOFILE
, &rl
);
2867 #elif !defined (RLIMIT_NOFILE)
2870 // Must return EINVAL errno.
2871 ACE_NOTSUP_RETURN (-1);
2872 #endif /* ACE_LACKS_RLIMIT */
2874 else if (increase_limit_only
== 0)
2876 // Decrease the limit.
2877 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2878 rl
.rlim_cur
= new_limit
;
2879 return ACE_OS::setrlimit (RLIMIT_NOFILE
, &rl
);
2881 // We give a chance to platforms without RLIMIT to work.
2882 // Instead of ACE_NOTSUP_RETURN (0), just return 0 because
2883 // new_limit is <= cur_limit, so it's a no-op.
2885 #endif /* ACE_LACKS_RLIMIT */
2891 /// Euclid's greatest common divisor algorithm.
2893 ACE::gcd (u_long x
, u_long y
)
2905 /// Calculates the minimum enclosing frame size for the given values.
2907 ACE::minimum_frame_size (u_long period1
, u_long period2
)
2909 // if one of the periods is zero, treat it as though it as
2910 // uninitialized and return the other period as the frame size
2920 // if neither is zero, find the greatest common divisor of the two periods
2921 u_long greatest_common_divisor
= ACE::gcd (period1
, period2
);
2923 // explicitly consider cases to reduce risk of possible overflow errors
2924 if (greatest_common_divisor
== 1)
2926 // periods are relative primes: just multiply them together
2927 return period1
* period2
;
2929 else if (greatest_common_divisor
== period1
)
2931 // the first period divides the second: return the second
2934 else if (greatest_common_divisor
== period2
)
2936 // the second period divides the first: return the first
2941 // the current frame size and the entry's effective period
2942 // have a non-trivial greatest common divisor: return the
2943 // product of factors divided by those in their gcd.
2944 return (period1
* period2
) / greatest_common_divisor
;
2949 ACE::is_prime (const u_long n
,
2950 const u_long min_factor
,
2951 const u_long max_factor
)
2954 for (u_long factor
= min_factor
;
2955 factor
<= max_factor
;
2957 if (n
/ factor
* factor
== n
)
2964 ACE::sock_error (int error
)
2966 #if defined (ACE_WIN32)
2967 static ACE_TCHAR unknown_msg
[64];
2971 case WSAVERNOTSUPPORTED
:
2972 return ACE_TEXT ("version of WinSock not supported");
2974 case WSASYSNOTREADY
:
2975 return ACE_TEXT ("WinSock not present or not responding");
2978 return ACE_TEXT ("app version not supported by DLL");
2980 case WSAHOST_NOT_FOUND
:
2981 return ACE_TEXT ("Authoritive: Host not found");
2984 return ACE_TEXT ("Non-authoritive: host not found or server failure");
2986 case WSANO_RECOVERY
:
2987 return ACE_TEXT ("Non-recoverable: refused or not implemented");
2990 return ACE_TEXT ("Valid name, no data record for type");
2994 return "Valid name, no MX record";
2996 case WSANOTINITIALISED
:
2997 return ACE_TEXT ("WSA Startup not initialized");
3000 return ACE_TEXT ("Network subsystem failed");
3002 case WSAEINPROGRESS
:
3003 return ACE_TEXT ("Blocking operation in progress");
3006 return ACE_TEXT ("Blocking call cancelled");
3008 case WSAEAFNOSUPPORT
:
3009 return ACE_TEXT ("address family not supported");
3012 return ACE_TEXT ("no file handles available");
3015 return ACE_TEXT ("no buffer space available");
3017 case WSAEPROTONOSUPPORT
:
3018 return ACE_TEXT ("specified protocol not supported");
3021 return ACE_TEXT ("protocol wrong type for this socket");
3023 case WSAESOCKTNOSUPPORT
:
3024 return ACE_TEXT ("socket type not supported for address family");
3027 return ACE_TEXT ("handle is not a socket");
3029 case WSAEWOULDBLOCK
:
3030 return ACE_TEXT ("resource temporarily unavailable");
3033 return ACE_TEXT ("address already in use");
3035 case WSAECONNABORTED
:
3036 return ACE_TEXT ("connection aborted");
3039 return ACE_TEXT ("connection reset");
3042 return ACE_TEXT ("not connected");
3045 return ACE_TEXT ("connection timed out");
3047 case WSAECONNREFUSED
:
3048 return ACE_TEXT ("connection refused");
3051 return ACE_TEXT ("host down");
3053 case WSAEHOSTUNREACH
:
3054 return ACE_TEXT ("host unreachable");
3056 case WSAEADDRNOTAVAIL
:
3057 return ACE_TEXT ("address not available");
3060 return ACE_TEXT ("socket is already connected");
3063 return ACE_TEXT ("network dropped connection on reset");
3066 return ACE_TEXT ("message too long");
3068 case WSAENETUNREACH
:
3069 return ACE_TEXT ("network is unreachable");
3072 return ACE_TEXT ("bad address");
3075 return ACE_TEXT ("graceful shutdown in progress");
3078 return ACE_TEXT ("permission denied");
3081 return ACE_TEXT ("cannot send after socket shutdown");
3084 return ACE_TEXT ("too many processes");
3087 return ACE_TEXT ("operation already in progress");
3089 case WSAEPFNOSUPPORT
:
3090 return ACE_TEXT ("protocol family not supported");
3092 case WSAENOPROTOOPT
:
3093 return ACE_TEXT ("bad protocol option");
3095 case WSATYPE_NOT_FOUND
:
3096 return ACE_TEXT ("class type not found");
3099 return ACE_TEXT ("operation not supported");
3101 case WSAEDESTADDRREQ
:
3102 return ACE_TEXT ("destination address required");
3105 ACE_OS::snprintf (unknown_msg
,
3106 sizeof unknown_msg
/ sizeof unknown_msg
[0],
3107 ACE_TEXT ("unknown error: %d"), error
);
3112 ACE_UNUSED_ARG (error
);
3113 ACE_NOTSUP_RETURN (0);
3114 #endif /* ACE_WIN32 */
3118 ACE::is_sock_error (int error
)
3120 #if defined (ACE_WIN32)
3123 case WSAVERNOTSUPPORTED
:
3124 case WSASYSNOTREADY
:
3126 case WSAHOST_NOT_FOUND
:
3128 case WSANO_RECOVERY
:
3133 case WSANOTINITIALISED
:
3135 case WSAEINPROGRESS
:
3137 case WSAEAFNOSUPPORT
:
3140 case WSAEPROTONOSUPPORT
:
3142 case WSAESOCKTNOSUPPORT
:
3144 case WSAEWOULDBLOCK
:
3146 case WSAECONNABORTED
:
3150 case WSAECONNREFUSED
:
3152 case WSAEHOSTUNREACH
:
3153 case WSAEADDRNOTAVAIL
:
3157 case WSAENETUNREACH
:
3164 case WSAEPFNOSUPPORT
:
3165 case WSAENOPROTOOPT
:
3166 case WSATYPE_NOT_FOUND
:
3171 ACE_UNUSED_ARG (error
);
3172 #endif /* ACE_WIN32 */
3177 ACE::strndup (const char *str
, size_t n
)
3179 const char *t
= str
;
3182 // Figure out how long this string is (remember, it might not be
3186 len
< n
&& *t
++ != '\0';
3191 #if defined (ACE_HAS_ALLOC_HOOKS)
3192 ACE_ALLOCATOR_RETURN (s
,
3193 (char *) ACE_Allocator::instance()->malloc (len
+ 1),
3196 ACE_ALLOCATOR_RETURN (s
,
3197 (char *) ACE_OS::malloc (len
+ 1),
3199 #endif /* ACE_HAS_ALLOC_HOOKS */
3200 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3203 #if defined (ACE_HAS_WCHAR)
3205 ACE::strndup (const wchar_t *str
, size_t n
)
3207 const wchar_t *t
= str
;
3210 // Figure out how long this string is (remember, it might not be
3214 len
< n
&& *t
++ != '\0';
3218 size_t const size
= (len
+ 1) * sizeof (wchar_t);
3220 #if defined (ACE_HAS_ALLOC_HOOKS)
3221 ACE_ALLOCATOR_RETURN (s
,
3222 static_cast<wchar_t*> (
3223 ACE_Allocator::instance ()->malloc (size
)),
3226 ACE_ALLOCATOR_RETURN (s
,
3227 static_cast<wchar_t*> (ACE_OS::malloc (size
)),
3229 #endif /* ACE_HAS_ALLOC_HOOKS */
3230 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3232 #endif /* ACE_HAS_WCHAR */
3235 ACE::strnnew (const char *str
, size_t n
)
3237 const char *t
= str
;
3240 // Figure out how long this string is (remember, it might not be
3244 len
< n
&& *t
++ != L
'\0';
3250 #if defined (ACE_HAS_ALLOC_HOOKS)
3251 ACE_ALLOCATOR_RETURN (s
,
3252 static_cast<char*> (ACE_Allocator::instance ()->malloc (sizeof (char) * (len
+ 1))),
3258 #endif /* ACE_HAS_ALLOC_HOOKS */
3260 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3263 #if defined (ACE_HAS_WCHAR)
3265 ACE::strnnew (const wchar_t *str
, size_t n
)
3267 const wchar_t *t
= str
;
3270 // Figure out how long this string is (remember, it might not be
3274 len
< n
&& *t
++ != ACE_TEXT_WIDE ('\0');
3282 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3284 #endif /* ACE_HAS_WCHAR */
3287 ACE::strend (const char *s
)
3289 while (*s
++ != '\0')
3295 #if defined ACE_HAS_WCHAR
3297 ACE::strend (const wchar_t *s
)
3299 while (*s
++ != ACE_TEXT_WIDE ('\0'))
3307 ACE::strnew (const char *s
)
3312 #if defined (ACE_HAS_ALLOC_HOOKS)
3313 ACE_ALLOCATOR_RETURN (t
,
3314 static_cast<char*> (ACE_Allocator::instance ()->malloc (sizeof (char) * (ACE_OS::strlen (s
) + 1))),
3318 char [ACE_OS::strlen (s
) + 1],
3320 #endif /* ACE_HAS_ALLOC_HOOKS */
3322 return ACE_OS::strcpy (t
, s
);
3325 #if defined (ACE_HAS_WCHAR)
3327 ACE::strnew (const wchar_t *s
)
3332 size_t const n
= ACE_OS::strlen (s
) + 1;
3334 #if defined (ACE_HAS_ALLOC_HOOKS)
3335 ACE_ALLOCATOR_RETURN (t
,
3336 static_cast<wchar_t*> (
3337 ACE_Allocator::instance ()->malloc (
3338 sizeof (wchar_t) * (n
))),
3341 ACE_NEW_RETURN (t
, wchar_t[n
], 0);
3342 #endif /* ACE_HAS_ALLOC_HOOKS */
3344 return ACE_OS::strcpy (t
, s
);
3346 #endif /* ACE_HAS_WCHAR */
3348 // helper functions for ACE::wild_match()
3351 inline bool equal_char (char a
, char b
, bool case_sensitive
)
3355 return ACE_OS::ace_tolower (a
) == ACE_OS::ace_tolower (b
);
3358 // precond: *p == '[' start of char class
3359 // postcond: *p == ']' end of the char class
3360 inline bool equal_class (char s
, const char *&p
, bool case_sensitive
)
3363 bool negate
= false;
3369 // ] and - are regular in 1st position
3370 for (bool first
= true; *p
&& (first
|| *p
!= ']'); ++p
)
3372 if (!first
&& *p
== '-' && p
[1] != ']')
3374 if (!p
[1] || p
[1] <= p
[-1]) // invalid range
3378 // Since we are in the POSIX locale, only the basic ASCII
3379 // characters are allowed as the range endpoints. These characters
3380 // are the same values in both signed and unsigned chars so we
3381 // don't have to account for any "pathological cases."
3382 for (char range
= static_cast<char> (p
[-1] + 1); range
<= p
[1]; ++range
)
3384 if (equal_char (s
, range
, case_sensitive
))
3386 while (*++p
!= ']') {}
3390 ++p
; // consume the character 1 past the -
3392 else if (equal_char (s
, *p
, case_sensitive
))
3394 while (*++p
!= ']') {}
3404 ACE::wild_match(const char *str
, const char *pat
, bool case_sensitive
,
3405 bool character_classes
)
3409 if (pat
== 0 || str
== 0)
3412 bool star
= false, escape
= false;
3413 const char *s
= str
;
3414 const char *p
= pat
;
3417 if (!escape
&& *p
== '\\')
3422 else if (!escape
&& *p
== '*')
3426 while (*++pat
== '*') {}
3432 else if (!escape
&& *p
== '?')
3437 else if (!escape
&& character_classes
&& *p
== '[')
3439 if (equal_class (*s
, p
, case_sensitive
))
3451 else if (!equal_char (*s
, *p
, case_sensitive
))
3467 while (*++p
== '*') {}
3472 // Close versioned namespace, if enabled by the user.
3473 ACE_END_VERSIONED_NAMESPACE_DECL