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