-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathAsioService.h
More file actions
142 lines (121 loc) · 4.06 KB
/
AsioService.h
File metadata and controls
142 lines (121 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2009, 2010, 2011 Object Computing, Inc.
// All rights reserved.
// See the file license.txt for licensing information.
//
#ifdef _MSC_VER
# pragma once
#endif
#ifndef ASIOSERVICE_H
#define ASIOSERVICE_H
#include "AsioService_fwd.h"
#include <Common/QuickFAST_Export.h>
#include <Common/Logger_fwd.h>
#include <Common/AtomicCounter.h>
// In gcc including asio.hpp in precompiled headers causes problems
#include <boost/asio.hpp>
namespace QuickFAST
{
namespace Communication
{
/// @brief Base class to allow sharing a boost::io_service
///
/// Normal case is for all classes derived from AsioService to share
/// the same boost::io_service. The alternate constructor gives the
/// application more control if it is needed.
class QuickFAST_Export AsioService : public boost::asio::io_service
{
public:
/// @brief Construct using the internal, common io service
AsioService();
/// @brief Construct using an external io service
AsioService(boost::asio::io_service & ioService);
~AsioService();
/// @brief define the logger to receive informative messages
/// @param logger to which messages will be written
void setLogger(Common::Logger & logger);
/// @brief Run the event loop with this threads and threadCount additional threads.
void runThreads(size_t threadCount = 0, bool useThisThread = true);
/// @brief run the event loop in this thread
///
/// Exceptions are caught, logged, and ignored. The event loop continues.
void run();
/// @brief run the event loop until one event is handled.
void run_one()
{
ioService_.run_one();
}
/// @brief execute any ready event handlers than return.
size_t poll()
{
return ioService_.poll();
}
/// @brief execute at most one ready event handler than return.
size_t poll_one()
{
return ioService_.poll_one();
}
/// @brief Allow external access (thereby blowing encapsulization)
boost::asio::io_service & ioService()
{
return ioService_;
}
/// @brief create additional threads to run the event loop
void startThreads(size_t threadCount)
{
runThreads(threadCount, false);
}
/// @brief join all additional threads after calling stopService()
///
/// If stopService() has not been called, this will block "forever".
void joinThreads();
/// @brief reset the IO service
///
/// should be called after joinThreads before calling run*, poll*, etc. again.
void resetService()
{
ioService_.reset();
stopping_ = false;
}
/// @brief stop the ioservice
void stopService();
/// @brief allow implicit cast to io_service
operator boost::asio::io_service &()
{
return ioService_;
}
///@brief Post a completion handler for later processing (usually in a different thread)
/// @param handler is the handler to be posted
template<typename CompletionHandler>
void post(CompletionHandler handler)
{
ioService_.post(handler);
}
/// @brief Attempt to determine how many threads are available to ASIO
/// @returns the number of threads.
long runningThreadCount()const
{
if(usingSharedService_)
{
return sharedRunningThreadCount_;
}
return runningThreadCount_;
}
private:
// if no io_service is specified, this one
// will be used (shared among all users)
static boost::asio::io_service sharedIoService_;
static AtomicCounter sharedRunningThreadCount_;
/// Pointer to a boost thread
typedef boost::shared_ptr<boost::thread> ThreadPtr;
bool stopping_;
boost::scoped_array<ThreadPtr> threads_;
size_t threadCount_;
size_t threadCapacity_;
AtomicCounter runningThreadCount_;
boost::asio::io_service & ioService_;
bool usingSharedService_;
Common::Logger * logger_;
};
}
}
#endif // ASIOSERVICE_H