Exemplo n.º 1
0
bool NFCNet::SendMsgToAllClient( const NFIPacket& msg )
{
    std::map<int, NetObject*>::iterator it = mmObject.begin();
    for (; it != mmObject.end(); ++it)
    {
        NetObject* pNetObject = (NetObject*)it->second;
        if (pNetObject && !pNetObject->GetRemoveState())
        {
            bufferevent* bev = pNetObject->GetBuffEvent();
            if (NULL != bev)
            {
                bufferevent_write(bev, msg.GetPacketData(), msg.GetPacketLen());
            }
        }
    }

    return true;
}
Exemplo n.º 2
0
bool NFCNet::SendMsgToAllClient( const char* msg, const uint32_t nLen )
{
    if (nLen <= 0)
    {
        return false;
    }

    std::map<int, NetObject*>::iterator it = mmObject.begin();
    for (; it != mmObject.end(); ++it)
    {
        NetObject* pNetObject = (NetObject*)it->second;
        if (pNetObject && !pNetObject->GetRemoveState())
        {
            bufferevent* bev = pNetObject->GetBuffEvent();
            if (NULL != bev)
            {
                bufferevent_write(bev, msg, nLen);
            }
        }
    }

    return true;
}
Exemplo n.º 3
0
void NFCNet::conn_readcb(struct bufferevent *bev, void *user_data)
{
    //接受到消息
    NetObject* pObject = (NetObject*)user_data;
    if (!pObject)
    {
        return;
    }

    NFCNet* pNet = (NFCNet*)pObject->GetNet();
    if (!pNet)
    {
        return;
    }

    if (pObject->GetRemoveState())
    {
        return;
    }

    struct evbuffer *input = bufferevent_get_input(bev);
    if (!input)
    {
        return;
    }

    size_t len = evbuffer_get_length(input);

    //返回给客户端
    //  	struct evbuffer *output = bufferevent_get_output(bev);
    //  	evbuffer_add_buffer(output, input);
    //      SendMsg(1, strData,len, pObject->GetFd());
    //////////////////////////////////////////////////////////////////////////
    if (len > NFIMsgHead::NF_MSGBUFF_LENGTH)
    {
        char* strMsg = new char[len];

        if(evbuffer_remove(input, strMsg, len) > 0)
        {
            pObject->AddBuff(strMsg, len);
        }

        delete[] strMsg;
    }
    else
    {

        memset(pNet->mstrMsgData, 0, NFIMsgHead::NF_MSGBUFF_LENGTH);

        if(evbuffer_remove(input, pNet->mstrMsgData, len) > 0)
        {
            pObject->AddBuff(pNet->mstrMsgData, len);
        }
    }

    while (1)
    {
        int nDataLen = pObject->GetBuffLen();
        if (nDataLen > pNet->mnHeadLength)
        {
            if (!pNet->Dismantle(pObject))
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}