Пример #1
0
void CApexConnHandler::OnDataReceived()
{
	uint32 uDealDataLen = (const_cast<CApexProxy*>(CApexProxy::GetInst()))->RecvConnData(m_Pipe->GetRecvData(), m_Pipe->GetRecvDataSize());
	m_Pipe->PopRecvData(uDealDataLen);
	if (m_Pipe->GetRecvDataSize() != 0 && uDealDataLen != 0)
	{
		OnDataReceived();
	}
}
Пример #2
0
void UDPSockets::WaitForDataCallback(IAsyncResult * async){
	socPakResult = __try_cast<SocketPack*>(async->AsyncState);
	int iRx = socPakResult->socket->EndReceiveFrom(async, &rcvEP);
	if(iRx>0){
		SocketPack * callBackPack = new SocketPack(iRx);	//repack the data that arrived into a new SocketPack
		callBackPack->socket = socPakResult->socket;
		for(int counter=0; counter < iRx; counter++)
		{
			callBackPack->sendBytes[counter] = socPakResult->sendBytes[counter];
		}
		OnDataReceived(callBackPack);
	}
}
///////////////////////////////////////////////////////////////////////////////
// Run
///////////////////////////////////////////////////////////////////////////////
// DESCRIPTION:
//      This function runs the main thread loop
//      this implementation can be overloaded.
//      This function calls CSocketComm::OnDataReceived() (Virtual Function)
// PARAMETERS:
// NOTES:
//      You should not wait on the thread to end in this function or overloads
///////////////////////////////////////////////////////////////////////////////
void CSocketComm::Run()
{
    stMessageProxy stMsgProxy;
    DWORD   dwBytes  = 0L;
    DWORD   dwTimeout = INFINITE;
    LPBYTE  lpData  = (LPBYTE)&stMsgProxy;
    DWORD   dwSize  = sizeof(stMsgProxy);

    bool bSmartAddressing = IsSmartAddressing();
    if ( !bSmartAddressing )
    {
        lpData = stMsgProxy.byData;
        dwSize = sizeof(stMsgProxy.byData);
    }

    // Should we run as server mode
    if (IsServer() && !bSmartAddressing)
    {
        if (!IsBroadcast())
        {
            SOCKET sock = (SOCKET) m_hComm;
            sock = WaitForConnection( sock );

            // Get new connection socket
            if (sock != INVALID_SOCKET)
            {
                ShutdownConnection( (SOCKET) m_hComm);
                m_hComm = (HANDLE) sock;
                OnEvent( EVT_CONSUCCESS, NULL ); // connect
            }
            else
            {
                // Do not send event if we are closing
                if (IsOpen())
                    OnEvent( EVT_CONFAILURE, NULL ); // wait fail
                return;
            }
        }
    }
    else
    {
        GetPeerName( stMsgProxy.address );
    }

    while( IsOpen() )
    {
        // Blocking mode: Wait for event
        dwBytes = ReadComm(lpData, dwSize, dwTimeout);

        // Error? - need to signal error
        if (dwBytes == (DWORD)-1L)
        {
            // Do not send event if we are closing
            if (IsOpen())
            {
                if ( bSmartAddressing )
                {
                    RemoveFromList( stMsgProxy.address );
                }
                OnEvent( EVT_CONDROP, &stMsgProxy.address ); // lost connection
            }

            // special case for UDP, alert about the event but do not stop
            if ( bSmartAddressing )
                continue;
            else
                break;
        }

        // Chars received?
        if ( bSmartAddressing && dwBytes == sizeof(SOCKADDR_IN))
        {
            OnEvent( EVT_ZEROLENGTH, NULL );
        }
        else if (dwBytes > 0L)
        {
            OnDataReceived( lpData, dwBytes);
        }

        //Sleep(0);
    }
}
Пример #4
0
void SampleSocketPort::pending(void)
{
//cerr << "Pending called " << endl;
	if(!m_bOpen)
		return;

	// Read all available bytes into our buffer
	int nBytesAvail = peek(m_pBuf, MAX_RXBUF);
//cerr << "Pending .. " << nBytesAvail << endl;

	if(!m_bReceptionStarted)
	{	//Start the receive timer
		ResetReadTimeout(MAX_RXTIMEOUT);	//Got 'n' seconds to get all the data else we timeout
		m_bReceptionStarted = true;
	}
	else
	{
		if(m_bTimedOut)	//The receive timer has expired...this is a timeout condition
		{
			ResetReadTimeout(MAX_RXTIMEOUT); //Clear the timeout flag
			m_nLastBytesAvail = 0;		//Reset the flags
			m_bReceptionStarted = false;
			OnRxTimeout();	//Do whatever 'we' do for a timeout (probably a flush or disconnect)...
			return;
		}
	}

	if(m_nLastBytesAvail == nBytesAvail)	//Check if any more data has been received since last time
	{										//No point in parsing unless this has changed!
		//Maybe yield in here!
		//Thread::yield();
		if(nBytesAvail == 0)		//If we have been called with 0 bytes available (twice now)
		{							//a disconnection has occurred
			if(!m_bDoDisconnect)
			{
				CloseSocket();	//Force the close
			}
		}
		return;
	}

	//Depending on your application you may want to attempt to process the extra data 
	//(or change your MAX_RXBUF).
	//
	//Here I just flush the whole lot, because I assume a 'legal' client wont send more than
	//we can receive....maybe someone is trying to flood / overrun us!
	if(nBytesAvail > MAX_RXBUF)	
	{
		cerr << "TCP/IP overflow..." << endl;
		FlushRxData();
		m_nLastBytesAvail = 0;
		m_bReceptionStarted = false;
		return;
	}
	m_nLastBytesAvail = nBytesAvail;

	//In this loop you may parse the received data to determine whether a whole
	//'packet' has arrived. What you do in here depends on what data you are sending.
	//Here we will just look for a /r/n terminator sequence.
	for(int i=0; i < nBytesAvail; i++)
	{

/***************************SHOULD BE CUSTOMISED*******************/

		if(m_pBuf[i] == '\r')
		{
			if(i+1 < nBytesAvail)
			{
				if(m_pBuf[i+1] == '\n')
				{	//Terminator sequence found

					/**************************************************************/
					// COMPULSORY ... Clear the flag and count..
					// do this when you have received a good packet
					m_nLastBytesAvail = 0;
					m_bReceptionStarted = false;
					/**************************************************************/

					// Now receive the data into a buffer and call our receive function
					int nLen = i+2;
					char *pszRxData = new char[nLen+1];	//Allow space for terminator
					receive(pszRxData, nLen);		//Receive the data
					pszRxData[nLen] = '\0';		//Terminate it
					OnDataReceived(pszRxData, nLen);
					delete [] pszRxData;
					return;
				}
			}
		}
/***************************END CUSTOMISATION*******************/

	}
}