Example #1
0
LRESULT CALLBACK CServer::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	CServer *pServer=(CServer *)GetWindowLongPtr(hWnd, GWLP_USERDATA);

	if (message == WM_CLOSE) {
		pServer->OnClose();
		return 0;
	}
	else if (hWnd && message == WM_DESTROY) {
		ASSERT (hWnd == pServer->m_hWnd);
		HANDLE *handle = new HANDLE[pServer->m_ThreadArray.size()];
		unsigned int i = 0;
		for (auto iter = pServer->m_ThreadArray.begin(); iter != pServer->m_ThreadArray.end(); ++iter, ++i) {
			handle[i] = (*iter)->m_hThread;
			(*iter)->PostThreadMessage(WM_QUIT, 0, 0);
		}
		for (i = 0; i < pServer->m_ThreadArray.size(); ++i) {
			int res = WaitForSingleObject(handle[i], INFINITE);
			if (res == WAIT_FAILED)
				res = GetLastError();
		}
		delete [] handle;
		handle = new HANDLE[pServer->m_ClosedThreads.size()];
		i = 0;
		for (auto iter = pServer->m_ClosedThreads.begin(); iter != pServer->m_ClosedThreads.end(); ++iter, ++i) {
			handle[i] = (*iter)->m_hThread;
			(*iter)->PostThreadMessage(WM_QUIT, 0, 0);
		}
		for (i = 0; i < pServer->m_ClosedThreads.size(); ++i) {
			int res = WaitForSingleObject(handle[i], INFINITE);
			if (res == WAIT_FAILED)
				res = GetLastError();
		}
		delete [] handle;
		pServer->m_AdminListenSocketList.clear();
		delete pServer->m_pAdminInterface;
		pServer->m_pAdminInterface = NULL;
		delete pServer->m_pOptions;
		pServer->m_pOptions = NULL;
		if (pServer->m_nTimerID) {
			KillTimer(pServer->m_hWnd, pServer->m_nTimerID);
			pServer->m_nTimerID = 0;
		}
		PostQuitMessage(0);
		return 0;
	}
	else if (message == WM_TIMER)
		pServer->OnTimer(wParam);
	else if (message == WM_FILEZILLA_RELOADCONFIG) {
		COptions options;
		options.ReloadConfig();
		CPermissions perm = CPermissions(std::function<void()>());
		perm.ReloadConfig();
	}
	else if (message >= WM_FILEZILLA_SERVERMSG) {
		UINT index = message - WM_FILEZILLA_SERVERMSG;
		if (index >= pServer->m_ThreadNotificationIDs.size())
			return 0;

		CServerThread *pThread = pServer->m_ThreadNotificationIDs[index];
		if (pThread) {
			std::list<CServerThread::t_Notification> notifications;
			pThread->GetNotifications(notifications);
			for (std::list<CServerThread::t_Notification>::const_iterator iter = notifications.begin(); iter != notifications.end(); iter++)
				if (pServer->OnServerMessage(pThread, iter->wParam, iter->lParam) != 0)
					break;
		}
		return 0;
	}

	return ::DefWindowProc(hWnd, message, wParam, lParam);
}
Example #2
0
LRESULT CServer::OnServerMessage(WPARAM wParam, LPARAM lParam)
{
	if (wParam==FSM_STATUSMESSAGE)
	{
		t_statusmsg *msg=reinterpret_cast<t_statusmsg *>(lParam);
		CStdString str;

		FILETIME fFileTime;
		SystemTimeToFileTime(&msg->time, &fFileTime);
		_int64 time = ((_int64)fFileTime.dwHighDateTime<<32) + fFileTime.dwLowDateTime;
		
		str.Format("(%06d)- %s (%s)> %s", msg->userid, (LPCTSTR)msg->user, (LPCTSTR)msg->ip, (LPCTSTR)msg->status);
		ShowStatus(time, str, msg->type);
		delete [] msg->user;
		delete [] msg->status;
		delete msg;
	}
	else if (wParam==FSM_CONNECTIONDATA)
	{
		t_connop *pConnOp = reinterpret_cast<t_connop*>(lParam);
		if (pConnOp->op != USERCONTROL_CONNOP_REMOVE)
			m_UsersList[pConnOp->data->userid] = *pConnOp->data;
		else
		{
			std::map<int, t_connectiondata>::iterator iter=m_UsersList.find(pConnOp->data->userid);
			if (iter!=m_UsersList.end())
				m_UsersList.erase(iter);
		}
		USES_CONVERSION;
		int userlen = pConnOp->data->user ? strlen(pConnOp->data->user) : 0;
		int len = 2 + 4 + strlen(pConnOp->data->ip)+2 + 4 + userlen+2;
		unsigned char *buffer = new unsigned char[len];
		buffer[0] = USERCONTROL_CONNOP;
		buffer[1] = pConnOp->op;
		memcpy(buffer+2, &pConnOp->data->userid, 4);
		buffer[2 + 4] = strlen(pConnOp->data->ip)/256;
		buffer[2 + 4 + 1] = strlen(pConnOp->data->ip)%256;
		memcpy(buffer + 2 + 4 + 2, T2CA(pConnOp->data->ip), strlen(T2CA(pConnOp->data->ip)));
		memcpy(buffer + 2 + 4 +2 + strlen(pConnOp->data->ip)+2, &pConnOp->data->port, 4);
		buffer[2 + 4 + strlen(pConnOp->data->ip)+2 + 4]= userlen/256;
		buffer[2 + 4 + strlen(pConnOp->data->ip)+2 + 4 + 1]= userlen%256;
		if (pConnOp->data->user)
			memcpy(buffer + 2 + 4 + strlen(pConnOp->data->ip)+2 + 4 + 2, T2CA(pConnOp->data->user), userlen);
		m_pAdminInterface->SendCommand(2, 3, buffer, len);
		delete [] buffer;
		delete pConnOp->data;
		delete pConnOp;
	}
	else if (wParam==FSM_THREADCANQUIT)
	{
		CServerThread *pThread=(CServerThread *)lParam;
		for (std::list<CServerThread *>::iterator iter=m_ThreadArray.begin(); iter!=m_ThreadArray.end(); iter++)
		{
			if (*iter==pThread)
			{
				HANDLE handle=pThread->m_hThread;
				pThread->PostThreadMessage(WM_QUIT, 0, 0);
				int res=WaitForSingleObject(handle, INFINITE);
				if (res==WAIT_FAILED)
					res=GetLastError();
				m_ThreadArray.erase(iter);
				if (!m_ThreadArray.size())
				{
					if (!m_bQuit)
						ShowStatus(_T("Server offline."), 1);
					else
						DestroyWindow(m_hWnd);
				}
				break;
			}
			
		}
	}
	else if (wParam==FSM_SEND)
	{
		char buffer[5];
		buffer[0] = 1;
		memcpy(buffer+1, &lParam, 4);
		m_pAdminInterface->SendCommand(2, 7, buffer, 5);
		m_nSendCount += lParam;
	}
	else if (wParam==FSM_RECV)
	{
		char buffer[5];
		buffer[0] = 0;
		memcpy(buffer+1, &lParam, 4);
		m_pAdminInterface->SendCommand(2, 7, buffer, 5);
		m_nRecvCount += lParam;
	}
	else if (wParam == FSM_RELOADCONFIG)
	{
		COptions options;
		options.ReloadConfig();
		CPermissions perm;
		perm.ReloadConfig();
	}
	return 0;
}