xref: /aoo4110/main/binaryurp/source/writer.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_BINARYURP_SOURCE_WRITER_HXX
25 #define INCLUDED_BINARYURP_SOURCE_WRITER_HXX
26 
27 #include "sal/config.h"
28 
29 #include <cstddef>
30 #include <deque>
31 #include <vector>
32 
33 #include "boost/noncopyable.hpp"
34 #include "osl/conditn.hxx"
35 #include "osl/mutex.hxx"
36 #include "osl/thread.hxx"
37 #include "rtl/byteseq.hxx"
38 #include "rtl/ref.hxx"
39 #include "rtl/ustring.hxx"
40 #include "sal/types.h"
41 #include "salhelper/simplereferenceobject.hxx"
42 #include "typelib/typedescription.hxx"
43 #include "uno/dispatcher.hxx"
44 
45 #include "binaryany.hxx"
46 #include "marshal.hxx"
47 #include "writerstate.hxx"
48 
49 namespace binaryurp { class Bridge; }
50 
51 namespace binaryurp {
52 
53 class Writer:
54     public osl::Thread, public salhelper::SimpleReferenceObject,
55     private boost::noncopyable
56 {
57 public:
operator new(std::size_t size)58     static void * operator new(std::size_t size)
59     { return Thread::operator new(size); }
60 
operator delete(void * pointer)61     static void operator delete(void * pointer)
62     { Thread::operator delete(pointer); }
63 
64     explicit Writer(rtl::Reference< Bridge > const & bridge);
65 
66     // Only called from Bridge::reader_ thread, and only before Bridge::writer_
67     // thread is unblocked:
68     void sendDirectRequest(
69         rtl::ByteSequence const & tid, rtl::OUString const & oid,
70         com::sun::star::uno::TypeDescription const & type,
71         com::sun::star::uno::TypeDescription const & member,
72         std::vector< BinaryAny > const & inArguments);
73 
74     // Only called from Bridge::reader_ thread, and only before Bridge::writer_
75     // thread is unblocked:
76     void sendDirectReply(
77         rtl::ByteSequence const & tid,
78         com::sun::star::uno::TypeDescription const & member,
79         bool exception, BinaryAny const & returnValue,
80         std::vector< BinaryAny > const & outArguments);
81 
82     void queueRequest(
83         rtl::ByteSequence const & tid, rtl::OUString const & oid,
84         com::sun::star::uno::TypeDescription const & type,
85         com::sun::star::uno::TypeDescription const & member,
86         std::vector< BinaryAny > const & inArguments);
87 
88     void queueReply(
89         rtl::ByteSequence const & tid,
90         com::sun::star::uno::TypeDescription const & member, bool setter,
91         bool exception, BinaryAny const & returnValue,
92         std::vector< BinaryAny > const & outArguments,
93         bool setCurrentContextMode);
94 
95     void unblock();
96 
97     void stop();
98 
99 private:
100     virtual ~Writer();
101 
102     virtual void SAL_CALL run();
103 
104     virtual void SAL_CALL onTerminated();
105 
106     void sendRequest(
107         rtl::ByteSequence const & tid, rtl::OUString const & oid,
108         com::sun::star::uno::TypeDescription const & type,
109         com::sun::star::uno::TypeDescription const & member,
110         std::vector< BinaryAny > const & inArguments, bool currentContextMode,
111         com::sun::star::uno::UnoInterfaceReference const & currentContext);
112 
113     void sendReply(
114         rtl::ByteSequence const & tid,
115         com::sun::star::uno::TypeDescription const & member, bool setter,
116         bool exception, BinaryAny const & returnValue,
117         std::vector< BinaryAny > const & outArguments);
118 
119     void sendMessage(std::vector< unsigned char > const & buffer);
120 
121     struct Item {
122         Item();
123 
124         // Request:
125         Item(
126             rtl::ByteSequence const & theTid, rtl::OUString const & theOid,
127             com::sun::star::uno::TypeDescription const & theType,
128             com::sun::star::uno::TypeDescription const & theMember,
129             std::vector< BinaryAny > const & inArguments,
130             com::sun::star::uno::UnoInterfaceReference const &
131                 theCurrentContext);
132 
133         // Reply:
134         Item(
135             rtl::ByteSequence const & theTid,
136             com::sun::star::uno::TypeDescription const & theMember,
137             bool theSetter, bool theException, BinaryAny const & theReturnValue,
138             std::vector< BinaryAny > const & outArguments,
139             bool theSetCurrentContextMode);
140 
141         bool request;
142 
143         rtl::ByteSequence tid; // request + reply
144 
145         rtl::OUString oid; // request
146 
147         com::sun::star::uno::TypeDescription type; // request
148 
149         com::sun::star::uno::TypeDescription member; // request + reply
150 
151         bool setter; // reply
152 
153         std::vector< BinaryAny > arguments;
154             // request: inArguments; reply: outArguments
155 
156         bool exception; // reply
157 
158         BinaryAny returnValue; // reply
159 
160         com::sun::star::uno::UnoInterfaceReference currentContext; // request
161 
162         bool setCurrentContextMode; // reply
163     };
164 
165     rtl::Reference< Bridge > bridge_;
166     WriterState state_;
167     Marshal marshal_;
168     com::sun::star::uno::TypeDescription lastType_;
169     rtl::OUString lastOid_;
170     rtl::ByteSequence lastTid_;
171     osl::Condition unblocked_;
172     osl::Condition items_;
173 
174     osl::Mutex mutex_;
175     std::deque< Item > queue_;
176     bool stop_;
177 };
178 
179 }
180 
181 #endif
182