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