Debian Codegraph @

Network/UdpConnection.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <arpa/inet.h>
24 #include <string>
25
26 #include "Logger/Logger.h"
27
28 namespace Network
29 {
30 class UdpConnection
31 {
32 public:
33 UdpConnection() = delete;
34
35 UdpConnection(Logger::Logger* logger, const std::string& server, const unsigned short port);
36
37 UdpConnection(Logger::Logger* logger, struct sockaddr* sockaddr);
38
39 inline ~UdpConnection()
40 {
41 Terminate();
42 }
43
44 void Terminate();
45
46 bool Bind();
47
48 inline bool IsConnected()
49 {
50 return connected;
51 }
52
53 int Send(const char* buffer, const size_t bufferLength);
54
55 inline int Send(const unsigned char* buffer, const size_t bufferLength)
56 {
57 return Send(reinterpret_cast<const char*>(buffer), bufferLength);
58 }
59
60 inline int Send(const std::string& string)
61 {
62 return Send(string.c_str(), string.size());
63 }
64
65 int Receive(char* buffer, const size_t bufferLength);
66
67 inline int Receive(unsigned char* buffer, const size_t bufferLength)
68 {
69 return Receive(reinterpret_cast<char*>(buffer), bufferLength);
70 }
71
72 private:
73 void CreateUdpSocket();
74
75 Logger::Logger* const logger;
76 int connectionSocket;
77 volatile bool connected;
78 struct sockaddr sockaddr;
79 std::string server;
80 unsigned short port;
81 };
82 }