示例#1
0
void cNetworkPortController::ReleasePort(const UINT nPort, const int iFlags)
{
   CAsyncSocket *pSocket = NULL;

   if ( iFlags & UDP )
   {
      m_mOpenUdpPorts.Lookup( nPort, (CObject*&)pSocket );

      if ( pSocket > 0 )
      {
         pSocket->Close();
         delete pSocket;

         m_mOpenUdpPorts.RemoveKey( nPort );
      }
   }

   pSocket = NULL;

   if ( iFlags & TCP )
   {
      m_mOpenTcpPorts.Lookup( nPort, (CObject*&)pSocket );

      if ( pSocket > 0 )
      {
         pSocket->Close();
         delete pSocket;

         m_mOpenTcpPorts.RemoveKey( nPort );
      }
   }
}
示例#2
0
/*................无用...............*/
BOOL CWorkThread::Accept(CListenSocket& ListenSocket)
{
	CAsyncSocket asAccept;
	if(!ListenSocket.Accept(asAccept))
		return FALSE;

	CString strOut;
	strOut.Format(_T("CPoolThread::Accept() socket :%d Accept!\n"),asAccept.m_hSocket);
	TRACE(strOut);
	
	//线程池是否已满
	if(!WorkItem::IsPoolFull())
	{
		if(fpQueueUserWorkItem != NULL)
		{
			//
			fpQueueUserWorkItem(WorkItem::ClientWorkItemThread, (LPVOID)asAccept.Detach(), WT_EXECUTELONGFUNCTION);
		}
		else
		{
			//启动WorkItem类的ClientWorkThread线程
			AfxBeginThread(WorkItem::ClientWorkThread, (LPVOID)asAccept.Detach());
		}
	}
	else
	{
		// refuse request,close socket!拒绝客户端请求,关闭socket
		asAccept.Close();
	}

	return TRUE;
}
示例#3
0
文件: WySock.cpp 项目: trieck/source
void CWySock :: OnAccept(int nErrorCode)
{
    CWysApp* pApp = (CWysApp*)AfxGetApp();
    CWysWnd* pWnd = (CWysWnd*)pApp->m_pMainWnd;
    SCREENLINE sl;
    INT i;

    // make sure socket is listening
    if (this->State != LISTENING)
        return;

    if (nErrorCode == WSAENETDOWN) {
        sl.uColor = COLOR_RED;
        sl.szText.LoadString(GetLastError());
        pWnd->AddListItem(&sl);
        return;
    }

    // find a free socket
    if ((i = pApp->GetSocket()) == NOTFOUND) {
        // no space to accept this connection. however will accept
        // just using simple socket variable and close. Otherwise
        // client program will think it is connected.
        CAsyncSocket sdTemp;
        Accept(sdTemp, NULL,NULL);
        sdTemp.Close();
        sl.uColor = COLOR_RED;
        sl.szText.LoadString(IDS_NOMORESOCKETS);
        pWnd->AddListItem(&sl);
        return;
    }

    // accept the incoming connection
    SOCKADDR_IN		acc_sin;
    INT				acc_sin_len;
    CWySock* pSock	= pApp->pSockets[i];
    acc_sin_len		= sizeof(acc_sin);

    this->Accept(*pSock, (LPSOCKADDR)&acc_sin,
                 (LPINT)&acc_sin_len);

    if (pSock->m_hSocket == INVALID_SOCKET) {
        sl.uColor = COLOR_RED;
        sl.szText.LoadString(IDS_ACCEPTERR);
        pWnd->AddListItem(&sl);
        return;
    }

    CString szTemp;

    sl.uColor = COLOR_BLUE;
    szTemp.LoadString(IDS_ACCEPTCONNECT);
    strcpy(pSock->IPAddress, inet_ntoa(acc_sin.sin_addr));
    pSock->State = CONNECTED;
    sl.szText.Format(szTemp, pSock->IPAddress);
    pWnd->AddListItem(&sl);

    return;
}
示例#4
0
void cNetworkPortController::ReleaseAllPorts(const int iFlags)
{
   POSITION Pos;
   CAsyncSocket *pSocket = NULL;
   unsigned short nPort = 0;

   if ( iFlags & UDP )
   {
      Pos = m_mOpenUdpPorts.GetStartPosition();

      while ( Pos )
      {
         m_mOpenUdpPorts.GetNextAssoc( Pos, nPort, (CObject*&)pSocket );
         
         if ( pSocket > 0 )
         {
            pSocket->Close();
            delete pSocket;
         }
      }
      m_mOpenUdpPorts.RemoveAll();
   }

   if ( iFlags & TCP )
   {
      Pos = m_mOpenTcpPorts.GetStartPosition();

      while ( Pos )
      {
         m_mOpenTcpPorts.GetNextAssoc( Pos, nPort, (CObject*&)pSocket );

         if ( pSocket > 0 )
         {
            pSocket->Close();
            delete pSocket;
         }
      }
      m_mOpenTcpPorts.RemoveAll();
   }
}
示例#5
0
	void CLANConnection::Run()
	{
		VERIFY(AfxSocketInit());

		// Socket erzeugen, falls noch nicht geschehen
		int nError;
		if (!CreateSocket(nError))
		{
			if (m_pListener)
			{
				m_pListener->OnSocketError(nError, this);
				m_pListener->OnConnectionLost(this);
			}
			return;
		}

		CAsyncSocket socket;
		ASSERT(m_hSocket != INVALID_SOCKET);
		socket.Attach(m_hSocket);

		// IP-Adresse und tatsächlichen Port ermitteln
		SOCKADDR_IN sockaddr;
		memset(&sockaddr, 0, sizeof(sockaddr));
		int nSockAddrLen = sizeof(SOCKADDR_IN);
		if (!socket.GetSockName((SOCKADDR *)&sockaddr, &nSockAddrLen)) goto error;

		m_nPort = ntohs(sockaddr.sin_port);
		m_dwIP = ntohl(sockaddr.sin_addr.S_un.S_addr);

		// main thread loop
		BYTE buf[LAN_BUFSIZE];
		while (!IsInterrupted())
		{
			// Senden
			{
			CReference<CLANMessage> message;
			if (message = GetNextMessage())
			{
				// Magic Number
				memcpy(buf, "BotE", 4);

				// Nachricht serialisieren
				CMemFile memFile(&buf[4], LAN_BUFSIZE - 4);
				CArchive ar(&memFile, CArchive::store);
				message->Serialize(ar);
				ar.Close();
				UINT nSize = memFile.GetPosition() + 4;
				memFile.Detach();

				// Empfänger setzen
				memset(&sockaddr, 0, sizeof(sockaddr));
				sockaddr.sin_family = AF_INET;
				sockaddr.sin_addr.S_un.S_addr = htonl(message->GetReceiverIP());
				sockaddr.sin_port = htons(message->GetReceiverPort());

				// Nachricht versenden, setzt bei Fehler m_bInterrupted
				if (!SendMessageTo(socket, message, buf, nSize, &sockaddr)) break;

				// Broadcast-Nachricht auch an den lokalen Host senden
				if (message->GetReceiverIP() == INADDR_BROADCAST)
				{
					sockaddr.sin_addr.S_un.S_addr = htonl(INADDR_LOOPBACK);
					if (!SendMessageTo(socket, message, buf, nSize, &sockaddr))
						break;
				}
			}
			}

			// Empfangen
			memset(&sockaddr, 0, sizeof(sockaddr));
			int nSockAddrLen = sizeof(sockaddr);
			int nCount = socket.ReceiveFrom(&buf, LAN_BUFSIZE, (SOCKADDR *)&sockaddr, &nSockAddrLen);
			if (nCount > 0)
			{
				// Magic Number prüfen
				if (memcmp(&buf, "BotE", 4) != 0) continue;

				// Nachricht deserialisieren
				CMemFile memFile(&buf[4], nCount - 4);
				CArchive ar(&memFile, CArchive::load);
				CReference<CLANMessage> message(new CLANMessage());
				message->Serialize(ar);
				ar.Close();
				memFile.Detach();

				// IP und Port des Absenders
				DWORD dwIP = ntohl(sockaddr.sin_addr.S_un.S_addr);
				UINT nPort = ntohs(sockaddr.sin_port);
				message->SetSenderIP(dwIP);
				message->SetSenderPort(nPort);

				// Ankunft einer Nachricht melden
				if (m_pListener) m_pListener->OnMessageReceived(message, this);
			}
			else if (nCount == 0)
			{
				// "Verbindung" wurde getrennt
				ASSERT(FALSE);
				break;
			}
			else if (nCount == SOCKET_ERROR)
			{
				int nError = socket.GetLastError();
				if (nError != WSAEWOULDBLOCK)
				{
					if (m_pListener) m_pListener->OnSocketError(nError, this);
					// WSAECONNRESET tritt auf, wenn wir zuvor eine Nachricht an eine Adresse gesendet
					// haben, an der kein Server läuft; Thread dann nicht abbrechen
					if (nError != WSAECONNRESET)
					{
						__super::Interrupt();
						break;
					}
				}
			}

			Sleep(50);
		}

		// verbleibende Nachrichten löschen
		{
			CReference<CLANMessage> message;
			while (message = GetNextMessage())
			{
				if (m_pListener) m_pListener->OnMessageDiscarded(message, this);
			}
		}

		// socket schließen, Thread beenden
		socket.Close();
		m_hSocket = INVALID_SOCKET;
		if (m_bSendLost && m_pListener) m_pListener->OnConnectionLost(this);
		return;

error:
		if (m_pListener) m_pListener->OnSocketError(socket.GetLastError(), this);
		m_hSocket = INVALID_SOCKET;
		if (m_pListener) m_pListener->OnConnectionLost(this);
	}