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 ::osl;
27 : using namespace ::cppu;
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::lang;
30 : using namespace ::com::sun::star::connection;
31 : using namespace ::com::sun::star::io;
32 :
33 :
34 : namespace io_acceptor
35 : {
36 :
37 : typedef WeakImplHelper1< XConnection > MyPipeConnection;
38 :
39 : class PipeConnection :
40 : public MyPipeConnection
41 : {
42 : public:
43 : PipeConnection( const OUString &sConnectionDescription);
44 : virtual ~PipeConnection();
45 :
46 : virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead )
47 : throw(::com::sun::star::io::IOException,
48 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
49 : virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData )
50 : throw(::com::sun::star::io::IOException,
51 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
52 : virtual void SAL_CALL flush( ) throw(
53 : ::com::sun::star::io::IOException,
54 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 : virtual void SAL_CALL close( )
56 : throw(::com::sun::star::io::IOException,
57 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 : virtual OUString SAL_CALL getDescription( )
59 : throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
60 : public:
61 : ::osl::StreamPipe m_pipe;
62 : oslInterlockedCount m_nStatus;
63 : OUString m_sDescription;
64 : };
65 :
66 :
67 :
68 194 : PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
69 : m_nStatus( 0 ),
70 194 : m_sDescription( sConnectionDescription )
71 : {
72 : // make it unique
73 194 : m_sDescription += ",uniqueValue=";
74 388 : m_sDescription += OUString::number(
75 : sal::static_int_cast<sal_Int64 >(
76 : reinterpret_cast< sal_IntPtr >(&m_pipe)),
77 194 : 10 );
78 194 : }
79 :
80 376 : PipeConnection::~PipeConnection()
81 : {
82 376 : }
83 :
84 1047518 : sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
85 : throw(::com::sun::star::io::IOException,
86 : ::com::sun::star::uno::RuntimeException, std::exception)
87 : {
88 1047518 : if( ! m_nStatus )
89 : {
90 1047518 : if( aReadBytes.getLength() < nBytesToRead )
91 : {
92 1047518 : aReadBytes.realloc( nBytesToRead );
93 : }
94 1047518 : sal_Int32 n = m_pipe.read( aReadBytes.getArray(), nBytesToRead );
95 : OSL_ASSERT( n >= 0 && n <= aReadBytes.getLength() );
96 1047518 : if( n < aReadBytes.getLength() )
97 : {
98 98 : aReadBytes.realloc( n );
99 : }
100 2095036 : return n;
101 : }
102 : else {
103 0 : throw IOException();
104 : }
105 : }
106 :
107 415615 : void PipeConnection::write( const Sequence < sal_Int8 > &seq )
108 : throw(::com::sun::star::io::IOException,
109 : ::com::sun::star::uno::RuntimeException, std::exception)
110 : {
111 415615 : if( ! m_nStatus )
112 : {
113 415615 : if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
114 : {
115 0 : throw IOException();
116 : }
117 : }
118 : else {
119 0 : throw IOException();
120 : }
121 415615 : }
122 :
123 0 : void PipeConnection::flush( )
124 : throw( ::com::sun::star::io::IOException,
125 : ::com::sun::star::uno::RuntimeException, std::exception)
126 : {
127 0 : }
128 :
129 98 : void PipeConnection::close()
130 : throw( ::com::sun::star::io::IOException,
131 : ::com::sun::star::uno::RuntimeException, std::exception)
132 : {
133 98 : if( 1 == osl_atomic_increment( (&m_nStatus) ) )
134 : {
135 98 : m_pipe.close();
136 : }
137 98 : }
138 :
139 96 : OUString PipeConnection::getDescription()
140 : throw(::com::sun::star::uno::RuntimeException, std::exception)
141 : {
142 96 : return m_sDescription;
143 : }
144 :
145 : /***************
146 : * PipeAcceptor
147 : **************/
148 98 : PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
149 : m_sPipeName( sPipeName ),
150 : m_sConnectionDescription( sConnectionDescription ),
151 98 : m_bClosed( false )
152 : {
153 98 : }
154 :
155 :
156 98 : void PipeAcceptor::init()
157 : {
158 98 : m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
159 98 : if( ! m_pipe.is() )
160 : {
161 0 : OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
162 0 : throw ConnectionSetupException( error );
163 : }
164 98 : }
165 :
166 194 : Reference< XConnection > PipeAcceptor::accept( )
167 : {
168 194 : Pipe pipe;
169 : {
170 194 : MutexGuard guard( m_mutex );
171 194 : pipe = m_pipe;
172 : }
173 194 : if( ! pipe.is() )
174 : {
175 0 : OUString error = "io.acceptor: pipe already closed" + m_sPipeName;
176 0 : throw ConnectionSetupException( error );
177 : }
178 194 : PipeConnection *pConn = new PipeConnection( m_sConnectionDescription );
179 :
180 194 : oslPipeError status = pipe.accept( pConn->m_pipe );
181 :
182 194 : if( m_bClosed )
183 : {
184 : // stopAccepting was called !
185 96 : delete pConn;
186 96 : return Reference < XConnection >();
187 : }
188 98 : else if( osl_Pipe_E_None == status )
189 : {
190 98 : return Reference < XConnection > ( (XConnection * ) pConn );
191 : }
192 : else
193 : {
194 0 : OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
195 0 : throw ConnectionSetupException( error );
196 194 : }
197 : }
198 :
199 96 : void PipeAcceptor::stopAccepting()
200 : {
201 96 : m_bClosed = true;
202 96 : Pipe pipe;
203 : {
204 96 : MutexGuard guard( m_mutex );
205 96 : pipe = m_pipe;
206 96 : m_pipe.clear();
207 : }
208 96 : if( pipe.is() )
209 : {
210 96 : pipe.close();
211 96 : }
212 96 : }
213 : }
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|