RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
Asio.hpp
1 /*
2  * AsioService.hpp
3  *
4  * Copyright (C) 2020 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_ASIO_HPP
25 #define LAUNCHER_PLUGINS_ASIO_HPP
26 
27 #include <Noncopyable.hpp>
28 
29 #include <functional>
30 
31 #include <PImpl.hpp>
32 #include <utils/Functionals.hpp>
33 
34 namespace rstudio {
35 namespace launcher_plugins {
36 namespace system {
37 
38 // Forward declarations
39 class DateTime;
40 class TimeDuration;
41 
45 typedef std::function<void()> AsioFunction;
46 
50 typedef std::function<void(const char*, size_t)> OnReadBytes;
51 
55 typedef std::function<void(int)> OnSignal;
56 
60 class AsioService final : public Noncopyable
61 {
62 public:
63 
69  static void post(const AsioFunction& in_work);
70 
79  static void setSignalHandler(const OnSignal& in_onSignal);
80 
86  static void startThreads(size_t in_numThreads);
87 
94  static void stop();
95 
96  /*
97  * @brief Waits for the ASIO service to exit.
98  */
99  static void waitForExit();
100 
101 private:
105  AsioService();
106 
112  static AsioService& getAsioService();
113 
114  // Private implementation of AsioService.
115  PRIVATE_IMPL_SHARED(m_impl);
116 };
117 
121 class AsioStream final
122 {
123 public:
129  explicit AsioStream(int in_streamHandle);
130 
134  ~AsioStream() noexcept;
135 
139  void close() noexcept;
140 
147  void readBytes(const OnReadBytes& in_onReadBytes, const OnError& in_onError);
148 
155  void writeBytes(
156  const std::string& in_data,
157  const OnError& in_onError,
158  const AsioFunction& in_onFinishedWriting = AsioFunction());
159 
160 private:
161  // The private implementation of AsioStream.
162  PRIVATE_IMPL_SHARED(m_impl);
163 };
164 
168 class AsyncTimedEvent final
169 {
170 public:
174  AsyncTimedEvent();
175 
185  void start(const TimeDuration& in_timeDuration, const AsioFunction& in_event);
186 
190  void cancel();
191 
200  void reportError(const Error& in_error);
201 
202 private:
203 
204  // The private implementation of AsyncTimedEvent.
205  PRIVATE_IMPL_SHARED(m_impl);
206 };
207 
212 {
213 public:
220  AsyncDeadlineEvent(const AsioFunction& in_work, const DateTime& in_deadlineTime);
227  AsyncDeadlineEvent(const AsioFunction& in_work, const TimeDuration& in_waitTime);
228 
233 
237  void cancel();
238 
242  void start();
243 
244 private:
245  // The private implementation of AsioDeadlineEvent.
246  PRIVATE_IMPL_SHARED(m_impl);
247 };
248 
249 } // namespace system
250 } // namespace launcher_plugins
251 } // namespace rstudio
252 
253 #endif
rstudio::launcher_plugins::system::AsioStream::close
void close() noexcept
Closes the stream. Nothing may be read from or written to the stream after this is called.
rstudio::launcher_plugins::system::AsioService
Async input/output class which may be used to manage ASIO operations.
Definition: Asio.hpp:60
rstudio::launcher_plugins::system::AsyncDeadlineEvent::~AsyncDeadlineEvent
~AsyncDeadlineEvent()
Destructor. The event will be canceled if this invoked before the deadline time.
rstudio::launcher_plugins::system::AsyncDeadlineEvent
Class which may be used to post async work to be performed at a later time.
Definition: Asio.hpp:211
rstudio::launcher_plugins::system::AsioService::setSignalHandler
static void setSignalHandler(const OnSignal &in_onSignal)
Sets the signal handler on the ASIO service.
rstudio::launcher_plugins::system::AsyncDeadlineEvent::start
void start()
Starts waiting for the event.
rstudio::launcher_plugins::system::AsioStream::AsioStream
AsioStream(int in_streamHandle)
Constructor.
rstudio::launcher_plugins::system::AsyncTimedEvent
Class which performs an action asynchronously every specified number of seconds.
Definition: Asio.hpp:168
rstudio::launcher_plugins::system::AsyncDeadlineEvent::cancel
void cancel()
Cancels the event, if invoked before the deadline time.
rstudio::launcher_plugins::system::AsioStream
Class which allows reading from or writing to streams asynchronously.
Definition: Asio.hpp:121
rstudio::launcher_plugins::system::AsyncTimedEvent::start
void start(const TimeDuration &in_timeDuration, const AsioFunction &in_event)
Starts performing the specified event every in_timeDuration.
rstudio::launcher_plugins::system::AsioService::post
static void post(const AsioFunction &in_work)
Posts a job to be completed by this ASIO Service.
rstudio::launcher_plugins::OnError
std::function< void(const Error &)> OnError
Definition: Functionals.hpp:43
rstudio::launcher_plugins::system::AsyncTimedEvent::reportError
void reportError(const Error &in_error)
Reports a fatal error to the AsyncTimedEvent instance. Invoking this will cause the timed event to st...
rstudio::launcher_plugins::system::AsioService::startThreads
static void startThreads(size_t in_numThreads)
Creates and adds the specified number of worker threads to the ASIO service.
rstudio::launcher_plugins::system::DateTime
Class which represents a date and time in UTC.
Definition: DateTime.hpp:244
rstudio::launcher_plugins::system::AsyncTimedEvent::AsyncTimedEvent
AsyncTimedEvent()
Default constructor.
rstudio::launcher_plugins::Error
Class which represents an error.
Definition: Error.hpp:174
Functionals.hpp
rstudio::launcher_plugins::system::TimeDuration
Represents an duration of time (e.g. 5 hours, 43 minutes, and 21 seconds) as opposed to a point in ti...
Definition: DateTime.hpp:48
rstudio::launcher_plugins::system::AsioStream::~AsioStream
~AsioStream() noexcept
Destructor. Closes the stream.
rstudio::launcher_plugins::system::AsioService::stop
static void stop()
Stops the ASIO Service.
rstudio::launcher_plugins::Noncopyable
Class which can be inherited from to disallow copying of its child classes.
Definition: Noncopyable.hpp:34
rstudio::launcher_plugins::system::AsioStream::writeBytes
void writeBytes(const std::string &in_data, const OnError &in_onError, const AsioFunction &in_onFinishedWriting=AsioFunction())
Writes the provided data to the stream asynchronously.
rstudio::launcher_plugins::system::AsyncTimedEvent::cancel
void cancel()
Cancels the timed event.
rstudio::launcher_plugins::system::AsyncDeadlineEvent::AsyncDeadlineEvent
AsyncDeadlineEvent(const AsioFunction &in_work, const DateTime &in_deadlineTime)
Constructor.
rstudio::launcher_plugins::system::AsioStream::readBytes
void readBytes(const OnReadBytes &in_onReadBytes, const OnError &in_onError)
Attempts to read bytes from this ASIO stream.