bump product version to 6.4.0.3
[LibreOffice.git] / io / source / acceptor / acc_pipe.cxx
blobad9ce09a20cb974e4692a38756ea82965c1438c8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
38 namespace io_acceptor
41 class PipeConnection :
42 public WeakImplHelper< XConnection >
44 public:
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;
52 public:
53 ::osl::StreamPipe m_pipe;
54 oslInterlockedCount m_nStatus;
55 OUString m_sDescription;
59 PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
60 m_nStatus( 0 ),
61 m_sDescription( sConnectionDescription )
63 // make it unique
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 )
72 if( m_nStatus )
74 throw IOException();
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 );
86 return n;
90 void PipeConnection::write( const Sequence < sal_Int8 > &seq )
92 if( m_nStatus )
94 throw IOException();
96 if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
98 throw IOException();
102 void PipeConnection::flush( )
106 void PipeConnection::close()
108 if( 1 == osl_atomic_increment( (&m_nStatus) ) )
110 m_pipe.close();
114 OUString PipeConnection::getDescription()
116 return m_sDescription;
119 /***************
120 * PipeAcceptor
121 **************/
122 PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
123 m_sPipeName( sPipeName ),
124 m_sConnectionDescription( sConnectionDescription ),
125 m_bClosed( false )
130 void PipeAcceptor::init()
132 m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
133 if( ! m_pipe.is() )
135 OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
136 throw ConnectionSetupException( error );
140 Reference< XConnection > PipeAcceptor::accept( )
142 Pipe pipe;
144 MutexGuard guard( m_mutex );
145 pipe = m_pipe;
147 if( ! pipe.is() )
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 );
156 if( m_bClosed )
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()) );
165 else
167 OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
168 throw ConnectionSetupException( error );
172 void PipeAcceptor::stopAccepting()
174 m_bClosed = true;
175 Pipe pipe;
177 MutexGuard guard( m_mutex );
178 pipe = m_pipe;
179 m_pipe.clear();
181 if( pipe.is() )
183 pipe.close();
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */