3 #include "ace/Basic_Types.h"
4 #include "ace/Handle_Set.h"
5 #include "ace/SString.h"
6 #include "ace/Version.h"
7 #include "ace/Message_Block.h"
8 #include "ace/Log_Category.h"
9 #include "ace/Flag_Manip.h"
10 #include "ace/OS_NS_sys_select.h"
11 #include "ace/OS_NS_string.h"
12 #include "ace/OS_NS_strings.h"
13 #include "ace/OS_NS_signal.h"
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_sys_resource.h"
16 #include "ace/OS_NS_sys_wait.h"
17 #include "ace/OS_NS_sys_time.h"
18 #include "ace/OS_NS_time.h"
19 #include "ace/OS_NS_sys_uio.h"
20 #include "ace/OS_NS_sys_stat.h"
21 #include "ace/OS_NS_ctype.h"
22 #include "ace/OS_NS_fcntl.h"
23 #include "ace/OS_TLI.h"
24 #include "ace/Truncate.h"
26 #if !defined (__ACE_INLINE__)
27 #include "ace/ACE.inl"
28 #endif /* __ACE_INLINE__ */
30 #if defined (ACE_HAS_POLL)
31 # include "ace/OS_NS_poll.h"
32 #endif /* ACE_HAS_POLL */
34 // Open versioned namespace, if enabled by the user.
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
40 // Used internally so not exported.
42 // Size of allocation granularity.
43 size_t allocation_granularity_
= 0;
48 // Are we debugging ACE?
49 // Keeps track of whether we're in some global debug mode.
54 ACE::out_of_handles (int error
)
56 // EMFILE is common to all platforms.
57 if (error
== EMFILE
||
58 #if defined (ACE_WIN32)
59 // On Win32, we need to check for ENOBUFS also.
61 #elif defined (ACE_LINUX)
62 // On linux, we need to check for ENOENT also.
64 // For RedHat5.2, need to check for EINVAL too.
66 // Without threads check for EOPNOTSUPP
67 error
== EOPNOTSUPP
||
68 #elif defined (__FreeBSD__)
69 // On FreeBSD we need to check for EOPNOTSUPP (LinuxThreads) or
70 // ENOSYS (libc_r threads) also.
71 error
== EOPNOTSUPP
||
73 #elif defined (__OpenBSD__)
74 // OpenBSD appears to return EBADF.
76 #endif /* ACE_WIN32 */
86 return ACE_MAJOR_VERSION
;
92 return ACE_MINOR_VERSION
;
98 return ACE_MICRO_VERSION
;
102 ACE::micro_version ()
104 return ACE_MICRO_VERSION
;
108 ACE::compiler_name ()
113 return ACE_TEXT ("");
118 ACE::compiler_major_version ()
120 #ifdef ACE_CC_MAJOR_VERSION
121 return ACE_CC_MAJOR_VERSION
;
128 ACE::compiler_minor_version ()
130 #ifdef ACE_CC_MINOR_VERSION
131 return ACE_CC_MINOR_VERSION
;
138 ACE::compiler_beta_version ()
140 #ifdef ACE_CC_BETA_VERSION
141 return ACE_CC_BETA_VERSION
;
148 ACE::nibble2hex (u_int n
)
150 // Yes, this works for UNICODE
151 return ACE_TEXT ("0123456789abcdef")[n
& 0x0f];
157 //FUZZ: disable check_for_ace_log_categories
158 static const char *debug
= ACE_OS::getenv ("ACE_DEBUG");
159 //FUZZ: enable check_for_ace_log_categories
160 return (ACE::debug_
) ? ACE::debug_
: (debug
!= 0 ? (*debug
!= '0') : false);
164 ACE::debug (bool onoff
)
170 ACE::select (int width
,
171 ACE_Handle_Set
*readfds
,
172 ACE_Handle_Set
*writefds
,
173 ACE_Handle_Set
*exceptfds
,
174 const ACE_Time_Value
*timeout
)
176 int result
= ACE_OS::select (width
,
177 readfds
? readfds
->fdset () : 0,
178 writefds
? writefds
->fdset () : 0,
179 exceptfds
? exceptfds
->fdset () : 0,
183 # if !defined (ACE_WIN32)
184 // This isn't needed for Windows... it's a no-op anyway.
186 readfds
->sync ((ACE_HANDLE
) width
);
188 writefds
->sync ((ACE_HANDLE
) width
);
190 exceptfds
->sync ((ACE_HANDLE
) width
);
191 #endif /* ACE_WIN32 */
197 ACE::select (int width
,
198 ACE_Handle_Set
&readfds
,
199 const ACE_Time_Value
*timeout
)
201 int result
= ACE_OS::select (width
,
207 #if !defined (ACE_WIN32)
209 readfds
.sync ((ACE_HANDLE
) width
);
210 #endif /* ACE_WIN32 */
215 ACE::terminate_process (pid_t pid
)
217 #if defined (ACE_WIN32)
218 // Create a handle for the given process id.
219 ACE_HANDLE process_handle
=
220 ::OpenProcess (PROCESS_TERMINATE
,
221 FALSE
, // New handle is not inheritable.
224 if (process_handle
== ACE_INVALID_HANDLE
225 || process_handle
== 0)
229 // Kill the process associated with process_handle.
230 BOOL terminate_result
=
231 ::TerminateProcess (process_handle
, 0);
232 // Free up the kernel resources.
233 ACE_OS::close (process_handle
);
234 return terminate_result
? 0 : -1;
237 return ACE_OS::kill (pid
, 9);
238 #endif /* ACE_WIN32 */
242 ACE::process_active (pid_t pid
)
244 #if !defined(ACE_WIN32)
245 if (ACE_OS::kill (pid
, 0) == 0)
247 else if (errno
== ESRCH
)
252 // Create a handle for the given process id.
253 ACE_HANDLE process_handle
=
254 ::OpenProcess (PROCESS_QUERY_INFORMATION
, FALSE
, pid
);
255 if (process_handle
== ACE_INVALID_HANDLE
|| process_handle
== 0)
261 if (::GetExitCodeProcess (process_handle
,
263 || status
!= STILL_ACTIVE
)
266 ::CloseHandle (process_handle
);
269 #endif /* !ACE_WIN32 */
273 ACE::execname (const ACE_TCHAR
*old_name
)
275 #if defined (ACE_WIN32)
276 const ACE_TCHAR
*suffix
= ACE_OS::strrchr (old_name
, ACE_TEXT ('.'));
277 if (suffix
== 0 || ACE_OS::strcasecmp (suffix
, ACE_TEXT (".exe")) != 0)
279 ACE_TCHAR
*new_name
= 0;
282 ACE_OS::strlen (old_name
)
283 + ACE_OS::strlen (ACE_TEXT (".exe"))
286 ACE_NEW_RETURN (new_name
,
289 ACE_TCHAR
*end
= new_name
;
291 end
= ACE_OS::strecpy (new_name
, old_name
);
293 // Concatenate the .exe suffix onto the end of the executable.
294 // end points _after_ the terminating nul.
295 ACE_OS::strcpy (end
- 1, ACE_TEXT (".exe"));
299 #endif /* ACE_WIN32 */
304 ACE::hash_pjw (const char *str
, size_t len
)
308 for (size_t i
= 0; i
< len
; i
++)
310 const char temp
= str
[i
];
311 hash
= (hash
<< 4) + (temp
* 13);
313 u_long g
= hash
& 0xf0000000;
326 ACE::hash_pjw (const char *str
)
328 return ACE::hash_pjw (str
, ACE_OS::strlen (str
));
331 #if defined (ACE_HAS_WCHAR)
333 ACE::hash_pjw (const wchar_t *str
, size_t len
)
337 for (size_t i
= 0; i
< len
; i
++)
339 // @@ UNICODE: Does this function do the correct thing with wchar's?
341 const wchar_t temp
= str
[i
];
342 hash
= (hash
<< 4) + (temp
* 13);
344 u_long g
= hash
& 0xf0000000;
357 ACE::hash_pjw (const wchar_t *str
)
359 return ACE::hash_pjw (str
, ACE_OS::strlen (str
));
361 #endif /* ACE_HAS_WCHAR */
364 ACE::strenvdup (const ACE_TCHAR
*str
)
366 ACE_TRACE ("ACE::strenvdup");
368 return ACE_OS::strenvdup (str
);
376 ==================================================================
377 netsvc netsvc.dll libnetsvc.so
378 (PATH will be (LD_LIBRARY_PATH
379 evaluated) evaluated)
381 libnetsvc.dll libnetsvc.dll libnetsvc.dll + warning
382 netsvc.so netsvc.so + warning libnetsvc.so
384 ..\../libs/netsvc ..\..\libs\netsvc.dll ../../libs/netsvc.so
385 (absolute path used) (absolute path used)
390 ACE::basename (const ACE_TCHAR
*pathname
, ACE_TCHAR delim
)
392 ACE_TRACE ("ACE::basename");
393 const ACE_TCHAR
*temp
= ACE_OS::strrchr (pathname
, delim
);
402 ACE::dirname (const ACE_TCHAR
*pathname
, ACE_TCHAR delim
)
404 ACE_TRACE ("ACE::dirname");
405 static ACE_TCHAR return_dirname
[MAXPATHLEN
+ 1];
407 const ACE_TCHAR
*temp
= ACE_OS::strrchr (pathname
, delim
);
411 return_dirname
[0] = '.';
412 return_dirname
[1] = '\0';
414 return return_dirname
;
418 // When the len is truncated, there are problems! This should
419 // not happen in normal circomstances
420 size_t len
= temp
- pathname
+ 1;
421 if (len
> (sizeof return_dirname
/ sizeof (ACE_TCHAR
)))
422 len
= sizeof return_dirname
/ sizeof (ACE_TCHAR
);
424 ACE_OS::strsncpy (return_dirname
,
427 return return_dirname
;
432 ACE::recv (ACE_HANDLE handle
,
436 const ACE_Time_Value
*timeout
)
439 return ACE_OS::recv (handle
, (char *) buf
, len
, flags
);
443 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) ==-1)
447 ssize_t bytes_transferred
=
448 ACE_OS::recv (handle
, (char *) buf
, len
, flags
);
449 ACE::restore_non_blocking_mode (handle
, val
);
450 return bytes_transferred
;
455 #if defined (ACE_HAS_TLI)
458 ACE::t_rcv (ACE_HANDLE handle
,
462 const ACE_Time_Value
*timeout
)
465 return ACE_OS::t_rcv (handle
, (char *) buf
, len
, flags
);
469 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) ==-1)
473 ssize_t bytes_transferred
=
474 ACE_OS::t_rcv (handle
, (char *) buf
, len
, flags
);
475 ACE::restore_non_blocking_mode (handle
, val
);
476 return bytes_transferred
;
481 #endif /* ACE_HAS_TLI */
484 ACE::recv (ACE_HANDLE handle
,
487 const ACE_Time_Value
*timeout
)
490 return ACE::recv_i (handle
, buf
, n
);
494 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
498 ssize_t bytes_transferred
= ACE::recv_i (handle
, buf
, n
);
499 ACE::restore_non_blocking_mode (handle
, val
);
500 return bytes_transferred
;
506 ACE::recvmsg (ACE_HANDLE handle
,
509 const ACE_Time_Value
*timeout
)
512 return ACE_OS::recvmsg (handle
, msg
, flags
);
516 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
520 ssize_t bytes_transferred
= ACE_OS::recvmsg (handle
, msg
, flags
);
521 ACE::restore_non_blocking_mode (handle
, val
);
522 return bytes_transferred
;
528 ACE::recvfrom (ACE_HANDLE handle
,
532 struct sockaddr
*addr
,
534 const ACE_Time_Value
*timeout
)
537 return ACE_OS::recvfrom (handle
, buf
, len
, flags
, addr
, addrlen
);
541 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
545 ssize_t bytes_transferred
=
546 ACE_OS::recvfrom (handle
, buf
, len
, flags
, addr
, addrlen
);
547 ACE::restore_non_blocking_mode (handle
, val
);
548 return bytes_transferred
;
554 ACE::recv_n_i (ACE_HANDLE handle
,
561 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
564 for (bytes_transferred
= 0;
565 bytes_transferred
< len
;
566 bytes_transferred
+= n
)
568 // Try to transfer as much of the remaining data as possible.
569 n
= ACE_OS::recv (handle
,
570 static_cast <char *> (buf
) + bytes_transferred
,
571 len
- bytes_transferred
,
577 // Check for other errors.
580 // Check for possible blocking.
581 if (errno
== EWOULDBLOCK
)
583 // Wait for the blocking to subside.
584 int const result
= ACE::handle_read_ready (handle
, 0);
586 // Did select() succeed?
589 // Blocking subsided. Continue data transfer.
595 // Other data transfer or select() failures.
600 return static_cast<ssize_t
> (bytes_transferred
);
604 ACE::recv_n_i (ACE_HANDLE handle
,
608 const ACE_Time_Value
*timeout
,
612 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
618 ACE::record_and_set_non_blocking_mode (handle
, val
);
620 for (bytes_transferred
= 0;
621 bytes_transferred
< len
;
622 bytes_transferred
+= n
)
624 // Try to transfer as much of the remaining data as possible.
625 // Since the socket is in non-blocking mode, this call will not
627 n
= ACE_OS::recv (handle
,
628 static_cast <char *> (buf
) + bytes_transferred
,
629 len
- bytes_transferred
,
636 // Check for possible blocking.
638 errno
== EWOULDBLOCK
)
640 // Wait upto <timeout> for the blocking to subside.
641 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
643 // Did select() succeed?
646 // Blocking subsided in <timeout> period. Continue
653 // Wait in select() timed out or other data transfer or
654 // select() failures.
661 ACE::restore_non_blocking_mode (handle
, val
);
666 return static_cast<ssize_t
> (bytes_transferred
);
669 #if defined (ACE_HAS_TLI)
672 ACE::t_rcv_n_i (ACE_HANDLE handle
,
679 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
682 for (bytes_transferred
= 0;
683 bytes_transferred
< len
;
684 bytes_transferred
+= n
)
686 // Try to transfer as much of the remaining data as possible.
687 n
= ACE_OS::t_rcv (handle
,
688 (char *) buf
+ bytes_transferred
,
689 len
- bytes_transferred
,
695 // Check for other errors.
698 // Check for possible blocking.
699 if (errno
== EWOULDBLOCK
)
701 // Wait for the blocking to subside.
702 int const result
= ACE::handle_read_ready (handle
, 0);
704 // Did select() succeed?
707 // Blocking subsided. Continue data transfer.
713 // Other data transfer or select() failures.
718 return bytes_transferred
;
722 ACE::t_rcv_n_i (ACE_HANDLE handle
,
726 const ACE_Time_Value
*timeout
,
730 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
736 ACE::record_and_set_non_blocking_mode (handle
, val
);
738 for (bytes_transferred
= 0;
739 bytes_transferred
< len
;
740 bytes_transferred
+= n
)
742 // Try to transfer as much of the remaining data as possible.
743 // Since the socket is in non-blocking mode, this call will not
745 n
= ACE_OS::t_rcv (handle
,
746 (char *) buf
+ bytes_transferred
,
747 len
- bytes_transferred
,
754 // Check for possible blocking.
756 errno
== EWOULDBLOCK
)
758 // Wait upto <timeout> for the blocking to subside.
759 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
761 // Did select() succeed?
764 // Blocking subsided in <timeout> period. Continue
771 // Wait in select() timed out or other data transfer or
772 // select() failures.
779 ACE::restore_non_blocking_mode (handle
, val
);
784 return bytes_transferred
;
787 #endif /* ACE_HAS_TLI */
790 ACE::recv_n_i (ACE_HANDLE handle
,
796 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
799 for (bytes_transferred
= 0;
800 bytes_transferred
< len
;
801 bytes_transferred
+= n
)
803 // Try to transfer as much of the remaining data as possible.
804 n
= ACE::recv_i (handle
,
805 static_cast <char *> (buf
) + bytes_transferred
,
806 len
- bytes_transferred
);
812 // Check for other errors.
815 // Check for possible blocking.
816 if (errno
== EWOULDBLOCK
)
818 // Wait for the blocking to subside.
819 int const result
= ACE::handle_read_ready (handle
, 0);
821 // Did select() succeed?
824 // Blocking subsided. Continue data transfer.
830 // Other data transfer or select() failures.
835 return static_cast<ssize_t
> (bytes_transferred
);
839 ACE::recv_n_i (ACE_HANDLE handle
,
842 const ACE_Time_Value
*timeout
,
846 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
852 ACE::record_and_set_non_blocking_mode (handle
, val
);
854 for (bytes_transferred
= 0;
855 bytes_transferred
< len
;
856 bytes_transferred
+= n
)
858 // Try to transfer as much of the remaining data as possible.
859 // Since the socket is in non-blocking mode, this call will not
861 n
= ACE::recv_i (handle
,
862 static_cast <char *> (buf
) + bytes_transferred
,
863 len
- bytes_transferred
);
869 // Check for possible blocking.
871 errno
== EWOULDBLOCK
)
873 // Wait upto <timeout> for the blocking to subside.
874 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
876 // Did select() succeed?
879 // Blocking subsided in <timeout> period. Continue
886 // Wait in select() timed out or other data transfer or
887 // select() failures.
894 ACE::restore_non_blocking_mode (handle
, val
);
899 return static_cast<ssize_t
> (bytes_transferred
);
902 // This is basically an interface to ACE_OS::readv, that doesn't use
903 // the struct iovec explicitly. The ... can be passed as an arbitrary
904 // number of (char *ptr, int len) tuples. However, the count N is the
905 // *total* number of trailing arguments, *not* a couple of the number
907 #if !defined (ACE_LACKS_VA_FUNCTIONS)
909 ACE::recv (ACE_HANDLE handle
, size_t n
, ...)
912 int const total_tuples
= static_cast<int> (n
/ 2);
914 #if defined (ACE_HAS_ALLOCA)
915 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
917 # ifdef ACE_HAS_ALLOC_HOOKS
918 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
919 ACE_Allocator::instance ()->malloc (total_tuples
*
923 ACE_NEW_RETURN (iovp
,
926 # endif /* ACE_HAS_ALLOC_HOOKS */
927 #endif /* !defined (ACE_HAS_ALLOCA) */
931 for (int i
= 0; i
< total_tuples
; i
++)
933 iovp
[i
].iov_base
= va_arg (argp
, char *);
934 iovp
[i
].iov_len
= va_arg (argp
, int);
937 ssize_t
const result
= ACE_OS::recvv (handle
, iovp
, total_tuples
);
938 #if !defined (ACE_HAS_ALLOCA)
939 # ifdef ACE_HAS_ALLOC_HOOKS
940 ACE_Allocator::instance ()->free (iovp
);
943 # endif /* ACE_HAS_ALLOC_HOOKS */
944 #endif /* !defined (ACE_HAS_ALLOCA) */
948 #endif /* ACE_LACKS_VA_FUNCTIONS */
952 ACE::recvv (ACE_HANDLE handle
,
955 const ACE_Time_Value
*timeout
)
958 return ACE_OS::recvv (handle
, iov
, iovcnt
);
962 if (ACE::enter_recv_timedwait (handle
, timeout
, val
) == -1)
966 ssize_t bytes_transferred
= ACE_OS::recvv (handle
, iov
, iovcnt
);
967 ACE::restore_non_blocking_mode (handle
, val
);
968 return bytes_transferred
;
974 ACE::recvv_n_i (ACE_HANDLE handle
,
980 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
981 bytes_transferred
= 0;
983 for (int s
= 0; s
< iovcnt
; )
985 // Try to transfer as much of the remaining data as possible.
986 ssize_t n
= ACE_OS::recvv (handle
, iov
+ s
, iovcnt
- s
);
991 // Check for other errors.
994 // Check for possible blocking.
995 if (errno
== EWOULDBLOCK
)
997 // Wait for the blocking to subside.
998 int const result
= ACE::handle_read_ready (handle
, 0);
1000 // Did select() succeed?
1003 // Blocking subsided. Continue data transfer.
1008 // Other data transfer or select() failures.
1012 for (bytes_transferred
+= n
;
1014 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1016 n
-= iov
[s
].iov_len
;
1020 char *base
= static_cast<char *> (iov
[s
].iov_base
);
1021 iov
[s
].iov_base
= base
+ n
;
1022 // This blind cast is safe because n < iov_len, after above loop.
1023 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1027 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1031 ACE::recvv_n_i (ACE_HANDLE handle
,
1034 const ACE_Time_Value
*timeout
,
1038 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1039 bytes_transferred
= 0;
1044 ACE::record_and_set_non_blocking_mode (handle
, val
);
1046 for (int s
= 0; s
< iovcnt
; )
1048 // Try to transfer as much of the remaining data as possible.
1049 // Since the socket is in non-blocking mode, this call will not
1051 ssize_t n
= ACE_OS::recvv (handle
, iov
+ s
, iovcnt
- s
);
1053 // Check for errors.
1054 if (n
== 0 || n
== -1)
1056 // Check for possible blocking.
1057 if (n
== -1 && errno
== EWOULDBLOCK
)
1059 // Wait upto <timeout> for the blocking to subside.
1060 int const rtn
= ACE::handle_read_ready (handle
, timeout
);
1062 // Did select() succeed?
1065 // Blocking subsided in <timeout> period. Continue
1071 // Wait in select() timed out or other data transfer or
1072 // select() failures.
1078 for (bytes_transferred
+= n
;
1080 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1082 n
-= iov
[s
].iov_len
;
1086 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1087 iov
[s
].iov_base
= base
+ n
;
1088 // This blind cast is safe because n < iov_len, after above loop.
1089 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1093 ACE::restore_non_blocking_mode (handle
, val
);
1101 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1106 ACE::recv_n (ACE_HANDLE handle
,
1107 ACE_Message_Block
*message_block
,
1108 const ACE_Time_Value
*timeout
,
1112 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1113 bytes_transferred
= 0;
1115 iovec iov
[ACE_IOV_MAX
];
1118 while (message_block
!= 0)
1120 // Our current message block chain.
1121 const ACE_Message_Block
*current_message_block
= message_block
;
1123 while (current_message_block
!= 0)
1125 size_t current_message_block_length
=
1126 current_message_block
->length ();
1127 char *this_rd_ptr
= current_message_block
->rd_ptr ();
1129 // Check if this block has any space for incoming data.
1130 while (current_message_block_length
> 0)
1132 u_long
const this_chunk_length
=
1133 ACE_Utils::truncate_cast
<u_long
> (
1134 current_message_block_length
);
1136 // Collect the data in the iovec.
1137 iov
[iovcnt
].iov_base
= this_rd_ptr
;
1138 iov
[iovcnt
].iov_len
= this_chunk_length
;
1139 current_message_block_length
-= this_chunk_length
;
1140 this_rd_ptr
+= this_chunk_length
;
1142 // Increment iovec counter.
1145 // The buffer is full make a OS call. @@ TODO find a way to
1146 // find ACE_IOV_MAX for platforms that do not define it rather
1147 // than simply setting ACE_IOV_MAX to some arbitrary value such
1149 if (iovcnt
== ACE_IOV_MAX
)
1151 size_t current_transfer
= 0;
1153 ssize_t
const result
= ACE::recvv_n (handle
,
1159 // Add to total bytes transferred.
1160 bytes_transferred
+= current_transfer
;
1163 if (result
== -1 || result
== 0)
1166 // Reset iovec counter.
1171 // Select the next message block in the chain.
1172 current_message_block
= current_message_block
->cont ();
1175 // Selection of the next message block chain.
1176 message_block
= message_block
->next ();
1179 // Check for remaining buffers to be sent. This will happen when
1180 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1183 size_t current_transfer
= 0;
1185 ssize_t
const result
= ACE::recvv_n (handle
,
1191 // Add to total bytes transferred.
1192 bytes_transferred
+= current_transfer
;
1195 if (result
== -1 || result
== 0)
1201 // Return total bytes transferred.
1202 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1206 ACE::send (ACE_HANDLE handle
,
1210 const ACE_Time_Value
*timeout
)
1213 return ACE_OS::send (handle
, (const char *) buf
, n
, flags
);
1217 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1221 ssize_t
const bytes_transferred
=
1222 ACE_OS::send (handle
, (const char *) buf
, n
, flags
);
1223 ACE::restore_non_blocking_mode (handle
, val
);
1224 return bytes_transferred
;
1229 #if defined (ACE_HAS_TLI)
1232 ACE::t_snd (ACE_HANDLE handle
,
1236 const ACE_Time_Value
*timeout
)
1239 return ACE_OS::t_snd (handle
, (const char *) buf
, n
, flags
);
1243 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1247 ssize_t
const bytes_transferred
=
1248 ACE_OS::t_snd (handle
, (const char *) buf
, n
, flags
);
1249 ACE::restore_non_blocking_mode (handle
, val
);
1250 return bytes_transferred
;
1255 #endif /* ACE_HAS_TLI */
1258 ACE::send (ACE_HANDLE handle
,
1261 const ACE_Time_Value
*timeout
)
1264 return ACE::send_i (handle
, buf
, n
);
1268 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1272 ssize_t
const bytes_transferred
= ACE::send_i (handle
, buf
, n
);
1273 ACE::restore_non_blocking_mode (handle
, val
);
1274 return bytes_transferred
;
1280 ACE::sendmsg (ACE_HANDLE handle
,
1281 const struct msghdr
*msg
,
1283 const ACE_Time_Value
*timeout
)
1286 return ACE_OS::sendmsg (handle
, msg
, flags
);
1290 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1294 ssize_t
const bytes_transferred
=
1295 ACE_OS::sendmsg (handle
, msg
, flags
);
1296 ACE::restore_non_blocking_mode (handle
, val
);
1297 return bytes_transferred
;
1303 ACE::sendto (ACE_HANDLE handle
,
1307 const struct sockaddr
*addr
,
1309 const ACE_Time_Value
*timeout
)
1312 return ACE_OS::sendto (handle
, buf
, len
, flags
, addr
, addrlen
);
1316 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1320 ssize_t
const bytes_transferred
=
1321 ACE_OS::sendto (handle
, buf
, len
, flags
, addr
, addrlen
);
1322 ACE::restore_non_blocking_mode (handle
, val
);
1323 return bytes_transferred
;
1329 ACE::send_n_i (ACE_HANDLE handle
,
1336 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1339 for (bytes_transferred
= 0;
1340 bytes_transferred
< len
;
1341 bytes_transferred
+= n
)
1343 // Try to transfer as much of the remaining data as possible.
1344 n
= ACE_OS::send (handle
,
1345 (char *) buf
+ bytes_transferred
,
1346 len
- bytes_transferred
,
1352 // Check for other errors.
1355 // Check for possible blocking.
1356 #if defined (ACE_WIN32)
1357 if (errno
== EWOULDBLOCK
) // If enobufs no need to loop
1359 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1360 #endif /* ACE_WIN32 */
1362 // Wait for the blocking to subside.
1363 int const result
= ACE::handle_write_ready (handle
, 0);
1365 // Did select() succeed?
1368 // Blocking subsided. Continue data transfer.
1374 // Other data transfer or select() failures.
1379 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1383 ACE::send_n_i (ACE_HANDLE handle
,
1387 const ACE_Time_Value
*timeout
,
1391 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1397 ACE::record_and_set_non_blocking_mode (handle
, val
);
1399 for (bytes_transferred
= 0;
1400 bytes_transferred
< len
;
1401 bytes_transferred
+= n
)
1403 // Try to transfer as much of the remaining data as possible.
1404 // Since the socket is in non-blocking mode, this call will not
1406 n
= ACE_OS::send (handle
,
1407 (char *) buf
+ bytes_transferred
,
1408 len
- bytes_transferred
,
1411 // Check for errors.
1415 // Check for possible blocking.
1416 if (n
== -1 && (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1418 // Wait upto <timeout> for the blocking to subside.
1419 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1421 // Did select() succeed?
1424 // Blocking subsided in <timeout> period. Continue
1431 // Wait in select() timed out or other data transfer or
1432 // select() failures.
1439 ACE::restore_non_blocking_mode (handle
, val
);
1447 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1451 #if defined (ACE_HAS_TLI)
1454 ACE::t_snd_n_i (ACE_HANDLE handle
,
1461 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1464 for (bytes_transferred
= 0;
1465 bytes_transferred
< len
;
1466 bytes_transferred
+= n
)
1468 // Try to transfer as much of the remaining data as possible.
1469 n
= ACE_OS::t_snd (handle
,
1470 (char *) buf
+ bytes_transferred
,
1471 len
- bytes_transferred
,
1477 // Check for other errors.
1480 // Check for possible blocking.
1481 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1483 // Wait for the blocking to subside.
1484 int const result
= ACE::handle_write_ready (handle
, 0);
1486 // Did select() succeed?
1489 // Blocking subsided. Continue data transfer.
1495 // Other data transfer or select() failures.
1500 return bytes_transferred
;
1504 ACE::t_snd_n_i (ACE_HANDLE handle
,
1508 const ACE_Time_Value
*timeout
,
1512 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1518 ACE::record_and_set_non_blocking_mode (handle
, val
);
1520 for (bytes_transferred
= 0;
1521 bytes_transferred
< len
;
1522 bytes_transferred
+= n
)
1524 // Try to transfer as much of the remaining data as possible.
1525 // Since the socket is in non-blocking mode, this call will not
1527 n
= ACE_OS::t_snd (handle
,
1528 (char *) buf
+ bytes_transferred
,
1529 len
- bytes_transferred
,
1532 // Check for errors.
1536 // Check for possible blocking.
1538 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1540 // Wait upto <timeout> for the blocking to subside.
1541 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1543 // Did select() succeed?
1546 // Blocking subsided in <timeout> period. Continue
1553 // Wait in select() timed out or other data transfer or
1554 // select() failures.
1561 ACE::restore_non_blocking_mode (handle
, val
);
1566 return bytes_transferred
;
1569 #endif /* ACE_HAS_TLI */
1572 ACE::send_n_i (ACE_HANDLE handle
,
1578 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1581 for (bytes_transferred
= 0;
1582 bytes_transferred
< len
;
1583 bytes_transferred
+= n
)
1585 // Try to transfer as much of the remaining data as possible.
1586 n
= ACE::send_i (handle
,
1587 (char *) buf
+ bytes_transferred
,
1588 len
- bytes_transferred
);
1595 // Check for other errors.
1598 // Check for possible blocking.
1599 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1601 // Wait for the blocking to subside.
1602 int const result
= ACE::handle_write_ready (handle
, 0);
1604 // Did select() succeed?
1607 // Blocking subsided. Continue data transfer.
1613 // Other data transfer or select() failures.
1618 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1622 ACE::send_n_i (ACE_HANDLE handle
,
1625 const ACE_Time_Value
*timeout
,
1629 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1635 ACE::record_and_set_non_blocking_mode (handle
, val
);
1637 for (bytes_transferred
= 0;
1638 bytes_transferred
< len
;
1639 bytes_transferred
+= n
)
1641 // Try to transfer as much of the remaining data as possible.
1642 // Since the socket is in non-blocking mode, this call will not
1644 n
= ACE::send_i (handle
,
1645 (char *) buf
+ bytes_transferred
,
1646 len
- bytes_transferred
);
1648 // Check for errors.
1652 // Check for possible blocking.
1654 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1656 // Wait upto <timeout> for the blocking to subside.
1657 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1659 // Did select() succeed?
1662 // Blocking subsided in <timeout> period. Continue
1669 // Wait in select() timed out or other data transfer or
1670 // select() failures.
1677 ACE::restore_non_blocking_mode (handle
, val
);
1685 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1689 // Send N char *ptrs and int lengths. Note that the char *'s precede
1690 // the ints (basically, an varargs version of writev). The count N is
1691 // the *total* number of trailing arguments, *not* a couple of the
1692 // number of tuple pairs!
1693 #if !defined (ACE_LACKS_VA_FUNCTIONS)
1695 ACE::send (ACE_HANDLE handle
, size_t n
, ...)
1698 int total_tuples
= static_cast<int> (n
/ 2);
1700 #if defined (ACE_HAS_ALLOCA)
1701 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
1703 # ifdef ACE_HAS_ALLOC_HOOKS
1704 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
1705 ACE_Allocator::instance ()->malloc (total_tuples
*
1709 ACE_NEW_RETURN (iovp
,
1710 iovec
[total_tuples
],
1712 # endif /* ACE_HAS_ALLOC_HOOKS */
1713 #endif /* !defined (ACE_HAS_ALLOCA) */
1717 for (int i
= 0; i
< total_tuples
; i
++)
1719 iovp
[i
].iov_base
= va_arg (argp
, char *);
1720 iovp
[i
].iov_len
= va_arg (argp
, int);
1723 ssize_t result
= ACE_OS::sendv (handle
, iovp
, total_tuples
);
1724 #if !defined (ACE_HAS_ALLOCA)
1725 # ifdef ACE_HAS_ALLOC_HOOKS
1726 ACE_Allocator::instance ()->free (iovp
);
1729 # endif /* ACE_HAS_ALLOC_HOOKS */
1730 #endif /* !defined (ACE_HAS_ALLOCA) */
1734 #endif /* ACE_LACKS_VA_FUNCTIONS */
1737 ACE::sendv (ACE_HANDLE handle
,
1740 const ACE_Time_Value
*timeout
)
1743 return ACE_OS::sendv (handle
, iov
, iovcnt
);
1747 if (ACE::enter_send_timedwait (handle
, timeout
, val
) == -1)
1751 ssize_t bytes_transferred
= ACE_OS::sendv (handle
, iov
, iovcnt
);
1752 ACE::restore_non_blocking_mode (handle
, val
);
1753 return bytes_transferred
;
1759 ACE::sendv_n_i (ACE_HANDLE handle
,
1765 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1766 bytes_transferred
= 0;
1768 iovec
*iov
= const_cast<iovec
*> (i
);
1774 // Try to transfer as much of the remaining data as possible.
1775 ssize_t n
= ACE_OS::sendv (handle
, iov
+ s
, iovcnt
- s
);
1781 // Check for other errors.
1784 // Check for possible blocking.
1785 if (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
)
1787 // Wait for the blocking to subside.
1788 int const result
= ACE::handle_write_ready (handle
, 0);
1790 // Did select() succeed?
1793 // Blocking subsided. Continue data transfer.
1798 // Other data transfer or select() failures.
1802 for (bytes_transferred
+= n
;
1804 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1806 n
-= iov
[s
].iov_len
;
1810 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1811 iov
[s
].iov_base
= base
+ n
;
1812 // This blind cast is safe because n < iov_len, after above loop.
1813 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1817 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1821 ACE::sendv_n_i (ACE_HANDLE handle
,
1824 const ACE_Time_Value
*timeout
,
1828 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1829 bytes_transferred
= 0;
1834 ACE::record_and_set_non_blocking_mode (handle
, val
);
1836 iovec
*iov
= const_cast<iovec
*> (i
);
1842 // Try to transfer as much of the remaining data as possible.
1843 // Since the socket is in non-blocking mode, this call will not
1845 ssize_t n
= ACE_OS::sendv (handle
, iov
+ s
, iovcnt
- s
);
1847 // Check for errors.
1851 // Check for possible blocking.
1853 (errno
== EWOULDBLOCK
|| errno
== ENOBUFS
))
1855 // Wait upto <timeout> for the blocking to subside.
1856 int const rtn
= ACE::handle_write_ready (handle
, timeout
);
1858 // Did select() succeed?
1861 // Blocking subsided in <timeout> period. Continue
1867 // Wait in select() timed out or other data transfer or
1868 // select() failures.
1874 for (bytes_transferred
+= n
;
1876 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
1878 n
-= iov
[s
].iov_len
;
1882 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
1883 iov
[s
].iov_base
= base
+ n
;
1884 // This blind cast is safe because n < iov_len, after above loop.
1885 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
1889 ACE::restore_non_blocking_mode (handle
, val
);
1897 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1902 ACE::write_n (ACE_HANDLE handle
,
1903 const ACE_Message_Block
*message_block
,
1907 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
1908 bytes_transferred
= 0;
1910 iovec iov
[ACE_IOV_MAX
];
1913 while (message_block
!= 0)
1915 // Our current message block chain.
1916 const ACE_Message_Block
*current_message_block
= message_block
;
1918 while (current_message_block
!= 0)
1920 size_t current_message_block_length
=
1921 current_message_block
->length ();
1922 char *this_block_ptr
= current_message_block
->rd_ptr ();
1924 // Check if this block has any data to be sent.
1925 while (current_message_block_length
> 0)
1927 u_long
const this_chunk_length
=
1928 ACE_Utils::truncate_cast
<u_long
> (
1929 current_message_block_length
);
1931 // Collect the data in the iovec.
1932 iov
[iovcnt
].iov_base
= this_block_ptr
;
1933 iov
[iovcnt
].iov_len
= this_chunk_length
;
1934 current_message_block_length
-= this_chunk_length
;
1935 this_block_ptr
+= this_chunk_length
;
1937 // Increment iovec counter.
1940 // The buffer is full make a OS call. @@ TODO find a way to
1941 // find ACE_IOV_MAX for platforms that do not define it rather
1942 // than simply setting ACE_IOV_MAX to some arbitrary value such
1944 if (iovcnt
== ACE_IOV_MAX
)
1946 size_t current_transfer
= 0;
1948 ssize_t
const result
= ACE::writev_n (handle
,
1953 // Add to total bytes transferred.
1954 bytes_transferred
+= current_transfer
;
1957 if (result
== -1 || result
== 0)
1960 // Reset iovec counter.
1965 // Select the next message block in the chain.
1966 current_message_block
= current_message_block
->cont ();
1969 // Selection of the next message block chain.
1970 message_block
= message_block
->next ();
1973 // Check for remaining buffers to be sent. This will happen when
1974 // ACE_IOV_MAX is not a multiple of the number of message blocks.
1977 size_t current_transfer
= 0;
1979 ssize_t
const result
= ACE::writev_n (handle
,
1984 // Add to total bytes transferred.
1985 bytes_transferred
+= current_transfer
;
1988 if (result
== -1 || result
== 0)
1992 // Return total bytes transferred.
1993 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
1997 ACE::send_n (ACE_HANDLE handle
,
1998 const ACE_Message_Block
*message_block
,
1999 const ACE_Time_Value
*timeout
,
2003 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2004 bytes_transferred
= 0;
2006 iovec iov
[ACE_IOV_MAX
];
2009 while (message_block
!= 0)
2011 // Our current message block chain.
2012 const ACE_Message_Block
*current_message_block
= message_block
;
2014 while (current_message_block
!= 0)
2016 char *this_block_ptr
= current_message_block
->rd_ptr ();
2017 size_t current_message_block_length
=
2018 current_message_block
->length ();
2020 // Check if this block has any data to be sent.
2021 while (current_message_block_length
> 0)
2023 u_long
const this_chunk_length
=
2024 ACE_Utils::truncate_cast
<u_long
> (
2025 current_message_block_length
);
2027 // Collect the data in the iovec.
2028 iov
[iovcnt
].iov_base
= this_block_ptr
;
2029 iov
[iovcnt
].iov_len
= this_chunk_length
;
2030 current_message_block_length
-= this_chunk_length
;
2031 this_block_ptr
+= this_chunk_length
;
2033 // Increment iovec counter.
2036 // The buffer is full make a OS call. @@ TODO find a way to
2037 // find ACE_IOV_MAX for platforms that do not define it rather
2038 // than simply setting ACE_IOV_MAX to some arbitrary value such
2040 if (iovcnt
== ACE_IOV_MAX
)
2042 size_t current_transfer
= 0;
2044 ssize_t
const result
= ACE::sendv_n (handle
,
2050 // Add to total bytes transferred.
2051 bytes_transferred
+= current_transfer
;
2054 if (result
== -1 || result
== 0)
2057 // Reset iovec counter.
2062 // Select the next message block in the chain.
2063 current_message_block
= current_message_block
->cont ();
2066 // Selection of the next message block chain.
2067 message_block
= message_block
->next ();
2070 // Check for remaining buffers to be sent. This will happen when
2071 // ACE_IOV_MAX is not a multiple of the number of message blocks.
2074 size_t current_transfer
= 0;
2076 ssize_t
const result
= ACE::sendv_n (handle
,
2082 // Add to total bytes transferred.
2083 bytes_transferred
+= current_transfer
;
2086 if (result
== -1 || result
== 0)
2092 // Return total bytes transferred.
2093 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2097 ACE::readv_n (ACE_HANDLE handle
,
2103 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2104 bytes_transferred
= 0;
2110 ssize_t n
= ACE_OS::readv (handle
,
2114 if (n
== -1 || n
== 0)
2117 for (bytes_transferred
+= n
;
2119 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
2121 n
-= iov
[s
].iov_len
;
2125 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
2126 iov
[s
].iov_base
= base
+ n
;
2127 // This blind cast is safe because n < iov_len, after above loop.
2128 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
2132 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2136 ACE::writev_n (ACE_HANDLE handle
,
2142 size_t &bytes_transferred
= bt
== 0 ? temp
: *bt
;
2143 bytes_transferred
= 0;
2145 iovec
*iov
= const_cast<iovec
*> (i
);
2151 ssize_t n
= ACE_OS::writev (handle
,
2155 if (n
== -1 || n
== 0)
2160 for (bytes_transferred
+= n
;
2162 && n
>= static_cast<ssize_t
> (iov
[s
].iov_len
);
2164 n
-= iov
[s
].iov_len
;
2168 char *base
= reinterpret_cast<char *> (iov
[s
].iov_base
);
2169 iov
[s
].iov_base
= base
+ n
;
2170 // This blind cast is safe because n < iov_len, after above loop.
2171 iov
[s
].iov_len
= iov
[s
].iov_len
- static_cast<u_long
> (n
);
2175 return ACE_Utils::truncate_cast
<ssize_t
> (bytes_transferred
);
2179 ACE::handle_ready (ACE_HANDLE handle
,
2180 const ACE_Time_Value
*timeout
,
2183 bool exception_ready
)
2185 #if defined (ACE_HAS_POLL)
2186 ACE_UNUSED_ARG (exception_ready
);
2191 fds
.events
= read_ready
? POLLIN
: 0;
2195 fds
.events
|= POLLOUT
;
2200 int const result
= ACE_OS::poll (&fds
, 1, timeout
);
2202 ACE_Handle_Set handle_set
;
2203 handle_set
.set_bit (handle
);
2205 // Wait for data or for the timeout to elapse.
2206 int select_width
= 0;
2207 #if !defined (ACE_WIN32)
2208 select_width
= int (handle
) + 1;
2209 # endif /* ACE_WIN64 */
2210 int result
= ACE_OS::select (select_width
,
2211 read_ready
? handle_set
.fdset () : 0, // read_fds.
2212 write_ready
? handle_set
.fdset () : 0, // write_fds.
2213 exception_ready
? handle_set
.fdset () : 0, // exception_fds.
2216 #endif /* ACE_HAS_POLL */
2219 case 0: // Timer expired.
2222 case -1: // we got here directly - select() returned -1.
2224 case 1: // Handle has data.
2226 default: // default is case result > 0; return a
2227 // ACE_ASSERT (result == 1);
2233 ACE::enter_recv_timedwait (ACE_HANDLE handle
,
2234 const ACE_Time_Value
*timeout
,
2237 int const result
= ACE::handle_read_ready (handle
, timeout
);
2242 ACE::record_and_set_non_blocking_mode (handle
, val
);
2248 ACE::enter_send_timedwait (ACE_HANDLE handle
,
2249 const ACE_Time_Value
*timeout
,
2252 int const result
= ACE::handle_write_ready (handle
, timeout
);
2257 ACE::record_and_set_non_blocking_mode (handle
, val
);
2263 ACE::record_and_set_non_blocking_mode (ACE_HANDLE handle
, int &val
)
2265 // We need to record whether we are already *in* nonblocking mode,
2266 // so that we can correctly reset the state when we're done.
2267 val
= ACE::get_flags (handle
);
2269 if (ACE_BIT_DISABLED (val
, ACE_NONBLOCK
))
2270 // Set the handle into non-blocking mode if it's not already in
2272 ACE::set_flags (handle
, ACE_NONBLOCK
);
2276 ACE::restore_non_blocking_mode (ACE_HANDLE handle
, int val
)
2278 if (ACE_BIT_DISABLED (val
, ACE_NONBLOCK
))
2280 // Save/restore errno.
2281 ACE_Errno_Guard
error (errno
);
2282 // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
2284 ACE::clr_flags (handle
, ACE_NONBLOCK
);
2288 /// Format buffer into printable format. This is useful for debugging.
2289 /// Portions taken from mdump by J.P. Knight (J.P.Knight@lut.ac.uk)
2290 /// Modifications by Todd Montgomery.
2292 ACE::format_hexdump (const char *buffer
,
2297 ACE_TRACE ("ACE::format_hexdump");
2300 ACE_TCHAR textver
[16 + 1];
2302 // We can fit 16 bytes output in text mode per line, 4 chars per byte.
2303 size_t maxlen
= (obuf_sz
/ 68) * 16;
2304 const ACE_TCHAR
*const obuf_start
= obuf
;
2311 size_t const lines
= size
/ 16;
2312 for (i
= 0; i
< lines
; i
++)
2316 for (j
= 0 ; j
< 16; j
++)
2318 c
= (u_char
) buffer
[(i
<< 4) + j
]; // or, buffer[i*16+j]
2319 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2325 *obuf
++ = ACE_TEXT (' ');
2327 textver
[j
] = ACE_OS::ace_isprint (c
) ? c
: u_char ('.');
2332 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2333 ACE_TEXT (" %") ACE_TEXT_PRIs
ACE_TEXT ("\n"),
2336 while (*obuf
!= '\0')
2342 for (i
= 0 ; i
< size
% 16; i
++)
2344 c
= (u_char
) buffer
[size
- size
% 16 + i
];
2345 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2351 *obuf
++ = ACE_TEXT (' ');
2353 textver
[i
] = ACE_OS::ace_isprint (c
) ? c
: u_char ('.');
2356 for (i
= size
% 16; i
< 16; i
++)
2358 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2363 *obuf
++ = ACE_TEXT (' ');
2369 ACE_OS::snprintf (obuf
, obuf_sz
- (obuf
- obuf_start
),
2370 ACE_TEXT (" %") ACE_TEXT_PRIs
ACE_TEXT ("\n"),
2376 /// Returns the current timestamp in the form
2377 /// "hour:minute:second:microsecond." The month, day, and year are
2378 /// also stored in the beginning of the date_and_time array
2379 /// using ISO-8601 format.
2381 ACE::timestamp (ACE_TCHAR date_and_time
[],
2382 size_t date_and_timelen
,
2383 bool return_pointer_to_first_digit
)
2385 return ACE::timestamp (ACE_Time_Value::zero
,
2388 return_pointer_to_first_digit
);
2391 /// Returns the given timestamp in the form
2392 /// "hour:minute:second:microsecond." The month, day, and year are
2393 /// also stored in the beginning of the date_and_time array
2394 /// using ISO-8601 format.
2395 /// 012345678901234567890123456
2396 /// 2010-12-02 12:56:00.123456<nul>
2398 ACE::timestamp (const ACE_Time_Value
& time_value
,
2399 ACE_TCHAR date_and_time
[],
2400 size_t date_and_timelen
,
2401 bool return_pointer_to_first_digit
)
2403 //ACE_TRACE ("ACE::timestamp");
2405 // This magic number is from the formatting statement
2406 // farther down this routine.
2407 if (date_and_timelen
< 27)
2413 ACE_Time_Value cur_time
=
2414 (time_value
== ACE_Time_Value::zero
) ?
2415 ACE_Time_Value (ACE_OS::gettimeofday ()) : time_value
;
2416 time_t secs
= cur_time
.sec ();
2418 ACE_OS::localtime_r (&secs
, &tms
);
2419 ACE_OS::snprintf (date_and_time
,
2421 ACE_TEXT ("%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d.%06ld"),
2428 static_cast<long> (cur_time
.usec()));
2429 date_and_time
[date_and_timelen
- 1] = '\0';
2430 return &date_and_time
[10 + (return_pointer_to_first_digit
!= 0)];
2433 /// This function rounds the request to a multiple of the page size.
2435 ACE::round_to_pagesize (size_t len
)
2437 ACE_TRACE ("ACE::round_to_pagesize");
2439 if (ACE::pagesize_
== 0)
2440 ACE::pagesize_
= ACE_OS::getpagesize ();
2442 return (len
+ (ACE::pagesize_
- 1)) & ~(ACE::pagesize_
- 1);
2446 ACE::round_to_allocation_granularity (size_t len
)
2448 ACE_TRACE ("ACE::round_to_allocation_granularity");
2450 if (ACE::allocation_granularity_
== 0)
2451 ACE::allocation_granularity_
= ACE_OS::allocation_granularity ();
2453 return (len
+ (ACE::allocation_granularity_
- 1)) & ~(ACE::allocation_granularity_
- 1);
2457 ACE::handle_timed_complete (ACE_HANDLE h
,
2458 const ACE_Time_Value
*timeout
,
2461 ACE_TRACE ("ACE::handle_timed_complete");
2463 #if !defined (ACE_WIN32) && defined (ACE_HAS_POLL)
2468 fds
.events
= POLLIN
| POLLOUT
;
2472 ACE_Handle_Set rd_handles
;
2473 ACE_Handle_Set wr_handles
;
2474 rd_handles
.set_bit (h
);
2475 wr_handles
.set_bit (h
);
2476 #endif /* !ACE_WIN32 && ACE_HAS_POLL */
2478 #if defined (ACE_WIN32)
2479 // Winsock is different - it sets the exception bit for failed connect,
2480 // unlike other platforms, where the write bit is set for both success
2482 ACE_Handle_Set ex_handles
;
2483 ex_handles
.set_bit (h
);
2484 #endif /* ACE_WIN32 */
2486 bool need_to_check
= false;
2487 bool known_failure
= false;
2489 #if defined (ACE_WIN32)
2490 int n
= ACE_OS::select (0, // Ignored on Windows: int (h) + 1,
2496 # if defined (ACE_HAS_POLL)
2498 int n
= ACE_OS::poll (&fds
, 1, timeout
);
2503 n
= ACE_OS::select (int (h
) + 1,
2509 n
= ACE_OS::select (int (h
) + 1,
2514 # endif /* ACE_HAS_POLL */
2515 #endif /* ACE_WIN32 */
2517 // If we failed to connect within the time period allocated by the
2518 // caller, then we fail (e.g., the remote host might have been too
2519 // busy to accept our call).
2522 if (n
== 0 && timeout
!= 0)
2524 return ACE_INVALID_HANDLE
;
2527 // On Windows, a ready-for-write handle is successfully connected, and
2528 // ready-for-exception is a failure. On fails, we need to grab the error
2529 // code via getsockopt.
2530 // On BSD sockets using select(), the handle becomes writable on
2531 // completion either success or fail, so if the select() does not time
2532 // out, we need to check for success/fail.
2533 // It is believed that TLI sockets use the readable=fail, writeable=success
2534 // but that hasn't been as well tested.
2535 #if defined (ACE_WIN32)
2536 ACE_UNUSED_ARG (is_tli
);
2538 // On Win32, ex_handle set indicates a failure. We'll do the check
2539 // to try and get an errno value, but the connect failed regardless of
2540 // what getsockopt says about the error.
2541 if (ex_handles
.is_set (h
))
2543 need_to_check
= true;
2544 known_failure
= true;
2548 # if defined (ACE_HAS_POLL)
2549 need_to_check
= (fds
.revents
& POLLIN
) && !(fds
.revents
& POLLOUT
);
2551 need_to_check
= rd_handles
.is_set (h
) && !wr_handles
.is_set (h
);
2552 # endif /* ACE_HAS_POLL */
2555 # if defined (ACE_HAS_POLL)
2557 // The "official" bit for failed connect is POLLIN. However, POLLERR
2558 // is often set and there are occasional cases seen with some kernels
2559 // where only POLLERR is set on a failed connect.
2560 need_to_check
= (fds
.revents
& POLLIN
) || (fds
.revents
& POLLERR
);
2561 known_failure
= (fds
.revents
& POLLERR
);
2564 need_to_check
= true;
2565 # endif /* ACE_HAS_POLL */
2566 #endif /* ACE_WIN32 */
2570 #if defined (SOL_SOCKET) && defined (SO_ERROR)
2572 int sock_err_len
= sizeof (sock_err
);
2573 int sockopt_ret
= ACE_OS::getsockopt (h
, SOL_SOCKET
, SO_ERROR
,
2574 (char *)&sock_err
, &sock_err_len
);
2575 if (sockopt_ret
< 0)
2577 h
= ACE_INVALID_HANDLE
;
2580 if (sock_err
!= 0 || known_failure
)
2582 h
= ACE_INVALID_HANDLE
;
2588 // The following recv() won't block provided that the
2589 // ACE_NONBLOCK flag has not been turned off .
2590 n
= ACE::recv (h
, &dummy
, 1, MSG_PEEK
);
2592 // If no data was read/peeked at, check to see if it's because
2593 // of a non-connected socket (and therefore an error) or there's
2594 // just no data yet.
2599 errno
= ECONNREFUSED
;
2600 h
= ACE_INVALID_HANDLE
;
2602 else if (errno
!= EWOULDBLOCK
&& errno
!= EAGAIN
)
2603 h
= ACE_INVALID_HANDLE
;
2608 // 1. The HANDLE is ready for writing and doesn't need to be checked or
2609 // 2. recv() returned an indication of the state of the socket - if there is
2610 // either data present, or a recv is legit but there's no data yet,
2611 // the connection was successfully established.
2615 /// Wait up to @a timeout amount of time to accept a connection.
2617 ACE::handle_timed_accept (ACE_HANDLE listener
,
2618 ACE_Time_Value
*timeout
,
2621 ACE_TRACE ("ACE::handle_timed_accept");
2622 // Make sure we don't bomb out on erroneous values.
2623 if (listener
== ACE_INVALID_HANDLE
)
2626 #if defined (ACE_HAS_POLL)
2631 fds
.events
= POLLIN
;
2635 // Use the select() implementation rather than poll().
2636 ACE_Handle_Set rd_handle
;
2637 rd_handle
.set_bit (listener
);
2638 #endif /* ACE_HAS_POLL */
2640 // We need a loop here if <restart> is enabled.
2644 #if defined (ACE_HAS_POLL)
2646 int n
= ACE_OS::poll (&fds
, 1, timeout
);
2649 int select_width
= 0;
2650 # if !defined (ACE_WIN32)
2651 select_width
= int (listener
) + 1;
2652 # endif /* ACE_WIN32 */
2653 int n
= ACE_OS::select (select_width
,
2656 #endif /* ACE_HAS_POLL */
2661 if (errno
== EINTR
&& restart
)
2667 if (timeout
!= 0 && *timeout
== ACE_Time_Value::zero
)
2668 errno
= EWOULDBLOCK
;
2684 /// Make the current process a UNIX daemon. This is based on Stevens
2687 ACE::daemonize (const ACE_TCHAR pathname
[],
2688 bool close_all_handles
,
2689 const ACE_TCHAR program_name
[])
2691 ACE_TRACE ("ACE::daemonize");
2692 #if !defined (ACE_LACKS_FORK)
2693 pid_t pid
= ACE_OS::fork ();
2698 ACE_OS::exit (0); // Parent exits.
2700 // 1st child continues.
2701 ACE_OS::setsid (); // Become session leader.
2703 ACE_OS::signal (SIGHUP
, SIG_IGN
);
2705 pid
= ACE_OS::fork (program_name
);
2708 ACE_OS::exit (0); // First child terminates.
2710 // Second child continues.
2713 // change working directory.
2714 ACE_OS::chdir (pathname
);
2716 ACE_OS::umask (0); // clear our file mode creation mask.
2718 // Close down the I/O handles.
2719 if (close_all_handles
)
2721 for (int i
= ACE::max_handles () - 1; i
>= 0; i
--)
2724 int fd
= ACE_OS::open ("/dev/null", O_RDWR
, 0);
2727 ACE_OS::dup2 (fd
, ACE_STDIN
);
2728 ACE_OS::dup2 (fd
, ACE_STDOUT
);
2729 ACE_OS::dup2 (fd
, ACE_STDERR
);
2731 if (fd
> ACE_STDERR
)
2738 ACE_UNUSED_ARG (pathname
);
2739 ACE_UNUSED_ARG (close_all_handles
);
2740 ACE_UNUSED_ARG (program_name
);
2742 ACE_NOTSUP_RETURN (-1);
2743 #endif /* ACE_LACKS_FORK */
2747 ACE::fork (const ACE_TCHAR
*program_name
,
2750 if (avoid_zombies
== 0)
2751 return ACE_OS::fork (program_name
);
2754 // This algorithm is adapted from an example in the Stevens book
2755 // "Advanced Programming in the Unix Environment" and an item in
2756 // Andrew Gierth's Unix Programming FAQ. It creates an orphan
2757 // process that's inherited by the init process; init cleans up
2758 // when the orphan process terminates.
2760 // Another way to avoid zombies is to ignore or catch the
2761 // SIGCHLD signal; we don't use that approach here.
2763 pid_t pid
= ACE_OS::fork ();
2766 // The child process forks again to create a grandchild.
2767 switch (ACE_OS::fork (program_name
))
2769 case 0: // grandchild returns 0.
2771 case static_cast<pid_t
>(-1): // assumes all errnos are < 256
2772 ACE_OS::_exit (errno
);
2773 ACE_FALLTHROUGH
; // gcc sees this as a fallthrough
2774 default: // child terminates, orphaning grandchild
2779 // Parent process waits for child to terminate.
2780 ACE_exitcode status
;
2781 if (pid
< 0 || ACE_OS::waitpid (pid
, &status
, 0) < 0)
2784 // child terminated by calling exit()?
2785 if (WIFEXITED ((status
)))
2787 // child terminated normally?
2788 if (WEXITSTATUS ((status
)) == 0)
2791 errno
= WEXITSTATUS ((status
));
2794 // child didn't call exit(); perhaps it received a signal?
2804 ACE_TRACE ("ACE::max_handles");
2805 #if defined (RLIMIT_NOFILE) && !defined (ACE_LACKS_RLIMIT)
2807 int const r
= ACE_OS::getrlimit (RLIMIT_NOFILE
, &rl
);
2808 # if !defined (RLIM_INFINITY)
2812 if (r
== 0 && rl
.rlim_cur
!= RLIM_INFINITY
)
2814 // If == RLIM_INFINITY, fall through to the ACE_LACKS_RLIMIT sections
2815 # endif /* RLIM_INFINITY */
2816 #endif /* RLIMIT_NOFILE && !ACE_LACKS_RLIMIT */
2818 #if defined (_SC_OPEN_MAX) && !defined (ACE_LACKS_SYSCONF)
2819 return static_cast<int> (ACE_OS::sysconf (_SC_OPEN_MAX
));
2820 #elif defined (FD_SETSIZE)
2823 ACE_NOTSUP_RETURN (-1);
2824 #endif /* _SC_OPEN_MAX */
2827 // Set the number of currently open handles in the process.
2829 // If NEW_LIMIT == -1 set the limit to the maximum allowable.
2830 // Otherwise, set it to be the value of NEW_LIMIT.
2833 ACE::set_handle_limit (int new_limit
,
2834 int increase_limit_only
)
2836 ACE_TRACE ("ACE::set_handle_limit");
2837 int cur_limit
= ACE::max_handles ();
2838 int max_limit
= cur_limit
;
2840 if (cur_limit
== -1)
2843 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2846 ACE_OS::memset ((void *) &rl
, 0, sizeof rl
);
2847 int r
= ACE_OS::getrlimit (RLIMIT_NOFILE
, &rl
);
2849 max_limit
= rl
.rlim_max
;
2850 #endif /* ACE_LACKS_RLIMIT */
2852 if (new_limit
== -1)
2853 new_limit
= max_limit
;
2860 else if (new_limit
> cur_limit
)
2862 // Increase the limit.
2863 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2864 rl
.rlim_cur
= new_limit
;
2865 return ACE_OS::setrlimit (RLIMIT_NOFILE
, &rl
);
2866 #elif !defined (RLIMIT_NOFILE)
2869 // Must return EINVAL errno.
2870 ACE_NOTSUP_RETURN (-1);
2871 #endif /* ACE_LACKS_RLIMIT */
2873 else if (increase_limit_only
== 0)
2875 // Decrease the limit.
2876 #if !defined (ACE_LACKS_RLIMIT) && defined (RLIMIT_NOFILE)
2877 rl
.rlim_cur
= new_limit
;
2878 return ACE_OS::setrlimit (RLIMIT_NOFILE
, &rl
);
2880 // We give a chance to platforms without RLIMIT to work.
2881 // Instead of ACE_NOTSUP_RETURN (0), just return 0 because
2882 // new_limit is <= cur_limit, so it's a no-op.
2884 #endif /* ACE_LACKS_RLIMIT */
2890 /// Euclid's greatest common divisor algorithm.
2892 ACE::gcd (u_long x
, u_long y
)
2904 /// Calculates the minimum enclosing frame size for the given values.
2906 ACE::minimum_frame_size (u_long period1
, u_long period2
)
2908 // if one of the periods is zero, treat it as though it as
2909 // uninitialized and return the other period as the frame size
2919 // if neither is zero, find the greatest common divisor of the two periods
2920 u_long greatest_common_divisor
= ACE::gcd (period1
, period2
);
2922 // explicitly consider cases to reduce risk of possible overflow errors
2923 if (greatest_common_divisor
== 1)
2925 // periods are relative primes: just multiply them together
2926 return period1
* period2
;
2928 else if (greatest_common_divisor
== period1
)
2930 // the first period divides the second: return the second
2933 else if (greatest_common_divisor
== period2
)
2935 // the second period divides the first: return the first
2940 // the current frame size and the entry's effective period
2941 // have a non-trivial greatest common divisor: return the
2942 // product of factors divided by those in their gcd.
2943 return (period1
* period2
) / greatest_common_divisor
;
2948 ACE::is_prime (const u_long n
,
2949 const u_long min_factor
,
2950 const u_long max_factor
)
2953 for (u_long factor
= min_factor
;
2954 factor
<= max_factor
;
2956 if (n
/ factor
* factor
== n
)
2963 ACE::sock_error (int error
)
2965 #if defined (ACE_WIN32)
2966 static ACE_TCHAR unknown_msg
[64];
2970 case WSAVERNOTSUPPORTED
:
2971 return ACE_TEXT ("version of WinSock not supported");
2973 case WSASYSNOTREADY
:
2974 return ACE_TEXT ("WinSock not present or not responding");
2977 return ACE_TEXT ("app version not supported by DLL");
2979 case WSAHOST_NOT_FOUND
:
2980 return ACE_TEXT ("Authoritive: Host not found");
2983 return ACE_TEXT ("Non-authoritive: host not found or server failure");
2985 case WSANO_RECOVERY
:
2986 return ACE_TEXT ("Non-recoverable: refused or not implemented");
2989 return ACE_TEXT ("Valid name, no data record for type");
2993 return "Valid name, no MX record";
2995 case WSANOTINITIALISED
:
2996 return ACE_TEXT ("WSA Startup not initialized");
2999 return ACE_TEXT ("Network subsystem failed");
3001 case WSAEINPROGRESS
:
3002 return ACE_TEXT ("Blocking operation in progress");
3005 return ACE_TEXT ("Blocking call cancelled");
3007 case WSAEAFNOSUPPORT
:
3008 return ACE_TEXT ("address family not supported");
3011 return ACE_TEXT ("no file handles available");
3014 return ACE_TEXT ("no buffer space available");
3016 case WSAEPROTONOSUPPORT
:
3017 return ACE_TEXT ("specified protocol not supported");
3020 return ACE_TEXT ("protocol wrong type for this socket");
3022 case WSAESOCKTNOSUPPORT
:
3023 return ACE_TEXT ("socket type not supported for address family");
3026 return ACE_TEXT ("handle is not a socket");
3028 case WSAEWOULDBLOCK
:
3029 return ACE_TEXT ("resource temporarily unavailable");
3032 return ACE_TEXT ("address already in use");
3034 case WSAECONNABORTED
:
3035 return ACE_TEXT ("connection aborted");
3038 return ACE_TEXT ("connection reset");
3041 return ACE_TEXT ("not connected");
3044 return ACE_TEXT ("connection timed out");
3046 case WSAECONNREFUSED
:
3047 return ACE_TEXT ("connection refused");
3050 return ACE_TEXT ("host down");
3052 case WSAEHOSTUNREACH
:
3053 return ACE_TEXT ("host unreachable");
3055 case WSAEADDRNOTAVAIL
:
3056 return ACE_TEXT ("address not available");
3059 return ACE_TEXT ("socket is already connected");
3062 return ACE_TEXT ("network dropped connection on reset");
3065 return ACE_TEXT ("message too long");
3067 case WSAENETUNREACH
:
3068 return ACE_TEXT ("network is unreachable");
3071 return ACE_TEXT ("bad address");
3074 return ACE_TEXT ("graceful shutdown in progress");
3077 return ACE_TEXT ("permission denied");
3080 return ACE_TEXT ("cannot send after socket shutdown");
3083 return ACE_TEXT ("too many processes");
3086 return ACE_TEXT ("operation already in progress");
3088 case WSAEPFNOSUPPORT
:
3089 return ACE_TEXT ("protocol family not supported");
3091 case WSAENOPROTOOPT
:
3092 return ACE_TEXT ("bad protocol option");
3094 case WSATYPE_NOT_FOUND
:
3095 return ACE_TEXT ("class type not found");
3098 return ACE_TEXT ("operation not supported");
3100 case WSAEDESTADDRREQ
:
3101 return ACE_TEXT ("destination address required");
3104 ACE_OS::snprintf (unknown_msg
,
3105 sizeof unknown_msg
/ sizeof unknown_msg
[0],
3106 ACE_TEXT ("unknown error: %d"), error
);
3111 ACE_UNUSED_ARG (error
);
3112 ACE_NOTSUP_RETURN (0);
3113 #endif /* ACE_WIN32 */
3117 ACE::is_sock_error (int error
)
3119 #if defined (ACE_WIN32)
3122 case WSAVERNOTSUPPORTED
:
3123 case WSASYSNOTREADY
:
3125 case WSAHOST_NOT_FOUND
:
3127 case WSANO_RECOVERY
:
3132 case WSANOTINITIALISED
:
3134 case WSAEINPROGRESS
:
3136 case WSAEAFNOSUPPORT
:
3139 case WSAEPROTONOSUPPORT
:
3141 case WSAESOCKTNOSUPPORT
:
3143 case WSAEWOULDBLOCK
:
3145 case WSAECONNABORTED
:
3149 case WSAECONNREFUSED
:
3151 case WSAEHOSTUNREACH
:
3152 case WSAEADDRNOTAVAIL
:
3156 case WSAENETUNREACH
:
3163 case WSAEPFNOSUPPORT
:
3164 case WSAENOPROTOOPT
:
3165 case WSATYPE_NOT_FOUND
:
3170 ACE_UNUSED_ARG (error
);
3171 #endif /* ACE_WIN32 */
3176 ACE::strndup (const char *str
, size_t n
)
3178 const char *t
= str
;
3181 // Figure out how long this string is (remember, it might not be
3185 len
< n
&& *t
++ != '\0';
3190 #if defined (ACE_HAS_ALLOC_HOOKS)
3191 ACE_ALLOCATOR_RETURN (s
,
3192 (char *) ACE_Allocator::instance()->malloc (len
+ 1),
3195 ACE_ALLOCATOR_RETURN (s
,
3196 (char *) ACE_OS::malloc (len
+ 1),
3198 #endif /* ACE_HAS_ALLOC_HOOKS */
3199 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3202 #if defined (ACE_HAS_WCHAR)
3204 ACE::strndup (const wchar_t *str
, size_t n
)
3206 const wchar_t *t
= str
;
3209 // Figure out how long this string is (remember, it might not be
3213 len
< n
&& *t
++ != '\0';
3217 size_t const size
= (len
+ 1) * sizeof (wchar_t);
3219 #if defined (ACE_HAS_ALLOC_HOOKS)
3220 ACE_ALLOCATOR_RETURN (s
,
3221 static_cast<wchar_t*> (
3222 ACE_Allocator::instance ()->malloc (size
)),
3225 ACE_ALLOCATOR_RETURN (s
,
3226 static_cast<wchar_t*> (ACE_OS::malloc (size
)),
3228 #endif /* ACE_HAS_ALLOC_HOOKS */
3229 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3231 #endif /* ACE_HAS_WCHAR */
3234 ACE::strnnew (const char *str
, size_t n
)
3236 const char *t
= str
;
3239 // Figure out how long this string is (remember, it might not be
3243 len
< n
&& *t
++ != L
'\0';
3249 #if defined (ACE_HAS_ALLOC_HOOKS)
3250 ACE_ALLOCATOR_RETURN (s
,
3251 static_cast<char*> (ACE_Allocator::instance ()->malloc (sizeof (char) * (len
+ 1))),
3257 #endif /* ACE_HAS_ALLOC_HOOKS */
3259 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3262 #if defined (ACE_HAS_WCHAR)
3264 ACE::strnnew (const wchar_t *str
, size_t n
)
3266 const wchar_t *t
= str
;
3269 // Figure out how long this string is (remember, it might not be
3273 len
< n
&& *t
++ != ACE_TEXT_WIDE ('\0');
3281 return ACE_OS::strsncpy (s
, str
, len
+ 1);
3283 #endif /* ACE_HAS_WCHAR */
3286 ACE::strend (const char *s
)
3288 while (*s
++ != '\0')
3294 #if defined ACE_HAS_WCHAR
3296 ACE::strend (const wchar_t *s
)
3298 while (*s
++ != ACE_TEXT_WIDE ('\0'))
3306 ACE::strnew (const char *s
)
3311 #if defined (ACE_HAS_ALLOC_HOOKS)
3312 ACE_ALLOCATOR_RETURN (t
,
3313 static_cast<char*> (ACE_Allocator::instance ()->malloc (sizeof (char) * (ACE_OS::strlen (s
) + 1))),
3317 char [ACE_OS::strlen (s
) + 1],
3319 #endif /* ACE_HAS_ALLOC_HOOKS */
3321 return ACE_OS::strcpy (t
, s
);
3324 #if defined (ACE_HAS_WCHAR)
3326 ACE::strnew (const wchar_t *s
)
3331 size_t const n
= ACE_OS::strlen (s
) + 1;
3333 #if defined (ACE_HAS_ALLOC_HOOKS)
3334 ACE_ALLOCATOR_RETURN (t
,
3335 static_cast<wchar_t*> (
3336 ACE_Allocator::instance ()->malloc (
3337 sizeof (wchar_t) * (n
))),
3340 ACE_NEW_RETURN (t
, wchar_t[n
], 0);
3341 #endif /* ACE_HAS_ALLOC_HOOKS */
3343 return ACE_OS::strcpy (t
, s
);
3345 #endif /* ACE_HAS_WCHAR */
3347 // helper functions for ACE::wild_match()
3350 inline bool equal_char (char a
, char b
, bool case_sensitive
)
3354 return ACE_OS::ace_tolower (a
) == ACE_OS::ace_tolower (b
);
3357 // precond: *p == '[' start of char class
3358 // postcond: *p == ']' end of the char class
3359 inline bool equal_class (char s
, const char *&p
, bool case_sensitive
)
3362 bool negate
= false;
3368 // ] and - are regular in 1st position
3369 for (bool first
= true; *p
&& (first
|| *p
!= ']'); ++p
)
3371 if (!first
&& *p
== '-' && p
[1] != ']')
3373 if (!p
[1] || p
[1] <= p
[-1]) // invalid range
3377 // Since we are in the POSIX locale, only the basic ASCII
3378 // characters are allowed as the range endpoints. These characters
3379 // are the same values in both signed and unsigned chars so we
3380 // don't have to account for any "pathological cases."
3381 for (char range
= static_cast<char> (p
[-1] + 1); range
<= p
[1]; ++range
)
3383 if (equal_char (s
, range
, case_sensitive
))
3385 while (*++p
!= ']') {}
3389 ++p
; // consume the character 1 past the -
3391 else if (equal_char (s
, *p
, case_sensitive
))
3393 while (*++p
!= ']') {}
3403 ACE::wild_match(const char *str
, const char *pat
, bool case_sensitive
,
3404 bool character_classes
)
3408 if (pat
== 0 || str
== 0)
3411 bool star
= false, escape
= false;
3412 const char *s
= str
;
3413 const char *p
= pat
;
3416 if (!escape
&& *p
== '\\')
3421 else if (!escape
&& *p
== '*')
3425 while (*++pat
== '*') {}
3431 else if (!escape
&& *p
== '?')
3436 else if (!escape
&& character_classes
&& *p
== '[')
3438 if (equal_class (*s
, p
, case_sensitive
))
3450 else if (!equal_char (*s
, *p
, case_sensitive
))
3466 while (*++p
== '*') {}
3471 // Close versioned namespace, if enabled by the user.
3472 ACE_END_VERSIONED_NAMESPACE_DECL