summaryrefslogtreecommitdiff
path: root/src/components/transport_manager/src/websocket_server/websocket_session.cc
blob: 1def04d70065876b441d155bc2e4ede4b4da528c (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
/*
Copyright (c) 2020 Livio, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

* Neither the name of SmartDeviceLink Consortium, Inc. nor the names of its
 contributors may be used to endorse or promote products derived from
 this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "transport_manager/websocket_server/websocket_session.h"
#include <unistd.h>
#include "transport_manager/transport_adapter/transport_adapter_controller.h"

namespace transport_manager {
namespace transport_adapter {

SDL_CREATE_LOG_VARIABLE("WebSocketSession")

using namespace boost::beast::websocket;

template <>
WebSocketSession<tcp::socket&>::WebSocketSession(
    boost::asio::ip::tcp::socket socket,
    DataReceiveCallback data_receive,
    DataSendDoneCallback data_send_done,
    DataSendFailedCallback data_send_failed,
    OnIOErrorCallback on_error)
    : socket_(std::move(socket))
    , ws_(socket_)
    , data_receive_(data_receive)
    , data_send_done_(data_send_done)
    , data_send_failed_(data_send_failed)
    , on_io_error_(on_error) {
  ws_.binary(true);
}

#ifdef ENABLE_SECURITY
template <>
WebSocketSession<ssl::stream<tcp::socket&> >::WebSocketSession(
    boost::asio::ip::tcp::socket socket,
    ssl::context& ctx,
    DataReceiveCallback data_receive,
    DataSendDoneCallback data_send_done,
    DataSendFailedCallback data_send_failed,
    OnIOErrorCallback on_error)
    : socket_(std::move(socket))
    , ws_(socket_, ctx)
    , data_receive_(data_receive)
    , data_send_done_(data_send_done)
    , data_send_failed_(data_send_failed)
    , on_io_error_(on_error) {
  ws_.binary(true);
}
template class WebSocketSession<ssl::stream<tcp::socket&> >;
#endif  // ENABLE_SECURITY

template <typename ExecutorType>
WebSocketSession<ExecutorType>::~WebSocketSession() {}

template <typename ExecutorType>
void WebSocketSession<ExecutorType>::AsyncAccept() {
  SDL_LOG_AUTO_TRACE();
  ws_.async_accept(std::bind(&WebSocketSession::AsyncRead,
                             this->shared_from_this(),
                             std::placeholders::_1));
}

template <typename ExecutorType>
void WebSocketSession<ExecutorType>::AsyncRead(boost::system::error_code ec) {
  SDL_LOG_AUTO_TRACE();
  if (ec) {
    auto str_err = "ErrorMessage: " + ec.message();
    SDL_LOG_ERROR(str_err);
    return;
  }

  ws_.async_read(buffer_,
                 std::bind(&WebSocketSession::Read,
                           this->shared_from_this(),
                           std::placeholders::_1,
                           std::placeholders::_2));
}

template <typename ExecutorType>
void WebSocketSession<ExecutorType>::WriteDown(Message message) {
  boost::system::error_code ec;
  ws_.write(boost::asio::buffer(message->data(), message->data_size()), ec);

  if (ec) {
    SDL_LOG_ERROR("A system error has occurred: " << ec.message());
    data_send_failed_(message);
    on_io_error_();
    return;
  }
  data_send_done_(message);
}

template <typename ExecutorType>
void WebSocketSession<ExecutorType>::Read(boost::system::error_code ec,
                                          std::size_t bytes_transferred) {
  SDL_LOG_AUTO_TRACE();
  boost::ignore_unused(bytes_transferred);
  if (ec) {
    SDL_LOG_ERROR(ec.message());
    buffer_.consume(buffer_.size());
    on_io_error_();
    return;
  }

  auto size = buffer_.size();
  const auto data = boost::asio::buffer_cast<const uint8_t*>(
      boost::beast::buffers_front(buffer_.data()));

  SDL_LOG_DEBUG("Msg: " << boost::beast::buffers_to_string(buffer_.data())
                        << " Size: " << size;);

  auto frame =
      std::make_shared<protocol_handler::RawMessage>(0, 0, data, size, false);

  data_receive_(frame);

  buffer_.consume(buffer_.size());
  AsyncRead(ec);
}

template <typename ExecutorType>
bool WebSocketSession<ExecutorType>::Shutdown() {
  SDL_LOG_AUTO_TRACE();
  boost::system::error_code ec;
  if (socket_.is_open()) {
    socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
    socket_.close();
  }
  buffer_.consume(buffer_.size());
  if (ec) {
    SDL_LOG_ERROR(ec.message());
    return false;
  }
  return true;
}

template class WebSocketSession<tcp::socket&>;

}  // namespace transport_adapter
}  // namespace transport_manager