My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
socket.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 #ifndef _OSL_SOCKET_HXX_
29 #define _OSL_SOCKET_HXX_
30 
31 #include <osl/socket_decl.hxx>
32 
33 namespace osl
34 {
35  //______________________________________________________________________________
38  {}
39 
40  //______________________________________________________________________________
41  inline SocketAddr::SocketAddr(const SocketAddr& Addr)
42  : m_handle( osl_copySocketAddr( Addr.m_handle ) )
43  {
44  }
45 
46  //______________________________________________________________________________
48  : m_handle( osl_copySocketAddr( Addr ) )
49  {
50  }
51 
52  //______________________________________________________________________________
54  : m_handle( Addr )
55  {
56  }
57 
58  //______________________________________________________________________________
59  inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
60  : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
61  {
62  if(! m_handle )
63  {
64  m_handle = osl_resolveHostname(strAddrOrHostName.pData);
65 
66  // host found?
67  if(m_handle)
68  {
70  }
71  else
72  {
74  m_handle = 0;
75  }
76  }
77  }
78 
79  //______________________________________________________________________________
81  {
82  if( m_handle )
84  }
85 
86  //______________________________________________________________________________
87  inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
88  {
89  ::rtl::OUString hostname;
90  oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
91  if( pResult )
92  *pResult = result;
93  return hostname;
94  }
95 
96  //______________________________________________________________________________
97  inline sal_Int32 SAL_CALL SocketAddr::getPort() const
98  {
100  }
101 
102  //______________________________________________________________________________
103  inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
104  {
105  return osl_setInetPortOfSocketAddr(m_handle, nPort );
106  }
107 
108  inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
109  {
110  *this = SocketAddr( sDottedIpOrHostname , getPort() );
111  return is();
112  }
113 
114  //______________________________________________________________________________
115  inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
116  {
117  return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
118  == osl_Socket_Ok;
119  }
120 
121  inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
122  {
123  ::rtl::ByteSequence sequence;
125  if( pResult )
126  *pResult = result;
127  return sequence;
128  }
129 
130  //______________________________________________________________________________
132  {
133  oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
134  if( m_handle )
136  m_handle = pNewAddr;
137  return *this;
138  }
139 
140  //______________________________________________________________________________
141  inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
142  {
143  *this = (Addr.getHandle());
144  return *this;
145  }
146 
148  {
149  if( m_handle )
151  m_handle = Addr;
152  return *this;
153  }
154 
155  //______________________________________________________________________________
156  inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
157  {
158  return osl_isEqualSocketAddr( m_handle, Addr );
159  }
160 
162  {
163  return m_handle;
164  }
165 
166  //______________________________________________________________________________
167  inline sal_Bool SocketAddr::is() const
168  {
169  return m_handle != 0;
170  }
171 
172  // (static method)______________________________________________________________
173  inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
174  {
175  ::rtl::OUString hostname;
176  oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
177  if(pResult )
178  *pResult = result;
179  return hostname;
180  }
181 
182  // (static method)______________________________________________________________
183  inline void SAL_CALL SocketAddr::resolveHostname(
184  const ::rtl::OUString & strHostName, SocketAddr &Addr)
185  {
186  Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
187  }
188 
189  // (static method)______________________________________________________________
190  inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
191  const ::rtl::OUString& strServiceName,
192  const ::rtl::OUString & strProtocolName )
193  {
194  return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
195  }
196 
197  //______________________________________________________________________________
199  oslAddrFamily Family,
200  oslProtocol Protocol)
201  : m_handle( osl_createSocket(Family, Type, Protocol) )
202  {}
203 
204  //______________________________________________________________________________
205  inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
206  : m_handle( socketHandle )
207  {}
208 
209  //______________________________________________________________________________
210  inline Socket::Socket( oslSocket socketHandle )
211  : m_handle( socketHandle )
212  {
214  }
215 
216  //______________________________________________________________________________
217  inline Socket::Socket( const Socket & socket )
218  : m_handle( socket.getHandle() )
219  {
221  }
222 
223  //______________________________________________________________________________
225  {
227  }
228 
229  //______________________________________________________________________________
230  inline Socket& Socket::operator= ( oslSocket socketHandle)
231  {
232  osl_acquireSocket( socketHandle );
234  m_handle = socketHandle;
235  return *this;
236  }
237 
238  //______________________________________________________________________________
239  inline Socket& Socket::operator= (const Socket& sock)
240  {
241  return (*this) = sock.getHandle();
242  }
243 
244  //______________________________________________________________________________
245  inline sal_Bool Socket::operator==( const Socket& rSocket ) const
246  {
247  return m_handle == rSocket.getHandle();
248  }
249 
250  //______________________________________________________________________________
251  inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
252  {
253  return m_handle == socketHandle;
254  }
255 
256  //______________________________________________________________________________
257  inline void Socket::shutdown( oslSocketDirection Direction )
258  {
259  osl_shutdownSocket( m_handle , Direction );
260  }
261 
262  //______________________________________________________________________________
263  inline void Socket::close()
264  {
266  }
267 
268  //______________________________________________________________________________
269  inline void Socket::getLocalAddr( SocketAddr & addr) const
270  {
272  }
273 
274  //______________________________________________________________________________
275  inline sal_Int32 Socket::getLocalPort() const
276  {
277  SocketAddr addr( 0 );
278  getLocalAddr( addr );
279  return addr.getPort();
280  }
281 
282  //______________________________________________________________________________
283  inline ::rtl::OUString Socket::getLocalHost() const
284  {
285  SocketAddr addr( 0 );
286  getLocalAddr( addr );
287  return addr.getHostname();
288  }
289 
290  //______________________________________________________________________________
291  inline void Socket::getPeerAddr( SocketAddr &addr ) const
292  {
294  }
295 
296  //______________________________________________________________________________
297  inline sal_Int32 Socket::getPeerPort() const
298  {
299  SocketAddr addr( 0 );
300  getPeerAddr( addr );
301  return addr.getPort();
302  }
303 
304  //______________________________________________________________________________
305  inline ::rtl::OUString Socket::getPeerHost() const
306  {
307  SocketAddr addr( 0 );
308  getPeerAddr( addr );
309  return addr.getHostname();
310  }
311 
312  //______________________________________________________________________________
313  inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
314  {
315  return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
316  }
317 
318  //______________________________________________________________________________
319  inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const
320  {
321  return osl_isReceiveReady( m_handle , pTimeout );
322  }
323 
324  //______________________________________________________________________________
325  inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const
326  {
327  return osl_isSendReady( m_handle, pTimeout );
328  }
329 
330  //______________________________________________________________________________
331  inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
332  {
333  return osl_isExceptionPending( m_handle, pTimeout );
334  }
335 
336  //______________________________________________________________________________
338  {
339  return osl_getSocketType( m_handle );
340  }
341 
342  //______________________________________________________________________________
343  inline sal_Int32 Socket::getOption(
344  oslSocketOption Option,
345  void* pBuffer,
346  sal_uInt32 BufferLen,
347  oslSocketOptionLevel Level) const
348  {
349  return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
350  }
351 
352  //______________________________________________________________________________
354  void* pBuffer,
355  sal_uInt32 BufferLen,
356  oslSocketOptionLevel Level ) const
357  {
358  return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
359  }
360 
361  //______________________________________________________________________________
362  inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue )
363  {
364  return setOption( option, &nValue, sizeof( nValue ) );
365  }
366 
367  //______________________________________________________________________________
368  inline sal_Int32 Socket::getOption( oslSocketOption option ) const
369  {
370  sal_Int32 n;
371  getOption( option, &n, sizeof( n ) );
372  return n;
373  }
374 
375  //______________________________________________________________________________
377  {
378  return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
379  }
380 
381  //______________________________________________________________________________
383  {
385  }
386 
387  //______________________________________________________________________________
388  inline void SAL_CALL Socket::clearError() const
389  {
390  sal_Int32 err = 0;
391  getOption(osl_Socket_OptionError, &err, sizeof(err));
392  }
393 
394  //______________________________________________________________________________
396  {
398  }
399 
400  //______________________________________________________________________________
401  inline ::rtl::OUString Socket::getErrorAsString( ) const
402  {
403  ::rtl::OUString error;
404  osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
405  return error;
406  }
407 
408  //______________________________________________________________________________
410  {
411  return m_handle;
412  }
413 
414  //______________________________________________________________________________
416  oslProtocol Protocol,
417  oslSocketType Type )
418  : Socket( Type, Family, Protocol )
419  {}
420 
421  //______________________________________________________________________________
422  inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
423  : Socket( socketHandle, noacquire )
424  {}
425 
426  //______________________________________________________________________________
427  inline StreamSocket::StreamSocket( oslSocket socketHandle )
428  : Socket( socketHandle )
429  {}
430 
431  //______________________________________________________________________________
432  inline StreamSocket::StreamSocket( const StreamSocket & socket )
433  : Socket( socket )
434  {}
435 
436  //______________________________________________________________________________
437  inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
438  {
439  return osl_readSocket( m_handle, pBuffer, n );
440  }
441 
442  //______________________________________________________________________________
443  inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
444  {
445  return osl_writeSocket( m_handle, pBuffer, n );
446  }
447 
448 
449  //______________________________________________________________________________
450  inline sal_Int32 StreamSocket::recv(void* pBuffer,
451  sal_uInt32 BytesToRead,
452  oslSocketMsgFlag Flag)
453  {
454  return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
455  }
456 
457  //______________________________________________________________________________
458  inline sal_Int32 StreamSocket::send(const void* pBuffer,
459  sal_uInt32 BytesToSend,
460  oslSocketMsgFlag Flag)
461  {
462  return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
463  }
464 
465  //______________________________________________________________________________
467  oslProtocol Protocol,
468  oslSocketType Type)
469  : StreamSocket( Family, Protocol ,Type )
470  {}
471 
472  //______________________________________________________________________________
474  const TimeValue* pTimeout )
475  {
476  return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
477  }
478 
479  //______________________________________________________________________________
481  oslProtocol Protocol ,
482  oslSocketType Type )
483  : Socket( Type, Family, Protocol )
484  {}
485 
486  //______________________________________________________________________________
487  inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
488  {
489  return osl_listenOnSocket( m_handle, MaxPendingConnections );
490  }
491 
492  //______________________________________________________________________________
494  {
497  if( o )
498  {
499  Connection = StreamSocket( o , SAL_NO_ACQUIRE );
500  }
501  else
502  {
503  Connection = StreamSocket();
504  status = osl_Socket_Error;
505  }
506  return status;
507  }
508 
509  //______________________________________________________________________________
511  StreamSocket& Connection, SocketAddr & PeerAddr)
512  {
513  // TODO change in/OUT parameter
516  if( o )
517  {
518  Connection = StreamSocket( o , SAL_NO_ACQUIRE );
519  }
520  else
521  {
522  Connection = StreamSocket();
523  status = osl_Socket_Error;
524  }
525  return status;
526  }
527 
528  //______________________________________________________________________________
530  oslProtocol Protocol,
531  oslSocketType Type)
532  : Socket( Type, Family, Protocol )
533  {}
534 
535  //______________________________________________________________________________
536  inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer,
537  sal_uInt32 BufferSize,
538  SocketAddr* pSenderAddr,
539  oslSocketMsgFlag Flag )
540  {
541  sal_Int32 nByteRead;
542  if( pSenderAddr )
543  {
544  // TODO : correct the out-parameter pSenderAddr outparameter
545  nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
546  BufferSize, Flag);
547 // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
548 // BufferSize, Flag);
549  }
550  else
551  {
552  nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize , Flag );
553  }
554  return nByteRead;
555  }
556 
557  //______________________________________________________________________________
558  inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
559  const void* pBuffer,
560  sal_uInt32 BufferSize,
561  oslSocketMsgFlag Flag )
562  {
563  return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
564  }
565 }
566 #endif
567 
568 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */