1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "osl/security.hxx"
21 #include "acceptor.hxx"
22 #include <com/sun/star/connection/ConnectionSetupException.hpp>
24 #include <cppuhelper/implbase1.hxx>
26 using namespace ::rtl
;
27 using namespace ::osl
;
28 using namespace ::cppu
;
29 using namespace ::com::sun::star::uno
;
30 using namespace ::com::sun::star::lang
;
31 using namespace ::com::sun::star::connection
;
32 using namespace ::com::sun::star::io
;
38 typedef WeakImplHelper1
< XConnection
> MyPipeConnection
;
40 class PipeConnection
:
41 public MyPipeConnection
44 PipeConnection( const OUString
&sConnectionDescription
);
47 virtual sal_Int32 SAL_CALL
read( Sequence
< sal_Int8
>& aReadBytes
, sal_Int32 nBytesToRead
)
48 throw(::com::sun::star::io::IOException
,
49 ::com::sun::star::uno::RuntimeException
);
50 virtual void SAL_CALL
write( const Sequence
< sal_Int8
>& aData
)
51 throw(::com::sun::star::io::IOException
,
52 ::com::sun::star::uno::RuntimeException
);
53 virtual void SAL_CALL
flush( ) throw(
54 ::com::sun::star::io::IOException
,
55 ::com::sun::star::uno::RuntimeException
);
56 virtual void SAL_CALL
close( )
57 throw(::com::sun::star::io::IOException
,
58 ::com::sun::star::uno::RuntimeException
);
59 virtual OUString SAL_CALL
getDescription( )
60 throw(::com::sun::star::uno::RuntimeException
);
62 ::osl::StreamPipe m_pipe
;
63 oslInterlockedCount m_nStatus
;
64 OUString m_sDescription
;
69 PipeConnection::PipeConnection( const OUString
&sConnectionDescription
) :
71 m_sDescription( sConnectionDescription
)
73 g_moduleCount
.modCnt
.acquire( &g_moduleCount
.modCnt
);
76 m_sDescription
+= OUString(",uniqueValue=");
77 m_sDescription
+= OUString::valueOf(
78 sal::static_int_cast
<sal_Int64
>(
79 reinterpret_cast< sal_IntPtr
>(&m_pipe
)),
83 PipeConnection::~PipeConnection()
85 g_moduleCount
.modCnt
.release( &g_moduleCount
.modCnt
);
88 sal_Int32
PipeConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
89 throw(::com::sun::star::io::IOException
,
90 ::com::sun::star::uno::RuntimeException
)
94 if( aReadBytes
.getLength() < nBytesToRead
)
96 aReadBytes
.realloc( nBytesToRead
);
98 sal_Int32 n
= m_pipe
.read( aReadBytes
.getArray(), nBytesToRead
);
99 OSL_ASSERT( n
>= 0 && n
<= aReadBytes
.getLength() );
100 if( n
< aReadBytes
.getLength() )
102 aReadBytes
.realloc( n
);
111 void PipeConnection::write( const Sequence
< sal_Int8
> &seq
)
112 throw(::com::sun::star::io::IOException
,
113 ::com::sun::star::uno::RuntimeException
)
117 if( m_pipe
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
127 void PipeConnection::flush( )
128 throw( ::com::sun::star::io::IOException
,
129 ::com::sun::star::uno::RuntimeException
)
133 void PipeConnection::close()
134 throw( ::com::sun::star::io::IOException
,
135 ::com::sun::star::uno::RuntimeException
)
137 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
143 OUString
PipeConnection::getDescription()
144 throw(::com::sun::star::uno::RuntimeException
)
146 return m_sDescription
;
152 PipeAcceptor::PipeAcceptor( const OUString
&sPipeName
, const OUString
& sConnectionDescription
) :
153 m_sPipeName( sPipeName
),
154 m_sConnectionDescription( sConnectionDescription
),
155 m_bClosed( sal_False
)
160 void PipeAcceptor::init()
162 m_pipe
= Pipe( m_sPipeName
.pData
, osl_Pipe_CREATE
, osl::Security() );
165 OUString error
= OUString("io.acceptor: Couldn't setup pipe ");
166 error
+= m_sPipeName
;
167 throw ConnectionSetupException( error
, Reference
< XInterface
> () );
171 Reference
< XConnection
> PipeAcceptor::accept( )
175 MutexGuard
guard( m_mutex
);
180 OUString error
= OUString("io.acceptor: pipe already closed");
181 error
+= m_sPipeName
;
182 throw ConnectionSetupException( error
, Reference
< XInterface
> () );
184 PipeConnection
*pConn
= new PipeConnection( m_sConnectionDescription
);
186 oslPipeError status
= pipe
.accept( pConn
->m_pipe
);
190 // stopAccepting was called !
192 return Reference
< XConnection
>();
194 else if( osl_Pipe_E_None
== status
)
196 return Reference
< XConnection
> ( (XConnection
* ) pConn
);
200 OUString error
= OUString("io.acceptor: Couldn't setup pipe ");
201 error
+= m_sPipeName
;
202 throw ConnectionSetupException( error
, Reference
< XInterface
> ());
206 void PipeAcceptor::stopAccepting()
208 m_bClosed
= sal_True
;
211 MutexGuard
guard( m_mutex
);
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */