Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / SPIPE_Stream.inl
blobe52600d48888ce3a5aa4ade4e358933e0fd4ae31
1 // -*- C++ -*-
2 //
3 // $Id: SPIPE_Stream.inl 80826 2008-03-04 14:51:23Z wotte $
5 #include "ace/OS_NS_sys_uio.h"
6 #include "ace/OS_NS_errno.h"
7 #include "ace/OS_NS_unistd.h"
8 #if defined (ACE_WIN32)
9 #include "ace/OS_NS_sys_socket.h"
10 #endif
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 // Create an ACE_SPIPE_Stream.
16 ACE_INLINE int
17 ACE_SPIPE_Stream::get_remote_addr (ACE_SPIPE_Addr &remote_sap) const
19   ACE_TRACE ("ACE_SPIPE_Stream::get_remote_addr");
20   remote_sap = this->remote_addr_;
21   return 0;
24 // Send exactly N bytes from BUF to this socket.  Keeping trying until
25 // this many bytes are sent.
27 ACE_INLINE ssize_t
28 ACE_SPIPE_Stream::send_n (const void *buf, size_t n) const
30   ACE_TRACE ("ACE_SPIPE_Stream::send_n");
31   return ACE::write_n (this->get_handle (), buf, n);
34 // Receive exactly N bytes from this socket into BUF.  Keep trying
35 // until this many bytes are received.
37 ACE_INLINE ssize_t
38 ACE_SPIPE_Stream::recv_n (void *buf, size_t n) const
40   ACE_TRACE ("ACE_SPIPE_Stream::recv_n");
41   return ACE::read_n (this->get_handle (), buf, n);
44 ACE_INLINE ssize_t
45 ACE_SPIPE_Stream::send (const void *buf, size_t n) const
47   ACE_TRACE ("ACE_SPIPE_Stream::send");
48   return ACE_OS::write (this->get_handle (), (const char *) buf, n);
51 ACE_INLINE ssize_t
52 ACE_SPIPE_Stream::recv (void *buf, size_t n) const
54   ACE_TRACE ("ACE_SPIPE_Stream::recv");
55   return ACE_OS::read (this->get_handle (), (char *) buf, n);
58 ACE_INLINE ssize_t
59 ACE_SPIPE_Stream::send (const ACE_Str_Buf *cntl, const ACE_Str_Buf *data, int flags) const
61   ACE_TRACE ("ACE_SPIPE_Stream::send");
62   return ACE_OS::putmsg (this->get_handle (), (strbuf *) cntl, (strbuf *) data, flags);
65 ACE_INLINE ssize_t
66 ACE_SPIPE_Stream::recv (ACE_Str_Buf *cntl, ACE_Str_Buf *data, int *flags) const
68   ACE_TRACE ("ACE_SPIPE_Stream::recv");
69   return ACE_OS::getmsg (this->get_handle (), (strbuf *) cntl, (strbuf *) data, flags);
72 ACE_INLINE ssize_t
73 ACE_SPIPE_Stream::send (const ACE_Str_Buf *cntl, const ACE_Str_Buf *data, int band, int flags) const
75   ACE_TRACE ("ACE_SPIPE_Stream::send");
76   return ACE_OS::putpmsg (this->get_handle (), (strbuf *) cntl, (strbuf *) data, band, flags);
79 ACE_INLINE ssize_t
80 ACE_SPIPE_Stream::recv (ACE_Str_Buf *cntl, ACE_Str_Buf *data, int *band, int *flags) const
82   ACE_TRACE ("ACE_SPIPE_Stream::recv");
83   return ACE_OS::getpmsg (this->get_handle (), (strbuf *) cntl, (strbuf *) data, band, flags);
86 ACE_INLINE ssize_t
87 ACE_SPIPE_Stream::send (const iovec iov[], int n) const
89   ACE_TRACE ("ACE_SPIPE_Stream::send");
90   return ACE_OS::writev (this->get_handle (), iov, n);
93 ACE_INLINE ssize_t
94 ACE_SPIPE_Stream::recv (iovec iov[], int n) const
96   ACE_TRACE ("ACE_SPIPE_Stream::recv");
97   return ACE_OS::readv (this->get_handle (), iov, n);
100 // This routine sends an open file descriptor to this socket.
102 ACE_INLINE int
103 ACE_SPIPE_Stream::send_handle (ACE_HANDLE handle) const
105   ACE_TRACE ("ACE_SPIPE_Stream::send_handle");
106 #if defined (ACE_HAS_STREAM_PIPES)
107   return ACE_OS::ioctl (this->get_handle (), I_SENDFD, (void *) handle);
108 #elif defined (ACE_WIN32) && \
109       (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)) && \
110        !defined (ACE_HAS_WINCE)   /* CE4 has WS2 but not WSADuplicateSocket */
111   DWORD procID;
112   WSAPROTOCOL_INFO protInfo;
113   ssize_t res = this->recv(&procID, sizeof(procID));
114   if (res != sizeof(procID))
115   {
116     if(res != -1)
117         errno = ENXIO;
118     return -1;
119   }
120   if (::WSADuplicateSocket ((SOCKET)handle, procID, &protInfo) == -1)
121   {
122     ACE_OS::set_errno_to_wsa_last_error();
123       return -1;
124   }
125   res = this->send(&protInfo, sizeof(protInfo));
126   if (res != sizeof(protInfo))
127   {
128     if(res != -1)
129         errno = ENXIO;
130     return -1;
131   }
132   // This is just for synchronization, we will ignore the data
133   res = this->recv(&procID, sizeof(procID));
134   if (res != sizeof(procID))
135   {
136     if(res != -1)
137         errno = ENXIO;
138     return -1;
139   }
140   return 0;
141 #else
142   handle = handle;
143   ACE_NOTSUP_RETURN (-1);
144 #endif /* ACE_HAS_STREAM_PIPES */
147 // This file receives an open file descriptor from this socket.
149 ACE_INLINE int
150 ACE_SPIPE_Stream::recv_handle (ACE_HANDLE &handle) const
152   ACE_TRACE ("ACE_SPIPE_Stream::recv_handle");
153 #if defined (ACE_HAS_STREAM_PIPES)
154   strrecvfd recvfd;
156   if (ACE_OS::ioctl (this->get_handle (), I_RECVFD, (void *) &recvfd) == -1)
157     return -1;
158   else
159     {
160       handle = recvfd.fd;
161       return 0;
162     }
163 #elif defined (ACE_WIN32) && \
164       (defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0))
165   pid_t procID = ACE_OS::getpid();
166   WSAPROTOCOL_INFO protInfo;
167   ssize_t res = this->send(&procID, sizeof(procID));
168   if (res != sizeof(procID))
169   {
170     if(res != -1)
171         errno = ENXIO;
172     return -1;
173   }
174   res = this->recv(&protInfo, sizeof(protInfo));
175   if (res != sizeof(protInfo))
176   {
177     if(res != -1)
178         errno = ENXIO;
179      return -1;
180   }
181   handle = ACE_OS::socket (FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
182                            &protInfo, 0, 0);
183   if (handle == ACE_INVALID_HANDLE)
184   {
185     return -1;
186   }
187   // Since it does not matter what the data is, just send something to
188   // synchronize the end of the exchange
189   res = this->send(&procID, sizeof(procID));
190   if (res != sizeof(procID))
191   {
192     if(res != -1)
193         errno = ENXIO;
194     return -1;
195   }
196   return 0;
197 #else
198   handle = handle;
199   ACE_NOTSUP_RETURN (-1);
200 #endif /* ACE_HAS_STREAM_PIPES */
203 // This file receives an open file descriptor from this socket and
204 // also passes back the information about the address...
206 ACE_INLINE int
207 ACE_SPIPE_Stream::recv_handle (strrecvfd &recvfd) const
209   ACE_TRACE ("ACE_SPIPE_Stream::recv_handle");
210 #if defined (ACE_HAS_STREAM_PIPES)
211   return ACE_OS::ioctl (this->get_handle (), I_RECVFD, (void *) &recvfd);
212 #else
213   ACE_UNUSED_ARG (recvfd);
214   ACE_NOTSUP_RETURN (-1);
215 #endif /* ACE_HAS_STREAM_PIPES */
218 ACE_INLINE ssize_t
219 ACE_SPIPE_Stream::send (const void *buf, size_t n,
220                         ACE_OVERLAPPED *overlapped) const
222   ACE_TRACE ("ACE_SPIPE_Stream::send");
223   return ACE_OS::write (this->get_handle (),
224                         (const char *) buf, n,
225                         overlapped);
228 ACE_INLINE ssize_t
229 ACE_SPIPE_Stream::recv (void *buf, size_t n,
230                         ACE_OVERLAPPED *overlapped) const
232   ACE_TRACE ("ACE_SPIPE_Stream::recv");
233   return ACE_OS::read (this->get_handle (),
234                        (char *) buf, n,
235                        overlapped);
238 ACE_INLINE ssize_t
239 ACE_SPIPE_Stream::sendv_n (const iovec iov[],
240                            int n) const
242   ACE_TRACE ("ACE_SPIPE_Stream::sendv_n");
243   return ACE::writev_n (this->get_handle (),
244                         iov,
245                         n);
248 // Recv an n byte message from the Stream.
250 ACE_INLINE ssize_t
251 ACE_SPIPE_Stream::recvv_n (iovec iov[],
252                            int n) const
254   ACE_TRACE ("ACE_SPIPE_Stream::recvv_n");
255   // @@ Carlos, can you please update this to call the
256   // new ACE::recvv_n() method that you write?
257   return ACE_OS::readv (this->get_handle (),
258                         iov,
259                         n);
262 // Send an <iovec> of size <n> to the Stream.
264 ACE_INLINE ssize_t
265 ACE_SPIPE_Stream::sendv (const iovec iov[],
266                          int n) const
268   ACE_TRACE ("ACE_SPIPE_Stream::sendv");
269   return ACE_OS::writev (this->get_handle (),
270                          iov,
271                          n);
274 ACE_END_VERSIONED_NAMESPACE_DECL