[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Read callback problem



Hello! My send function is next:

static void WriteNeighbor(Neighbor *neighbor, unsigned int size, uint8_t *data)
    {
        uint8_t pre_data[4];

        Utils::put_be32(pre_data, command);

        if (bufferevent_write(neighbor->bev,pre_data,4)==-1)
            ErrorFactory::Die(Errors::WriteNeighborPreDataError);
        if (bufferevent_write(neighbor->bev,data,size) ==-1)
            ErrorFactory::Die(Errors::WriteNeighborDataError);
    }

My setup of connection on server side:

static void OnNewNeighbor(struct evconnlistener *listener, evutil_socket_t fd,
        struct sockaddr *sa, int socklen, void *user_data)
    {
        struct bufferevent *bev;
        Neighbor* new_neighbor;

	bev = bufferevent_socket_new(EventLoop::base, fd, BEV_OPT_CLOSE_ON_FREE);
	if (!bev)
            ErrorFactory::Die(Errors::OnNewNeighborBufEventError);

        new_neighbor = AddNeighbor(bev,-1);

        bufferevent_setcb(bev,OnNeighborRead,NULL,OnNeighborEvent,new_neighbor);
        bufferevent_enable(bev,EV_READ);
        Hello(new_neighbor);
        
        fprintf(stderr,"some1 connectefd\n");
    }

My setup of connection on client side:

static void Connect(Peer *tmp)
{
		sock = socket(PF_INET,SOCK_STREAM,0);
                if (sock < 0)
                    ErrorFactory::Die(Errors::OnFoundNeighborsSockInitError);
                bev = bufferevent_socket_new(EventLoop::base,sock,BEV_OPT_CLOSE_ON_FREE);
                if (bev==NULL)
                    ErrorFactory::Die(Errors::OnFoundNeighborsBufferEventNewError);
                //fprintf(stderr,"hostname: %s, port: %d, peer_id: %d\n",tmp->hostname,tmp->port,tmp->peer_id);
                if(bufferevent_socket_connect_hostname(bev,NULL,AF_INET,tmp->hostname,tmp->port)==-1)
                {
                    ErrorFactory::LogError(log_level,Errors::OnFoundNeighborsConnectError);
                    continue;
                }
                neighbor = NeighborObject::AddNeighbor(bev,tmp->peer_id);
                bufferevent_setcb(bev,NeighborObject::OnNeighborRead,NULL,NeighborObject::OnNeighborEvent,neighbor);
                bufferevent_enable(bev,EV_READ);
                NeighborObject::Hello(neighbor);
}

OnNeighborRead is the same function in both variants and is equal to OnRead function in previous post.

Libevent setup:

int Init()
    {
        base = event_base_new();
        if (!base)
            return ErrorFactory::LogError(log_level,Errors::InitNewBaseError);
        Private::signal_event = evsignal_new(base, SIGINT,OnError, NULL);

        if (!Private::signal_event || event_add(Private::signal_event, NULL)<0)
            return ErrorFactory::LogError(log_level,Errors::InitSignalAddError);

        return 0;
    }
    
    void Start()
    {
        event_base_dispatch(base);
    }


Still got this problem. If i try to send data with nc (netcat) everything is going fine. So probably my send function is not valid?***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.