What does event_base_dispatch do?

1

Source code (src/httpserver.cpp):

static void ThreadHTTP(struct event_base* base, struct evhttp* http)
{
    RenameThread("bitcoin-http");
    LogPrint("http", "Entering http event loop\n");
    event_base_dispatch(base);
    // Event loop will be interrupted by InterruptHTTPServer()
    LogPrint("http", "Exited http event loop\n");
}

Can anyone help me understand this portion of logic? In particular

  • What is the HTTP magic happening here event_base_dispatch(base)?
  • Why are we renaming threads?

Paarth

Posted 2015-12-15T05:29:46.080

Reputation: 105

Answers

3

event_base_dispatch is part of libevent, a library that takes events from network sockets, and uses them to call callbacks in your program. To find out what it's calling, you should look at the struct event_base* base variable.

bool InitHTTPServer()
{
    // [...]
    base = event_base_new(); // XXX RAII
    // [...]
    http = evhttp_new(base); // XXX RAII
    // [...]
    evhttp_set_gencb(http, http_request_cb, NULL);

There are three important calls here. event_base_new creates the struct that tells event_base_dispatch how to handle incoming connections. evhttp_new tells libevent to interpret incoming data as the HTTP protocol, instead of passing along raw bytes. evhttp_set_gencb tells libevent which function to call when new requests come in. This is where that ends:

/** HTTP request callback */
static void http_request_cb(struct evhttp_request* req, void* arg)

Why are we renaming threads?

Debuggging and performance logging.

Nick ODell

Posted 2015-12-15T05:29:46.080

Reputation: 26 536

Thanks for the clear explanation! As I understand, here we are registering handlers while we are listening to socket connections. But where is the logic which actually causes the invocation of these callback functions? Lets say, I have established connection between 2 nodes A and B and I want to send a INV message from B to A.Paarth 2015-12-16T04:06:57.330

@Paarth But where is the logic which actually causes the invocation Somewhere in libevent, I think. I don't think I understand your question. Lets say, I have established connection That's not handled by this code at all. This is JSON-RPC code, and you're looking for the peer to peer code. That's in ProcessMessage in main.cpp on the receiving side, and SendMessages in main.cpp on the sending side. Look for NetMsgType::INVNick ODell 2015-12-16T06:39:50.763

Thank you once again. So JSON-RPC is used ONLY for communicating between bitcoin-d servers and its client? But the mining nodes individually talk via some other logic to exchange messages (like inv, ack or blk ) etcPaarth 2015-12-17T05:31:42.360

1) Yes. 2) Yes, but ack and blk aren't messages. The closest thing in the p2p protocol to ack and blk is pong and block.Nick ODell 2015-12-17T05:44:54.367

Really sorry to bother you again. My background in networks is pretty bad. But where exactly is the socket connection happening for p2p protocol? All the logic I can see in codebase is associated with JSON-RPC in httpserver.cpp. The main.cpp you had pointed out implicitly boils down to overloaded function PushMessage, which uses BeginMessage in net.cpp. The logic am looking for is how the data in PushMessage (or BeginMessage) is actually being sent to other nodes. It needs to send the data via some opened socket right? If I am not wrong.Paarth 2015-12-17T11:05:02.267