xref: /trunk/main/io/source/connector/ctr_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 
27cdf0e10cSrcweir #include "connector.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace ::osl;
30cdf0e10cSrcweir using namespace ::rtl;
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::io;
33cdf0e10cSrcweir using namespace ::com::sun::star::connection;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir namespace stoc_connector {
37cdf0e10cSrcweir 
PipeConnection(const OUString & sConnectionDescription)38cdf0e10cSrcweir     PipeConnection::PipeConnection( const OUString & sConnectionDescription ) :
39cdf0e10cSrcweir         m_nStatus( 0 ),
40cdf0e10cSrcweir         m_sDescription( sConnectionDescription )
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir         g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
43cdf0e10cSrcweir         // make it unique
44cdf0e10cSrcweir         m_sDescription += OUString::createFromAscii( ",uniqueValue=" );
45cdf0e10cSrcweir         m_sDescription += OUString::valueOf(
46cdf0e10cSrcweir             sal::static_int_cast< sal_Int64 >(
47cdf0e10cSrcweir                 reinterpret_cast< sal_IntPtr >(&m_pipe)),
48cdf0e10cSrcweir             10 );
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
~PipeConnection()51cdf0e10cSrcweir     PipeConnection::~PipeConnection()
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
read(Sequence<sal_Int8> & aReadBytes,sal_Int32 nBytesToRead)56cdf0e10cSrcweir     sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
57cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
58cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         if( ! m_nStatus )
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             if( aReadBytes.getLength() != nBytesToRead )
63cdf0e10cSrcweir             {
64cdf0e10cSrcweir                 aReadBytes.realloc( nBytesToRead );
65cdf0e10cSrcweir             }
66cdf0e10cSrcweir             return m_pipe.read( aReadBytes.getArray()  , aReadBytes.getLength() );
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir         else {
69cdf0e10cSrcweir             throw IOException();
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
write(const Sequence<sal_Int8> & seq)73cdf0e10cSrcweir     void PipeConnection::write( const Sequence < sal_Int8 > &seq )
74cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
75cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         if( ! m_nStatus )
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
80cdf0e10cSrcweir             {
81cdf0e10cSrcweir                 throw IOException();
82cdf0e10cSrcweir             }
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir         else {
85cdf0e10cSrcweir             throw IOException();
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
flush()89cdf0e10cSrcweir     void PipeConnection::flush( )
90cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
91cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
close()96cdf0e10cSrcweir     void PipeConnection::close()
97cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
98cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         // ensure that close is called only once
101cdf0e10cSrcweir         if(1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             m_pipe.close();
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
getDescription()107cdf0e10cSrcweir     OUString PipeConnection::getDescription()
108cdf0e10cSrcweir             throw( ::com::sun::star::uno::RuntimeException)
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         return m_sDescription;
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir }
114