bump product version to 5.0.4.1
[LibreOffice.git] / io / source / acceptor / acc_pipe.cxx
blob34d9bf65d15e7570ea49c5045d332880b02cb19b
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/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;
36 namespace io_acceptor
39 typedef WeakImplHelper1< XConnection > MyPipeConnection;
41 class PipeConnection :
42 public MyPipeConnection
44 public:
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;
62 public:
63 ::osl::StreamPipe m_pipe;
64 oslInterlockedCount m_nStatus;
65 OUString m_sDescription;
70 PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
71 m_nStatus( 0 ),
72 m_sDescription( sConnectionDescription )
74 // make it unique
75 m_sDescription += ",uniqueValue=";
76 m_sDescription += OUString::number(
77 sal::static_int_cast<sal_Int64 >(
78 reinterpret_cast< sal_IntPtr >(&m_pipe)),
79 10 );
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)
90 if( ! m_nStatus )
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 );
102 return n;
104 else {
105 throw IOException();
109 void PipeConnection::write( const Sequence < sal_Int8 > &seq )
110 throw(::com::sun::star::io::IOException,
111 ::com::sun::star::uno::RuntimeException, std::exception)
113 if( ! m_nStatus )
115 if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
117 throw IOException();
120 else {
121 throw IOException();
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) ) )
137 m_pipe.close();
141 OUString PipeConnection::getDescription()
142 throw(::com::sun::star::uno::RuntimeException, std::exception)
144 return m_sDescription;
147 /***************
148 * PipeAcceptor
149 **************/
150 PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
151 m_sPipeName( sPipeName ),
152 m_sConnectionDescription( sConnectionDescription ),
153 m_bClosed( false )
158 void PipeAcceptor::init()
160 m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
161 if( ! m_pipe.is() )
163 OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
164 throw ConnectionSetupException( error );
168 Reference< XConnection > PipeAcceptor::accept( )
170 Pipe pipe;
172 MutexGuard guard( m_mutex );
173 pipe = m_pipe;
175 if( ! pipe.is() )
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 );
184 if( m_bClosed )
186 // stopAccepting was called !
187 delete pConn;
188 return Reference < XConnection >();
190 else if( osl_Pipe_E_None == status )
192 return Reference < XConnection > ( (XConnection * ) pConn );
194 else
196 OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
197 throw ConnectionSetupException( error );
201 void PipeAcceptor::stopAccepting()
203 m_bClosed = true;
204 Pipe pipe;
206 MutexGuard guard( m_mutex );
207 pipe = m_pipe;
208 m_pipe.clear();
210 if( pipe.is() )
212 pipe.close();
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */