Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / SOCK_IO.cpp
blobba586c153b61f0e818ee9b784c9c983ed0baf87c
1 #include "ace/SOCK_IO.h"
3 #include "ace/OS_NS_sys_socket.h"
4 #include "ace/OS_Memory.h"
5 #include "ace/Truncate.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (__ACE_INLINE__)
12 #include "ace/SOCK_IO.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_IO)
19 void
20 ACE_SOCK_IO::dump () const
22 #if defined (ACE_HAS_DUMP)
23 ACE_TRACE ("ACE_SOCK_IO::dump");
24 #endif /* ACE_HAS_DUMP */
27 // Allows a client to read from a socket without having to provide
28 // a buffer to read. This method determines how much data is in the
29 // socket, allocates a buffer of this size, reads in the data, and
30 // returns the number of bytes read.
32 ssize_t
33 ACE_SOCK_IO::recvv (iovec *io_vec,
34 const ACE_Time_Value *timeout) const
36 ACE_TRACE ("ACE_SOCK_IO::recvv");
37 #if defined (FIONREAD)
38 io_vec->iov_base = 0;
39 if( ACE::handle_read_ready (this->get_handle (), timeout) != 1 )
41 return -1;
44 int inlen = 0;
46 if (ACE_OS::ioctl (this->get_handle (),
47 FIONREAD,
48 &inlen) == -1)
49 return -1;
50 else if (inlen > 0)
52 #if defined (ACE_HAS_ALLOC_HOOKS)
53 ACE_ALLOCATOR_RETURN (io_vec->iov_base,
54 static_cast<char*>(ACE_Allocator::instance()->malloc(sizeof(char) * inlen)),
55 -1);
56 #else
57 ACE_NEW_RETURN (io_vec->iov_base,
58 char[inlen],
59 -1);
60 #endif /* ACE_HAS_ALLOC_HOOKS */
62 // It's ok to blindly cast this value since 'inlen' is an int and, thus,
63 // we can't get more than that back. Besides, if the recv() fails, we
64 // don't want that value cast to unsigned and returned.
65 ssize_t recv_len = this->recv (io_vec->iov_base, inlen);
66 if (recv_len > 0)
67 // u_long is the Windows type; size_t is everyone else's. A u_long
68 // should go into a size_t anywhere without an issue.
69 io_vec->iov_len = static_cast<u_long> (recv_len);
70 return recv_len;
72 else
73 return 0;
74 #else
75 ACE_UNUSED_ARG (io_vec);
76 ACE_UNUSED_ARG (timeout);
77 ACE_NOTSUP_RETURN (-1);
78 #endif /* FIONREAD */
81 // Send N char *ptrs and int lengths. Note that the char *'s precede
82 // the ints (basically, an varargs version of writev). The count N is
83 // the *total* number of trailing arguments, *not* a couple of the
84 // number of tuple pairs!
85 #ifndef ACE_LACKS_VA_FUNCTIONS
86 ssize_t
87 ACE_SOCK_IO::send (size_t n, ...) const
89 ACE_TRACE ("ACE_SOCK_IO::send");
91 va_list argp;
92 int const total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
93 iovec *iovp = 0;
94 #if defined (ACE_HAS_ALLOCA)
95 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
96 #else
97 # ifdef ACE_HAS_ALLOC_HOOKS
98 ACE_ALLOCATOR_RETURN (iovp, (iovec *)
99 ACE_Allocator::instance ()->malloc (total_tuples *
100 sizeof (iovec)),
101 -1);
102 # else
103 ACE_NEW_RETURN (iovp,
104 iovec[total_tuples],
105 -1);
106 # endif /* ACE_HAS_ALLOC_HOOKS */
107 #endif /* !defined (ACE_HAS_ALLOCA) */
109 va_start (argp, n);
111 for (int i = 0; i < total_tuples; i++)
113 iovp[i].iov_base = va_arg (argp, char *);
114 iovp[i].iov_len = va_arg (argp, int);
117 ssize_t const result = ACE_OS::sendv (this->get_handle (),
118 iovp,
119 total_tuples);
120 #if !defined (ACE_HAS_ALLOCA)
121 # ifdef ACE_HAS_ALLOC_HOOKS
122 ACE_Allocator::instance ()->free (iovp);
123 # else
124 delete [] iovp;
125 # endif /* ACE_HAS_ALLOC_HOOKS */
126 #endif /* !defined (ACE_HAS_ALLOCA) */
127 va_end (argp);
128 return result;
131 // This is basically an interface to ACE_OS::readv, that doesn't use
132 // the struct iovec_Base explicitly. The ... can be passed as an arbitrary
133 // number of (char *ptr, int len) tuples. However, the count N is the
134 // *total* number of trailing arguments, *not* a couple of the number
135 // of tuple pairs!
137 ssize_t
138 ACE_SOCK_IO::recv (size_t n, ...) const
140 ACE_TRACE ("ACE_SOCK_IO::recv");
142 va_list argp;
143 int const total_tuples = ACE_Utils::truncate_cast<int> (n / 2);
144 iovec *iovp;
145 #if defined (ACE_HAS_ALLOCA)
146 iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
147 #else
148 # ifdef ACE_HAS_ALLOC_HOOKS
149 ACE_ALLOCATOR_RETURN (iovp, (iovec *)
150 ACE_Allocator::instance ()->malloc (total_tuples *
151 sizeof (iovec)),
152 -1);
153 # else
154 ACE_NEW_RETURN (iovp,
155 iovec[total_tuples],
156 -1);
157 # endif /* ACE_HAS_ALLOC_HOOKS */
158 #endif /* !defined (ACE_HAS_ALLOCA) */
160 va_start (argp, n);
162 for (int i = 0; i < total_tuples; i++)
164 iovp[i].iov_base = va_arg (argp, char *);
165 iovp[i].iov_len = va_arg (argp, int);
168 ssize_t const result = ACE_OS::recvv (this->get_handle (),
169 iovp,
170 total_tuples);
171 #if !defined (ACE_HAS_ALLOCA)
172 # ifdef ACE_HAS_ALLOC_HOOKS
173 ACE_Allocator::instance ()->free (iovp);
174 # else
175 delete [] iovp;
176 # endif /* ACE_HAS_ALLOC_HOOKS */
177 #endif /* !defined (ACE_HAS_ALLOCA) */
178 va_end (argp);
179 return result;
181 #endif // ACE_LACKS_VA_FUNCTIONS
183 ACE_END_VERSIONED_NAMESPACE_DECL