Update ooo320-m1
[ooovba.git] / io / source / acceptor / acc_pipe.cxx
blobf697d3caaed056a4869c44b33fc1cd930eca3d3f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: acc_pipe.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_io.hxx"
33 #include "osl/security.hxx"
34 #include "acceptor.hxx"
35 #include <com/sun/star/connection/ConnectionSetupException.hpp>
37 #include <cppuhelper/implbase1.hxx>
39 using namespace ::rtl;
40 using namespace ::osl;
41 using namespace ::cppu;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::connection;
45 using namespace ::com::sun::star::io;
48 namespace io_acceptor
51 typedef WeakImplHelper1< XConnection > MyPipeConnection;
53 class PipeConnection :
54 public MyPipeConnection
56 public:
57 PipeConnection( const OUString &sConnectionDescription);
58 ~PipeConnection();
60 virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead )
61 throw(::com::sun::star::io::IOException,
62 ::com::sun::star::uno::RuntimeException);
63 virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData )
64 throw(::com::sun::star::io::IOException,
65 ::com::sun::star::uno::RuntimeException);
66 virtual void SAL_CALL flush( ) throw(
67 ::com::sun::star::io::IOException,
68 ::com::sun::star::uno::RuntimeException);
69 virtual void SAL_CALL close( )
70 throw(::com::sun::star::io::IOException,
71 ::com::sun::star::uno::RuntimeException);
72 virtual ::rtl::OUString SAL_CALL getDescription( )
73 throw(::com::sun::star::uno::RuntimeException);
74 public:
75 ::osl::StreamPipe m_pipe;
76 oslInterlockedCount m_nStatus;
77 OUString m_sDescription;
82 PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
83 m_nStatus( 0 ),
84 m_sDescription( sConnectionDescription )
86 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
88 // make it unique
89 m_sDescription += OUString::createFromAscii( ",uniqueValue=" );
90 m_sDescription += OUString::valueOf(
91 sal::static_int_cast<sal_Int64 >(
92 reinterpret_cast< sal_IntPtr >(&m_pipe)),
93 10 );
96 PipeConnection::~PipeConnection()
98 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
101 sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
102 throw(::com::sun::star::io::IOException,
103 ::com::sun::star::uno::RuntimeException)
105 if( ! m_nStatus )
107 if( aReadBytes.getLength() != nBytesToRead )
109 aReadBytes.realloc( nBytesToRead );
111 return m_pipe.read( aReadBytes.getArray() , aReadBytes.getLength() );
113 else {
114 throw IOException();
118 void PipeConnection::write( const Sequence < sal_Int8 > &seq )
119 throw(::com::sun::star::io::IOException,
120 ::com::sun::star::uno::RuntimeException)
122 if( ! m_nStatus )
124 if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
126 throw IOException();
129 else {
130 throw IOException();
134 void PipeConnection::flush( )
135 throw( ::com::sun::star::io::IOException,
136 ::com::sun::star::uno::RuntimeException)
140 void PipeConnection::close()
141 throw( ::com::sun::star::io::IOException,
142 ::com::sun::star::uno::RuntimeException)
144 if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
146 m_pipe.close();
150 OUString PipeConnection::getDescription()
151 throw(::com::sun::star::uno::RuntimeException)
153 return m_sDescription;
156 /***************
157 * PipeAcceptor
158 **************/
159 PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
160 m_sPipeName( sPipeName ),
161 m_sConnectionDescription( sConnectionDescription ),
162 m_bClosed( sal_False )
167 void PipeAcceptor::init()
169 m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
170 if( ! m_pipe.is() )
172 OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " );
173 error += m_sPipeName;
174 throw ConnectionSetupException( error, Reference< XInterface > () );
178 Reference< XConnection > PipeAcceptor::accept( )
180 Pipe pipe;
182 MutexGuard guard( m_mutex );
183 pipe = m_pipe;
185 if( ! pipe.is() )
187 OUString error = OUString::createFromAscii( "io.acceptor: pipe already closed" );
188 error += m_sPipeName;
189 throw ConnectionSetupException( error, Reference< XInterface > () );
191 PipeConnection *pConn = new PipeConnection( m_sConnectionDescription );
193 oslPipeError status = pipe.accept( pConn->m_pipe );
195 if( m_bClosed )
197 // stopAccepting was called !
198 delete pConn;
199 return Reference < XConnection >();
201 else if( osl_Pipe_E_None == status )
203 return Reference < XConnection > ( (XConnection * ) pConn );
205 else
207 OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " );
208 error += m_sPipeName;
209 throw ConnectionSetupException( error, Reference< XInterface > ());
213 void PipeAcceptor::stopAccepting()
215 m_bClosed = sal_True;
216 Pipe pipe;
218 MutexGuard guard( m_mutex );
219 pipe = m_pipe;
220 m_pipe.clear();
222 if( pipe.is() )
224 pipe.close();