| 1 |
#ifndef _PROTO_DEFS |
| 2 |
#define _PROTO_DEFS |
| 3 |
|
| 4 |
#ifdef WIN32 |
| 5 |
#include <time.h> // for mktime() |
| 6 |
#include <basetsd.h> // for UINT32/INT32 types |
| 7 |
#ifdef _WIN32_WCE |
| 8 |
#include <winbase.h> // for FILETIME,etc |
| 9 |
#endif // _WIN32_WCE |
| 10 |
#include <winsock2.h> // for struct timeval definition |
| 11 |
const char PROTO_PATH_DELIMITER = '\\'; |
| 12 |
|
| 13 |
// Note: Windows already defines INT32 and UINT32 types |
| 14 |
typedef signed char INT8; |
| 15 |
typedef unsigned char UINT8; |
| 16 |
typedef short INT16; |
| 17 |
typedef unsigned short UINT16; |
| 18 |
|
| 19 |
#else |
| 20 |
#include <sys/types.h> // must include this first to make SGI's compiler happy |
| 21 |
#include <sys/time.h> // for UNIX gettimeofday(), etc |
| 22 |
#ifdef FREEBSD4 |
| 23 |
#include <wchar.h> |
| 24 |
#else |
| 25 |
#include <stdint.h> // for proper uint32_t, etc definitions |
| 26 |
#endif // if/else freebsd4/other Unix |
| 27 |
#include <limits.h> // for PATH_MAX |
| 28 |
const char PROTO_PATH_DELIMITER = '/'; |
| 29 |
#endif // if/else WIN32/UNIX |
| 30 |
|
| 31 |
// Stuff for handling UNICODE |
| 32 |
#if defined(UNICODE) || defined(_UNICODE) |
| 33 |
#ifndef UNICODE |
| 34 |
#define UNICODE |
| 35 |
#endif |
| 36 |
#ifndef _UNICODE |
| 37 |
#define _UNICODE |
| 38 |
#endif |
| 39 |
#ifndef TCHAR |
| 40 |
#define TCHAR wchar_t |
| 41 |
#endif |
| 42 |
#else |
| 43 |
#ifndef TCHAR |
| 44 |
#define TCHAR char |
| 45 |
#endif |
| 46 |
#endif // if/else UNICODE || _UNICODE |
| 47 |
|
| 48 |
#ifdef SIMULATE |
| 49 |
#ifdef NS2 |
| 50 |
#include "scheduler.h" |
| 51 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 52 |
{ |
| 53 |
double now = Scheduler::instance().clock(); |
| 54 |
theTime.tv_sec = (unsigned long)(now); |
| 55 |
theTime.tv_usec = (unsigned long)((now - theTime.tv_sec) * 1.0e06); |
| 56 |
} |
| 57 |
#endif // NS2 |
| 58 |
|
| 59 |
#ifdef NS3 |
| 60 |
#include "ns3/simulator.h" |
| 61 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 62 |
{ |
| 63 |
double now = ns3::Simulator::Now().GetSeconds(); |
| 64 |
theTime.tv_sec = (unsigned long)(now); |
| 65 |
theTime.tv_usec = (unsigned long)(now - theTime.tv_sec)*1.0e06; |
| 66 |
} |
| 67 |
#endif // NS3 |
| 68 |
|
| 69 |
#ifdef OPNET |
| 70 |
#include "opnet.h" |
| 71 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 72 |
{ |
| 73 |
double current_time = op_sim_time(); |
| 74 |
theTime.tv_sec = (unsigned long)(current_time); |
| 75 |
theTime.tv_usec = (unsigned long)((current_time - theTime.tv_sec) * 1.0e06); |
| 76 |
} |
| 77 |
#endif // OPNET |
| 78 |
|
| 79 |
#else // !SIMULATE |
| 80 |
|
| 81 |
#if defined(WIN32) && ((__cplusplus >= 201103L) || (_MSC_VER >= 1900)) |
| 82 |
|
| 83 |
#include <chrono> |
| 84 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 85 |
{ |
| 86 |
std::chrono::microseconds epochTime = std::chrono::duration_cast<std::chrono::microseconds>( |
| 87 |
std::chrono::system_clock::now().time_since_epoch() |
| 88 |
); |
| 89 |
theTime.tv_sec = (long)(epochTime.count() / 1000000); |
| 90 |
theTime.tv_usec = (long)(epochTime.count() % 1000000); |
| 91 |
} |
| 92 |
|
| 93 |
#elif defined(WIN32) |
| 94 |
|
| 95 |
// NOTE: On _some_ WIN32 systems, it _may_ be beneficial to enable |
| 96 |
// the "USE_PERFORMANCE_COUNTER" macro. It is done for WinCE |
| 97 |
// builds here because it is required to get sub-second |
| 98 |
// time of day granularity! It is probably safer (for |
| 99 |
// the moment) to leave this undefined for most Win32 systems |
| 100 |
// because of various issues with the performance counter stuff, |
| 101 |
// but on certain systems may be useful to achieve high clock |
| 102 |
// granularity. Uncomment the following line if desired: |
| 103 |
#define USE_PERFORMANCE_COUNTER 1 |
| 104 |
|
| 105 |
#ifdef _WIN32_WCE |
| 106 |
#define USE_PERFORMANCE_COUNTER 1 |
| 107 |
#endif // _WIN32_WCE |
| 108 |
|
| 109 |
#ifdef USE_PERFORMANCE_COUNTER |
| 110 |
// These funcs/variables are defined/inited in "protoTime.cpp" |
| 111 |
inline LARGE_INTEGER ProtoGetPerformanceCounterFrequency() |
| 112 |
{ |
| 113 |
extern bool proto_performance_counter_init; |
| 114 |
extern LARGE_INTEGER proto_performance_counter_frequency; |
| 115 |
extern long proto_system_time_last_sec; |
| 116 |
extern unsigned long proto_system_count_roll_sec; |
| 117 |
extern LARGE_INTEGER proto_system_count_last; |
| 118 |
if (!proto_performance_counter_init) |
| 119 |
{ |
| 120 |
QueryPerformanceFrequency(&proto_performance_counter_frequency); |
| 121 |
// Determine perf counter offset from system time |
| 122 |
extern long proto_performance_counter_offset; |
| 123 |
if (0 != proto_performance_counter_frequency.QuadPart) |
| 124 |
{ |
| 125 |
LARGE_INTEGER count; |
| 126 |
FILETIME ft; |
| 127 |
#ifdef _WIN32_WCE |
| 128 |
SYSTEMTIME st; |
| 129 |
QueryPerformanceCounter(&count); |
| 130 |
GetSystemTime(&st); |
| 131 |
SystemTimeToFileTime(&st, &ft); |
| 132 |
#else |
| 133 |
QueryPerformanceCounter(&count); |
| 134 |
GetSystemTimeAsFileTime(&ft); |
| 135 |
#endif // if/else _WIN32_WCE |
| 136 |
ULARGE_INTEGER systemTime = {ft.dwLowDateTime, ft.dwHighDateTime}; |
| 137 |
// Convert system time to Jan 1, 1970 epoch |
| 138 |
const ULARGE_INTEGER epochTime = {0xD53E8000, 0x019DB1DE}; |
| 139 |
systemTime.QuadPart -= epochTime.QuadPart; |
| 140 |
// Convert sytem time to seconds |
| 141 |
systemTime.QuadPart /= 10000000; |
| 142 |
// Save time and counter to detect possible counter rollover |
| 143 |
proto_system_time_last_sec = systemTime.LowPart; |
| 144 |
proto_system_count_last = count; |
| 145 |
proto_system_count_roll_sec = 0xffffffff / proto_performance_counter_frequency.LowPart; |
| 146 |
// Convert counter value to seconds |
| 147 |
count.QuadPart /= proto_performance_counter_frequency.QuadPart; |
| 148 |
proto_performance_counter_offset = systemTime.LowPart - count.LowPart; |
| 149 |
} |
| 150 |
proto_performance_counter_init = true; |
| 151 |
} |
| 152 |
return proto_performance_counter_frequency; |
| 153 |
} // end ProtoGetPerformanceCounterFrequency() |
| 154 |
#endif // USE_PERFORMANCE_COUNTER |
| 155 |
|
| 156 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 157 |
{ |
| 158 |
#ifdef USE_PERFORMANCE_COUNTER |
| 159 |
// We use the perf counter on WinCE so we can get better than |
| 160 |
// the one second granularity offered by WinCE GetSystemTime() |
| 161 |
// (TBD) This breaks if the user changes the system time _while_ |
| 162 |
// a Protolib app is running, so this needs a better fix in |
| 163 |
// the long run. For example, we could update the |
| 164 |
// "proto_performance_counter_offset" once in a while or if |
| 165 |
// we detect a significant delta in the counter-based time |
| 166 |
// and the actual system time ... |
| 167 |
LARGE_INTEGER counterFreq = ProtoGetPerformanceCounterFrequency(); |
| 168 |
if (0 != counterFreq.QuadPart) |
| 169 |
{ |
| 170 |
extern long proto_performance_counter_offset; |
| 171 |
extern long proto_system_time_last_sec; |
| 172 |
extern unsigned long proto_system_count_roll_sec; |
| 173 |
extern LARGE_INTEGER proto_system_count_last; |
| 174 |
// 1) Get current system time and performance counter count |
| 175 |
LARGE_INTEGER count; |
| 176 |
FILETIME ft; |
| 177 |
#ifdef _WIN32_WCE |
| 178 |
SYSTEMTIME st; |
| 179 |
QueryPerformanceCounter(&count); |
| 180 |
GetSystemTime(&st); |
| 181 |
SystemTimeToFileTime(&st, &ft); |
| 182 |
#else |
| 183 |
QueryPerformanceCounter(&count); |
| 184 |
GetSystemTimeAsFileTime(&ft); |
| 185 |
#endif // if/else _WIN32_WCE |
| 186 |
ULARGE_INTEGER usec = {ft.dwLowDateTime, ft.dwHighDateTime}; |
| 187 |
const ULARGE_INTEGER epochTime = {0xD53E8000, 0x019DB1DE}; |
| 188 |
usec.QuadPart -= epochTime.QuadPart; |
| 189 |
ULARGE_INTEGER systemSecs; |
| 190 |
systemSecs.QuadPart = (usec.QuadPart / 10000000); |
| 191 |
|
| 192 |
// Count rollover correction, if applicable |
| 193 |
if (count.HighPart < proto_system_count_last.HighPart) |
| 194 |
count.HighPart = proto_system_count_last.HighPart; |
| 195 |
// Rollover detection |
| 196 |
long timeDeltaSec = systemSecs.LowPart - proto_system_time_last_sec; |
| 197 |
bool rollover = false; |
| 198 |
if (count.QuadPart < proto_system_count_last.QuadPart) |
| 199 |
{ |
| 200 |
rollover = true; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
LARGE_INTEGER countDelta; |
| 205 |
countDelta.QuadPart = count.QuadPart - proto_system_count_last.QuadPart; |
| 206 |
countDelta.QuadPart /= counterFreq.QuadPart; // delta in seconds |
| 207 |
long countDeltaSecs = countDelta.LowPart; |
| 208 |
if ((timeDeltaSec - countDeltaSecs) > (long)(proto_system_count_roll_sec >> 1)) |
| 209 |
rollover = true; |
| 210 |
} |
| 211 |
if (rollover) |
| 212 |
{ |
| 213 |
long rolloverCount = (timeDeltaSec / proto_system_count_roll_sec); |
| 214 |
if (0 != rolloverCount) |
| 215 |
count.HighPart += rolloverCount; |
| 216 |
else |
| 217 |
count.HighPart += 1; |
| 218 |
} |
| 219 |
proto_system_count_last = count; |
| 220 |
proto_system_time_last_sec = systemSecs.LowPart; |
| 221 |
|
| 222 |
// Compute whole seconds using (possibly corrected) counter value |
| 223 |
LARGE_INTEGER counterSec; |
| 224 |
counterSec.QuadPart = (count.QuadPart / counterFreq.QuadPart); |
| 225 |
theTime.tv_sec = counterSec.LowPart; |
| 226 |
// Adjust whole seconds by stored offset |
| 227 |
theTime.tv_sec += proto_performance_counter_offset; |
| 228 |
// Double-check our offset in case the user has changed system time |
| 229 |
// (Note counter drift may make this change, but only over _hopefully_ |
| 230 |
// a long interval time as "backwards" times might result |
| 231 |
long systemDelta = systemSecs.LowPart - theTime.tv_sec; |
| 232 |
if (labs(systemDelta) > 2) |
| 233 |
{ |
| 234 |
theTime.tv_sec += systemDelta; |
| 235 |
proto_performance_counter_offset += systemDelta; |
| 236 |
} |
| 237 |
// Compute whole second count |
| 238 |
counterSec.QuadPart = counterSec.QuadPart * counterFreq.QuadPart; |
| 239 |
// Subtract to get fractional second count |
| 240 |
count.QuadPart = count.QuadPart - counterSec.QuadPart; |
| 241 |
// Scale to usec count |
| 242 |
count.QuadPart = 1000000 * count.QuadPart; |
| 243 |
// Compute usec |
| 244 |
LARGE_INTEGER counterUsec; |
| 245 |
counterUsec.QuadPart = count.QuadPart / counterFreq.QuadPart; |
| 246 |
theTime.tv_usec = counterUsec.LowPart; |
| 247 |
} |
| 248 |
else |
| 249 |
#endif // USE_PERFORMANCE_COUNTER |
| 250 |
{ |
| 251 |
FILETIME ft; |
| 252 |
#ifdef _WIN32_WCE |
| 253 |
SYSTEMTIME st; |
| 254 |
GetSystemTime(&st); |
| 255 |
SystemTimeToFileTime(&st, &ft); |
| 256 |
#else |
| 257 |
GetSystemTimeAsFileTime(&ft); |
| 258 |
#endif // if/else _WIN32_WCE |
| 259 |
ULARGE_INTEGER usecTotal = {ft.dwLowDateTime, ft.dwHighDateTime}; |
| 260 |
//epochTime = time for 00:00:0.00 1 Jan, 1970 |
| 261 |
const ULARGE_INTEGER epochTime = {0xD53E8000, 0x019DB1DE}; |
| 262 |
usecTotal.QuadPart -= epochTime.QuadPart; |
| 263 |
usecTotal.QuadPart /= 10; // convert to microseconds |
| 264 |
theTime.tv_sec = (long)(usecTotal.QuadPart / 1000000); |
| 265 |
theTime.tv_usec = (long)(usecTotal.QuadPart - theTime.tv_sec * 1000000); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#else |
| 270 |
|
| 271 |
inline void ProtoSystemTime(struct timeval& theTime) |
| 272 |
{ |
| 273 |
struct timezone tz; |
| 274 |
gettimeofday(&theTime, &tz); |
| 275 |
} |
| 276 |
|
| 277 |
#endif // if/else WIN32/UNIX |
| 278 |
|
| 279 |
#endif // if/else SIMULATE |
| 280 |
|
| 281 |
#ifndef PATH_MAX |
| 282 |
#ifdef WIN32 |
| 283 |
#define PATH_MAX MAX_PATH |
| 284 |
#else |
| 285 |
#define PATH_MAX _POSIX_PATH_MAX |
| 286 |
#endif // if/else WIN32/UNIX |
| 287 |
#endif // !PATH_MAX |
| 288 |
|
| 289 |
#ifndef NULL |
| 290 |
#define NULL 0 |
| 291 |
#endif // !NULL |
| 292 |
|
| 293 |
#ifndef WIN32 |
| 294 |
// Note: These are all already defined in WIN32 <basetsd.h> |
| 295 |
typedef int8_t INT8; |
| 296 |
typedef int16_t INT16; |
| 297 |
#ifdef _USING_X11 |
| 298 |
typedef long int INT32; |
| 299 |
#else |
| 300 |
typedef int32_t INT32; |
| 301 |
#endif // if/else _USING_X11 |
| 302 |
typedef uint8_t UINT8; |
| 303 |
typedef uint16_t UINT16; |
| 304 |
typedef uint32_t UINT32; |
| 305 |
#endif // !WIN32 |
| 306 |
|
| 307 |
#ifndef MAX |
| 308 |
#define MAX(X,Y) ((X>Y)?(X):(Y)) |
| 309 |
#endif //!MAX |
| 310 |
#ifndef MIN |
| 311 |
#define MIN(X,Y) ((X<Y)?(X):(Y)) |
| 312 |
#endif //!MIN |
| 313 |
|
| 314 |
//#define USE_PROTO_CHECK |
| 315 |
#include "protoCheck.h" |
| 316 |
|
| 317 |
#endif // _PROTO_DEFS |