1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: socket.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #include <rtl/memory.h>
35 #include <vos/socket.hxx>
36 #include <vos/diagnose.hxx>
37 //#include <osl/tools.h>
42 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocketAddr
, vos
),
43 VOS_NAMESPACE(OSocketAddr
, vos
),
44 VOS_NAMESPACE(OObject
, vos
), 0);
47 /*****************************************************************************/
49 /*****************************************************************************/
50 OSocketAddr::OSocketAddr()
55 /*****************************************************************************/
57 /*****************************************************************************/
58 OSocketAddr::OSocketAddr(oslSocketAddr Addr
)
63 /*****************************************************************************/
65 /*****************************************************************************/
66 OSocketAddr::OSocketAddr(const OSocketAddr
& Addr
) :
67 ISocketAddr(), OObject()
69 m_SockAddr
= osl_copySocketAddr((oslSocketAddr
)Addr
);
72 /*****************************************************************************/
74 /*****************************************************************************/
75 OSocketAddr::~OSocketAddr()
77 osl_destroySocketAddr(m_SockAddr
);
81 /*****************************************************************************/
83 /*****************************************************************************/
84 OSocketAddr::TAddrFamily
OSocketAddr::getFamily() const
86 return (TAddrFamily
)osl_getFamilyOfSocketAddr(m_SockAddr
);
89 /*****************************************************************************/
90 // operator oslSocketAddr ()
91 /*****************************************************************************/
92 OSocketAddr::operator oslSocketAddr() const
97 /*****************************************************************************/
99 /*****************************************************************************/
100 OSocketAddr::TResult
OSocketAddr::getHostname(rtl::OUString
& rBuffer
) const
102 return (TResult
)osl_getHostnameOfSocketAddr(m_SockAddr
, &rBuffer
.pData
);
105 /*****************************************************************************/
106 // getLocalHostname()
107 /*****************************************************************************/
108 OSocketAddr::TResult
OSocketAddr::getLocalHostname( rtl::OUString
& pBuffer
)
110 return (TResult
)osl_getLocalHostname( &pBuffer
.pData
);
113 /*****************************************************************************/
115 /*****************************************************************************/
116 oslSocketAddr
OSocketAddr::resolveHostname(const rtl::OUString
& ustrHostname
)
118 return osl_resolveHostname( ustrHostname
.pData
);
121 /*****************************************************************************/
122 // operator= (oslSocketAddr Addr)
123 /*****************************************************************************/
124 void OSocketAddr::operator= (oslSocketAddr Addr
)
127 osl_destroySocketAddr(m_SockAddr
);
133 /*****************************************************************************/
134 // operator== (oslSocketAddr Addr)
135 /*****************************************************************************/
136 sal_Bool
OSocketAddr::operator== (oslSocketAddr Addr
)
138 return (osl_isEqualSocketAddr(m_SockAddr
, Addr
));
141 /*****************************************************************************/
142 // operator=(const OSocketAddr& Addr)
143 /*****************************************************************************/
144 OSocketAddr
& OSocketAddr::operator=(const OSocketAddr
& Addr
)
147 osl_destroySocketAddr(m_SockAddr
);
150 m_SockAddr
= osl_copySocketAddr((oslSocketAddr
)Addr
);
156 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OInetSocketAddr
, vos
),
157 VOS_NAMESPACE(OInetSocketAddr
, vos
),
158 VOS_NAMESPACE(OSocketAddr
, vos
), 0);
162 /*****************************************************************************/
164 // creates arbitrary inet-address (INADDR_ANY)
165 /*****************************************************************************/
166 OInetSocketAddr::OInetSocketAddr()
168 m_SockAddr
= osl_createEmptySocketAddr(osl_Socket_FamilyInet
);
171 /*****************************************************************************/
173 // uses the given oslSocketAddr as its own
174 /*****************************************************************************/
175 OInetSocketAddr::OInetSocketAddr(oslSocketAddr Addr
) :
178 VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr
) == osl_Socket_FamilyInet
);
181 /*****************************************************************************/
183 // Create a socket address either from a dotted decimal address
184 //(e.g. 141.99.128.50) or a hostname (e.g. www.stardiv.de).
185 /*****************************************************************************/
186 OInetSocketAddr::OInetSocketAddr(const rtl::OUString
& ustrAddrOrHostname
, sal_Int32 Port
)
188 // first try as dotted address.
189 m_SockAddr
= osl_createInetSocketAddr(ustrAddrOrHostname
.pData
, Port
);
191 // create failed, maybe it's an hostname
194 m_SockAddr
= osl_resolveHostname(ustrAddrOrHostname
.pData
);
199 // set port will fail if addrtype is not osl_Socket_FamilyInet
200 VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr
, Port
));
205 /*****************************************************************************/
206 // OInetSocketAddr(const OInetSocketAddr&)
207 /*****************************************************************************/
208 OInetSocketAddr::OInetSocketAddr(const OInetSocketAddr
& sa
) :
211 VOS_ASSERT(getFamily() == TFamily_Inet
);
214 /*****************************************************************************/
215 // OInetSocketAddr(const OSocketAddr&)
216 /*****************************************************************************/
217 OInetSocketAddr::OInetSocketAddr(const OSocketAddr
& sa
) :
220 VOS_ASSERT(sa
.getFamily() == TFamily_Inet
);
223 /*****************************************************************************/
225 /*****************************************************************************/
226 OInetSocketAddr::~OInetSocketAddr()
230 /*****************************************************************************/
231 // operator= (oslSocketAddr Addr)
232 /*****************************************************************************/
233 void OInetSocketAddr::operator= (oslSocketAddr Addr
)
235 VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr
) == osl_Socket_FamilyInet
,
236 "oslSocketAddr not of type osl_Socket_FamilyInet!");
238 OSocketAddr::operator=(Addr
);
241 /*****************************************************************************/
242 // operator== (oslSocketAddr Addr)
243 /*****************************************************************************/
244 sal_Bool
OInetSocketAddr::operator== (oslSocketAddr Addr
)
246 return (osl_isEqualSocketAddr(m_SockAddr
, Addr
));
249 /*****************************************************************************/
250 // operator=(const OInetSocketAddr& Addr)
251 /*****************************************************************************/
252 OInetSocketAddr
& OInetSocketAddr::operator=(const OInetSocketAddr
& Addr
)
254 VOS_ASSERT(Addr
.getFamily() == TFamily_Inet
);
256 OSocketAddr::operator=(Addr
);
261 /*****************************************************************************/
262 // operator=(const OSocketAddr& Addr)
263 /*****************************************************************************/
264 OInetSocketAddr
& OInetSocketAddr::operator=(const OSocketAddr
& Addr
)
266 VOS_ASSERT(Addr
.getFamily() == TFamily_Inet
);
268 OSocketAddr::operator=(Addr
);
273 /*****************************************************************************/
275 /*****************************************************************************/
276 sal_Int32
OInetSocketAddr::getServicePort(const rtl::OUString
& ustrServiceName
,
277 const rtl::OUString
& ustrProtocolName
)
279 return osl_getServicePort(ustrServiceName
.pData
, ustrProtocolName
.pData
);
283 /*****************************************************************************/
285 /*****************************************************************************/
286 sal_Int32
OInetSocketAddr::getPort () const
288 return osl_getInetPortOfSocketAddr(m_SockAddr
);
292 /*****************************************************************************/
294 /*****************************************************************************/
295 sal_Bool
OInetSocketAddr::setPort (sal_Int32 Port
)
297 return osl_setInetPortOfSocketAddr(m_SockAddr
, Port
);
301 /*****************************************************************************/
303 /*****************************************************************************/
304 OSocketAddr::TResult
OInetSocketAddr::getDottedAddr( rtl::OUString
& pBuffer
) const
306 return (TResult
)osl_getDottedInetAddrOfSocketAddr(m_SockAddr
, &pBuffer
.pData
);
309 /*****************************************************************************/
311 /*****************************************************************************/
312 sal_Bool
OInetSocketAddr::setAddr(const rtl::OUString
& ustrAddrOrHostname
)
322 osl_destroySocketAddr(m_SockAddr
);
326 // first try as dotted address.
327 m_SockAddr
= osl_createInetSocketAddr(ustrAddrOrHostname
.pData
, Port
);
329 // create failed, maybe it's an hostname
333 m_SockAddr
= osl_resolveHostname( ustrAddrOrHostname
.pData
);
341 // set port will fail if addrtype is not osl_Socket_FamilyInet
342 VOS_VERIFY(osl_setInetPortOfSocketAddr(m_SockAddr
, Port
));
349 ///////////////////////////////////////////////////////////////////////////////
352 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OIpxSocketAddr
, vos
),
353 VOS_NAMESPACE(OIpxSocketAddr
, vos
),
354 VOS_NAMESPACE(OSocketAddr
, vos
), 0);
357 /*****************************************************************************/
359 /*****************************************************************************/
360 OIpxSocketAddr::OIpxSocketAddr()
362 m_SockAddr
= osl_createEmptySocketAddr(osl_Socket_FamilyIpx
);
365 /*****************************************************************************/
366 // OIpxSocketAddr(oslSocketAddr)
367 /*****************************************************************************/
368 OIpxSocketAddr::OIpxSocketAddr(oslSocketAddr Addr
) :
371 VOS_ASSERT(osl_getFamilyOfSocketAddr(Addr
) == osl_Socket_FamilyIpx
);
374 /*****************************************************************************/
376 /*****************************************************************************/
377 OIpxSocketAddr::OIpxSocketAddr(const rtl::OUString
&,
378 const rtl::OUString
&,
381 // jbu : functionality removed from vos
384 /*****************************************************************************/
385 // OIpxSocketAddr(OIpxSocketAddr&)
386 /*****************************************************************************/
387 OIpxSocketAddr::OIpxSocketAddr(const OIpxSocketAddr
& sa
) :
390 VOS_ASSERT(sa
.getFamily() == TFamily_Ipx
);
394 /*****************************************************************************/
395 // OIpxSocketAddr(OSocketAddr&)
396 /*****************************************************************************/
397 OIpxSocketAddr::OIpxSocketAddr(const OSocketAddr
& sa
) :
400 VOS_ASSERT(sa
.getFamily() == TFamily_Ipx
);
403 /*****************************************************************************/
405 /*****************************************************************************/
406 OIpxSocketAddr::~OIpxSocketAddr()
411 /*****************************************************************************/
413 /*****************************************************************************/
414 void OIpxSocketAddr::operator= (oslSocketAddr Addr
)
416 VOS_PRECOND(osl_getFamilyOfSocketAddr(Addr
) == osl_Socket_FamilyIpx
,
417 "oslSocketAddr not of type osl_Socket_FamilyIpx!");
419 OSocketAddr::operator=(Addr
);
422 /*****************************************************************************/
423 // operator== (oslSocketAddr Addr)
424 /*****************************************************************************/
425 sal_Bool
OIpxSocketAddr::operator== (oslSocketAddr Addr
)
427 return (osl_isEqualSocketAddr(m_SockAddr
, Addr
));
430 /*****************************************************************************/
431 // operator=(const OIpxSocketAddr& Addr)
432 /*****************************************************************************/
433 OIpxSocketAddr
& OIpxSocketAddr::operator=(const OIpxSocketAddr
& Addr
)
435 VOS_ASSERT(Addr
.getFamily() == TFamily_Ipx
);
437 OSocketAddr::operator=(Addr
);
442 /*****************************************************************************/
443 // operator=(const OSocketAddr& Addr)
444 /*****************************************************************************/
445 OIpxSocketAddr
& OIpxSocketAddr::operator=(const OSocketAddr
& Addr
)
447 VOS_ASSERT(Addr
.getFamily() == TFamily_Ipx
);
449 OSocketAddr::operator=(Addr
);
454 /*****************************************************************************/
456 /*****************************************************************************/
457 OSocketAddr::TResult
OIpxSocketAddr::getNetNumber(TIpxNetNumber
&) const
459 // jbu : functionality removed from vos
463 /*****************************************************************************/
465 /*****************************************************************************/
466 OSocketAddr::TResult
OIpxSocketAddr::getNodeNumber(TIpxNodeNumber
& ) const
468 // jbu : functionality removed from vos
472 /*****************************************************************************/
474 /*****************************************************************************/
475 sal_uInt32
OIpxSocketAddr::getSocketNumber() const
477 // return osl_getIpxSocketNumber(m_SockAddr);
482 /*****************************************************************************/
483 // getAddressString()
484 /*****************************************************************************/
485 //void OIpxSocketAddr::getAddressString(sal_Char* Buffer, sal_uInt32 Len) const
486 void OIpxSocketAddr::getAddressString( rtl::OUString
& ) const
488 // jbu : functionality removed from vos
492 ///////////////////////////////////////////////////////////////////////////////
496 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OSocket
, vos
),
497 VOS_NAMESPACE(OSocket
, vos
),
498 VOS_NAMESPACE(OObject
, vos
), 0);
500 /*****************************************************************************/
502 /*****************************************************************************/
512 /*****************************************************************************/
514 /*****************************************************************************/
515 OSocket::OSocket(TSocketType Type
,
523 new SockRef(osl_createSocket((oslAddrFamily
)Family
,
525 (oslProtocol
)Protocol
));
527 VOS_POSTCOND(m_pSockRef
!= 0, "OSocket(): new failed.\n");
528 VOS_POSTCOND((*m_pSockRef
)(), "OSocket(): creation of socket failed!\n");
531 /*****************************************************************************/
533 /*****************************************************************************/
534 OSocket::OSocket(const OSocket
& sock
) :
535 ISocketTypes(), OReference(), OObject()
541 VOS_ASSERT(sock
.m_pSockRef
!= 0);
543 if ( sock
.m_pSockRef
!=0 )
545 m_pSockRef
= sock
.m_pSockRef
;
547 setRecvTimeout(sock
.m_pRecvTimeout
);
548 setSendTimeout(sock
.m_pSendTimeout
);
550 m_pSockRef
->acquire();
554 /*****************************************************************************/
556 /*****************************************************************************/
557 OSocket::OSocket(oslSocket Socket
)
562 m_pSockRef
= new SockRef(Socket
);
566 /*****************************************************************************/
568 /*****************************************************************************/
573 delete m_pRecvTimeout
;
574 delete m_pSendTimeout
;
578 /*****************************************************************************/
580 /*****************************************************************************/
581 sal_Bool
OSocket::create(TSocketType Type
,
585 // if this was a valid socket, decrease reference
586 if ((m_pSockRef
) && (m_pSockRef
->release() == 0))
588 osl_releaseSocket((*m_pSockRef
)());
594 new SockRef(osl_createSocket((oslAddrFamily
)Family
,
596 (oslProtocol
)Protocol
));
598 VOS_POSTCOND(m_pSockRef
!= 0, "OSocket(): new failed.\n");
600 return (*m_pSockRef
)() != 0;
603 /*****************************************************************************/
605 /*****************************************************************************/
606 OSocket
& OSocket::operator= (const OSocket
& sock
)
608 VOS_PRECOND(sock
.m_pSockRef
!= 0, "OSocket::operator=: tried to assign an empty/invalid socket\n");
610 if (m_pSockRef
== sock
.m_pSockRef
)
613 // if this was a valid socket, decrease reference
614 if ((m_pSockRef
) && (m_pSockRef
->release() == 0))
616 osl_releaseSocket((*m_pSockRef
)());
621 m_pSockRef
= sock
.m_pSockRef
;
623 setRecvTimeout(sock
.m_pRecvTimeout
);
624 setSendTimeout(sock
.m_pSendTimeout
);
626 m_pSockRef
->acquire();
631 /*****************************************************************************/
632 // operator oslSocket()
633 /*****************************************************************************/
634 OSocket::operator oslSocket() const
636 VOS_ASSERT(m_pSockRef
);
637 return (*m_pSockRef
)();
640 /*****************************************************************************/
642 /*****************************************************************************/
643 sal_Bool
OSocket::isValid() const
645 return m_pSockRef
!= 0 && (*m_pSockRef
)() != 0;
649 /*****************************************************************************/
651 /*****************************************************************************/
652 void OSocket::close()
654 if (m_pSockRef
&& (*m_pSockRef
)() && (m_pSockRef
->release() == 0))
656 osl_releaseSocket((*m_pSockRef
)());
663 /*****************************************************************************/
665 /*****************************************************************************/
666 void OSocket::getLocalAddr(OSocketAddr
& sa
) const
668 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
670 if ( m_pSockRef
&& (*m_pSockRef
)() )
672 sa
= osl_getLocalAddrOfSocket((*m_pSockRef
)());
676 /*****************************************************************************/
678 /*****************************************************************************/
679 sal_Int32
OSocket::getLocalPort() const
681 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
683 sal_Int32 Port
= OSL_INVALID_PORT
;
685 if ( m_pSockRef
&& (*m_pSockRef
)() )
687 oslSocketAddr Addr
= osl_getLocalAddrOfSocket((*m_pSockRef
)());
691 Port
= osl_getInetPortOfSocketAddr(Addr
);
692 osl_destroySocketAddr(Addr
);
699 /*****************************************************************************/
701 /*****************************************************************************/
702 OSocket::TResult
OSocket::getLocalHost( rtl::OUString
& pBuffer
) const
704 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
706 if ( m_pSockRef
&& (*m_pSockRef
)() )
708 oslSocketAddr Addr
= osl_getLocalAddrOfSocket((*m_pSockRef
)());
712 // TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
713 // pBuffer, BufferSize);
714 TResult Result
= (TResult
)osl_getHostnameOfSocketAddr(Addr
,
717 osl_destroySocketAddr(Addr
);
723 return TResult_Error
;
726 /*****************************************************************************/
728 /*****************************************************************************/
729 void OSocket::getPeerAddr(OSocketAddr
& sa
) const
731 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
733 if ( m_pSockRef
&& (*m_pSockRef
)() )
735 sa
= osl_getPeerAddrOfSocket((*m_pSockRef
)());
739 /*****************************************************************************/
741 /*****************************************************************************/
742 sal_Int32
OSocket::getPeerPort() const
744 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
746 sal_Int32 Port
= OSL_INVALID_PORT
;
748 if ( m_pSockRef
&& (*m_pSockRef
)() )
750 oslSocketAddr Addr
= osl_getPeerAddrOfSocket((*m_pSockRef
)());
754 Port
= osl_getInetPortOfSocketAddr(Addr
);
755 osl_destroySocketAddr(Addr
);
762 /*****************************************************************************/
764 /*****************************************************************************/
765 OSocket::TResult
OSocket::getPeerHost( rtl::OUString
& pBuffer
) const
767 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
769 if ( m_pSockRef
&& (*m_pSockRef
)() )
771 oslSocketAddr Addr
= osl_getPeerAddrOfSocket((*m_pSockRef
)());
775 // TResult Result= (TResult)osl_getHostnameOfSocketAddr(Addr,
776 // pBuffer, BufferSize);
777 TResult Result
= (TResult
)osl_getHostnameOfSocketAddr(Addr
,
780 osl_destroySocketAddr(Addr
);
786 return TResult_Error
;
789 /*****************************************************************************/
791 /*****************************************************************************/
792 sal_Bool
OSocket::bind(const OSocketAddr
& Addr
)
794 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
796 if ( m_pSockRef
&& (*m_pSockRef
)() )
798 return osl_bindAddrToSocket((*m_pSockRef
)(), (oslSocketAddr
)Addr
);
805 /*****************************************************************************/
807 /*****************************************************************************/
808 void OSocket::setSendTimeout(const TimeValue
* pTimeout
)
810 delete m_pSendTimeout
;
813 m_pSendTimeout
= new TimeValue(*pTimeout
);
818 /*****************************************************************************/
820 /*****************************************************************************/
821 void OSocket::setRecvTimeout(const TimeValue
* pTimeout
)
823 delete m_pRecvTimeout
;
826 m_pRecvTimeout
= new TimeValue(*pTimeout
);
831 /*****************************************************************************/
833 /*****************************************************************************/
834 sal_Bool
OSocket::isRecvReady(const TimeValue
* pTimeout
) const
836 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
838 if ( m_pSockRef
&& (*m_pSockRef
)() )
840 return osl_isReceiveReady((*m_pSockRef
)(), pTimeout
);
846 /*****************************************************************************/
848 /*****************************************************************************/
849 sal_Bool
OSocket::isSendReady(const TimeValue
* pTimeout
) const
851 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
853 if ( m_pSockRef
&& (*m_pSockRef
)() )
855 return osl_isSendReady((*m_pSockRef
)(), pTimeout
);
861 /*****************************************************************************/
862 // isExceptionPending
863 /*****************************************************************************/
864 sal_Bool
OSocket::isExceptionPending(const TimeValue
* pTimeout
) const
866 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
868 if ( m_pSockRef
&& (*m_pSockRef
)() )
870 return osl_isExceptionPending((*m_pSockRef
)(), pTimeout
);
877 /*****************************************************************************/
879 /*****************************************************************************/
880 sal_Int32
OSocket::getOption(TSocketOption Option
,
882 sal_uInt32 BufferLen
,
883 TSocketOptionLevel Level
) const
885 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
887 if ( m_pSockRef
&& (*m_pSockRef
)() )
889 return osl_getSocketOption((*m_pSockRef
)(),
890 (oslSocketOptionLevel
)Level
,
891 (oslSocketOption
)Option
,
899 /*****************************************************************************/
901 /*****************************************************************************/
902 sal_Bool
OSocket::setOption(TSocketOption Option
,
904 sal_uInt32 BufferLen
,
905 TSocketOptionLevel Level
) const
907 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
909 if ( m_pSockRef
&& (*m_pSockRef
)() )
911 return osl_setSocketOption((*m_pSockRef
)(),
912 (oslSocketOptionLevel
)Level
,
913 (oslSocketOption
)Option
,
922 /*****************************************************************************/
923 // enableNonBlockingMode
924 /*****************************************************************************/
925 sal_Bool
OSocket::enableNonBlockingMode(sal_Bool On
)
927 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
929 if ( m_pSockRef
&& (*m_pSockRef
)() )
931 return osl_enableNonBlockingMode((*m_pSockRef
)(), On
);
937 /*****************************************************************************/
939 /*****************************************************************************/
940 sal_Bool
OSocket::isNonBlockingMode() const
942 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
944 if ( m_pSockRef
&& (*m_pSockRef
)() )
946 return osl_isNonBlockingMode((*m_pSockRef
)());
952 /*****************************************************************************/
954 /*****************************************************************************/
955 OSocket::TSocketType
OSocket::getType() const
957 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
959 if ( m_pSockRef
&& (*m_pSockRef
)() )
961 return (TSocketType
)osl_getSocketType((*m_pSockRef
)());
964 return TType_Invalid
;
967 /*****************************************************************************/
969 /*****************************************************************************/
970 sal_Int32
OSocket::clearError() const
974 getOption(TOption_Error
, &err
, sizeof(err
));
979 /*****************************************************************************/
981 /*****************************************************************************/
982 sal_Int32
OSocket::setDebug(sal_Int32 opt
) const
986 getOption(TOption_Debug
, &old
, sizeof(old
));
989 setOption(TOption_Debug
, &opt
, sizeof(opt
));
994 /*****************************************************************************/
996 /*****************************************************************************/
997 sal_Int32
OSocket::setReuseAddr(sal_Int32 opt
) const
1001 getOption(TOption_ReuseAddr
, &old
, sizeof(old
));
1004 setOption(TOption_ReuseAddr
, &opt
, sizeof(opt
));
1009 /*****************************************************************************/
1011 /*****************************************************************************/
1012 sal_Int32
OSocket::setKeepAlive(sal_Int32 opt
) const
1016 getOption(TOption_KeepAlive
, &old
, sizeof(old
));
1019 setOption(TOption_KeepAlive
, &opt
, sizeof(opt
));
1024 /*****************************************************************************/
1026 /*****************************************************************************/
1027 sal_Int32
OSocket::setDontRoute(sal_Int32 opt
) const
1031 getOption(TOption_DontRoute
, &old
, sizeof(old
));
1034 setOption(TOption_DontRoute
, &opt
, sizeof(opt
));
1039 /*****************************************************************************/
1041 /*****************************************************************************/
1042 sal_Int32
OSocket::setBroadcast(sal_Int32 opt
) const
1046 getOption(TOption_Broadcast
, &old
, sizeof(old
));
1049 setOption(TOption_Broadcast
, &opt
, sizeof(opt
));
1054 /*****************************************************************************/
1056 /*****************************************************************************/
1057 sal_Int32
OSocket::setOobinline(sal_Int32 opt
) const
1061 getOption(TOption_OOBinLine
, &old
, sizeof(old
));
1064 setOption(TOption_OOBinLine
, &opt
, sizeof(opt
));
1069 /*****************************************************************************/
1071 /*****************************************************************************/
1072 sal_Int32
OSocket::setLinger(sal_Int32 time
) const
1074 /* local decl. of linger-struct */
1077 sal_Int32 m_onoff
; // option on/off
1078 sal_Int32 m_linger
; // linger time
1082 SockLinger old
= { 0, 0 };
1084 getOption(TOption_Linger
, &old
, sizeof(old
));
1086 if (time
> 0) // enable linger with wait-times > 0
1088 SockLinger nw
= { 1, time
};
1089 setOption(TOption_Linger
, &nw
, sizeof(nw
));
1091 else if (time
== 0) // disable linger with wait-time == 0
1093 SockLinger nw
= { 0, old
.m_linger
};
1094 setOption(TOption_Linger
, &nw
, sizeof(nw
));
1097 // returns 0 if linger was off, else the linger-time
1098 return (old
.m_onoff
? old
.m_linger
: 0);
1101 /*****************************************************************************/
1103 /*****************************************************************************/
1104 sal_Int32
OSocket::setSendBufSize(sal_Int32 sz
) const
1108 getOption(TOption_SndBuf
, &old
, sizeof(old
));
1111 setOption(TOption_SndBuf
, &sz
, sizeof(sz
));
1116 /*****************************************************************************/
1118 /*****************************************************************************/
1119 sal_Int32
OSocket::setRecvBufSize(sal_Int32 sz
) const
1123 getOption(TOption_RcvBuf
, &old
, sizeof(old
));
1126 setOption(TOption_RcvBuf
, &sz
, sizeof(sz
));
1131 /*****************************************************************************/
1133 /*****************************************************************************/
1134 sal_Int32
OSocket::setTcpNoDelay(sal_Int32 sz
) const
1138 getOption(TOption_TcpNoDelay
, &old
, sizeof(old
), TLevel_Tcp
);
1141 setOption(TOption_TcpNoDelay
, &sz
, sizeof(sz
), TLevel_Tcp
);
1146 /*****************************************************************************/
1148 /*****************************************************************************/
1149 //void OSocket::getError(sal_Char* pBuffer, sal_uInt32 nSize) const
1150 void OSocket::getError( rtl::OUString
& pBuffer
) const
1152 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1154 if (m_pSockRef
&& (*m_pSockRef
)())
1155 osl_getLastSocketErrorDescription((*m_pSockRef
)(), &pBuffer
.pData
);
1157 osl_getLastSocketErrorDescription(NULL
, &pBuffer
.pData
);
1160 /*****************************************************************************/
1162 /*****************************************************************************/
1163 OSocket::TSocketError
OSocket::getError() const
1165 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1167 if (m_pSockRef
&& (*m_pSockRef
)())
1168 return (TSocketError
)osl_getLastSocketError((*m_pSockRef
)());
1170 return (TSocketError
)osl_getLastSocketError(NULL
);
1175 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OAcceptorSocket
, vos
),
1176 VOS_NAMESPACE(OAcceptorSocket
, vos
),
1177 VOS_NAMESPACE(OSocket
, vos
), 0);
1180 /*****************************************************************************/
1182 /*****************************************************************************/
1183 OAcceptorSocket::OAcceptorSocket(TAddrFamily Family
,
1186 OSocket(Type
, Family
, Protocol
)
1190 /*****************************************************************************/
1192 /*****************************************************************************/
1193 OAcceptorSocket::OAcceptorSocket(const OAcceptorSocket
& sock
) :
1198 /*****************************************************************************/
1200 /*****************************************************************************/
1201 OAcceptorSocket::~OAcceptorSocket()
1203 if (m_pSockRef
&& (*m_pSockRef
)() && (m_pSockRef
->release() == 0))
1205 /* mfe: prepare for forthcoming api change */
1206 osl_closeSocket((*m_pSockRef
)());
1207 osl_releaseSocket((*m_pSockRef
)());
1213 /*****************************************************************************/
1215 /*****************************************************************************/
1216 void OAcceptorSocket::close()
1218 if (m_pSockRef
&& (*m_pSockRef
)() && (m_pSockRef
->release() == 0))
1220 osl_closeSocket((*m_pSockRef
)());
1226 /*****************************************************************************/
1228 /*****************************************************************************/
1229 sal_Bool
OAcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
1231 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1233 if ( m_pSockRef
&& (*m_pSockRef
)() )
1235 return osl_listenOnSocket((*m_pSockRef
)(), MaxPendingConnections
);
1242 /*****************************************************************************/
1244 /*****************************************************************************/
1245 OSocket::TResult
OAcceptorSocket::acceptConnection(OStreamSocket
& connection
)
1247 if (m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1248 return TResult_TimedOut
;
1250 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1251 OStreamSocket aSocket
;
1253 if ( m_pSockRef
&& (*m_pSockRef
)() )
1255 aSocket
= osl_acceptConnectionOnSocket((*m_pSockRef
)(), 0);
1258 if( aSocket
.isValid() )
1260 connection
= aSocket
;
1265 return TResult_Error
;
1270 /*****************************************************************************/
1272 /*****************************************************************************/
1273 OSocket::TResult
OAcceptorSocket::acceptConnection(OStreamSocket
& connection
,
1276 oslSocketAddr PeerAddr
= 0;
1277 oslSocket Socket
= 0;
1279 if (m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1280 return TResult_TimedOut
;
1282 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1284 if ( m_pSockRef
&& (*m_pSockRef
)() )
1286 Socket
= osl_acceptConnectionOnSocket((*m_pSockRef
)(), &PeerAddr
);
1297 return TResult_Error
;
1302 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamSocket
, vos
),
1303 VOS_NAMESPACE(OStreamSocket
, vos
),
1304 VOS_NAMESPACE(OSocket
, vos
), 0);
1308 /*****************************************************************************/
1310 /*****************************************************************************/
1311 OStreamSocket::OStreamSocket()
1315 /*****************************************************************************/
1317 /*****************************************************************************/
1318 OStreamSocket::OStreamSocket(TAddrFamily Family
,
1321 OSocket(Type
, Family
, Protocol
)
1326 /*****************************************************************************/
1328 /*****************************************************************************/
1329 OStreamSocket::OStreamSocket(oslSocket Socket
) :
1334 /*****************************************************************************/
1337 /*****************************************************************************/
1338 OStreamSocket::OStreamSocket(const OStreamSocket
& sock
) :
1339 OSocket(sock
), IStream()
1343 /*****************************************************************************/
1346 /*****************************************************************************/
1347 OStreamSocket::OStreamSocket(const OSocket
& sock
) :
1352 /*****************************************************************************/
1354 /*****************************************************************************/
1355 OStreamSocket::~OStreamSocket()
1359 /*****************************************************************************/
1361 /*****************************************************************************/
1362 void OStreamSocket::close()
1364 if (m_pSockRef
&& (*m_pSockRef
)() && (m_pSockRef
->release() == 0))
1367 osl_releaseSocket((*m_pSockRef
)());
1375 /*****************************************************************************/
1376 // operator=(oslSocket)
1377 /*****************************************************************************/
1378 OStreamSocket
& OStreamSocket::operator=(oslSocket Socket
)
1380 OSocket::operator=(Socket
);
1385 /*****************************************************************************/
1387 /*****************************************************************************/
1388 OStreamSocket
& OStreamSocket::operator= (const OSocket
& sock
)
1390 OSocket::operator=(sock
);
1395 /*****************************************************************************/
1397 /*****************************************************************************/
1398 OStreamSocket
& OStreamSocket::operator= (const OStreamSocket
& sock
)
1400 OSocket::operator=(sock
);
1405 /*****************************************************************************/
1407 /*****************************************************************************/
1408 sal_Int32
OStreamSocket::read(void* pBuffer
, sal_uInt32 n
) const
1410 sal_uInt8
*Ptr
= (sal_uInt8
*)pBuffer
;
1412 if (m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1415 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1417 if ( ! ( m_pSockRef
&& (*m_pSockRef
)() ) )
1422 /* loop until all desired bytes were read or an error occured */
1423 sal_uInt32 BytesRead
= 0;
1424 sal_uInt32 BytesToRead
= n
;
1425 while (BytesToRead
> 0)
1428 RetVal
= osl_receiveSocket((*m_pSockRef
)(),
1431 osl_Socket_MsgNormal
);
1433 /* error occured? */
1439 BytesToRead
-= RetVal
;
1440 BytesRead
+= RetVal
;
1443 /* wait for next available data or timeout */
1444 if ( BytesToRead
> 0 && m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1452 /*****************************************************************************/
1454 /*****************************************************************************/
1455 sal_Int32
OStreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
1457 sal_uInt8
*Ptr
= (sal_uInt8
*)pBuffer
;
1459 if (m_pSendTimeout
&& ! isSendReady(m_pSendTimeout
))
1462 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1464 if ( ! ( m_pSockRef
&& (*m_pSockRef
)() ) )
1469 /* loop until all desired bytes were send or an error occured */
1470 sal_uInt32 BytesSend
= 0;
1471 sal_uInt32 BytesToSend
= n
;
1472 while (BytesToSend
> 0)
1476 RetVal
= osl_sendSocket((*m_pSockRef
)(),
1479 osl_Socket_MsgNormal
);
1481 /* error occured? */
1487 BytesToSend
-= RetVal
;
1488 BytesSend
+= RetVal
;
1491 /* wait till new data is available or timeout occures */
1492 if ( BytesToSend
> 0 && m_pSendTimeout
&& ! isSendReady(m_pSendTimeout
))
1499 sal_Bool
OStreamSocket::isEof() const
1505 /*****************************************************************************/
1507 /*****************************************************************************/
1508 sal_Int32
OStreamSocket::recv(void* pBuffer
,
1509 sal_uInt32 BytesToRead
,
1510 TSocketMsgFlag Flag
)
1512 if (m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1515 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1517 if ( ! ( m_pSockRef
&& (*m_pSockRef
)() ) )
1522 return osl_receiveSocket((*m_pSockRef
)(),
1525 (oslSocketMsgFlag
)Flag
);
1528 /*****************************************************************************/
1530 /*****************************************************************************/
1531 sal_Int32
OStreamSocket::send(const void* pBuffer
,
1532 sal_uInt32 BytesToSend
,
1533 TSocketMsgFlag Flag
)
1535 if (m_pSendTimeout
&& ! isSendReady(m_pSendTimeout
))
1538 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1540 if ( ! ( m_pSockRef
&& (*m_pSockRef
)() ) )
1545 return osl_sendSocket((*m_pSockRef
)(),
1548 (oslSocketMsgFlag
)Flag
);
1551 /*****************************************************************************/
1553 /*****************************************************************************/
1554 sal_Bool
OStreamSocket::shutdown(TSocketDirection Direction
)
1556 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1558 if ( m_pSockRef
&& (*m_pSockRef
)() )
1560 return osl_shutdownSocket((*m_pSockRef
)(), (oslSocketDirection
)Direction
);
1568 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OConnectorSocket
, vos
),
1569 VOS_NAMESPACE(OConnectorSocket
, vos
),
1570 VOS_NAMESPACE(OStreamSocket
, vos
), 0);
1574 /*****************************************************************************/
1576 /*****************************************************************************/
1577 OConnectorSocket::OConnectorSocket(TAddrFamily Family
,
1580 OStreamSocket(Family
, Protocol
, Type
)
1584 /*****************************************************************************/
1586 /*****************************************************************************/
1587 OConnectorSocket::OConnectorSocket(const OConnectorSocket
& sock
) :
1592 /*****************************************************************************/
1593 // ~OConnectorSocket
1594 /*****************************************************************************/
1595 OConnectorSocket::~OConnectorSocket()
1599 /*****************************************************************************/
1601 /*****************************************************************************/
1602 OSocket::TResult
OConnectorSocket::connect(const OSocketAddr
& Addr
,
1603 const TimeValue
* pTimeout
)
1606 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1608 if ( m_pSockRef
&& (*m_pSockRef
)() )
1610 return (TResult
)osl_connectSocketTo((*m_pSockRef
)(),
1611 (oslSocketAddr
)Addr
,
1615 return TResult_Error
;
1619 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(ODatagramSocket
, vos
),
1620 VOS_NAMESPACE(ODatagramSocket
, vos
),
1621 VOS_NAMESPACE(OSocket
, vos
), 0);
1624 /*****************************************************************************/
1626 /*****************************************************************************/
1627 ODatagramSocket::ODatagramSocket(TAddrFamily Family
,
1630 OSocket(Type
, Family
, Protocol
)
1634 /*****************************************************************************/
1636 /*****************************************************************************/
1637 ODatagramSocket::ODatagramSocket(const ODatagramSocket
& sock
) :
1642 /*****************************************************************************/
1644 /*****************************************************************************/
1645 ODatagramSocket::~ODatagramSocket()
1650 /*****************************************************************************/
1652 /*****************************************************************************/
1653 sal_Int32
ODatagramSocket::recvFrom(void* pBuffer
,
1654 sal_uInt32 BufferSize
,
1655 OSocketAddr
* pSenderAddr
,
1656 TSocketMsgFlag Flag
)
1659 if (m_pRecvTimeout
&& ! isRecvReady(m_pRecvTimeout
))
1662 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1664 if ( ! ( m_pSockRef
&& (*m_pSockRef
)() ) )
1669 sal_Int32 BytesRead
;
1673 // we are interested in the senders address
1674 oslSocketAddr SenderAddr
= osl_createEmptySocketAddr(osl_Socket_FamilyInet
);
1677 osl_receiveFromSocket((*m_pSockRef
)(),
1681 (oslSocketMsgFlag
)Flag
);
1683 *pSenderAddr
= SenderAddr
;
1687 // we don't want to know the senders address
1690 osl_receiveFromSocket((*m_pSockRef
)(),
1694 (oslSocketMsgFlag
)Flag
);
1701 /*****************************************************************************/
1703 /*****************************************************************************/
1704 sal_Int32
ODatagramSocket::sendTo(const OSocketAddr
& ReceiverAddr
,
1705 const void* pBuffer
,
1706 sal_uInt32 BufferSize
,
1707 TSocketMsgFlag Flag
)
1709 if (m_pSendTimeout
&& ! isSendReady(m_pSendTimeout
))
1712 VOS_ASSERT(m_pSockRef
&& (*m_pSockRef
)());
1714 if ( ( m_pSockRef
&& (*m_pSockRef
)() ) )
1717 return osl_sendToSocket((*m_pSockRef
)(),
1718 (oslSocketAddr
)ReceiverAddr
,
1721 (oslSocketMsgFlag
)Flag
);