summaryrefslogtreecommitdiffhomepage
path: root/tests/websocketserverprocess.cpp
blob: 89a50ee7edd371d66abe9bb9598d591017b166fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "websocketserverprocess.h"

#include <boost/test/data/dataset.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <fmt/core.h>

#include <chrono>
#include <exception>
#include <filesystem>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>

#include <ext/stdio_filebuf.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>

#include <libreichwein/file.h>
#include <libreichwein/process.h>

#include "webserver.h"
#include "response.h"

#include "helper.h"

using namespace std::string_literals;
namespace fs = std::filesystem;
namespace pt = boost::property_tree;
using namespace boost::unit_test;
using namespace Reichwein;

WebsocketServerProcess::WebsocketServerProcess()
{
 // RAII pattern for shared memory allocation/deallocation
 m_shared = std::unique_ptr<shared_data_t, std::function<void(shared_data_t*)>>(
                            (shared_data_t*)mmap(NULL, sizeof(shared_data_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0),
                            [this](shared_data_t*){munmap(m_shared.get(), sizeof(shared_data_t));});
 start();
}

WebsocketServerProcess::~WebsocketServerProcess()
{
 stop();
}

void WebsocketServerProcess::do_session(boost::asio::ip::tcp::socket socket)
{
 try
 {
  // Construct the stream by moving in the socket
  boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws{std::move(socket)};

  // Set a decorator to change the Server of the handshake
  ws.set_option(boost::beast::websocket::stream_base::decorator(
      [](boost::beast::websocket::response_type& res)
      {
       res.set(boost::beast::http::field::server,
           std::string("Reichwein.IT Test Websocket Server"));
      }));

  boost::beast::http::request_parser<boost::beast::http::string_body> parser;
  request_type req;
  boost::beast::flat_buffer buffer;

  boost::beast::http::read(ws.next_layer(), buffer, parser);
  req = parser.get();
  {
   std::lock_guard lock{m_shared->mutex};
   strncpy(m_shared->subprotocol, std::string{req[http::field::sec_websocket_protocol]}.data(), sizeof(m_shared->subprotocol));
   strncpy(m_shared->target, std::string{req.target()}.data(), sizeof(m_shared->target));
  }

  ws.accept(req);

  for(;;)
  {
   boost::beast::flat_buffer buffer;

   ws.read(buffer);

   // Reply with <request>: <counter>
   ws.text(ws.got_text());
   std::string data(boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data()));
   data += ": " + std::to_string(m_count++);
   buffer.consume(buffer.size());
   boost::beast::ostream(buffer) << data;
   ws.write(buffer.data());
  }
 }
 catch(boost::beast::system_error const& se)
 {
  // This indicates that the session was closed
  if(se.code() != boost::beast::websocket::error::closed)
      std::cerr << "Error: " << se.code().message() << std::endl;
 }
 catch(std::exception const& e)
 {
  std::cerr << "Error: " << e.what() << std::endl;
 }
}

bool WebsocketServerProcess::is_running()
{
 if (m_pid == 0)
  return false;

 return Reichwein::Process::is_running(m_pid);
}

void WebsocketServerProcess::start()
{
 if (m_pid != 0)
  throw std::runtime_error("Process already running, so it can't be started");

 // connect stdout of new child process to stream of parent, via pipe
 m_pid = fork();
 if (m_pid < 0)
  throw std::runtime_error("Fork unsuccessful.");

 if (m_pid == 0) { // child process branch
  try
  {
   auto const address = boost::asio::ip::make_address("::1");
   auto const port = static_cast<unsigned short>(8765);

   // The io_context is required for all I/O
   boost::asio::io_context ioc{1};

   // The acceptor receives incoming connections
   boost::asio::ip::tcp::acceptor acceptor{ioc, {address, port}};
   for(;;)
   {
    // This will receive the new connection
    boost::asio::ip::tcp::socket socket{ioc};

    // Block until we get a connection
    acceptor.accept(socket);

    // Launch the session, transferring ownership of the socket
    std::thread(
        &WebsocketServerProcess::do_session, this,
        std::move(socket)).detach();
   }
  }
  catch (const std::exception& e)
  {
   std::cerr << "Error: " << e.what() << std::endl;
  }
  exit(0);
 }

 wait_for_pid_listening_on(m_pid, 8765);
}

void WebsocketServerProcess::stop()
{
 if (!is_running())
  throw std::runtime_error("Process not running, so it can't be stopped");
 
 if (kill(m_pid, SIGTERM) != 0)
  throw std::runtime_error("Unable to kill process");

 if (int result = waitpid(m_pid, NULL, 0); result != m_pid)
  throw std::runtime_error("waitpid returned "s + std::to_string(result));

 m_pid = 0;
}

std::string WebsocketServerProcess::subprotocol()
{
 std::lock_guard lock{m_shared->mutex};
 return m_shared->subprotocol;
}

std::string WebsocketServerProcess::target()
{
 std::lock_guard lock{m_shared->mutex};
 return m_shared->target;
}