1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_io.hxx" 26 #include "osl/security.hxx" 27 #include "acceptor.hxx" 28 #include <com/sun/star/connection/ConnectionSetupException.hpp> 29 30 #include <cppuhelper/implbase1.hxx> 31 32 using namespace ::rtl; 33 using namespace ::osl; 34 using namespace ::cppu; 35 using namespace ::com::sun::star::uno; 36 using namespace ::com::sun::star::lang; 37 using namespace ::com::sun::star::connection; 38 using namespace ::com::sun::star::io; 39 40 41 namespace io_acceptor 42 { 43 44 typedef WeakImplHelper1< XConnection > MyPipeConnection; 45 46 class PipeConnection : 47 public MyPipeConnection 48 { 49 public: 50 PipeConnection( const OUString &sConnectionDescription); 51 ~PipeConnection(); 52 53 virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead ); 54 virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData ); 55 virtual void SAL_CALL flush( ); 56 virtual void SAL_CALL close( ); 57 virtual ::rtl::OUString SAL_CALL getDescription( ); 58 public: 59 ::osl::StreamPipe m_pipe; 60 oslInterlockedCount m_nStatus; 61 OUString m_sDescription; 62 }; 63 64 65 66 PipeConnection::PipeConnection( const OUString &sConnectionDescription) : 67 m_nStatus( 0 ), 68 m_sDescription( sConnectionDescription ) 69 { 70 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); 71 72 // make it unique 73 m_sDescription += OUString::createFromAscii( ",uniqueValue=" ); 74 m_sDescription += OUString::valueOf( 75 sal::static_int_cast<sal_Int64 >( 76 reinterpret_cast< sal_IntPtr >(&m_pipe)), 77 10 ); 78 } 79 80 PipeConnection::~PipeConnection() 81 { 82 g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); 83 } 84 85 sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) 86 { 87 if( ! m_nStatus ) 88 { 89 if( aReadBytes.getLength() < nBytesToRead ) 90 { 91 aReadBytes.realloc( nBytesToRead ); 92 } 93 sal_Int32 n = m_pipe.read( aReadBytes.getArray(), nBytesToRead ); 94 OSL_ASSERT( n >= 0 && n <= aReadBytes.getLength() ); 95 if( n < aReadBytes.getLength() ) 96 { 97 aReadBytes.realloc( n ); 98 } 99 return n; 100 } 101 else { 102 throw IOException(); 103 } 104 } 105 106 void PipeConnection::write( const Sequence < sal_Int8 > &seq ) 107 { 108 if( ! m_nStatus ) 109 { 110 if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) 111 { 112 throw IOException(); 113 } 114 } 115 else { 116 throw IOException(); 117 } 118 } 119 120 void PipeConnection::flush( ) 121 { 122 } 123 124 void PipeConnection::close() 125 { 126 if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) ) 127 { 128 m_pipe.close(); 129 } 130 } 131 132 OUString PipeConnection::getDescription() 133 { 134 return m_sDescription; 135 } 136 137 /*************** 138 * PipeAcceptor 139 **************/ 140 PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) : 141 m_sPipeName( sPipeName ), 142 m_sConnectionDescription( sConnectionDescription ), 143 m_bClosed( sal_False ) 144 { 145 } 146 147 148 void PipeAcceptor::init() 149 { 150 m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() ); 151 if( ! m_pipe.is() ) 152 { 153 OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " ); 154 error += m_sPipeName; 155 throw ConnectionSetupException( error, Reference< XInterface > () ); 156 } 157 } 158 159 Reference< XConnection > PipeAcceptor::accept( ) 160 { 161 Pipe pipe; 162 { 163 MutexGuard guard( m_mutex ); 164 pipe = m_pipe; 165 } 166 if( ! pipe.is() ) 167 { 168 OUString error = OUString::createFromAscii( "io.acceptor: pipe already closed" ); 169 error += m_sPipeName; 170 throw ConnectionSetupException( error, Reference< XInterface > () ); 171 } 172 PipeConnection *pConn = new PipeConnection( m_sConnectionDescription ); 173 174 oslPipeError status = pipe.accept( pConn->m_pipe ); 175 176 if( m_bClosed ) 177 { 178 // stopAccepting was called ! 179 delete pConn; 180 return Reference < XConnection >(); 181 } 182 else if( osl_Pipe_E_None == status ) 183 { 184 return Reference < XConnection > ( (XConnection * ) pConn ); 185 } 186 else 187 { 188 OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " ); 189 error += m_sPipeName; 190 throw ConnectionSetupException( error, Reference< XInterface > ()); 191 } 192 } 193 194 void PipeAcceptor::stopAccepting() 195 { 196 m_bClosed = sal_True; 197 Pipe pipe; 198 { 199 MutexGuard guard( m_mutex ); 200 pipe = m_pipe; 201 m_pipe.clear(); 202 } 203 if( pipe.is() ) 204 { 205 pipe.close(); 206 } 207 } 208 } 209