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