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