μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProtocolUDP.hpp
Go to the documentation of this file.
1/*
2---------------------------------------------------------------------------
3
4 This file is part of uHAL.
5
6 uHAL is a hardware access library and programming framework
7 originally developed for upgrades of the Level-1 trigger of the CMS
8 experiment at CERN.
9
10 uHAL is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 uHAL is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22
23
24 Andrew Rose, Imperial College, London
25 email: awr01 <AT> imperial.ac.uk
26
27 Marc Magrans de Abril, CERN
28 email: marc.magrans.de.abril <AT> cern.ch
29
30---------------------------------------------------------------------------
31*/
32
39#ifndef _uhal_ProtocolUDP_hpp_
40#define _uhal_ProtocolUDP_hpp_
41
42
43#include <condition_variable>
44#include <deque>
45#include <iostream>
46#include <memory>
47#include <stdint.h>
48#include <string>
49#include <thread>
50#include <vector>
51
52#include <boost/asio/io_service.hpp>
53#include <boost/asio/ip/udp.hpp>
54#include <boost/asio/deadline_timer.hpp>
55
58
59
60namespace boost {
61 namespace system { class error_code; }
62}
63
64namespace uhal
65{
66 // Forward declarations
67 class Buffers;
68 struct URI;
69
70 namespace exception
71 {
73 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( UdpTimeout , ClientTimeout , "Exception class to handle the case where the UDP connection timed out." )
75 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ErrorAtUdpSocketCreation , TransportLayerError , "Exception class to handle a failure to create a UDP socket." )
77 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ASIOUdpError , TransportLayerError , "Exception class to handle the case where ASIO returned an error." )
78 }
79
81 template < typename InnerProtocol >
82 class UDP : public InnerProtocol
83 {
84
85 private:
91 UDP ( const UDP& aUDP ); // non-copyable
92
99 UDP& operator= ( const UDP& aUDP ); // non-assignable
100
101 public:
107 UDP ( const std::string& aId, const URI& aUri );
108
110 virtual ~UDP();
111
112 private:
118 void implementDispatch ( std::shared_ptr< Buffers > aBuffers );
119
121 virtual void Flush( );
122
124 virtual void dispatchExceptionHandler();
125
130 uint32_t getMaxSendSize();
131
136 uint32_t getMaxReplySize();
137
139 void connect();
140
146 void write ( );
147
153 void write_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
154
160 void read ( );
161
167 void read_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
168
170 void CheckDeadline();
171
176 void NotifyConditionalVariable ( const bool& aValue );
177
179 void WaitOnConditionalVariable();
180
181 private:
184
186 boost::asio::io_service mIOservice;
187
189 boost::asio::ip::udp::socket mSocket;
190
192 boost::asio::ip::udp::endpoint mEndpoint;
193
195 boost::asio::deadline_timer mDeadlineTimer;
196
202 std::vector<uint8_t> mReplyMemory;
203
205 boost::asio::io_service::work mIOserviceWork;
206
208 std::thread mDispatchThread;
209
212
214 std::deque < std::shared_ptr< Buffers > > mDispatchQueue;
216 std::deque < std::shared_ptr< Buffers > > mReplyQueue;
217
220
224 std::condition_variable mConditionalVariable;
227
229 std::shared_ptr< Buffers > mDispatchBuffers;
231 std::shared_ptr< Buffers > mReplyBuffers;
232
238
239 };
240
241
242}
243
244
245#endif
Wrapper to generate a new Python exception type.
Definition: pybind11.h:2517
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
Transport protocol to transfer an IPbus buffer via UDP.
Definition: ProtocolUDP.hpp:83
std::deque< std::shared_ptr< Buffers > > mDispatchQueue
The list of buffers still waiting to be sent.
uint32_t mPacketsInFlight
Counter of how many writes have been sent, for which no reply has yet been received.
std::condition_variable mConditionalVariable
A conditional variable for blocking the main thread until the variable with which it is associated is...
std::mutex mConditionalVariableMutex
A mutex for use by the conditional variable.
std::mutex mTransportLayerMutex
A MutEx lock used to make sure the access functions are thread safe.
std::shared_ptr< Buffers > mDispatchBuffers
The send operation currently in progress.
boost::asio::io_service mIOservice
The boost::asio::io_service used to create the connections.
std::vector< uint8_t > mReplyMemory
A block of memory into which we write replies, before copying them to their final destination.
boost::asio::io_service::work mIOserviceWork
Needed when multi-threading to stop the boost::asio::io_service thinking it has nothing to do and so ...
boost::asio::ip::udp::endpoint mEndpoint
A shared pointer to a boost::asio udp endpoint - used in the ASIO send and receive functions (UDP has...
std::deque< std::shared_ptr< Buffers > > mReplyQueue
The list of buffers still awaiting a reply.
UDP(const UDP &aUDP)
Copy Constructor This creates a new socket, dispatch queue, dispatch thread, etc.
bool mFlushDone
A variable associated with the conditional variable which specifies whether all packets have been sen...
boost::asio::deadline_timer mDeadlineTimer
The mechanism for providing the time-out.
std::thread mDispatchThread
The Worker thread in Multi-threaded mode.
size_t mMaxPayloadSize
The maximum UDP payload size (in bytes)
std::shared_ptr< Buffers > mReplyBuffers
The receive operation currently in progress or the next to be done.
boost::asio::ip::udp::socket mSocket
A shared pointer to a boost::asio udp socket through which the operation will be performed.
uhal::exception::exception * mAsynchronousException
A pointer to an exception object for passing exceptions from the worker thread to the main thread.
An abstract base exception class, including an interface to throw as the derived type (for passing ex...
Definition: exception.hpp:71
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:49
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50