コード例 #1
0
ファイル: os_mutex.c プロジェクト: osrf/opensplice
static Error os_mutexMapFindAndRemove( os_mutex *mutex )
{
   int res;
   mapEntry *toDestroy;
   Error err;

   res = pthread_mutex_lock(&mapMutex);
   assert(res == 0);

   toDestroy = &semMap[mutex->index];

   assert( toDestroy->semaphore != 0 && toDestroy->uid != 0 );
#ifndef NDEBUG
   assert( toDestroy->mutex == mutex );
#endif

   err = CloseLink((Link)toDestroy->semaphore);
   assert(err == Success);

   toDestroy->semaphore = 0;
   toDestroy->uid = 0;
#ifndef NDEBUG
   toDestroy->mutex = NULL;
#endif

   res = pthread_mutex_unlock(&mapMutex);
   assert(res == 0);
   (void) res;

   return (err);
}
コード例 #2
0
ファイル: os_mutex.c プロジェクト: osrf/opensplice
static void os_mutexAddToMap( os_mutex *mutex, Semaphore sem )
{
   int res;
   struct mapEntry *current;

   res = pthread_mutex_lock(&mapMutex);
   assert( res == 0 );

#ifndef NDEBUG
   if ( mutex->index > semMapIndexCeiling )
   {
      assert( mutex->index < DDS_MAX_SEMAPHORES );
      semMapIndexCeiling = mutex->index;
   }
#endif
   current = &semMap[mutex->index];

   if ( current->uid != 0 )
   {
      /* Slot previously used, free old link */
      CloseLink( (Link)current->semaphore );
   }
   current->semaphore = sem;
   current->uid = mutex->uid;
#ifndef NDEBUG
   current->mutex = mutex;
#endif

   res = pthread_mutex_unlock(&mapMutex);
   assert( res == 0 );
   (void) res;
}
コード例 #3
0
ファイル: os_mutex.c プロジェクト: osrf/opensplice
static Semaphore os_mutexMapFind(os_mutex *mutex)
{
   int res;
   Semaphore result = 0;
   mapEntry *current;

   res = pthread_mutex_lock(&mapMutex);
   assert(res == 0);

#ifndef NDEBUG
   if ( mutex->index > semMapIndexCeiling )
   {
      assert( mutex->index < DDS_MAX_SEMAPHORES );
      semMapIndexCeiling = mutex->index;
   }
#endif

   current = &semMap[mutex->index];
   if ( current->uid != mutex->uid )
   {
      /* The index has been reused and we have a stale link */
      CloseLink( (Link)current->semaphore );
      current->uid = 0;
      current->semaphore = 0;
#ifndef NDEBUG
      current->mutex = NULL;
#endif
   }

   if (!current->uid)
   {
      /* We have no local entry in cache */
      Error err;

      err = rsGetSemaphore( &current->semaphore, mutex->index);
      if ( err == Success )
      {
         result = current->semaphore;
         current->uid = mutex->uid;
#ifndef NDEBUG
         current->mutex = mutex;
#endif
      }
   }
   else
   {
      result = current->semaphore;
   }

   res = pthread_mutex_unlock( &mapMutex );
   assert(res == 0);
   (void) res;
   assert( result != 0 );

   return (result);
}
コード例 #4
0
ファイル: PeerLink.cpp プロジェクト: zxymd5/MyWinBitTorrent
void CPeerLink::OnTimer( int nTimerID )
{
    if (nTimerID == m_nConnTimeoutID)
    {
        m_nConnTimeoutID = 0;
        if (m_nPeerState == PS_CONNECTING)
        {
            CloseLink();
        }
    }
}
コード例 #5
0
ファイル: CliSession.cpp プロジェクト: albertclass/XStudio
xgc_void CClientSession::OnRecv( xgc_lpvoid data, xgc_size size )
{
	MessageHeader &header = *(MessageHeader*)data;
	auto length = htons( header.length );
	auto message = htons( header.message );

	auto ptr = (xgc_lpstr)data + sizeof( MessageHeader );
	auto len = (xgc_int32)( size - sizeof( MessageHeader ) );

	switch( message )
	{
		case gate::GATE_LOGIN_REQ:
		{
			gate::login_req req;
			req.ParseFromArray( ptr, len );

			auto ret = theServer.VerificationUser( req.username(), req.password(), user_id_ );
			if( ret < 0 )
			{
				gate::login_ack ack;
				ack.set_result( ret );

				Send( gate::GATE_LOGIN_ACK, ack );
			}

			ret = theServer.UserLogin( user_id_, req.username(), handle() );
			if( ret < 0 )
			{
				gate::login_ack ack;
				ack.set_result( ret - 100 );

				Send( gate::GATE_LOGIN_ACK, ack );
			}
		}
		break;

		case gate::GATE_LOGOUT_REQ:
		{
			gate::logout_req req;
			req.ParseFromArray( ptr, len );

			theServer.UserLogout( user_id_ );

			CloseLink( handle_ );
		}
		break;
	}
}
コード例 #6
0
ファイル: PeerLink.cpp プロジェクト: zxymd5/MyWinBitTorrent
int CPeerLink::ProcCmdPiece( void *pData, int nDataLen )
{
    if (nDataLen <= 8)
    {
        return -1;
    }

    int nIndex = *(int *)pData;
    nIndex = ntohl(nIndex);
    int nOffset = *((int *)((char *)pData + 4));
    nOffset = ntohl(nOffset);

    int nLen = nDataLen - 8;
    if (nIndex != m_clPieceRequest.GetPieceIndex())
    {
        return -1;
    }

    if (!m_clPieceRequest.AddPieceData(nOffset, (const char *)pData + 8, nLen))
    {
        return -1;
    }

    if (m_clPieceRequest.Complete())
    {
        string strPieceData = m_clPieceRequest.GetPiece();
        if (ShaString((char *)strPieceData.data(), strPieceData.size()) == 
            m_pPeerManager->GetTorrentTask()->GetTorrentFile()->GetPieceHash(nIndex))
        {
            m_pPeerManager->GetTorrentTask()->GetTaskStorage()->WritePiece(nIndex, strPieceData);
            m_pPeerManager->BroadcastHave(nIndex);
            GetNewPieceTask();
        }
        else
        {
            m_clPieceRequest.CreatePieceRequestList(-1, 0, 0);
            CloseLink();
        }
    }

    DoPieceRequest();
    return 0;
}
コード例 #7
0
ファイル: QTable.cpp プロジェクト: Lavoro922/Erd-Creator
void QTableManager::CloseLink(QgraphicsItemTable *table)
{
    if (isStartLink()){
        if ((nodo->start!=table)&& (table!=NULL)){
    nodo->end = table;
    if ( ProjectData->addNode(nodo)){
    nodo = new Node();
    nodo->start = NULL;
    nodo->link = NULL;
    }
    else
    CloseLink(NULL);
    }
    else {
    delete  nodo->link;
        nodo->start = NULL;
        nodo->link = NULL;
    }
    }
}
コード例 #8
0
ファイル: ClientComm.cpp プロジェクト: lufb/code
void ClientComm::OnConnectClose(const char * errmsg)
{
	slib_WriteError( Global_UnitNo, 0, "[通讯单元]:与服务器[%s : %d]的连接断开,断开原因[%s]", m_strCurrentServerAdd.c_str(), m_sCurrentServerPort,errmsg );
	//断开且切换
	CloseLink( true );
}
コード例 #9
0
void LinkOptions::OnOk()
{
    static const int length = 256;
    int timeout;
    CString timeoutStr;
    CString host;
    CString title;
    CString addressMessage;

    UpdateData(TRUE);

    // Close any previous link
    CloseLink();

    m_serverip.GetWindowText(host);
    m_timeout.GetWindowText(timeoutStr);
    sscanf(timeoutStr, "%d", &timeout);
    SetLinkTimeout(timeout);

    LinkMode newMode = (LinkMode)m_type;

    if (newMode == LINK_DISCONNECTED) {
        linkTimeout = timeout;
        linkMode = LINK_DISCONNECTED;
        theApp.linkHostAddr = host;
        CDialog::OnOK();
        return;
    }

    bool needsServerHost = newMode == LINK_GAMECUBE_DOLPHIN || (newMode == LINK_CABLE_SOCKET && !m_server) || (newMode == LINK_RFU_SOCKET && !m_server);

    if (needsServerHost) {
        bool valid = SetLinkServerHost(host);
        if (!valid) {
            AfxMessageBox("You must enter a valid host name", MB_OK | MB_ICONSTOP);
            return;
        }
    }

    EnableSpeedHacks(linkHacks);
    EnableLinkServer(m_server, linkNumPlayers + 1);

    if (m_server) {
        char localhost[length];
        GetLinkServerHost(localhost, length);

        title = "Waiting for clients...";
        addressMessage.Format("Server IP address is: %s\n", localhost);
    } else {
        title = "Waiting for connection...";
        addressMessage.Format("Connecting to %s\n", host);
    }

    // Init link
    ConnectionState state = InitLink(newMode);

    // Display a progress dialog while the connection is establishing
    if (state == LINK_NEEDS_UPDATE) {
        ServerWait* dlg = new ServerWait();
        dlg->Create(ServerWait::IDD, this);
        dlg->m_plconn[1] = title;
        dlg->m_serveraddress = addressMessage;
        dlg->ShowWindow(SW_SHOW);

        while (state == LINK_NEEDS_UPDATE) {
            // Ask the core for updates
            char message[length];
            state = ConnectLinkUpdate(message, length);

            // Update the wait message
            if (strlen(message)) {
                dlg->m_plconn[1] = message;
            }

            // Step the progress bar and update dialog data
            dlg->m_prgctrl.StepIt();
            dlg->UpdateData(false);

            // Process Windows messages
            MSG msg;
            while (PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
                AfxGetApp()->PumpMessage();
            }

            // Check whether the user has aborted
            if (dlg->m_userAborted) {
                state = LINK_ABORT;
            }
        }

        delete dlg;
    }

    // The user canceled the connection attempt
    if (state == LINK_ABORT) {
        CloseLink();
        return;
    }

    // Something failed during init
    if (state == LINK_ERROR) {
        AfxMessageBox("Error occurred.\nPlease try again.", MB_OK | MB_ICONSTOP);
        return;
    }

    linkTimeout = timeout;
    linkMode = GetLinkMode();
    theApp.linkHostAddr = host;

    CDialog::OnOK();
    return;
}
コード例 #10
0
ファイル: wxvbam.cpp プロジェクト: MrSwiss/visualboyadvance-m
MainFrame::~MainFrame()
{
#ifndef NO_LINK
	CloseLink();
#endif
}
コード例 #11
0
ファイル: PeerLink.cpp プロジェクト: zxymd5/MyWinBitTorrent
int CPeerLink::DoRead( long long llCount )
{
    int nReadCount = 0;
    char *pBuff = new char [RECV_BUFFER_SIZE];

    for (; nReadCount < llCount; )
    {
        int nReadSize = RECV_BUFFER_SIZE;
        if (nReadSize > llCount - nReadCount)
        {
            nReadSize = llCount - nReadCount;
        }

        int nRet = recv(GetHandle(), pBuff, nReadSize, 0);
        if (nRet == 0)
        {
            CloseLink();
            delete [] pBuff;
            return nReadCount;
        }

        if (nRet == -1)
        {
            int nErrCode = WSAGetLastError();
            if (nErrCode == WSAEWOULDBLOCK)
            {
                m_bCanRead = false;
                break;
            }

            if (nErrCode == WSAEINTR)
            {
                continue;
            }

            CloseLink();
            delete [] pBuff;
            return nReadCount;
        }

        if (nRet > 0)
        {
            nReadCount += nRet;
            m_nDownloadCount += nRet;
            m_pPeerManager->GetTorrentTask()->AddDownloadCount(nRet);

            m_strRecvBuffer.append((const char *)pBuff, nRet);
        }
    }

    delete [] pBuff;

    ProcRecvData();

    if (m_bCanRead)
    {
        if (GetHandleMask() & READ_MASK)
        {
            RemoveHandleMask(READ_MASK);
        }
    }
    else
    {
        if (!(GetHandleMask() & READ_MASK))
        {
            SetHandleMask(READ_MASK);
        }
    }

    return nReadCount;
}
コード例 #12
0
ファイル: PeerLink.cpp プロジェクト: zxymd5/MyWinBitTorrent
int CPeerLink::DoWrite( long long llCount )
{
    int nSendCount = 0;
    if (m_nPeerState == PS_ESTABLISHED)
    {
        for (; (m_strSendBuffer.size() > 0) && (nSendCount < llCount); )
        {
            int nSendSize = m_strSendBuffer.size();
            if (nSendSize > llCount)
            {
                nSendSize = llCount;
            }
            
            int nRet = send(GetHandle(), m_strSendBuffer.data(), nSendSize, 0);
            
            if (nRet == -1)
            {
                int nErrCode = WSAGetLastError();
                if (nErrCode == WSAEINTR || nErrCode == WSAEWOULDBLOCK )
                {
                    m_bCanWrite = false;
                }
                else
                {
                    CloseLink();
                    return nSendCount;
                }

                break;
            }

            nSendCount += nRet;
            m_nUploadCount += nRet;
            m_pPeerManager->GetTorrentTask()->AddUploadCount(nRet);

            m_strSendBuffer.erase(0, nRet);
            if (m_strSendBuffer.size() == 0)
            {
                OnSendComplete();
                break;
            }
        }
    }

    if (m_bCanWrite)
    {
        if (GetHandleMask() & WRITE_MASK)
        {
            RemoveHandleMask(WRITE_MASK);
        }
    }
    else
    {
        if (!(GetHandleMask() & WRITE_MASK))
        {
            SetHandleMask(WRITE_MASK);
        }
    }

    return nSendCount;
}