2001-10-08 23:24:46 +04:00
|
|
|
#include "CServer.h"
|
2002-05-31 18:34:16 +04:00
|
|
|
#include "CConfig.h"
|
2002-05-31 18:25:26 +04:00
|
|
|
#include "CLog.h"
|
|
|
|
#include "CMutex.h"
|
2001-11-19 03:33:36 +03:00
|
|
|
#include "CNetwork.h"
|
2002-05-31 18:25:26 +04:00
|
|
|
#include "CThread.h"
|
2002-06-01 23:26:11 +04:00
|
|
|
#include "stdfstream.h"
|
|
|
|
#include <assert.h>
|
2002-05-31 21:32:26 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// config file stuff
|
|
|
|
//
|
|
|
|
|
|
|
|
static const char* s_configFileName = "synergy.conf";
|
|
|
|
|
2002-05-31 18:25:26 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// logging thread safety
|
|
|
|
//
|
|
|
|
|
|
|
|
static CMutex* s_logMutex = NULL;
|
|
|
|
|
|
|
|
static void logLock(bool lock)
|
|
|
|
{
|
|
|
|
assert(s_logMutex != NULL);
|
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
s_logMutex->lock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s_logMutex->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// main
|
|
|
|
//
|
2001-10-08 23:24:46 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
void realMain()
|
2001-10-08 23:24:46 +04:00
|
|
|
{
|
2002-05-31 18:25:26 +04:00
|
|
|
// initialize threading library
|
2001-10-14 22:29:43 +04:00
|
|
|
CThread::init();
|
2002-05-31 18:25:26 +04:00
|
|
|
|
|
|
|
// make logging thread safe
|
|
|
|
CMutex logMutex;
|
|
|
|
s_logMutex = &logMutex;
|
|
|
|
CLog::setLock(&logLock);
|
|
|
|
|
|
|
|
// initialize network library
|
2001-11-19 03:33:36 +03:00
|
|
|
CNetwork::init();
|
2001-10-08 23:24:46 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
CServer* server = NULL;
|
2001-10-08 23:24:46 +04:00
|
|
|
try {
|
2002-05-31 21:32:26 +04:00
|
|
|
CConfig config;
|
|
|
|
{
|
|
|
|
log((CLOG_DEBUG "opening configuration"));
|
2002-06-01 23:26:11 +04:00
|
|
|
std::ifstream configStream(s_configFileName);
|
2002-05-31 21:32:26 +04:00
|
|
|
if (!configStream) {
|
|
|
|
throw XConfigRead("cannot open configuration");
|
|
|
|
}
|
|
|
|
configStream >> config;
|
|
|
|
log((CLOG_DEBUG "configuration read successfully"));
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
server = new CServer();
|
2002-05-31 18:43:23 +04:00
|
|
|
server->setConfig(config);
|
2001-10-08 23:24:46 +04:00
|
|
|
server->run();
|
2001-11-19 03:33:36 +03:00
|
|
|
delete server;
|
|
|
|
CNetwork::cleanup();
|
2002-05-31 18:25:26 +04:00
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete server;
|
|
|
|
CNetwork::cleanup();
|
2002-05-31 18:25:26 +04:00
|
|
|
CLog::setLock(NULL);
|
|
|
|
s_logMutex = NULL;
|
2001-11-19 03:33:36 +03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-31 18:25:26 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// platform dependent entry points
|
|
|
|
//
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
|
|
|
|
#include "CMSWindowsScreen.h"
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
|
|
|
|
{
|
|
|
|
CMSWindowsScreen::init(instance);
|
|
|
|
|
|
|
|
if (__argc != 1) {
|
|
|
|
CString msg = "no arguments allowed. exiting.";
|
|
|
|
MessageBox(NULL, msg.c_str(), "error", MB_OK | MB_ICONERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
realMain();
|
|
|
|
return 0;
|
2001-10-08 23:24:46 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2002-05-31 21:32:26 +04:00
|
|
|
log((CLOG_CRIT "failed: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
CString msg = "failed: ";
|
|
|
|
msg += e.what();
|
|
|
|
MessageBox(NULL, msg.c_str(), "error", MB_OK | MB_ICONERROR);
|
2001-10-08 23:24:46 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2001-10-08 23:24:46 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 1) {
|
|
|
|
fprintf(stderr, "usage: %s\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
realMain();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2002-05-31 21:32:26 +04:00
|
|
|
log((CLOG_CRIT "failed: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
fprintf(stderr, "failed: %s\n", e.what());
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-08 23:24:46 +04:00
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
#endif
|