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 <osl/diagnose.h>
25 #include <osl/mutex.hxx>
26 #include <cppuhelper/implbase1.hxx>
28 using namespace ::osl
;
29 using namespace ::cppu
;
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::lang
;
32 using namespace ::com::sun::star::connection
;
33 using namespace ::com::sun::star::io
;
39 typedef WeakImplHelper1
< XConnection
> MyPipeConnection
;
41 class PipeConnection
:
42 public MyPipeConnection
45 PipeConnection( const OUString
&sConnectionDescription
);
46 virtual ~PipeConnection();
48 virtual sal_Int32 SAL_CALL
read( Sequence
< sal_Int8
>& aReadBytes
, sal_Int32 nBytesToRead
)
49 throw(::com::sun::star::io::IOException
,
50 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
51 virtual void SAL_CALL
write( const Sequence
< sal_Int8
>& aData
)
52 throw(::com::sun::star::io::IOException
,
53 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
54 virtual void SAL_CALL
flush( ) throw(
55 ::com::sun::star::io::IOException
,
56 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
57 virtual void SAL_CALL
close( )
58 throw(::com::sun::star::io::IOException
,
59 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
60 virtual OUString SAL_CALL
getDescription( )
61 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
63 ::osl::StreamPipe m_pipe
;
64 oslInterlockedCount m_nStatus
;
65 OUString m_sDescription
;
70 PipeConnection::PipeConnection( const OUString
&sConnectionDescription
) :
72 m_sDescription( sConnectionDescription
)
75 m_sDescription
+= ",uniqueValue=";
76 m_sDescription
+= OUString::number(
77 sal::static_int_cast
<sal_Int64
>(
78 reinterpret_cast< sal_IntPtr
>(&m_pipe
)),
82 PipeConnection::~PipeConnection()
86 sal_Int32
PipeConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
87 throw(::com::sun::star::io::IOException
,
88 ::com::sun::star::uno::RuntimeException
, std::exception
)
92 if( aReadBytes
.getLength() < nBytesToRead
)
94 aReadBytes
.realloc( nBytesToRead
);
96 sal_Int32 n
= m_pipe
.read( aReadBytes
.getArray(), nBytesToRead
);
97 OSL_ASSERT( n
>= 0 && n
<= aReadBytes
.getLength() );
98 if( n
< aReadBytes
.getLength() )
100 aReadBytes
.realloc( n
);
109 void PipeConnection::write( const Sequence
< sal_Int8
> &seq
)
110 throw(::com::sun::star::io::IOException
,
111 ::com::sun::star::uno::RuntimeException
, std::exception
)
115 if( m_pipe
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
125 void PipeConnection::flush( )
126 throw( ::com::sun::star::io::IOException
,
127 ::com::sun::star::uno::RuntimeException
, std::exception
)
131 void PipeConnection::close()
132 throw( ::com::sun::star::io::IOException
,
133 ::com::sun::star::uno::RuntimeException
, std::exception
)
135 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
141 OUString
PipeConnection::getDescription()
142 throw(::com::sun::star::uno::RuntimeException
, std::exception
)
144 return m_sDescription
;
150 PipeAcceptor::PipeAcceptor( const OUString
&sPipeName
, const OUString
& sConnectionDescription
) :
151 m_sPipeName( sPipeName
),
152 m_sConnectionDescription( sConnectionDescription
),
158 void PipeAcceptor::init()
160 m_pipe
= Pipe( m_sPipeName
.pData
, osl_Pipe_CREATE
, osl::Security() );
163 OUString error
= "io.acceptor: Couldn't setup pipe " + m_sPipeName
;
164 throw ConnectionSetupException( error
);
168 Reference
< XConnection
> PipeAcceptor::accept( )
172 MutexGuard
guard( m_mutex
);
177 OUString error
= "io.acceptor: pipe already closed" + m_sPipeName
;
178 throw ConnectionSetupException( error
);
180 PipeConnection
*pConn
= new PipeConnection( m_sConnectionDescription
);
182 oslPipeError status
= pipe
.accept( pConn
->m_pipe
);
186 // stopAccepting was called !
188 return Reference
< XConnection
>();
190 else if( osl_Pipe_E_None
== status
)
192 return Reference
< XConnection
> ( (XConnection
* ) pConn
);
196 OUString error
= "io.acceptor: Couldn't setup pipe " + m_sPipeName
;
197 throw ConnectionSetupException( error
);
201 void PipeAcceptor::stopAccepting()
206 MutexGuard
guard( m_mutex
);
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */