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/XConnection.hpp>
23 #include <com/sun/star/connection/ConnectionSetupException.hpp>
24 #include <com/sun/star/io/IOException.hpp>
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
28 #include <cppuhelper/implbase.hxx>
30 using namespace ::osl
;
31 using namespace ::cppu
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::connection
;
35 using namespace ::com::sun::star::io
;
41 class PipeConnection
:
42 public WeakImplHelper
< XConnection
>
45 explicit PipeConnection( const OUString
&sConnectionDescription
);
47 virtual sal_Int32 SAL_CALL
read( Sequence
< sal_Int8
>& aReadBytes
, sal_Int32 nBytesToRead
) override
;
48 virtual void SAL_CALL
write( const Sequence
< sal_Int8
>& aData
) override
;
49 virtual void SAL_CALL
flush( ) override
;
50 virtual void SAL_CALL
close( ) override
;
51 virtual OUString SAL_CALL
getDescription( ) override
;
53 ::osl::StreamPipe m_pipe
;
54 oslInterlockedCount m_nStatus
;
55 OUString m_sDescription
;
59 PipeConnection::PipeConnection( const OUString
&sConnectionDescription
) :
61 m_sDescription( sConnectionDescription
)
64 m_sDescription
+= ",uniqueValue=";
65 m_sDescription
+= OUString::number(
66 sal::static_int_cast
<sal_Int64
>(
67 reinterpret_cast< sal_IntPtr
>(&m_pipe
)) );
70 sal_Int32
PipeConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
76 if( aReadBytes
.getLength() < nBytesToRead
)
78 aReadBytes
.realloc( nBytesToRead
);
80 sal_Int32 n
= m_pipe
.read( aReadBytes
.getArray(), nBytesToRead
);
81 OSL_ASSERT( n
>= 0 && n
<= aReadBytes
.getLength() );
82 if( n
< aReadBytes
.getLength() )
84 aReadBytes
.realloc( n
);
90 void PipeConnection::write( const Sequence
< sal_Int8
> &seq
)
96 if( m_pipe
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
102 void PipeConnection::flush( )
106 void PipeConnection::close()
108 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
114 OUString
PipeConnection::getDescription()
116 return m_sDescription
;
122 PipeAcceptor::PipeAcceptor( const OUString
&sPipeName
, const OUString
& sConnectionDescription
) :
123 m_sPipeName( sPipeName
),
124 m_sConnectionDescription( sConnectionDescription
),
130 void PipeAcceptor::init()
132 m_pipe
= Pipe( m_sPipeName
.pData
, osl_Pipe_CREATE
, osl::Security() );
135 OUString error
= "io.acceptor: Couldn't setup pipe " + m_sPipeName
;
136 throw ConnectionSetupException( error
);
140 Reference
< XConnection
> PipeAcceptor::accept( )
144 MutexGuard
guard( m_mutex
);
149 OUString error
= "io.acceptor: pipe already closed" + m_sPipeName
;
150 throw ConnectionSetupException( error
);
152 std::unique_ptr
<PipeConnection
> pConn(new PipeConnection( m_sConnectionDescription
));
154 oslPipeError status
= pipe
.accept( pConn
->m_pipe
);
158 // stopAccepting was called !
159 return Reference
< XConnection
>();
161 else if( osl_Pipe_E_None
== status
)
163 return Reference
< XConnection
> ( static_cast<XConnection
*>(pConn
.release()) );
167 OUString error
= "io.acceptor: Couldn't setup pipe " + m_sPipeName
;
168 throw ConnectionSetupException( error
);
172 void PipeAcceptor::stopAccepting()
177 MutexGuard
guard( m_mutex
);
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */