Project
Loading...
Searching...
No Matches
DISocket.cxx
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#include "DISocket.h"
12#include <fairlogger/Logger.h>
13
14#define ASIO_CATCH(customMessage) \
15 catch (boost::system::system_error & err) \
16 { \
17 auto msg = std::string{err.what()}; \
18 auto code = std::to_string(err.code().value()); \
19 throw std::runtime_error{"Exception in DataInspector (boost_code=" + code + ", boost_msg=" + msg + ") - " + customMessage}; \
20 }
21
22DIMessage::Header::Type DIMessage::Header::type() const
23{
24 return static_cast<DIMessage::Header::Type>(boost::endian::little_to_native(typeLE));
25}
26
27uint64_t DIMessage::Header::payloadSize() const
28{
29 return boost::endian::little_to_native(payloadSizeLE);
30}
31
32DIMessage::DIMessage(const DIMessage& other) noexcept : header(other.header)
33{
34 this->payload = new char[other.header.payloadSize()];
35 std::memcpy(this->payload, other.payload, other.header.payloadSize());
36}
37
39{
40 if (&other == this) {
41 return *this;
42 }
43
44 this->header = Header{other.header};
45
46 delete[] payload;
47 this->payload = new char[other.header.payloadSize()];
48 std::memcpy(this->payload, other.payload, other.header.payloadSize());
49
50 return *this;
51}
52
53DIMessage::DIMessage(DIMessage&& other) noexcept : header(other.header), payload(other.payload)
54{
55 other.payload = nullptr;
56}
57
59{
60 header = Header{other.header};
61 delete[] payload;
62 payload = other.payload;
63
64 other.payload = nullptr;
65 return *this;
66}
67
69{
70 delete[] payload;
71}
72
73DISocket::DISocket(const std::string& address, int port) : ioContext(), socket(ioContext)
74{
75 try {
76 auto ip_address = boost::asio::ip::make_address(address);
77 socket.connect(boost::asio::ip::tcp::endpoint(ip_address, port));
78 }
79 ASIO_CATCH("DISocket::DISocket")
80}
81
83{
84 socket.close();
85}
86
88{
89 return socket.available() >= sizeof(DIMessage::Header);
90}
91
93{
94 try {
95 socket.send(std::array<boost::asio::const_buffer, 2>{
96 boost::asio::buffer(&message.header, sizeof(DIMessage::Header)),
97 boost::asio::buffer(message.payload, message.header.payloadSize())});
98 }
99 ASIO_CATCH("DISocket::send")
100}
101
103{
104 try {
106 socket.receive(boost::asio::buffer(&message.header, sizeof(DIMessage::Header)));
107
108 if (message.header.payloadSize() > 0) {
109 message.payload = new char[message.header.payloadSize()];
110 socket.receive(boost::asio::buffer(message.payload, message.header.payloadSize()));
111 }
112
113 return message;
114 }
115 ASIO_CATCH("DISocket::receive")
116 return {};
117}
#define ASIO_CATCH(customMessage)
Definition DISocket.cxx:14
bool isMessageAvailable()
Definition DISocket.cxx:87
void send(const DIMessage &message)
Definition DISocket.cxx:92
DISocket(const std::string &address, int port)
Definition DISocket.cxx:73
DIMessage receive()
Definition DISocket.cxx:102
GLuint GLuint64EXT address
Definition glcorearb.h:5846
GLuint GLsizei const GLchar * message
Definition glcorearb.h:2517
DIMessage & operator=(const DIMessage &other) noexcept
Definition DISocket.cxx:38
DIMessage()
Definition DISocket.h:63
char * payload
Definition DISocket.h:86
VectorOfTObjectPtrs other