Common source code for samples.
#include <OnixS/FIXEngine/FIX/FIX44.h>
#include <cstdio>
#include <fstream>
#ifdef _WIN32
# include <conio.h>
# include <ctype.h>
#else
# include <pthread.h>
# include <csignal>
# include <cstdlib>
# include <vector>
#endif
#ifdef __linux__
#include <sys/poll.h>
#else
#include <conio.h>
#endif
#ifndef _WIN32
class SignalHelper
{
public:
typedef std::vector<int> SuppressedSignals;
static void manageLinuxSignals()
{
SuppressedSignals suppressedSignals;
suppressedSignals.push_back(SIGPIPE);
pthread_t signalThreadId;
int status;
sigemptyset(&signalSet());
for (SuppressedSignals::const_iterator i = suppressedSignals.begin(), e = suppressedSignals.end(); i != e; ++i)
sigaddset(&signalSet(), *i);
status = pthread_sigmask(SIG_BLOCK, &signalSet(), ONIXS_FIXENGINE_NULLPTR);
if (status != 0)
errorAbort(status, "Set signal mask");
status = pthread_create(&signalThreadId, ONIXS_FIXENGINE_NULLPTR, signalWaiter, ONIXS_FIXENGINE_NULLPTR);
if (status != 0)
errorAbort(status, "Create signalWaiter");
}
private:
static void errorAbort(int status, const char* reason)
{
printf("Aborted due to status %d: %s\n", status, reason ? reason : "no error message");
exit(1);
}
static void* signalWaiter(void*)
{
int signalNumber;
while (true)
{
sigwait(&signalSet(), &signalNumber);
printf("Signal %d received and suppressed.\n", signalNumber);
}
}
static sigset_t& signalSet()
{
static sigset_t set;
return set;
}
};
#endif
inline
void manageLinuxSignals()
{
#ifndef _WIN32
SignalHelper::manageLinuxSignals();
#endif
}
inline
void waitUntilEnterKey(const std::string& waitText = std::string())
{
if(!waitText.empty())
std::clog << std::endl << waitText << std::endl;
#ifdef _WIN32
_getch();
#else
getchar();
#endif
}
inline
bool keyPressed()
{
#ifdef __linux__
pollfd pfd = { 0, POLLIN, 0 };
return poll(&pfd, 1, 0) != 0 ? getchar(), true : false;
#else
return _kbhit() != 0 ? _getch(), true : false;
#endif
}
inline
{
order->
set(OnixS::FIX::FIX44::Tags::HandlInst, OnixS::FIX::FIX44::Values::HandlInst::AutoExecPriv)
.
set(OnixS::FIX::FIX44::Tags::ClOrdID,
"Unique identifier for Order")
.
set(OnixS::FIX::FIX44::Tags::Symbol,
"IBM")
.
set(OnixS::FIX::FIX44::Tags::Side, OnixS::FIX::FIX44::Values::Side::Buy)
.
set(OnixS::FIX::FIX44::Tags::OrderQty, 1000)
.
set(OnixS::FIX::FIX44::Tags::OrdType, OnixS::FIX::FIX44::Values::OrdType::Market)
}
inline
void processSampleException(const std::string & exceptionText)
{
std::cerr << "EXCEPTION: " << exceptionText << std::endl;
#ifdef _WIN32
if(exceptionText.find("System error code = 10013") != std::string::npos)
{
std::clog << "The socket is already in use, or access to the port is restricted on this OS. Please try to change the listen port in 'Settings.h' to another one." << std::endl;
std::clog << "You can view a list of which ports are excluded from your user by running this command: 'netsh interface ipv4 show excludedportrange protocol=tcp'" << std::endl;
}
#endif
}
inline
std::string readBinaryFile(const std::string & path)
{
std::ifstream in (path.c_str(), std::ios_base::binary);
assert(in);
return std::string((std::istreambuf_iterator< char > (in)), std::istreambuf_iterator< char >()) ;
}
inline
std::string readTextFile(const std::string & path, bool skipEol = false)
{
std::ifstream in (path.c_str());
assert(in);
std::string str((std::istreambuf_iterator< char > (in)), std::istreambuf_iterator< char >());
if(skipEol)
{
while(str.length() && (*str.rbegin() == '\r' || *str.rbegin() == '\n'))
str.erase(str.length() - 1);
}
return str;
}
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __linux__
#include <unistd.h>
# include <ctime>
#ifndef _POSIX_TIMERS
#error NEED_POSIX_TIMERS
#endif
#endif // #ifdef __linux__
#include <cmath>
#include <cstdio>
#include <stdexcept>
#include <iostream>
class PrecisionTimer
{
public:
PrecisionTimer();
void start();
unsigned long stop();
unsigned long elapsed();
long itemsPerSecond(size_t nItems);
private:
#ifdef _WIN32
typedef LARGE_INTEGER Timespec;
#endif
#ifdef __linux__
typedef timespec Timespec;
#endif
Timespec frequency_;
Timespec counter_;
};
inline
PrecisionTimer::PrecisionTimer()
{
#ifdef _WIN32
::QueryPerformanceFrequency(&frequency_);
#endif
#ifdef __linux__
if(0 != ::clock_getres(CLOCK_REALTIME, &frequency_))
throw std::domain_error("PrecisionTimer(),clock_getres() failed");
#endif
start();
}
inline
void PrecisionTimer::start()
{
#ifdef _WIN32
::QueryPerformanceCounter(&counter_);
#endif
#ifdef __linux__
if(0 != ::clock_gettime(CLOCK_REALTIME, &counter_))
throw std::domain_error("PrecisionTimer::start(),clock_gettime() failed");
#endif
}
inline
unsigned long PrecisionTimer::stop()
{
#ifdef _WIN32
LARGE_INTEGER current;
QueryPerformanceCounter(¤t);
current.QuadPart -= counter_.QuadPart;
counter_.QuadPart = current.QuadPart;
return elapsed();
#endif
#ifdef __linux__
Timespec current;
if(0 != ::clock_gettime(CLOCK_REALTIME, ¤t))
throw std::domain_error("PrecisionTimer::stop(),clock_gettime() failed");
current.tv_sec -= counter_.tv_sec;
current.tv_nsec -= counter_.tv_nsec;
counter_.tv_sec = current.tv_sec;
counter_.tv_nsec = current.tv_nsec;
return elapsed();
#endif
}
inline
unsigned long PrecisionTimer::elapsed()
{
#ifdef _WIN32
double elapsed = (double)(counter_.QuadPart) * 1000 / (double) frequency_.QuadPart;
return (unsigned long) elapsed;
#endif
#ifdef __linux__
return (counter_.tv_sec * 1000 + counter_.tv_nsec / 1000000);
#endif
}
inline
long PrecisionTimer::itemsPerSecond(size_t nItems)
{
double rv = (elapsed() > 0) ? std::floor(nItems * 1000.0 / elapsed()) : 0;
return static_cast<long>(rv);
}