예제 #1
0
void NFCNet::listener_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *user_data)
{
    //怕你们重了
    NFCNet* pNet = (NFCNet*)user_data;
    bool bClose = pNet->CloseNetObject(fd);
    if (bClose)
    {
        //error
        return;
    }

    if (pNet->mmObject.size() >= pNet->mnMaxConnect)
    {
        //应该T掉,拒绝
        return;
    }

    struct event_base *base = pNet->base;
    //创建一个基于socket的bufferevent
    struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    if (!bev)
    {
        //应该T掉,拒绝
        fprintf(stderr, "Error constructing bufferevent!");
        //event_base_loopbreak(base);
        return;
    }

    //我获得一个新连接。为其创建一个bufferevent--FD需要管理
    struct sockaddr_in* pSin = (sockaddr_in*)sa;

    NetObject* pObject = new NetObject(pNet, fd, *pSin, bev);
    pObject->GetNet()->AddNetObject(fd, pObject);

    //为bufferevent设置各种回调
    bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, (void*)pObject);

    //开启bufferevent的读写
    bufferevent_enable(bev, EV_READ|EV_WRITE);

    //模拟客户端已连接事件
    conn_eventcb(bev, BEV_EVENT_CONNECTED, (void*)pObject);
    //////////////////////////////////////////////////////////////////////////

    struct timeval tv;
    /* 设置读超时120秒, 可做为心跳机制, 120秒没收到消息就T */
    tv.tv_sec = 120;
    tv.tv_usec = 0;
    bufferevent_set_timeouts(bev, &tv, NULL);
}
예제 #2
0
void NFCNet::conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{

    NetObject* pObject = (NetObject*)user_data;
    NFCNet* pNet = (NFCNet*)pObject->GetNet();
    if(pNet->mEventCB)
    {
        pNet->mEventCB(pObject->GetFd(), NF_NET_EVENT(events), pNet);
    }

    if (events & BEV_EVENT_EOF)
    {
        //printf("%d Connection closed.\n", pObject->GetFd());
        pNet->CloseNetObject(pObject->GetFd());
        if (!pNet->mbServer)
        {
            //客户端断线重连
            pNet->ReqReset();
        }
    }
    else if (events & BEV_EVENT_ERROR)
    {
        //printf("%d Got an error on the connection: %d\n", pObject->GetFd(),	errno);/*XXX win32*/
        pNet->CloseNetObject(pObject->GetFd());
        if (!pNet->mbServer)
        {
            //客户端断线重连
            pNet->ReqReset();
        }
    }
    else if (events & BEV_EVENT_TIMEOUT)
    {
        //printf("%d read timeout: %d\n", pObject->GetFd(), errno);/*XXX win32*/
        pNet->CloseNetObject(pObject->GetFd());

        if (!pNet->mbServer)
        {
            //客户端断线重连
            pNet->ReqReset();
        }
    }
    else if (events & BEV_EVENT_CONNECTED)
    {
        pNet->mfRunTimeReseTime = 0.0f;
        //printf("%d Connection successed\n", pObject->GetFd());/*XXX win32*/
    }
}
예제 #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;
        }
    }
}