<p><code>event_base_dispatch</code> 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 <code>struct event_base* base</code> variable.</p>

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

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

<pre><code>/** HTTP request callback */
static void http_request_cb(struct evhttp_request* req, void* arg)
</code></pre>

<blockquote>
  <p>Why are we renaming threads?</p>
</blockquote>

<p>Debuggging and performance logging.</p>

CC BY-SA 3.0
2015-12-16T04:06:57.330Z
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
CC BY-SA 3.0
2015-12-16T06:39:50.763Z
@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::INV`
Nick ODell
CC BY-SA 3.0
2015-12-17T05:31:42.360Z
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 ) etc
Paarth
CC BY-SA 3.0
2015-12-17T05:44:54.367Z
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
CC BY-SA 3.0
2015-12-17T11:05:02.267Z
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
CC BY-SA 3.0
2015-12-17T18:39:55.230Z
@Paarth The connection is opened here: https://github.com/bitcoin/bitcoin/blob/7a5040155ed59f8c9c51734bb2ee29f1593eaa6a/src/net.cpp#L1615  The data is sent here: https://github.com/bitcoin/bitcoin/blob/7a5040155ed59f8c9c51734bb2ee29f1593eaa6a/src/net.cpp#L764
Nick ODell
CC BY-SA 3.0
2015-12-15T17:55:14.803Z
2015-12-15T17:55:14.803Z
Nick ODell
<p>Source code (<code>src/httpserver.cpp</code>):</p>

<pre><code>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");
}
</code></pre>

<p>Can anyone help me understand this portion of logic? In particular </p>

<ul>
<li>What is the HTTP magic happening here <code>event_base_dispatch(base)</code>?</li>
<li>Why are we renaming threads?</li>
</ul>

<bitcoind><client>
What does event_base_dispatch do?
Paarth
# What does event_base_dispatch do?

(Asked by: username_0 on 2015-12-15)

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?

**Tags:** bitcoind, client


---

## 1 answers

### Answer by username_1 on 2015-12-15. Score: 3 | Accepted answer

`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.

> **Comments:**
> **username_0**: 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`.  
> **username_1**: @username_0 `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::INV`  
> **username_0**: 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 ) etc  
> **username_1**: 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`.  
> **username_0**: 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.  
> **username_1**: @username_0 The connection is opened here: https://github.com/bitcoin/bitcoin/blob/7a5040155ed59f8c9c51734bb2ee29f1593eaa6a/src/net.cpp#L1615  The data is sent here: https://github.com/bitcoin/bitcoin/blob/7a5040155ed59f8c9c51734bb2ee29f1593eaa6a/src/net.cpp#L764  

---

