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

traintastic / traintastic / 24288605255

11 Apr 2026 06:17PM UTC coverage: 25.599% (-2.4%) from 27.99%
24288605255

push

github

web-flow
Merge pull request #222 from traintastic/cbus

Added CBUS/VLCB hardware support

169 of 3369 new or added lines in 99 files covered. (5.02%)

5 existing lines in 4 files now uncovered.

8300 of 32423 relevant lines covered (25.6%)

178.31 hits per line

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

0.0
/server/src/hardware/protocol/cbus/iohandler/cbuscanetheriohandler.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 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 "cbuscanetheriohandler.hpp"
23
#include <boost/asio/write.hpp>
24
#include "../cbuskernel.hpp"
25
#include "../../../../core/eventloop.hpp"
26
#include "../../../../log/log.hpp"
27
#include "../../../../log/logmessageexception.hpp"
28

29
namespace CBUS {
30

NEW
31
CANEtherIOHandler::CANEtherIOHandler(Kernel& kernel, std::string hostname, uint16_t port)
×
32
  : ASCIIIOHandler(kernel)
NEW
33
  , m_hostname{std::move(hostname)}
×
NEW
34
  , m_port{port}
×
NEW
35
  , m_socket{m_kernel.ioContext()}
×
36
{
NEW
37
}
×
38

NEW
39
void CANEtherIOHandler::start()
×
40
{
NEW
41
  boost::system::error_code ec;
×
42

NEW
43
  m_endpoint.port(m_port);
×
NEW
44
  m_endpoint.address(boost::asio::ip::make_address(m_hostname, ec));
×
NEW
45
  if(ec)
×
46
  {
NEW
47
    throw LogMessageException(LogMessage::E2003_MAKE_ADDRESS_FAILED_X, ec);
×
48
  }
49

NEW
50
  m_socket.async_connect(m_endpoint,
×
NEW
51
    [this](const boost::system::error_code& err)
×
52
    {
NEW
53
      if(!err)
×
54
      {
NEW
55
        m_socket.set_option(boost::asio::socket_base::linger(true, 0));
×
NEW
56
        m_socket.set_option(boost::asio::ip::tcp::no_delay(true));
×
57

NEW
58
        m_connected = true;
×
59

NEW
60
        read();
×
61

NEW
62
        if(!m_writeQueue.empty())
×
63
        {
NEW
64
          write();
×
65
        }
66

NEW
67
        m_kernel.started();
×
68
      }
NEW
69
      else if(err != boost::asio::error::operation_aborted)
×
70
      {
NEW
71
        EventLoop::call(
×
NEW
72
          [this, err]()
×
73
          {
NEW
74
            Log::log(m_kernel.logId, LogMessage::E2005_SOCKET_CONNECT_FAILED_X, err);
×
NEW
75
            m_kernel.error();
×
NEW
76
          });
×
77
      }
NEW
78
    });
×
NEW
79
}
×
80

NEW
81
void CANEtherIOHandler::stop()
×
82
{
NEW
83
  boost::system::error_code ec;
×
NEW
84
  m_socket.cancel(ec);
×
NEW
85
  m_socket.close(ec);
×
86
  // ignore errors
NEW
87
  m_connected = false;
×
NEW
88
}
×
89

NEW
90
void CANEtherIOHandler::read()
×
91
{
NEW
92
  m_socket.async_read_some(boost::asio::buffer(m_readBuffer.data() + m_readBufferOffset, m_readBuffer.size() - m_readBufferOffset),
×
NEW
93
    [this](const boost::system::error_code& ec, std::size_t bytesTransferred)
×
94
    {
NEW
95
      if(!ec)
×
96
      {
NEW
97
        processRead(bytesTransferred);
×
NEW
98
        read();
×
99
      }
NEW
100
      else if(ec != boost::asio::error::operation_aborted)
×
101
      {
NEW
102
        EventLoop::call(
×
NEW
103
          [this, ec]()
×
104
          {
NEW
105
            Log::log(m_kernel.logId, LogMessage::E1007_SOCKET_READ_FAILED_X, ec);
×
NEW
106
            m_kernel.error();
×
NEW
107
          });
×
108
      }
NEW
109
    });
×
NEW
110
}
×
111

NEW
112
void CANEtherIOHandler::write()
×
113
{
NEW
114
  if(!m_connected)
×
115
  {
NEW
116
    return;
×
117
  }
118

NEW
119
  assert(!m_writeQueue.empty());
×
NEW
120
  const auto& message = m_writeQueue.front();
×
NEW
121
  boost::asio::async_write(m_socket, boost::asio::buffer(message.data(), message.size()),
×
NEW
122
    [this](const boost::system::error_code& ec, std::size_t /*bytesTransferred*/)
×
123
    {
NEW
124
      if(!ec)
×
125
      {
NEW
126
        m_writeQueue.pop();
×
NEW
127
        if(!m_writeQueue.empty())
×
128
        {
NEW
129
          write();
×
130
        }
131
      }
NEW
132
      else if(ec != boost::asio::error::operation_aborted)
×
133
      {
NEW
134
        EventLoop::call(
×
NEW
135
          [this, ec]()
×
136
          {
NEW
137
            Log::log(m_kernel.logId, LogMessage::E1006_SOCKET_WRITE_FAILED_X, ec);
×
NEW
138
            m_kernel.error();
×
NEW
139
          });
×
140
      }
NEW
141
    });
×
142
}
143

144
}
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