//--------------------------------------------------------------------------------
bool CLockedResource::GetExclusiveLock(CSingleLock& rLock, DWORD nWait)
	{
	nWait /= 2;

	if(! rLock.Lock(nWait))
		return false;

	// if there are still readers then wait til they're done reading
	if(m_nCurrentUserCount > 0)
		{
		CTime ctStart(CTime::GetCurrentTime());

		::Sleep(20);

		while(m_nCurrentUserCount > 0)
			{
			if(nWait != INFINITE)
				// if the wait time is exceeded then bail
				if(ctStart + nWait < CTime::GetCurrentTime())
					{
					rLock.Unlock();
					return false;
					}

			::Sleep(20);
			}
		}

	m_nCurrentUserCount = 0;
	return true;
	}
Beispiel #2
0
void CCommMsgContainer::AddCommMsg( CCommMsg & msg )
{
	CSingleLock lock (&m_Mutex);
	lock.Lock();
	if (m_arrCommMsg.size() >= MAX_COMM_MSG_CNT)
	{
		lock.Unlock();
		return;
	}

	m_arrCommMsg.push_back(msg);
	lock.Unlock();
}
Beispiel #3
0
bool  CPingMsgContainer::GetPingMsg(  CPingMsg & msg )
{
	CSingleLock lock (&m_Mutex);
	lock.Lock();
	if (m_arrPingMsg.empty())
	{
		lock.Unlock();
		return false;
	}
	CPingMsg  ret_msg = m_arrPingMsg.front();
	m_arrPingMsg.pop_front();
	msg = ret_msg;
	return true;
}
Beispiel #4
0
void CPingMsgContainer::AddPingMsg(CPingMsg &msg)
{	
	GetSystemTime(& msg.m_stReply);
	m_nPingCnt ++;

	CSingleLock lock (&m_Mutex);
	lock.Lock();
	if (m_arrPingMsg.size() >= MAX_COMM_MSG_CNT)
	{
		lock.Unlock();
		return;
	}
	msg.m_nIdx = m_nPingCnt;
	if (msg.m_nStatus)
	{
		m_nLostCnt++;
	}
	else
	{
		m_nReplyCnt ++;
	}
	m_arrPingMsg.push_back(msg);
	lock.Unlock();
}
Beispiel #5
0
bool
ProgressDialogImpl::StartProgressDialog (/*[in]*/ HWND hwndParent)
{
  if (haveProgressDialog)
    {
      FATAL_MIKTEX_ERROR ("ProgressDialogImpl::StartProgressDialog",
			  T_("A progress dialog window is already open."),
			  0);
    }

  CWnd * pParent = 0;

  if (hwndParent != 0)
    {
      pParent = CWnd::FromHandlePermanent(hwndParent);
      if (pParent == 0)
	{
	  INVALID_ARGUMENT ("ProgressDialogImpl::StartProgressDialog", 0);
	}
    }

  pParent = CWnd::GetSafeOwner(pParent, 0);

  if (pParent != 0)
    {
      hwndParent = pParent->GetSafeHwnd();
    }

  hParentWindow = hwndParent;

  // disable mouse and keyboard input in the parent window
  if (hParentWindow != 0)
    {
      EnableWindow (hParentWindow, FALSE);
    }

  // create the user interface thread
  pThread = new ProgressUIThread(0, &readyEvent);
  pThread->CreateThread ();

  // wait for the progress window to become available
  CSingleLock singleLock (&readyEvent);
  if (! singleLock.Lock(1000))
    {
      FATAL_MIKTEX_ERROR ("ProgressDialogImpl::StartProgressDialog",
			  T_("The progress window is not available."),
			  0);
    }
  hWindow = pThread->GetProgressWindow();
  MIKTEX_ASSERT (IsWindow(hWindow));
  haveProgressDialog = true;

  // set the window texts
  SetTitle (title.c_str());
  SetLine (1, lines[0].c_str());
  SetLine (2, lines[1].c_str());

  // make the progress window visible
  ShowWindow (hWindow, SW_SHOW);

  return (true);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// Initialise MFC et affiche un message d'erreur en cas d'échec
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: modifiez le code d'erreur selon les besoins
			_tprintf(_T("Erreur irrécupérable : l'initialisation MFC a échoué\n"));
			nRetCode = 1;
		}
		else
		{
			// TODO: codez le comportement de l'application à cet emplacement.

			// Windsock initialization.
			WSADATA WSAData;
			WSAStartup(MAKEWORD(2,2), &WSAData);


			// Events initialization.
			CEvent* ptrDataRecvEvent = new CEvent(false, true , NULL, NULL); // A packet has been received
			CEvent* ptrDataSentEvent = new CEvent(true, true , NULL, NULL); // A packet has been sent
			CEvent* ptrStopEvent = new CEvent(false, true , NULL, NULL); // Client order to stop application

			// This variable indicate if the downloading has completed.
			BOOL downloadingCompleted = false;

			// ClientSocket creation and connection.
			ClientSocket* ptrClientSocket = new ClientSocket(ptrStopEvent, &downloadingCompleted); // Socket to communicate with the server
			if (ptrClientSocket->openConnection() == 1){
				getchar();
				return 1;
			}

			// File creation
			HANDLE file; // File received by downloading
			file = CreateFile(_T("IAG0010ObjClient_pohikiri.doc"),GENERIC_READ | GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
			if (file == INVALID_HANDLE_VALUE) {
				_tcout << "Unable to create file, error " << GetLastError() << endl;
				getchar();
				return 1;
			}

			// Thread creation and starting.
			ReadingKeyboardThread* ptrReadingKeyboardThread = new ReadingKeyboardThread(ptrStopEvent); // Thread to received the "exit" command throw the terminal.
			ptrReadingKeyboardThread->CreateThread(); // Call Run.
			ReceivingThread* ptrReceivingThread = new ReceivingThread(ptrClientSocket, ptrDataRecvEvent, ptrDataSentEvent, ptrStopEvent, &downloadingCompleted, &file); // Thread to receive data from the server.
			ptrReceivingThread->CreateThread(); // Call Run.
			SendingThread* ptrSendingThread = new SendingThread(ptrClientSocket, ptrDataRecvEvent, ptrDataSentEvent, ptrStopEvent, &downloadingCompleted); // Thread to send data to the server.
			ptrSendingThread->CreateThread(); // Call Run.

			CSingleLock* ptrStopEventLock = new CSingleLock(ptrStopEvent); // Wait stopEvent.

			// Application closing

			ptrStopEventLock->Lock(INFINITE);
			Sleep(5000);
			ptrClientSocket->closeConnection();
			CloseHandle(file);
			WSACleanup();
			if(downloadingCompleted)
				system("IAG0010ObjClient_pohikiri.doc");


			// TODO: Until there.
		}
	}
	else
	{
		// TODO: modifiez le code d'erreur selon les besoins
		_tprintf(_T("Erreur irrécupérable : échec de GetModuleHandle\n"));
		nRetCode = 1;
	}

	return nRetCode;
}