μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProtocolTCP.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_ProtocolTCP_hpp_
40#define _uhal_ProtocolTCP_hpp_
41
42
43#include <chrono>
44#include <condition_variable>
45#include <deque>
46#include <iostream>
47#include <memory>
48#include <stdint.h>
49#include <string>
50#include <thread>
51#include <vector>
52
53#include <boost/asio/io_service.hpp>
54#include <boost/asio/ip/tcp.hpp>
55#include <boost/asio/deadline_timer.hpp>
56
60
61namespace uhal
62{
63 // Forward declarations
64 class Buffers;
65 struct URI;
66
67
68 namespace exception
69 {
71 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( TcpTimeout , ClientTimeout , "Exception class to handle the case where the TCP connection timed out." )
73 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ErrorAtTcpSocketCreation , TransportLayerError , "Exception class to handle a failure to create a TCP socket." )
75 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( TcpConnectionFailure , TransportLayerError , "Exception class to handle the case where the TCP connection was refused or aborted." )
77 UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( ASIOTcpError , TransportLayerError , "Exception class to handle the case where ASIO returned an error." )
78
79 }
80
82 template < typename InnerProtocol , std::size_t nr_buffers_per_send >
83 class TCP : public InnerProtocol
84 {
85
86 private:
92 TCP ( const TCP& aTCP ); // non-copyable
93
100 TCP& operator= ( const TCP& aTCP ); // non-assignable
101
102 public:
108 TCP ( const std::string& aId, const URI& aUri );
109
111 virtual ~TCP();
112
113 private:
119 void implementDispatch ( std::shared_ptr< Buffers > aBuffers );
120
122 virtual void Flush( );
123
125 virtual void dispatchExceptionHandler();
126
131 uint32_t getMaxSendSize();
132
137 uint32_t getMaxReplySize();
138
140 void connect();
141
147 void write ( );
148
154 void write_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
155
161 void read ( );
162
168 void read_callback ( const boost::system::error_code& aErrorCode , std::size_t aBytesTransferred );
169
171 void CheckDeadline();
172
177 void NotifyConditionalVariable ( const bool& aValue );
178
180 void WaitOnConditionalVariable();
181
182
183 private:
184 typedef std::chrono::steady_clock SteadyClock_t;
185
188
190 boost::asio::io_service mIOservice;
191
193 boost::asio::ip::tcp::socket mSocket;
194
196 boost::asio::ip::tcp::resolver::iterator mEndpoint;
197
199 boost::asio::deadline_timer mDeadlineTimer;
200
202 boost::asio::io_service::work mIOserviceWork;
203
205 std::thread mDispatchThread;
206
209
211 std::deque < std::shared_ptr< Buffers > > mDispatchQueue;
213 std::deque < std::pair<std::vector< std::shared_ptr< Buffers > >, SteadyClock_t::time_point> > mReplyQueue;
214
217
220
224 std::condition_variable mConditionalVariable;
227
235
242
247 std::vector< std::shared_ptr< Buffers > > mDispatchBuffers;
248
253 std::pair< std::vector< std::shared_ptr< Buffers > >, SteadyClock_t::time_point > mReplyBuffers;
254
260
261
262 SteadyClock_t::time_point mLastSendQueued;
263 SteadyClock_t::time_point mLastRecvQueued;
264
269 };
270
271}
272
273
274#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 TCP.
Definition: ProtocolTCP.hpp:84
boost::asio::deadline_timer mDeadlineTimer
The mechanism for providing the time-out.
TimeIntervalStats mRTTStats
std::pair< std::vector< std::shared_ptr< Buffers > >, SteadyClock_t::time_point > mReplyBuffers
The buffers containing the payloads for the receive operation that's currently in progress.
std::vector< std::shared_ptr< Buffers > > mDispatchBuffers
The buffers containing the payload for the send operation that's currently in progress.
TCP(const TCP &aTCP)
Copy Constructor This creates a new socket, dispatch queue, dispatch thread, etc.
std::mutex mTransportLayerMutex
A MutEx lock used to make sure the access functions are thread safe.
TimeIntervalStats mLSTStats
uint32_t mPacketsInFlight
Counter of how many writes have been sent, for which no reply has yet been received.
bool mFlushStarted
Boolean specifying whether or not the main thread is within TCP::Flush method. Its value checked by t...
size_t mMaxPayloadSize
The maximum UDP payload size (in bytes)
std::chrono::steady_clock SteadyClock_t
std::condition_variable mConditionalVariable
A conditional variable for blocking the main thread until the variable with which it is associated is...
bool mFlushDone
A variable associated with the conditional variable which specifies whether all packets have been sen...
std::thread mDispatchThread
The Worker thread in Multi-threaded mode.
SteadyClock_t::time_point mLastSendQueued
SteadyClock_t::time_point mLastRecvQueued
std::deque< std::pair< std::vector< std::shared_ptr< Buffers > >, SteadyClock_t::time_point > > mReplyQueue
The list of buffers still awaiting a reply.
TimeIntervalStats mInterRecvTimeStats
boost::asio::ip::tcp::resolver::iterator mEndpoint
A shared pointer to a boost::asio tcp endpoint - used by the delayed (open-on-first-use) connect.
uint32_t mSendByteCounter
Variable storing "number of bytes to follow" field for the TCP chunk currently being sent.
std::mutex mConditionalVariableMutex
A mutex for use by the conditional variable.
std::deque< std::shared_ptr< Buffers > > mDispatchQueue
The list of buffers still waiting to be sent.
uint32_t mReplyByteCounter
Variable used to store "number of bytes to follow" field for the next/current TCP chunk being receive...
boost::asio::ip::tcp::socket mSocket
A shared pointer to a boost::asio tcp socket through which the operation will be performed.
boost::asio::io_service mIOservice
The boost::asio::io_service used to create the connections.
TimeIntervalStats mInterSendTimeStats
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 ...
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