18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.

This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
peri4
2013-03-18 12:07:44 +04:00
parent cfc5eed75e
commit 66c53a27fc
72 changed files with 4407 additions and 960 deletions

185
piincludes.h Executable file → Normal file
View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
Global includes
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,11 +20,11 @@
#ifndef PIINCLUDES_H
#define PIINCLUDES_H
#define PIP_VERSION 0x000200
#define PIP_VERSION 0x000206
#define PIP_VERSION_MAJOR (PIP_VERSION & 0xFF0000) >> 16
#define PIP_VERSION_MINOR (PIP_VERSION & 0xFF00) >> 8
#define PIP_VERSION_REVISION PIP_VERSION & 0xFF
#define PIP_VERSION_SUFFIX ""
#define PIP_VERSION_SUFFIX "_beta"
#if WIN32 || WIN64 || _WIN32 || _WIN64 || __WIN32__ || __WIN64__
# define WINDOWS
@@ -32,15 +32,33 @@
#if __QNX__ || __QNXNTO__
# define QNX
#endif
#if __FreeBSD__
# define FREE_BSD
#endif
#if __APPLE__ || __MACH__
# define MAC_OS
#endif
#ifndef WINDOWS
# ifndef QNX
# define LINUX
# ifndef FREE_BSD
# ifndef MAC_OS
# define LINUX
# endif
# endif
# endif
#endif
#if __GNUC__
# define CC_GCC
# define CC_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
# if CC_GCC_VERSION > 0x025F // > 2.95
# ifdef LINUX
# define HAS_LOCALE
# endif
# endif
#elif _MSC_VER
# define CC_VC
#else
# define CC_OTHER
#endif
#ifdef WINDOWS
@@ -70,6 +88,9 @@
#include <sys/types.h>
#include <errno.h>
#include <cctype>
#include <ctime>
#include <csignal>
//#include <signal.h>
#include <typeinfo>
#include <algorithm>
#include <string.h>
@@ -80,10 +101,46 @@
#include <deque>
#include <stack>
#include <set>
#include <map>
#ifdef WINDOWS
typedef int socklen_t;
# include <conio.h>
# include <io.h>
# include <winsock2.h>
# ifdef CC_VC
# define SHUT_RDWR 2
# pragma comment(lib, "Ws2_32.lib")
# pragma comment(lib, "Iphlpapi.lib")
# else
# define SHUT_RDWR SD_BOTH
# endif
# include <windows.h>
# include <wincon.h>
# include <iphlpapi.h>
#else
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/socket.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include <net/if.h>
#endif
#ifndef QNX
# ifndef WINDOWS
# ifndef FREE_BSD
# define environ __environ
# endif
# endif
#endif
#if !defined(WINDOWS) && !defined(MAC_OS)
# define PIP_TIMER_RT
#endif
#ifdef FREE_BSD
extern char ** environ;
#endif
#ifdef MAC_OS
typedef long time_t;
clock_serv_t __pi_mac_clock;
#endif
#include "pimonitor.h"
@@ -110,18 +167,65 @@ using std::queue;
using std::deque;
using std::stack;
using std::set;
using std::map;
using std::string;
#ifndef QNX
using std::wstring;
# ifndef WINDOWS
static locale_t currentLocale_t = 0;
# endif
//# ifndef WINDOWS
//static locale_t currentLocale_t = 0;
//# endif
#else
typedef std::basic_string<wchar_t> wstring;
#endif
#ifdef HAS_LOCALE
static locale_t currentLocale_t = 0;
#endif
template<typename T> inline void piSwap(T & f, T & s) {T t = f; f = s; s = t;}
template<typename T> inline int piRound(const T & v) {return int(v >= T(0.) ? v + T(0.5) : v - T(0.5));}
template<typename T> inline int piFloor(const T & v) {return v < T(0) ? int(v) - 1 : int(v);}
template<typename T> inline int piCeil(const T & v) {return v < T(0) ? int(v) : int(v) + 1;}
template<typename T> inline T piAbs(const T & v) {return (v >= T(0) ? v : -v);}
template<typename T> inline T piMin(const T & f, const T & s) {return (f > s) ? s : f;}
template<typename T> inline T piMin(const T & f, const T & s, const T & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);}
template<typename T> inline T piMax(const T & f, const T & s) {return (f < s) ? s : f;}
template<typename T> inline T piMax(const T & f, const T & s, const T & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);}
template<typename T> inline T piClamp(const T & v, const T & min, const T & max) {return (v > max ? max : (v < min ? min : v));}
#define piRoundf piRound<float>
#define piRoundd piRound<double>
#define piFloorf piFloor<float>
#define piFloord piFloor<double>
#define piCeilf piCeil<float>
#define piCeild piCeil<double>
#define piAbss piAbs<short>
#define piAbsi piAbs<int>
#define piAbsl piAbs<long>
#define piAbsll piAbs<llong>
#define piAbsf piAbs<float>
#define piAbsd piAbs<double>
#define piMins piMin<short>
#define piMini piMin<int>
#define piMinl piMin<long>
#define piMinll piMin<llong>
#define piMinf piMin<float>
#define piMind piMin<double>
#define piMaxs piMax<short>
#define piMaxi piMax<int>
#define piMaxl piMax<long>
#define piMaxll piMax<llong>
#define piMaxf piMax<float>
#define piMaxd piMax<double>
#define piClamps piClamp<short>
#define piClampi piClamp<int>
#define piClampl piClamp<long>
#define piClampll piClamp<llong>
#define piClampf piClamp<float>
#define piClampd piClamp<double>
extern bool isPIInit;
extern bool piDebug;
extern string ifconfigPath;
#define piCout if (piDebug) cout
@@ -130,8 +234,34 @@ public:
PIInit() {
if (isPIInit) return;
isPIInit = true;
#ifndef WINDOWS
sigset_t ss;
sigemptyset(&ss);
sigaddset(&ss, SIGALRM);
if (pthread_sigmask(SIG_BLOCK, &ss, 0) == -1) {
//cout << "[PITimer] sigaction error: " << errorString() << endl;
//return 0;
}
ifconfigPath = "/bin/ifconfig";
if (!fileExists(ifconfigPath)) {
ifconfigPath = "/sbin/ifconfig";
if (!fileExists(ifconfigPath)) {
ifconfigPath = "/usr/bin/ifconfig";
if (!fileExists(ifconfigPath)) {
ifconfigPath = "/usr/sbin/ifconfig";
if (!fileExists(ifconfigPath)) {
ifconfigPath = "";
}
}
}
}
#else
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
//piDebug = true;
#ifdef LINUX
#ifdef HAS_LOCALE
//cout << "has locale" << endl;
if (currentLocale_t != 0) {
freelocale(currentLocale_t);
currentLocale_t = 0;
@@ -139,21 +269,26 @@ public:
currentLocale_t = newlocale(LC_ALL, setlocale(LC_ALL, ""), 0);
#else
setlocale(LC_ALL, "");
#endif
#ifdef MAC_OS
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock);
#endif
}
~PIInit() {
#ifdef WINDOWS
WSACleanup();
#endif
#ifdef MAC_OS
mach_port_deallocate(mach_task_self(), __pi_mac_clock);
#endif
//if (currentLocale_t != 0) freelocale(currentLocale_t);
}
private:
bool fileExists(const string & p) {FILE * f = fopen(p.c_str(), "r"); if (f == 0) return false; fclose(f); return true;}
};
static PIInit piInit;
static lconv * currentLocale = std::localeconv();
#ifdef CC_VC
inline string errorString() {char buff[1024]; strerror_s(buff, 1024, GetLastError()); return string(buff);}
#else
inline string errorString() {return string(strerror(errno));}
#endif
extern PIInit piInit;
extern lconv * currentLocale;
#ifdef WINDOWS
inline int random() {return rand();}
@@ -162,12 +297,6 @@ inline double round(const double & v) {return floor(v + 0.5);}
# endif
#endif
template<typename Type> inline void piSwap(Type & f, Type & s) {Type t = f; f = s; s = t;}
template<typename Type> inline Type piMin(const Type & f, const Type & s) {return (f > s) ? s : f;}
template<typename Type> inline Type piMin(const Type & f, const Type & s, const Type & t) {return (f < s && f < t) ? f : ((s < t) ? s : t);}
template<typename Type> inline Type piMax(const Type & f, const Type & s) {return (f < s) ? s : f;}
template<typename Type> inline Type piMax(const Type & f, const Type & s, const Type & t) {return (f > s && f > t) ? f : ((s > t) ? s : t);}
template<typename Type> inline Type piClamp(const Type & v, const Type & min, const Type & max) {return (v > max ? max : (v < min ? min : v));}
inline ushort letobe_s(ushort v) {return v = (v << 8) | (v >> 8);}
inline bool atob(const string & str) { return str == "1" ? true : false;};
inline string btos(const bool num) { return num ? "0" : "1";};
@@ -220,6 +349,18 @@ inline string dtos(const double num) {
#endif
return string(ch); };
#ifdef CC_VC
inline string errorString() {
char * msg;
int err = GetLastError();
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
return "code " + itos(err) + " - " + string(msg);
}
#else
inline void errorClear() {errno = 0;}
inline string errorString() {int e = errno; return "code " + itos(e) + " - " + string(strerror(e));}
#endif
inline string PIPVersion() {return itos(PIP_VERSION_MAJOR) + "." + itos(PIP_VERSION_MINOR) + "." + itos(PIP_VERSION_REVISION) + PIP_VERSION_SUFFIX;}
#endif // PIINCLUDES_H