Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SOCK_IO.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // SOCK_IO.cpp
00003 // $Id: SOCK_IO.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $
00004 
00005 #include "ace/SOCK_IO.h"
00006 #include "ace/Handle_Set.h"
00007 
00008 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00009 #include "ace/SOCK_IO.i"
00010 #endif /* ACE_LACKS_INLINE_FUNCTIONS */
00011 
00012 ACE_RCSID(ace, SOCK_IO, "$Id: SOCK_IO.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00013 
00014 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_IO)
00015 
00016 void
00017 ACE_SOCK_IO::dump (void) const
00018 {
00019   ACE_TRACE ("ACE_SOCK_IO::dump");
00020 }
00021 
00022 // Allows a client to read from a socket without having to provide
00023 // a buffer to read.  This method determines how much data is in the
00024 // socket, allocates a buffer of this size, reads in the data, and
00025 // returns the number of bytes read.
00026 
00027 ssize_t
00028 ACE_SOCK_IO::recvv (iovec *io_vec,
00029                     const ACE_Time_Value *timeout) const
00030 {
00031   ACE_TRACE ("ACE_SOCK_IO::recvv");
00032 #if defined (FIONREAD)
00033   ACE_Handle_Set handle_set;
00034   handle_set.reset ();
00035   handle_set.set_bit (this->get_handle ());
00036 
00037   io_vec->iov_base = 0;
00038 
00039   // Check the status of the current socket.
00040   int select_width;
00041 #  if defined (ACE_WIN64)
00042   // This arg is ignored on Windows and causes pointer truncation
00043   // warnings on 64-bit compiles.
00044   select_width = 0;
00045 #  else
00046   select_width = int (this->get_handle ()) + 1;
00047 #  endif /* ACE_WIN64 */
00048   switch (ACE_OS::select (select_width,
00049                           handle_set,
00050                           0, 0,
00051                           timeout))
00052     {
00053     case -1:
00054       return -1;
00055       /* NOTREACHED */
00056     case 0:
00057       errno = ETIME;
00058       return -1;
00059       /* NOTREACHED */
00060     default:
00061       // Goes fine, fallthrough to get data
00062       break;
00063     }
00064 
00065   u_long inlen;
00066 
00067   if (ACE_OS::ioctl (this->get_handle (),
00068                      FIONREAD,
00069                      (u_long *) &inlen) == -1)
00070     return -1;
00071   else if (inlen > 0)
00072     {
00073       ACE_NEW_RETURN (io_vec->iov_base,
00074                       char[inlen],
00075                       -1);
00076       io_vec->iov_len = this->recv (io_vec->iov_base,
00077                                     inlen);
00078       return io_vec->iov_len;
00079     }
00080   else
00081     return 0;
00082 #else
00083   ACE_UNUSED_ARG (io_vec);
00084   ACE_UNUSED_ARG (timeout);
00085   ACE_NOTSUP_RETURN (-1);
00086 #endif /* FIONREAD */
00087 }
00088 
00089 // Send N char *ptrs and int lengths.  Note that the char *'s precede
00090 // the ints (basically, an varargs version of writev).  The count N is
00091 // the *total* number of trailing arguments, *not* a couple of the
00092 // number of tuple pairs!
00093 
00094 ssize_t
00095 ACE_SOCK_IO::send (size_t n, ...) const
00096 {
00097   ACE_TRACE ("ACE_SOCK_IO::send");
00098 
00099   va_list argp;
00100   int total_tuples = ACE_static_cast (int, n) / 2;
00101   iovec *iovp;
00102 #if defined (ACE_HAS_ALLOCA)
00103   iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
00104 #else
00105   ACE_NEW_RETURN (iovp,
00106                   iovec[total_tuples],
00107                   -1);
00108 #endif /* !defined (ACE_HAS_ALLOCA) */
00109 
00110   va_start (argp, n);
00111 
00112   for (int i = 0; i < total_tuples; i++)
00113     {
00114       iovp[i].iov_base = va_arg (argp, char *);
00115       iovp[i].iov_len = va_arg (argp, int);
00116     }
00117 
00118   ssize_t result = ACE_OS::sendv (this->get_handle (),
00119                                   iovp,
00120                                   total_tuples);
00121 #if !defined (ACE_HAS_ALLOCA)
00122   delete [] iovp;
00123 #endif /* !defined (ACE_HAS_ALLOCA) */
00124   va_end (argp);
00125   return result;
00126 }
00127 
00128 // This is basically an interface to ACE_OS::readv, that doesn't use
00129 // the struct iovec_Base explicitly.  The ... can be passed as an arbitrary
00130 // number of (char *ptr, int len) tuples.  However, the count N is the
00131 // *total* number of trailing arguments, *not* a couple of the number
00132 // of tuple pairs!
00133 
00134 ssize_t
00135 ACE_SOCK_IO::recv (size_t n, ...) const
00136 {
00137   ACE_TRACE ("ACE_SOCK_IO::recv");
00138 
00139   va_list argp;
00140   int total_tuples = ACE_static_cast (int, (n / 2));
00141   iovec *iovp;
00142 #if defined (ACE_HAS_ALLOCA)
00143   iovp = (iovec *) alloca (total_tuples * sizeof (iovec));
00144 #else
00145   ACE_NEW_RETURN (iovp,
00146                   iovec[total_tuples],
00147                   -1);
00148 #endif /* !defined (ACE_HAS_ALLOCA) */
00149 
00150   va_start (argp, n);
00151 
00152   for (int i = 0; i < total_tuples; i++)
00153     {
00154       iovp[i].iov_base = va_arg (argp, char *);
00155       iovp[i].iov_len = va_arg (argp, int);
00156     }
00157 
00158   ssize_t result = ACE_OS::recvv (this->get_handle (),
00159                                   iovp,
00160                                   total_tuples);
00161 #if !defined (ACE_HAS_ALLOCA)
00162   delete [] iovp;
00163 #endif /* !defined (ACE_HAS_ALLOCA) */
00164   va_end (argp);
00165   return result;
00166 }

Generated on Mon Jun 16 11:21:21 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002