Exemplo n.º 1
0
void ServerRestart(HWND hWnd, HWND hCaller, UINT uMsg)
{
	if(!gServer.hProcess)
		ServerStart(hWnd, hCaller, uMsg);
	else
	{
		ServerStop(hWnd, hCaller, uMsg);
		ServerStart(hWnd, hCaller, uMsg);
	}
}
Exemplo n.º 2
0
int main(int argc, char ** argv){
    if (argc < 2){
        printf("Argc number must more than 3");
    }
    if (strcmp("client", argv[1]) == 0){
        ClientStart();
    }else if (strcmp("server", argv[1]) == 0){
        ServerStart();
    }else{
        HashMap* hmap = new HashMap();
        char* key = "a";
        char* value = "This is a";
        hmap->Put(key, value, 1);
        char* answer = (char*)hmap->Get(key, 1, 1);
        printf("%s\n", answer);
    }
    //printf(strlen(NULL));
//    KVServer* kvServer = new KVServer();
//    kvServer->Start();
//    socketTest();
//    printf("Hello WOrld");
//    KV* kvClient =new KV();
//    testPut(kvClient, "key1", "value1");
//    testPut(kvClient, "This is a Key2", "I am Value2");
//    testGet(kvClient, "key1");
//    testGet(kvClient, "This is a Key2");
}
Exemplo n.º 3
0
/**
Thread entry point for test thread.
Kills the existing ECOM server and launches a new instance

*/
TInt ServerStartThreadEntryL(TAny* /*a*/)
	{
	__UHEAP_MARK;

	//Threshold set so that a OOM failure occur whilst constructing CEComServer::iRegistryData
	const TInt KFailThreshold = 500;

	TInt dummy;
	TAny* ptr  = NULL;

	::KillEComServerL();

	//Invoking OOM failure by allocating most of the heap before running the server
	RHeap heap = User::Heap();
	TInt avail = heap.Available(dummy);
    ptr = User::Alloc(avail-KFailThreshold);

	TEST(ptr != NULL); //Check that the allocation was sucessful

	TInt result = ServerStart();
	delete ptr;

	__UHEAP_MARKEND;

	return result;
	}
Exemplo n.º 4
0
//  Oбрабатывает сообщения в главном окне.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Разобрать выбор в меню:
		switch (wmId)
		{
		case IDM_START:
			ServerStart(hWnd);
			break;
		case IDM_STOP:
			ServerStop(hWnd);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_OPTIONS:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_OPTIONS), hWnd, Options);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: добавьте любой код отрисовки...
		EndPaint(hWnd, &ps);
		break;
	case WM_CREATE:
		OnCreate();
		break;
	case WM_SIZE:
		MoveWindow(hLst,0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
		break;
	case WSA_ACCEPT:
		OnWSAAccept(hWnd,message,wParam,lParam);
		break;
	case WSA_NETEVENT:
		OnWSANetEvent(hWnd,message,wParam,lParam);
		break;
	case WM_DESTROY:
		WSACleanup();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
Exemplo n.º 5
0
int main(int argc, char *argv[]) {
	ServerConfig config;

	if (!load_args(argc, argv, config))
		return -1;

	ServerStart(config);

	while (1) {
		ServerUpdate();
	}
}
Exemplo n.º 6
0
int main(int argc, const char * argv[])
{
	// 初始化服务器所都要准备好的数据 
	Init();

	// 启动服务 
	ServerStart();

	// 执行退出的相关操作
	RunServerExit();

	return 0;

}
Exemplo n.º 7
0
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    //Window Settings
    this->setWindowTitle(tr("Test Chat Server"));
    this->resize(600,250);

    //Layout
    LayMain = new QVBoxLayout(this);
    EdtDisplay = new QTextEdit();
    EdtDisplay->setReadOnly(true);
    EdtDisplay->setFontFamily("courier");
    EdtDisplay->setFontPointSize(10);
    LayMain->addWidget(EdtDisplay);
    LayConfig = new QHBoxLayout();
    LayMain->addLayout(LayConfig);
    LblPort = new QLabel(tr("Port:"));
    LayConfig->addWidget(LblPort);
    BoxPort = new QSpinBox();
    BoxPort->setMinimum(1024);
    BoxPort->setMaximum(49151);
    BoxPort->setValue(2342);
    LayConfig->addWidget(BoxPort);
    LayConfig->addStretch();
    LayButtons = new QHBoxLayout();
    LayMain->addLayout(LayButtons);
    BtnStart = new QPushButton(tr("Server Starten"));
    BtnStop = new QPushButton(tr("Server Stoppen"));
    BtnQuit = new QPushButton(tr("Beenden"));
    LayButtons->addWidget(BtnStart);
    LayButtons->addWidget(BtnStop);
    LayButtons->addStretch();
    LayButtons->addWidget(BtnQuit);

    connect(BtnQuit, SIGNAL(clicked()), this, SLOT(close()));
    connect(BtnStart, SIGNAL(clicked()), this, SLOT(ServerStart()));
    connect(BtnStop, SIGNAL(clicked()), this, SLOT(ServerStop()));

    Server = new ChatServer();
    connect(Server, SIGNAL(newLog(QString)), this, SLOT(handleLog(QString)));

    handleLog(tr("Programm gestartet, Server läuft nicht."));
    handleLog(tr("Klicke auf Start um den Server zu starten."));
}
Exemplo n.º 8
0
int __cdecl main(int argc, char* argv[]) {
    ::SetDllDirectory("");

#ifndef _WIN64
    HINSTANCE hKernel32 = ::LoadLibrary("Kernel32.dll");

    typedef BOOL (WINAPI * SPDEPP)(DWORD);
    SPDEPP pSPDEPP = (SPDEPP)::GetProcAddress(hKernel32, "SetProcessDEPPolicy");

    if(pSPDEPP != NULL) {
        pSPDEPP(PROCESS_DEP_ENABLE);
    }

    ::FreeLibrary(hKernel32);
#endif

	sTitle = "PtokaX DC Hub " + string(PtokaXVersionString);
#ifdef _DEBUG
	sTitle += " [debug]";
#endif

#ifdef _DEBUG
//    AllocConsole();
//    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//    Cout("PtokaX Debug console\n");
#endif
	
	char sBuf[MAX_PATH+1];
	::GetModuleFileName(NULL, sBuf, MAX_PATH);
	char * sPath = strrchr(sBuf, '\\');
	if(sPath != NULL) {
		PATH = string(sBuf, sPath-sBuf);
	} else {
		PATH = sBuf;
	}

	char * sServiceName = NULL;
	
	bool bInstallService = false;
	
	for(int i = 0; i < argc; i++) {
	    if(stricmp(argv[i], "-s") == NULL || stricmp(argv[i], "/service") == NULL) {
	    	if(++i == argc) {
	            AppendLog("Missing service name!");
	            return EXIT_FAILURE;
	    	}
	    	sServiceName = argv[i];
	    	bService = true;
	    } else if(stricmp(argv[i], "-c") == 0) {
	        if(++i == argc) {
	            printf("Missing config directory!");
	            return EXIT_FAILURE;
	        }
	
	        size_t szLen = strlen(argv[i]);
	        if(szLen >= 1 && argv[i][0] != '\\' && argv[i][0] != '/') {
	            if(szLen < 4 || (argv[i][1] != ':' || (argv[i][2] != '\\' && argv[i][2] != '/'))) {
	                printf("Config directory must be absolute path!");
	                return EXIT_FAILURE;
	            }
	    	}
	
	    	if(argv[i][szLen - 1] == '/' || argv[i][szLen - 1] == '\\') {
	            PATH = string(argv[i], szLen - 1);
	    	} else {
	            PATH = string(argv[i], szLen);
	        }
	    
	        if(DirExist(PATH.c_str()) == false) {
	            if(CreateDirectory(PATH.c_str(), NULL) == 0) {
	                printf("Config directory not exist and can't be created!");
	                return EXIT_FAILURE;
	            }
	        }
	    } else if(stricmp(argv[i], "-i") == NULL || stricmp(argv[i], "/install") == NULL) {
	    	if(++i == argc) {
	            printf("Please specify service name!");
	    		return EXIT_FAILURE;
	    	}
	    	sServiceName = argv[i];
	    	bInstallService = true;
	    } else if(stricmp(argv[i], "-u") == NULL || stricmp(argv[i], "/uninstall") == NULL) {
	    	if(++i == argc) {
	            printf("Please specify service name!");
	    		return EXIT_FAILURE;
	    	}
	    	sServiceName = argv[i];
	    	return UninstallService(sServiceName);
	    } else if(stricmp(argv[i], "-v") == NULL || stricmp(argv[i], "/version") == NULL) {
	    	printf((sTitle+" built on "+__DATE__+" "+__TIME__).c_str());
	    	return EXIT_SUCCESS;
	    } else if(stricmp(argv[i], "-h") == NULL || stricmp(argv[i], "/help") == NULL) {
	    	printf("PtokaX [-c <configdir>] [-i <servicename>] [-u <servicename>] [-v]");
	    	return EXIT_SUCCESS;
	    } else if(stricmp(argv[i], "/nokeycheck") == NULL) {
	        bCmdNoKeyCheck = true;
	    } else if(stricmp(argv[i], "/generatexmllanguage") == NULL) {
	        LangMan::GenerateXmlExample();
	        return EXIT_SUCCESS;
	    }
	}

	if(bInstallService == true) {
	    if(sPath == NULL && strcmp(PATH.c_str(), sBuf) == 0) {
	        return InstallService(sServiceName, NULL);
		} else {
			return InstallService(sServiceName, PATH.c_str());
		}
	}

    ExceptionHandlingInitialize(PATH, sBuf);

	if(bService == false) {
	    ServerInitialize();
	
	    if(ServerStart() == false) {
	        printf("Server start failed!");

            ExceptionHandlingUnitialize();

	        return EXIT_FAILURE;
	    } else {
	        printf((sTitle+" running...\n").c_str());
	    }

	    MSG msg;
	    BOOL bRet;

	    while((bRet = ::GetMessage(&msg, NULL, 0, 0)) != 0) {
	        if(bRet == -1) {
	            // handle the error and possibly exit
	        } else {
	            if(msg.message == WM_USER+1) {
	                break;
	            } else if(msg.message == WM_TIMER) {
                    if(msg.wParam == srvLoopTimer) {
                        srvLoop->Looper();
                    } else if(msg.wParam == regtimer) {
                        ServerOnRegTimer();
                    } else {
                        //Must be script timer
                        ScriptOnTimer(msg.wParam);
                    }
                }
	
	    		::TranslateMessage(&msg);
	            ::DispatchMessage(&msg);
	        }
	    }

        ExceptionHandlingUnitialize();
	} else {
	    SERVICE_TABLE_ENTRY DispatchTable[] = {
	        { sServiceName, StartService },
	        { NULL, NULL }
	    };
	       
	    if(StartServiceCtrlDispatcher(DispatchTable) == false) {
			AppendLog("StartServiceCtrlDispatcher failed ("+string((uint32_t)GetLastError())+")!");

            ExceptionHandlingUnitialize();

	        return EXIT_FAILURE;
	    }
	}

    return EXIT_SUCCESS;
}
Exemplo n.º 9
0
static void WINAPI StartService(DWORD /*argc*/, char* argv[]) {
	ssh = RegisterServiceCtrlHandler(argv[0], CtrlHandler);
	
	if(ssh == NULL) {
		AppendLog("RegisterServiceCtrlHandler failed ("+string((uint32_t)GetLastError())+")!");
	    return;
	}
	
	ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	ss.dwCurrentState = SERVICE_START_PENDING;
	ss.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
	ss.dwWin32ExitCode = NO_ERROR;
	ss.dwCheckPoint = 0;
	ss.dwWaitHint = 10 * 1000;
	
	if(SetServiceStatus(ssh, &ss) == false) {
		AppendLog("StartService::SetServiceStatus failed ("+string((uint32_t)GetLastError())+")!");
		return;
	}
	
	ServerInitialize();
	
	if(ServerStart() == false) {
	    AppendLog("Server start failed!");
		ss.dwCurrentState = SERVICE_STOPPED;
		SetServiceStatus(ssh, &ss);
		return;
	}
	
	ss.dwCurrentState = SERVICE_RUNNING;
	
	if(SetServiceStatus(ssh, &ss) == false) {
		AppendLog("StartService::SetServiceStatus1 failed ("+string((uint32_t)GetLastError())+")!");
		return;
	}

	MSG msg;
	BOOL bRet;
	        
	while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
	    if(bRet == -1) {
	        // handle the error and possibly exit
	    } else {
	        if(msg.message == WM_USER+1) {
	            break;
	        } else if(msg.message == WM_TIMER) {
                if(msg.wParam == sectimer) {
                    ServerOnSecTimer();
                } else if(msg.wParam == srvLoopTimer) {
                    srvLoop->Looper();
                } else if(msg.wParam == regtimer) {
                    ServerOnRegTimer();
                } else {
                    //Must be script timer
                    ScriptOnTimer(msg.wParam);
                }
            }

	    	TranslateMessage(&msg);
	        DispatchMessage(&msg);
	    }
	}

    ExceptionHandlingUnitialize();

	ss.dwCurrentState = SERVICE_STOPPED;
	SetServiceStatus(ssh, &ss);
}
Exemplo n.º 10
0
int main(int argc, char* argv[]) {
	sTitle = "PtokaX DC Hub " + string(PtokaXVersionString);
	
#ifdef _DEBUG
	sTitle += " [debug]";
#endif
	
	for(int i = 0; i < argc; i++) {
	    if(strcasecmp(argv[i], "-d") == 0) {
	    	bDaemon = true;
	    } else if(strcasecmp(argv[i], "-c") == 0) {
	    	if(++i == argc) {
	            printf("Missing config directory!\n");
	            return EXIT_FAILURE;
	    	}
	
			if(argv[i][0] != '/') {
	            printf("Config directory must be absolute path!\n");
	            return EXIT_FAILURE;
			}
	
	        size_t szLen = strlen(argv[i]);
			if(argv[i][szLen - 1] == '/') {
	            PATH = string(argv[i], szLen - 1);
			} else {
	            PATH = string(argv[i], szLen);
	        }
	
	        if(DirExist(PATH.c_str()) == false) {
	        	if(mkdir(PATH.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP) == -1) {
	                if(bDaemon == true) {
	                    syslog(LOG_USER | LOG_ERR, "Config directory not exist and can't be created!\n");
	                } else {
	                    printf("Config directory not exist and can't be created!");
	                }
	            }
            }
	    } else if(strcasecmp(argv[i], "-v") == 0) {
	        printf("%s built on %s %s\n", sTitle.c_str(), __DATE__, __TIME__);
	        return EXIT_SUCCESS;
	    } else if(strcasecmp(argv[i], "-h") == 0) {
	        printf("PtokaX [-d] [-c <configdir>] [-v]\n");
	        return EXIT_SUCCESS;
	    } else if(strcasecmp(argv[i], "/generatexmllanguage") == 0) {
	        LangMan::GenerateXmlExample();
	        return EXIT_SUCCESS;
	    }
	}
	
	if(PATH.size() == 0) {
	    char* home;
	    char curdir[PATH_MAX];
	    if(bDaemon == true && (home = getenv("HOME")) != NULL) {
	        PATH = string(home) + "/.PtokaX";
	            
	        if(DirExist(PATH.c_str()) == false) {
	            if(mkdir(PATH.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP) == -1) {
	                syslog(LOG_USER | LOG_ERR, "Config directory not exist and can't be created!\n");
	            }
	        }
	    } else if(getcwd(curdir, PATH_MAX) != NULL) {
	        PATH = curdir;
	    } else {
	        PATH = ".";
	    }
	}
	
	if(bDaemon == true) {
	    printf("Starting %s as daemon using %s as config directory.\n", sTitle.c_str(), PATH.c_str());
	
	    pid_t pid1 = fork();
	    if(pid1 == -1) {
	        syslog(LOG_USER | LOG_ERR, "First fork failed!\n");
	        return EXIT_FAILURE;
	    } else if(pid1 > 0) {
	        return EXIT_SUCCESS;
	    }
	
	    if(setsid() == -1) {
	        syslog(LOG_USER | LOG_ERR, "Setsid failed!\n");
	        return EXIT_FAILURE;
	    }
	
	    pid_t pid2 = fork();
	    if(pid2 == -1) {
	        syslog(LOG_USER | LOG_ERR, "Second fork failed!\n");
	        return EXIT_FAILURE;
	    } else if(pid2 > 0) {
            return EXIT_SUCCESS;
	    }
	
	    chdir("/");
	
	    close(STDIN_FILENO);
	    close(STDOUT_FILENO);
	    close(STDERR_FILENO);
	
	    umask(117);
	
	    if(open("/dev/null", O_RDWR) == -1) {
	        syslog(LOG_USER | LOG_ERR, "Failed to open /dev/null!\n");
	        return EXIT_FAILURE;
	    }
	
	    dup(0);
	    dup(0);
	}
	
	sigset_t sst;
	sigemptyset(&sst);
	sigaddset(&sst, SIGPIPE);
	sigaddset(&sst, SIGURG);
	sigaddset(&sst, SIGALRM);
	sigaddset(&sst, SIGSCRTMR);
	sigaddset(&sst, SIGREGTMR);
	
	if(bDaemon == true) {
	    sigaddset(&sst, SIGHUP);
	}
	
	pthread_sigmask(SIG_BLOCK, &sst, NULL);
	
	struct sigaction sigact;
	sigact.sa_handler = SigHandler;
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	
	if(sigaction(SIGINT, &sigact, NULL) == -1) {
	    AppendDebugLog("%s - [ERR] Cannot create sigaction SIGINT in main\n", 0);
	    exit(EXIT_FAILURE);
	}
	
	if(sigaction(SIGTERM, &sigact, NULL) == -1) {
	    AppendDebugLog("%s - [ERR] Cannot create sigaction SIGTERM in main\n", 0);
	    exit(EXIT_FAILURE);
	}
	
	if(sigaction(SIGQUIT, &sigact, NULL) == -1) {
	    AppendDebugLog("%s - [ERR] Cannot create sigaction SIGQUIT in main\n", 0);
	    exit(EXIT_FAILURE);
	}
	
	if(bDaemon == false && sigaction(SIGHUP, &sigact, NULL) == -1) {
	    AppendDebugLog("%s - [ERR] Cannot create sigaction SIGHUP in main\n", 0);
	    exit(EXIT_FAILURE);
	}

	ServerInitialize();

	if(ServerStart() == false) {
	    if(bDaemon == false) {
	        printf("Server start failed!\n");
	    } else {
	        syslog(LOG_USER | LOG_ERR, "Server start failed!\n");
	    }
	    return EXIT_FAILURE;
	} else if(bDaemon == false) {
	    printf("%s running...\n", sTitle.c_str());
	}

    struct timespec sleeptime;
    sleeptime.tv_sec = 0;
    sleeptime.tv_nsec = 100000000;

	while(true) {
	    srvLoop->Looper();
	
	    if(bServerTerminated == true) {
	        break;
	    }
	
        nanosleep(&sleeptime, NULL);
	}

	if(bDaemon == false) {
	    printf("%s ending...\n", sTitle.c_str());
	}

    return EXIT_SUCCESS;
}