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 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 0 : PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
70 : m_nStatus( 0 ),
71 0 : m_sDescription( sConnectionDescription )
72 : {
73 0 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
74 :
75 : // make it unique
76 0 : m_sDescription += OUString(",uniqueValue=");
77 : m_sDescription += OUString::valueOf(
78 : sal::static_int_cast<sal_Int64 >(
79 : reinterpret_cast< sal_IntPtr >(&m_pipe)),
80 0 : 10 );
81 0 : }
82 :
83 0 : PipeConnection::~PipeConnection()
84 : {
85 0 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
86 0 : }
87 :
88 0 : 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 0 : if( ! m_nStatus )
93 : {
94 0 : if( aReadBytes.getLength() < nBytesToRead )
95 : {
96 0 : aReadBytes.realloc( nBytesToRead );
97 : }
98 0 : sal_Int32 n = m_pipe.read( aReadBytes.getArray(), nBytesToRead );
99 : OSL_ASSERT( n >= 0 && n <= aReadBytes.getLength() );
100 0 : if( n < aReadBytes.getLength() )
101 : {
102 0 : aReadBytes.realloc( n );
103 : }
104 0 : return n;
105 : }
106 : else {
107 0 : throw IOException();
108 : }
109 : }
110 :
111 0 : void PipeConnection::write( const Sequence < sal_Int8 > &seq )
112 : throw(::com::sun::star::io::IOException,
113 : ::com::sun::star::uno::RuntimeException)
114 : {
115 0 : if( ! m_nStatus )
116 : {
117 0 : if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
118 : {
119 0 : throw IOException();
120 : }
121 : }
122 : else {
123 0 : throw IOException();
124 : }
125 0 : }
126 :
127 0 : void PipeConnection::flush( )
128 : throw( ::com::sun::star::io::IOException,
129 : ::com::sun::star::uno::RuntimeException)
130 : {
131 0 : }
132 :
133 0 : void PipeConnection::close()
134 : throw( ::com::sun::star::io::IOException,
135 : ::com::sun::star::uno::RuntimeException)
136 : {
137 0 : if( 1 == osl_atomic_increment( (&m_nStatus) ) )
138 : {
139 0 : m_pipe.close();
140 : }
141 0 : }
142 :
143 0 : OUString PipeConnection::getDescription()
144 : throw(::com::sun::star::uno::RuntimeException)
145 : {
146 0 : return m_sDescription;
147 : }
148 :
149 : /***************
150 : * PipeAcceptor
151 : **************/
152 0 : PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
153 : m_sPipeName( sPipeName ),
154 : m_sConnectionDescription( sConnectionDescription ),
155 0 : m_bClosed( sal_False )
156 : {
157 0 : }
158 :
159 :
160 0 : void PipeAcceptor::init()
161 : {
162 0 : m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
163 0 : if( ! m_pipe.is() )
164 : {
165 0 : OUString error = OUString("io.acceptor: Couldn't setup pipe ");
166 0 : error += m_sPipeName;
167 0 : throw ConnectionSetupException( error, Reference< XInterface > () );
168 : }
169 0 : }
170 :
171 0 : Reference< XConnection > PipeAcceptor::accept( )
172 : {
173 0 : Pipe pipe;
174 : {
175 0 : MutexGuard guard( m_mutex );
176 0 : pipe = m_pipe;
177 : }
178 0 : if( ! pipe.is() )
179 : {
180 0 : OUString error = OUString("io.acceptor: pipe already closed");
181 0 : error += m_sPipeName;
182 0 : throw ConnectionSetupException( error, Reference< XInterface > () );
183 : }
184 0 : PipeConnection *pConn = new PipeConnection( m_sConnectionDescription );
185 :
186 0 : oslPipeError status = pipe.accept( pConn->m_pipe );
187 :
188 0 : if( m_bClosed )
189 : {
190 : // stopAccepting was called !
191 0 : delete pConn;
192 0 : return Reference < XConnection >();
193 : }
194 0 : else if( osl_Pipe_E_None == status )
195 : {
196 0 : return Reference < XConnection > ( (XConnection * ) pConn );
197 : }
198 : else
199 : {
200 0 : OUString error = OUString("io.acceptor: Couldn't setup pipe ");
201 0 : error += m_sPipeName;
202 0 : throw ConnectionSetupException( error, Reference< XInterface > ());
203 0 : }
204 : }
205 :
206 0 : void PipeAcceptor::stopAccepting()
207 : {
208 0 : m_bClosed = sal_True;
209 0 : Pipe pipe;
210 : {
211 0 : MutexGuard guard( m_mutex );
212 0 : pipe = m_pipe;
213 0 : m_pipe.clear();
214 : }
215 0 : if( pipe.is() )
216 : {
217 0 : pipe.close();
218 0 : }
219 0 : }
220 : }
221 :
222 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|