1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
29 #include "osl/security.hxx"
30 #include "acceptor.hxx"
31 #include <com/sun/star/connection/ConnectionSetupException.hpp>
33 #include <cppuhelper/implbase1.hxx>
35 using namespace ::rtl
;
36 using namespace ::osl
;
37 using namespace ::cppu
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::connection
;
41 using namespace ::com::sun::star::io
;
47 typedef WeakImplHelper1
< XConnection
> MyPipeConnection
;
49 class PipeConnection
:
50 public MyPipeConnection
53 PipeConnection( const OUString
&sConnectionDescription
);
56 virtual sal_Int32 SAL_CALL
read( Sequence
< sal_Int8
>& aReadBytes
, sal_Int32 nBytesToRead
)
57 throw(::com::sun::star::io::IOException
,
58 ::com::sun::star::uno::RuntimeException
);
59 virtual void SAL_CALL
write( const Sequence
< sal_Int8
>& aData
)
60 throw(::com::sun::star::io::IOException
,
61 ::com::sun::star::uno::RuntimeException
);
62 virtual void SAL_CALL
flush( ) throw(
63 ::com::sun::star::io::IOException
,
64 ::com::sun::star::uno::RuntimeException
);
65 virtual void SAL_CALL
close( )
66 throw(::com::sun::star::io::IOException
,
67 ::com::sun::star::uno::RuntimeException
);
68 virtual ::rtl::OUString SAL_CALL
getDescription( )
69 throw(::com::sun::star::uno::RuntimeException
);
71 ::osl::StreamPipe m_pipe
;
72 oslInterlockedCount m_nStatus
;
73 OUString m_sDescription
;
78 PipeConnection::PipeConnection( const OUString
&sConnectionDescription
) :
80 m_sDescription( sConnectionDescription
)
82 g_moduleCount
.modCnt
.acquire( &g_moduleCount
.modCnt
);
85 m_sDescription
+= OUString(RTL_CONSTASCII_USTRINGPARAM(",uniqueValue="));
86 m_sDescription
+= OUString::valueOf(
87 sal::static_int_cast
<sal_Int64
>(
88 reinterpret_cast< sal_IntPtr
>(&m_pipe
)),
92 PipeConnection::~PipeConnection()
94 g_moduleCount
.modCnt
.release( &g_moduleCount
.modCnt
);
97 sal_Int32
PipeConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
98 throw(::com::sun::star::io::IOException
,
99 ::com::sun::star::uno::RuntimeException
)
103 if( aReadBytes
.getLength() < nBytesToRead
)
105 aReadBytes
.realloc( nBytesToRead
);
107 sal_Int32 n
= m_pipe
.read( aReadBytes
.getArray(), nBytesToRead
);
108 OSL_ASSERT( n
>= 0 && n
<= aReadBytes
.getLength() );
109 if( n
< aReadBytes
.getLength() )
111 aReadBytes
.realloc( n
);
120 void PipeConnection::write( const Sequence
< sal_Int8
> &seq
)
121 throw(::com::sun::star::io::IOException
,
122 ::com::sun::star::uno::RuntimeException
)
126 if( m_pipe
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
136 void PipeConnection::flush( )
137 throw( ::com::sun::star::io::IOException
,
138 ::com::sun::star::uno::RuntimeException
)
142 void PipeConnection::close()
143 throw( ::com::sun::star::io::IOException
,
144 ::com::sun::star::uno::RuntimeException
)
146 if( 1 == osl_incrementInterlockedCount( (&m_nStatus
) ) )
152 OUString
PipeConnection::getDescription()
153 throw(::com::sun::star::uno::RuntimeException
)
155 return m_sDescription
;
161 PipeAcceptor::PipeAcceptor( const OUString
&sPipeName
, const OUString
& sConnectionDescription
) :
162 m_sPipeName( sPipeName
),
163 m_sConnectionDescription( sConnectionDescription
),
164 m_bClosed( sal_False
)
169 void PipeAcceptor::init()
171 m_pipe
= Pipe( m_sPipeName
.pData
, osl_Pipe_CREATE
, osl::Security() );
174 OUString error
= OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: Couldn't setup pipe "));
175 error
+= m_sPipeName
;
176 throw ConnectionSetupException( error
, Reference
< XInterface
> () );
180 Reference
< XConnection
> PipeAcceptor::accept( )
184 MutexGuard
guard( m_mutex
);
189 OUString error
= OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: pipe already closed"));
190 error
+= m_sPipeName
;
191 throw ConnectionSetupException( error
, Reference
< XInterface
> () );
193 PipeConnection
*pConn
= new PipeConnection( m_sConnectionDescription
);
195 oslPipeError status
= pipe
.accept( pConn
->m_pipe
);
199 // stopAccepting was called !
201 return Reference
< XConnection
>();
203 else if( osl_Pipe_E_None
== status
)
205 return Reference
< XConnection
> ( (XConnection
* ) pConn
);
209 OUString error
= OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: Couldn't setup pipe "));
210 error
+= m_sPipeName
;
211 throw ConnectionSetupException( error
, Reference
< XInterface
> ());
215 void PipeAcceptor::stopAccepting()
217 m_bClosed
= sal_True
;
220 MutexGuard
guard( m_mutex
);
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */