• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 21101426787

17 Jan 2026 09:52PM UTC coverage: 28.005% (+0.01%) from 27.995%
21101426787

push

github

reinder
[traintastic] fixed restart crash (out of memory)

12 of 38 new or added lines in 11 files covered. (31.58%)

5 existing lines in 2 files now uncovered.

8165 of 29156 relevant lines covered (28.0%)

193.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/server/src/network/websocketconnection.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2025-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#include "websocketconnection.hpp"
23
#include <cassert>
24
#include <thread>
25
#include "server.hpp"
26
#include "../core/eventloop.hpp"
27
#include "../log/log.hpp"
28

29
namespace {
30

31
std::string createId(const boost::beast::websocket::stream<boost::beast::tcp_stream>& ws, std::string_view idPrefix)
×
32
{
33
  auto& socket = boost::beast::get_lowest_layer(ws).socket();
×
34
  return std::string(idPrefix)
×
35
    .append("[")
×
36
    .append(socket.remote_endpoint().address().to_string())
×
37
    .append(":")
×
38
    .append(std::to_string(socket.remote_endpoint().port()))
×
39
    .append("]");
×
40
}
41

42
}
43

44
WebSocketConnection::WebSocketConnection(Server& server, std::shared_ptr<boost::beast::websocket::stream<boost::beast::tcp_stream>> ws, std::string_view idPrefix)
×
45
  : m_server{server}
×
46
  , m_ws(std::move(ws))
×
47
  , id{createId(*m_ws, idPrefix)}
×
48
{
49
  assert(isServerThread());
×
50
  assert(m_ws);
×
51
}
×
52

53
WebSocketConnection::~WebSocketConnection()
×
54
{
55
  assert(isEventLoopThread());
×
56
  assert(!m_ws->is_open());
×
57
}
×
58

59
void WebSocketConnection::start()
×
60
{
61
  assert(isServerThread());
×
62
  doRead();
×
63
}
×
64

65
void WebSocketConnection::disconnect()
×
66
{
67
  assert(isEventLoopThread());
×
68

69
  m_server.m_ioContext.post(
×
NEW
70
    [this, serverWeak=m_server.weak_from_this()]()
×
71
    {
72
      if(m_ws->is_open())
×
73
      {
74
        boost::system::error_code ec;
×
75
        m_ws->close(boost::beast::websocket::close_code::normal, ec);
×
76
      }
77

78
      EventLoop::call(
×
NEW
79
        [this, serverWeak]()
×
80
        {
NEW
81
          if(auto server = serverWeak.lock()) // server might be gone when this is processed.
×
82
          {
NEW
83
            server->connectionGone(shared_from_this());
×
NEW
84
          }
×
85
        });
×
86
    });
×
87
}
×
88

89
#ifndef NDEBUG
90
bool WebSocketConnection::isServerThread() const
×
91
{
92
  return std::this_thread::get_id() == m_server.threadId();
×
93
}
94
#endif
95

96
boost::asio::io_context& WebSocketConnection::ioContext()
×
97
{
98
  return m_server.m_ioContext;
×
99
}
100

101
void WebSocketConnection::connectionLost()
×
102
{
103
  assert(isEventLoopThread());
×
104
  Log::log(id, LogMessage::I1004_CONNECTION_LOST);
×
105
  disconnect();
×
106
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc