Search code examples
csocketslibevent

libevent fires send when it receveives packet


I am building MQTT broker now.

SO, when I am receiving a MQTT packet from the client.That packet is also being send simultaneously.I can't find out the reason why. I am posting my code snippet below please do point out where am I going wrong .

static void onServEcho(struct bufferevent* bev, void* cbarg) {
    EvBufferEvent evbuf(bev);
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);
    size_t len = evbuffer_get_length(input);
    evutil_socket_t accept_fd = bufferevent_getfd(bev);
    // Copy all the data from the input buffer to the output buffer.
    char *data;
    //data =(char *) malloc(len);
    data = new char[len];
    // cout<<"dasd"<<data<<endl;

    evbuffer_copyout(input, data, len);
    // evbuf.copy_data(input, data, len);

    char converted[len];
    int i;
    for (i = 0; i < len; i++) {
        sprintf(&converted[i * 2], "%02X", data[i]);
        //  cout<<"data is"<< data[i]<<endl;



         */
    }
    // std::stringstream stream;
    //  char packet[len];
    //cout << "the hex data is" << converted << "with size" << len << endl;
    // for(i=0;i<(2*len;i=i+2) {
    //  string connect_char;
    //    connect_char.insert(0,"0x");
    ///  connect_char.append(converted,i,2);
    //char *buffer=static_cast<char*>(connect_char);
    ///   unsigned  int buffer=strtoul(connect_char.c_str(), NULL, 16);
    //  char W = static_cast<char>(buffer);
    // cout<<"the characterr is "<<W<<endl;
    // }
    //char *packet=convert(converted,20);
    //cout<<"the packet converted is "<<packet<<endl;
    string connect_comd;
    //connect_comd="0x";
    connect_comd.insert(0, "0x");
    connect_comd.append(converted, 0, 2);
    // strcpy(connect_comd,converted[0]);
    // strcpy(connect_comd,converted[1]);
    //unsigned int buf = strtoul(connect_comd.c_str(), NULL, 16);
    //int buf=0x10;
    int message_type = (ToBits(data[0]).to_ulong());
    //  cout<<"the message type received now is"<<GET_MESSAGE(data[0])<<endl;

    //cout << "the connectcommand is" << buf << endl;
    cout << "the message flag received now is" << ToBits(data[0]) << endl;
    if (GET_MESSAGE(message_type) == CONNECT) {
        cout << "connect packet received  from mqtt client" << endl;
        ConnectPack_Parser(data, len, accept_fd);
    } else if (GET_MESSAGE(message_type) == SUBSCRIBE) {

        cout << "subscribe packet received  from mqtt client" << endl;
        SubscribePack_Parser(data, len, accept_fd);
    }

    // }
    // hexify()
    //evbuffer_add_buffer(output, input);
    evbuffer_add_buffer(output, input);
    //evbuffer_remove_buffer(input,output,len);
    free(data);
}

edit: i am not just posting the code for a fix to the code but i am cluless as to how to avoid that send from the server end using lib event . the line 'evbuffer_add_buffer(output, input);' automatically sends the received packet which i can trace from the wireshark.The thing is this line according to the libevent documentation is needed for optimal performance.


Solution

  • the code is working for now.I am posting it for those who may need it. If you only want the read operation don't use the ' evbuffer_add_buffer(output, input)' code since this instantly writes it to the socket or in other words sends the data to the connected client so the CONNECT packet was send simultaneously