2001-10-08 23:24:46 +04:00
|
|
|
#include "CClient.h"
|
2001-11-19 03:33:36 +03:00
|
|
|
#include "CString.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"
|
2001-10-08 23:24:46 +04:00
|
|
|
#include "CNetworkAddress.h"
|
2001-10-14 22:29:43 +04:00
|
|
|
#include "CThread.h"
|
2002-06-02 22:49:35 +04:00
|
|
|
#include "XThread.h"
|
2002-06-01 23:26:11 +04:00
|
|
|
#include <assert.h>
|
2001-10-08 23:24:46 +04:00
|
|
|
|
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-11-19 03:33:36 +03:00
|
|
|
void realMain(const CString& name,
|
|
|
|
const CString& hostname,
|
2002-05-22 20:51:59 +04:00
|
|
|
UInt16 port)
|
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);
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
CClient* client = NULL;
|
|
|
|
try {
|
2002-06-02 22:49:35 +04:00
|
|
|
// initialize network library
|
|
|
|
CNetwork::init();
|
|
|
|
|
|
|
|
// run client
|
2001-11-19 03:33:36 +03:00
|
|
|
CNetworkAddress addr(hostname, port);
|
|
|
|
client = new CClient(name);
|
|
|
|
client->run(addr);
|
2002-06-02 22:49:35 +04:00
|
|
|
|
|
|
|
// clean up
|
2001-11-19 03:33:36 +03:00
|
|
|
delete client;
|
|
|
|
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 (...) {
|
2002-06-02 22:49:35 +04:00
|
|
|
// clean up
|
2001-11-19 03:33:36 +03:00
|
|
|
delete client;
|
|
|
|
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 != 2) {
|
|
|
|
CString msg = "hostname required. exiting.";
|
|
|
|
MessageBox(NULL, msg.c_str(), "error", MB_OK | MB_ICONERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-14 22:29:43 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
try {
|
2001-11-25 21:32:41 +03:00
|
|
|
realMain("secondary", __argv[1], 50001);
|
2001-11-19 03:33:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
CString msg = "failed: ";
|
|
|
|
msg += e.what();
|
|
|
|
MessageBox(NULL, msg.c_str(), "error", MB_OK | MB_ICONERROR);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-06-02 22:49:35 +04:00
|
|
|
catch (XThread&) {
|
|
|
|
// terminated
|
|
|
|
return 1;
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2001-10-08 23:24:46 +04:00
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "usage: %s <hostname>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2001-11-25 21:32:41 +03:00
|
|
|
realMain("secondary", argv[1], 50001);
|
2001-11-19 03:33:36 +03:00
|
|
|
return 0;
|
2001-10-08 23:24:46 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
fprintf(stderr, "failed: %s\n", e.what());
|
|
|
|
return 1;
|
|
|
|
}
|
2002-06-02 22:49:35 +04:00
|
|
|
catch (XThread&) {
|
|
|
|
// terminated
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-08 23:24:46 +04:00
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
#endif
|