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