Bump for 3.6-28
[LibreOffice.git] / io / source / acceptor / acc_pipe.cxx
blob751a1526b58b5d5c03242075fa09d1acaa7b5634
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "osl/security.hxx"
30 #include "acceptor.hxx"
31 #include <com/sun/star/connection/ConnectionSetupException.hpp>
33 #include <cppuhelper/implbase1.hxx>
35 using namespace ::rtl;
36 using namespace ::osl;
37 using namespace ::cppu;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::connection;
41 using namespace ::com::sun::star::io;
44 namespace io_acceptor
47 typedef WeakImplHelper1< XConnection > MyPipeConnection;
49 class PipeConnection :
50 public MyPipeConnection
52 public:
53 PipeConnection( const OUString &sConnectionDescription);
54 ~PipeConnection();
56 virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead )
57 throw(::com::sun::star::io::IOException,
58 ::com::sun::star::uno::RuntimeException);
59 virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData )
60 throw(::com::sun::star::io::IOException,
61 ::com::sun::star::uno::RuntimeException);
62 virtual void SAL_CALL flush( ) throw(
63 ::com::sun::star::io::IOException,
64 ::com::sun::star::uno::RuntimeException);
65 virtual void SAL_CALL close( )
66 throw(::com::sun::star::io::IOException,
67 ::com::sun::star::uno::RuntimeException);
68 virtual ::rtl::OUString SAL_CALL getDescription( )
69 throw(::com::sun::star::uno::RuntimeException);
70 public:
71 ::osl::StreamPipe m_pipe;
72 oslInterlockedCount m_nStatus;
73 OUString m_sDescription;
78 PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
79 m_nStatus( 0 ),
80 m_sDescription( sConnectionDescription )
82 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
84 // make it unique
85 m_sDescription += OUString(RTL_CONSTASCII_USTRINGPARAM(",uniqueValue="));
86 m_sDescription += OUString::valueOf(
87 sal::static_int_cast<sal_Int64 >(
88 reinterpret_cast< sal_IntPtr >(&m_pipe)),
89 10 );
92 PipeConnection::~PipeConnection()
94 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
97 sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
98 throw(::com::sun::star::io::IOException,
99 ::com::sun::star::uno::RuntimeException)
101 if( ! m_nStatus )
103 if( aReadBytes.getLength() < nBytesToRead )
105 aReadBytes.realloc( nBytesToRead );
107 sal_Int32 n = m_pipe.read( aReadBytes.getArray(), nBytesToRead );
108 OSL_ASSERT( n >= 0 && n <= aReadBytes.getLength() );
109 if( n < aReadBytes.getLength() )
111 aReadBytes.realloc( n );
113 return n;
115 else {
116 throw IOException();
120 void PipeConnection::write( const Sequence < sal_Int8 > &seq )
121 throw(::com::sun::star::io::IOException,
122 ::com::sun::star::uno::RuntimeException)
124 if( ! m_nStatus )
126 if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
128 throw IOException();
131 else {
132 throw IOException();
136 void PipeConnection::flush( )
137 throw( ::com::sun::star::io::IOException,
138 ::com::sun::star::uno::RuntimeException)
142 void PipeConnection::close()
143 throw( ::com::sun::star::io::IOException,
144 ::com::sun::star::uno::RuntimeException)
146 if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
148 m_pipe.close();
152 OUString PipeConnection::getDescription()
153 throw(::com::sun::star::uno::RuntimeException)
155 return m_sDescription;
158 /***************
159 * PipeAcceptor
160 **************/
161 PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
162 m_sPipeName( sPipeName ),
163 m_sConnectionDescription( sConnectionDescription ),
164 m_bClosed( sal_False )
169 void PipeAcceptor::init()
171 m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
172 if( ! m_pipe.is() )
174 OUString error = OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: Couldn't setup pipe "));
175 error += m_sPipeName;
176 throw ConnectionSetupException( error, Reference< XInterface > () );
180 Reference< XConnection > PipeAcceptor::accept( )
182 Pipe pipe;
184 MutexGuard guard( m_mutex );
185 pipe = m_pipe;
187 if( ! pipe.is() )
189 OUString error = OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: pipe already closed"));
190 error += m_sPipeName;
191 throw ConnectionSetupException( error, Reference< XInterface > () );
193 PipeConnection *pConn = new PipeConnection( m_sConnectionDescription );
195 oslPipeError status = pipe.accept( pConn->m_pipe );
197 if( m_bClosed )
199 // stopAccepting was called !
200 delete pConn;
201 return Reference < XConnection >();
203 else if( osl_Pipe_E_None == status )
205 return Reference < XConnection > ( (XConnection * ) pConn );
207 else
209 OUString error = OUString(RTL_CONSTASCII_USTRINGPARAM("io.acceptor: Couldn't setup pipe "));
210 error += m_sPipeName;
211 throw ConnectionSetupException( error, Reference< XInterface > ());
215 void PipeAcceptor::stopAccepting()
217 m_bClosed = sal_True;
218 Pipe pipe;
220 MutexGuard guard( m_mutex );
221 pipe = m_pipe;
222 m_pipe.clear();
224 if( pipe.is() )
226 pipe.close();
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */