Esempio n. 1
0
int Net::Send(sockaddr_in addr, const void* sendBuf, uint32_t pkgLen) {
	if (0 != Init()) {
		return -1;
	}

	return HandleSend(addr, sendBuf, pkgLen);
}
Esempio n. 2
0
void CTcpAgent::HandleIo(CONNID dwConnID, TSocketObj* pSocketObj, TBufferObj* pBufferObj, DWORD dwBytes, DWORD dwErrorCode)
{
	ASSERT(pBufferObj != nullptr);
	ASSERT(pSocketObj != nullptr);

	if(dwErrorCode != NO_ERROR)
	{
		HandleError(dwConnID, pBufferObj, dwErrorCode);
		return;
	}

	if(dwBytes == 0 && pBufferObj->operation != SO_CONNECT)
	{
		AddFreeSocketObj(dwConnID, SCF_CLOSE);
		AddFreeBufferObj(pBufferObj);
		return;
	}

	pBufferObj->buff.len = dwBytes;

	switch(pBufferObj->operation)
	{
	case SO_CONNECT:
		HandleConnect(dwConnID, pSocketObj, pBufferObj);
		break;
	case SO_SEND:
		HandleSend(dwConnID, pSocketObj, pBufferObj);
		break;
	case SO_RECEIVE:
		HandleReceive(dwConnID, pSocketObj, pBufferObj);
		break;
	default:
		ASSERT(FALSE);
	}
}
Esempio n. 3
0
void CUdpServer::HandleIo(CONNID dwConnID, TUdpBufferObj* pBufferObj, DWORD dwBytes, DWORD dwErrorCode)
{
	ASSERT(pBufferObj != nullptr);

	if(dwErrorCode != NO_ERROR)
	{
		HandleError(dwConnID, pBufferObj, dwErrorCode);
		return;
	}

	if(dwBytes == 0)
	{
		HandleZeroBytes(dwConnID, pBufferObj);
		return;
	}

	pBufferObj->buff.len = dwBytes;

	switch(pBufferObj->operation)
	{
	case SO_SEND:
		HandleSend(dwConnID, pBufferObj);
		break;
	case SO_RECEIVE:
		HandleReceive(dwConnID, pBufferObj);
		break;
	default:
		ASSERT(FALSE);
	}
}
	void TcpConnectionHandler::HandleSendThreaded()
	{
		while (m_run.load())
		{
			HandleSend();
			std::this_thread::sleep_for(std::chrono::milliseconds(5));
		}
	}
Esempio n. 5
0
///////////////////////////////////////////////////////////////////////////////
/// CConnection::Send
/// @description: Given a message and wether or not it should be sequenced,
///   write that message to the channel.
/// @pre: The CConnection object is initialized.
/// @post: If the window is in not full, the message will have been written to
///   to the channel. Before being sent the message has been signed with the
///   UUID, source hostname and sequence number (if it is being sequenced).
///   If the message is being sequenced  and the window is not already full,
///   the timeout timer is cancelled and reset.
/// @param p_mesg: A CMessage to write to the channel.
/// @param sequence: if true, the message will be sequenced and reliably
///   delievered in order. Otherwise it is immediately fired and forgotten.
///   this is mostly meant for use with ACKs. True by default.
///////////////////////////////////////////////////////////////////////////////
void CConnection::Send(CMessage p_mesg, bool sequence)
{
    Logger::Debug << __PRETTY_FUNCTION__ << std::endl;

    //Make a call to the dispatcher to sign the messages
    //With a bunch of shiny stuff.
    ptree x = static_cast<ptree>(p_mesg);
    unsigned int msgseq;

    //m_dispatch.HandleWrite(x);  

    CMessage outmsg(x);

    // Sign the message with the hostname, uuid, and squencenumber
    if(sequence == true)
    {
        if(m_synched == false)
        {
            m_synched = true;
            SendSYN();
        }
        msgseq = m_outsequenceno;
        outmsg.SetSequenceNumber(msgseq);
        m_outsequenceno = (m_outsequenceno+1) % GetSequenceModulo();
    }
    outmsg.SetSourceUUID(GetConnectionManager().GetUUID()); 
    outmsg.SetSourceHostname(GetConnectionManager().GetHostname());

    if(sequence == true)
    {
        // If it isn't squenced then don't put it in the queue.
        m_queue.Push( QueueItem(msgseq,outmsg) );
    }
    // Before, we would put it into a queue to be sent later, now we are going
    // to immediately write it to channel.

    if(m_queue.size() <= GetWindowSize() || sequence == false)
    {
        // Only try to write to the socket if the window isn't already full.
        // Or it is an unsequenced message
        
        HandleSend(outmsg);
        if(sequence == true)
        {
            m_timeout.cancel();
            m_timeout.expires_from_now(boost::posix_time::milliseconds(1000));
            m_timeout.async_wait(boost::bind(&CConnection::Resend,this,
                boost::asio::placeholders::error));
        }
    }
}
Esempio n. 6
0
void CIocpServer::HandleIo(TSocketObj* pSocketObj, TBufferObj* pBufferObj, DWORD dwBytes, DWORD dwErrorCode)
{
	ASSERT(pBufferObj != NULL);
	ASSERT(pSocketObj != NULL);

	//判断是否已经标记失败
	if(dwErrorCode != NO_ERROR)
	{
		if(pBufferObj->operation != SO_ACCEPT)
		{
			FireError(pSocketObj->connID, pBufferObj->operation, dwErrorCode);
			AddFreeSocketObj(pSocketObj->connID);
		}
		else
		{
			DeleteAcceptSocket(pBufferObj->client, TRUE);
		}

		AddFreeBufferObj(pBufferObj);
		return;
	}

	if(dwBytes == 0 && pBufferObj->operation != SO_ACCEPT)
	{
		FireClose(pSocketObj->connID);
		AddFreeSocketObj(pSocketObj->connID);
		AddFreeBufferObj(pBufferObj);
		return;
	}

	pBufferObj->buff.len = dwBytes;

	switch(pBufferObj->operation)
	{
	case SO_ACCEPT:
		HandleAccept((SOCKET)pSocketObj, pBufferObj);
		break;
	case SO_SEND:
		HandleSend(pSocketObj, pBufferObj);
		break;
	case SO_RECEIVE:
		HandleReceive(pSocketObj, pBufferObj);
		break;
	default:
		ASSERT(FALSE);
	}
}
 bool Producer::Send(const std::string& topic_name, int partition_id,
           const std::vector<std::string>& msgs, bool async)
 {
   // Async is not supported currently so we will ignore it.
   return HandleSend(topic_name, partition_id, msgs);
 }
Esempio n. 8
0
void IOCPPool::svc_worker()
{

	struct epoll_event evts[20];
	if(!hIOCPhandler.ok()) return;

	while (!test_canceled())
	{
		int nfds=epoll_wait(hIOCPhandler,evts,1,1000);

		if(nfds<0)
		{
			logger.LogError("ERROR:%s",strerror(errno));
			continue;
		}
		else if(nfds==0)
		{
			continue;
		}

		for(int i=0; i<nfds; i++)
		{
			int nevt=evts[i].events;
			Session* pkey=(Session*)evts[i].data.ptr;

			if(!pkey)
			{
				logger.LogError("ERROR: NO Hander");
				continue;
			}

			Session& ikey(*pkey);

			if((nevt&(EPOLLERR|EPOLLHUP))!=0)
			{
				ikey.Disconnect();
				continue;
			}

			if(ikey.IsError())
			{
				continue;
			}

			ikey.tpLast=accounter.tTimeStamp;
			if(nevt&EPOLLIN)
			{
				HandleRecv(ikey);
			}
			else if(nevt&EPOLLOUT)
			{
				HandleSend(ikey);
			}

			if(ikey.IsError())
			{
				continue;
			}
			else
			{
				ikey.m_nEpCtl.exchange(0);
			}

		}
	}
}