Project
Loading...
Searching...
No Matches
DISocket.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef O2_DISOCKET_HPP
12#define O2_DISOCKET_HPP
13
14#include <cstring>
15#include "Framework/Traits.h"
16#include "boost/asio.hpp"
17#include "boost/endian/conversion.hpp"
18#include <sstream>
19
20struct DIMessage {
21 struct __attribute__((packed)) Header {
22 enum class Type : uint32_t {
23 INVALID = 0,
24 DATA = 1,
25 DEVICE_ON = 2,
26 DEVICE_OFF = 3,
27 INSPECT_ON = 4,
28 INSPECT_OFF = 5,
29 TERMINATE = 6
30 };
31
32 Header(Type type, uint64_t payloadSize) : typeLE(boost::endian::native_to_little(static_cast<uint32_t>(type))), payloadSizeLE(boost::endian::native_to_little(payloadSize)) {}
33 Header(Type type) : Header(type, 0) {}
34 Header() : Header(Type::INVALID, 0) {}
35
36 Type type() const;
37 uint64_t payloadSize() const;
38
39 private:
40 uint32_t typeLE;
41 uint64_t payloadSizeLE;
42 };
43
44 template <typename T>
45 DIMessage(Header::Type type, const T& payload)
46 {
47 uint64_t payloadSize = 0;
48 if constexpr (std::is_base_of_v<std::string, T>) {
49 payloadSize = payload.size();
50 this->payload = new char[payloadSize];
51 std::memcpy(this->payload, payload.data(), payloadSize);
52 } else if constexpr (std::is_integral_v<T>) {
53 payloadSize = sizeof(T);
54 payload = boost::endian::native_to_little(payload);
55 this->payload = new char[payloadSize];
56 std::memcpy(this->payload, &payload, payloadSize);
57 } else {
58 static_assert(o2::framework::always_static_assert_v<T>, "DISocket: Cannot create message of this type.");
59 }
60
61 header = Header{type, payloadSize};
62 }
63 DIMessage() : header(Header::Type::INVALID), payload(nullptr) {}
64
65 DIMessage(const DIMessage& other) noexcept;
66 DIMessage& operator=(const DIMessage& other) noexcept;
67
68 DIMessage(DIMessage&& other) noexcept;
70
71 ~DIMessage();
72
73 template <typename T>
74 T get() const
75 {
76 if constexpr (std::is_same_v<std::string, T>) {
77 return std::string{payload, header.payloadSize()};
78 } else if constexpr (std::is_integral_v<T>) {
79 return boost::endian::little_to_native(*((T*)payload));
80 } else {
81 static_assert(o2::framework::always_static_assert_v<T>, "DISocket: Cannot create object of this type.");
82 }
83 }
84
86 char* payload;
87};
88
90{
91 public:
92 DISocket(const std::string& address, int port);
93 ~DISocket();
94
95 bool isMessageAvailable();
96 void send(const DIMessage& message);
98
99 private:
100 boost::asio::io_context ioContext;
101 boost::asio::ip::tcp::socket socket;
102};
103
104#endif // O2_DISOCKET_HPP
bool isMessageAvailable()
Definition DISocket.cxx:87
void send(const DIMessage &message)
Definition DISocket.cxx:92
DIMessage receive()
Definition DISocket.cxx:102
GLuint GLuint64EXT address
Definition glcorearb.h:5846
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLuint GLsizei const GLchar * message
Definition glcorearb.h:2517
DIMessage(Header::Type type, const T &payload)
Definition DISocket.h:45
Header header
Definition DISocket.h:85
DIMessage & operator=(const DIMessage &other) noexcept
Definition DISocket.cxx:38
DIMessage()
Definition DISocket.h:63
char * payload
Definition DISocket.h:86
T get() const
Definition DISocket.h:74
size_t payloadSize
Definition parser.cxx:30
VectorOfTObjectPtrs other