namespace Icinga
{
- static class Program
- {
- [DllImport("msi.dll", SetLastError = true)]
- static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
+ internal static class NativeMethods
+ {
+ [DllImport("msi.dll", CharSet = CharSet.Unicode)]
+ internal static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
- [DllImport("msi.dll", CharSet = CharSet.Unicode)]
- static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
+ [DllImport("msi.dll", CharSet = CharSet.Unicode)]
+ internal static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
+ }
+ static class Program
+ {
public static string Icinga2InstallDir
{
get
for (int index = 0; ; index++) {
szProduct = new StringBuilder(39);
- if (MsiEnumProducts(index, szProduct) != 0)
+ if (NativeMethods.MsiEnumProducts(index, szProduct) != 0)
break;
int cbName = 128;
StringBuilder szName = new StringBuilder(cbName);
- if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
+ if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
continue;
if (szName.ToString() != "Icinga 2")
int cbLocation = 1024;
StringBuilder szLocation = new StringBuilder(cbLocation);
- if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
+ if (NativeMethods.MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
return szLocation.ToString();
}
* @param index The index.
* @returns The value.
*/
-Value Array::Get(unsigned int index) const
+Value Array::Get(SizeType index) const
{
ObjectLock olock(this);
* @param index The index.
* @param value The value.
*/
-void Array::Set(unsigned int index, const Value& value)
+void Array::Set(SizeType index, const Value& value)
{
ObjectLock olock(this);
* @param index The index.
* @param value The value.
*/
-void Array::Set(unsigned int index, Value&& value)
+void Array::Set(SizeType index, Value&& value)
{
ObjectLock olock(this);
* @param index The index
* @param value The value to add
*/
-void Array::Insert(unsigned int index, const Value& value)
+void Array::Insert(SizeType index, const Value& value)
{
ObjectLock olock(this);
*
* @param index The index.
*/
-void Array::Remove(unsigned int index)
+void Array::Remove(SizeType index)
{
ObjectLock olock(this);
m_Data.erase(it);
}
-void Array::Resize(size_t new_size)
+void Array::Resize(SizeType newSize)
{
ObjectLock olock(this);
- m_Data.resize(new_size);
+ m_Data.resize(newSize);
}
void Array::Clear(void)
m_Data.clear();
}
-void Array::Reserve(size_t new_size)
+void Array::Reserve(SizeType newSize)
{
ObjectLock olock(this);
- m_Data.reserve(new_size);
+ m_Data.reserve(newSize);
}
void Array::CopyTo(const Array::Ptr& dest) const
inline ~Array(void)
{ }
- Value Get(unsigned int index) const;
- void Set(unsigned int index, const Value& value);
- void Set(unsigned int index, Value&& value);
+ Value Get(SizeType index) const;
+ void Set(SizeType index, const Value& value);
+ void Set(SizeType index, Value&& value);
void Add(const Value& value);
void Add(Value&& value);
size_t GetLength(void) const;
bool Contains(const Value& value) const;
- void Insert(unsigned int index, const Value& value);
- void Remove(unsigned int index);
+ void Insert(SizeType index, const Value& value);
+ void Remove(SizeType index);
void Remove(Iterator it);
- void Resize(size_t new_size);
+ void Resize(SizeType newSize);
void Clear(void);
- void Reserve(size_t new_size);
+ void Reserve(SizeType newSize);
void CopyTo(const Array::Ptr& dest) const;
Array::Ptr ShallowClone(void) const;
namespace icinga
{
+#define I2_TOKENPASTE(x, y) x ## y
+#define I2_TOKENPASTE2(x, y) I2_TOKENPASTE(x, y)
+
+#define I2_UNIQUE_NAME(prefix) I2_TOKENPASTE2(prefix, __COUNTER__)
+
I2_BASE_API bool InitializeOnceHelper(void (*func)(void), int priority = 0);
#define INITIALIZE_ONCE(func) \
- namespace { namespace UNIQUE_NAME(io) { \
+ namespace { namespace I2_UNIQUE_NAME(io) { \
I2_EXPORT bool l_InitializeOnce(icinga::InitializeOnceHelper(func)); \
} }
#define INITIALIZE_ONCE_WITH_PRIORITY(func, priority) \
- namespace { namespace UNIQUE_NAME(io) { \
+ namespace { namespace I2_UNIQUE_NAME(io) { \
I2_EXPORT bool l_InitializeOnce(icinga::InitializeOnceHelper(func, priority)); \
} }
}
*/
Socket::Ptr Socket::Accept(void)
{
- int fd;
sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
- fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
+ SOCKET fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
- if (fd < 0) {
#ifndef _WIN32
+ if (fd < 0) {
Log(LogCritical, "Socket")
- << "accept() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
+ << "accept() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
BOOST_THROW_EXCEPTION(socket_error()
- << boost::errinfo_api_function("accept")
- << boost::errinfo_errno(errno));
+ << boost::errinfo_api_function("accept")
+ << boost::errinfo_errno(errno));
+ }
#else /* _WIN32 */
+ if (fd == INVALID_SOCKET) {
Log(LogCritical, "Socket")
- << "accept() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
+ << "accept() failed with error code " << WSAGetLastError() << ", \"" << Utility::FormatErrorNumber(WSAGetLastError()) << "\"";
BOOST_THROW_EXCEPTION(socket_error()
- << boost::errinfo_api_function("accept")
- << errinfo_win32_error(WSAGetLastError()));
-#endif /* _WIN32 */
+ << boost::errinfo_api_function("accept")
+ << errinfo_win32_error(WSAGetLastError()));
}
+#endif /* _WIN32 */
return new Socket(fd);
}
<< errinfo_getaddrinfo_error(rc));
}
- int fd = INVALID_SOCKET;
+ SOCKET fd = INVALID_SOCKET;
for (addrinfo *info = result; info != NULL; info = info->ai_next) {
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
# define I2_HIDDEN
#endif /* _WIN32 */
-#define TOKENPASTE(x, y) x ## y
-#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
-
-#define UNIQUE_NAME(prefix) TOKENPASTE2(prefix, __COUNTER__)
-
#endif /* VISIBILITY_H */
using namespace icinga;
-int WorkQueue::m_NextID = 1;
+std::atomic<int> WorkQueue::m_NextID(1);
boost::thread_specific_ptr<WorkQueue *> l_ThreadWorkQueue;
WorkQueue::WorkQueue(size_t maxItems, int threadCount)
ASSERT(!m_Name.IsEmpty());
- int pending = m_Tasks.size();
+ size_t pending = m_Tasks.size();
double now = Utility::GetTime();
double gradient = (pending - m_PendingTasks) / (now - m_PendingTasksTimestamp);
m_TaskStats.InsertValue(now, 1);
}
-int WorkQueue::GetTaskCount(RingBuffer::SizeType span)
+size_t WorkQueue::GetTaskCount(RingBuffer::SizeType span)
{
boost::mutex::scoped_lock lock(m_StatsMutex);
return m_TaskStats.UpdateAndGetValues(Utility::GetTime(), span);
#include <boost/exception_ptr.hpp>
#include <queue>
#include <deque>
+#include <atomic>
namespace icinga
{
bool IsWorkerThread(void) const;
size_t GetLength(void) const;
- int GetTaskCount(RingBuffer::SizeType span);
+ size_t GetTaskCount(RingBuffer::SizeType span);
void SetExceptionCallback(const ExceptionCallback& callback);
private:
int m_ID;
String m_Name;
- static int m_NextID;
+ static std::atomic<int> m_NextID;
int m_ThreadCount;
bool m_Spawned;
mutable boost::mutex m_StatsMutex;
RingBuffer m_TaskStats;
- int m_PendingTasks;
+ size_t m_PendingTasks;
double m_PendingTasksTimestamp;
void WorkerThreadProc(void);
}
callback(boost::exception_ptr(), result);
- } catch (const std::exception& ex) {
+ } catch (const std::exception&) {
callback(boost::current_exception(), Empty);
}
}
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
req->AddHeader("Accept", "application/json");
m_Connection->SubmitRequest(req, std::bind(AutocompleteScriptHttpCompletionCallback, _1, _2, callback));
- } catch (const std::exception& ex) {
+ } catch (const std::exception&) {
callback(boost::current_exception(), nullptr);
}
}
}
callback(boost::exception_ptr(), suggestions);
- } catch (const std::exception& ex) {
+ } catch (const std::exception&) {
callback(boost::current_exception(), nullptr);
}
}
}
}
-int JsonRpcConnection::GetWorkQueueCount(void)
+size_t JsonRpcConnection::GetWorkQueueCount(void)
{
return l_JsonRpcConnectionWorkQueueCount;
}
-int JsonRpcConnection::GetWorkQueueLength(void)
+size_t JsonRpcConnection::GetWorkQueueLength(void)
{
- int itemCount = 0;
+ size_t itemCount = 0;
- for (int i = 0; i < GetWorkQueueCount(); i++) {
+ for (size_t i = 0; i < GetWorkQueueCount(); i++)
itemCount += l_JsonRpcConnectionWorkQueues[i].GetLength();
- }
return itemCount;
}
double JsonRpcConnection::GetWorkQueueRate(void)
{
double rate = 0.0;
- int count = GetWorkQueueCount();
+ size_t count = GetWorkQueueCount();
/* If this is a standalone environment, we don't have any queues. */
if (count == 0)
return 0.0;
- for (int i = 0; i < count; i++) {
+ for (size_t i = 0; i < count; i++)
rate += l_JsonRpcConnectionWorkQueues[i].GetTaskCount(60) / 60.0;
- }
return rate / count;
}
static void HeartbeatTimerHandler(void);
static Value HeartbeatAPIHandler(const intrusive_ptr<MessageOrigin>& origin, const Dictionary::Ptr& params);
- static int GetWorkQueueCount(void);
- static int GetWorkQueueLength(void);
+ static size_t GetWorkQueueCount(void);
+ static size_t GetWorkQueueLength(void);
static double GetWorkQueueRate(void);
static void SendCertificateRequest(const JsonRpcConnection::Ptr& aclient, const intrusive_ptr<MessageOrigin>& origin, const String& path);
printInfo.unit = BunitB;
}
- printInfo.showUsed = vm.count("show-used");
+ printInfo.showUsed = vm.count("show-used") > 0;
if (vm.count("debug"))
debug = TRUE;
std::wstring removeZero(DOUBLE val)
{
std::wstring ret = boost::lexical_cast<std::wstring>(val);
- INT pos = ret.length();
+ std::wstring::size_type pos = ret.length();
if (ret.find_first_of(L".") == std::string::npos)
return ret;
for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {