| 1293 | } |
| 1294 | |
| 1295 | static void send_message_batch(struct peer *peer, u8 **msgs) |
| 1296 | { |
| 1297 | size_t size; |
| 1298 | size_t hdr_size = tal_bytelen(towire_protocol_batch_element(tmpctx, |
| 1299 | &peer->channel_id, |
| 1300 | 0)); |
| 1301 | u8 *batch_msg, *final_msg, *final_msg_ptr; |
| 1302 | struct tlv_start_batch_tlvs *tlvs; |
| 1303 | |
| 1304 | assert(tal_count(msgs) > 0); |
| 1305 | |
| 1306 | /* When sending one message, no batching is required */ |
| 1307 | if (tal_count(msgs) == 1) { |
| 1308 | peer_write(peer->pps, msgs[0]); |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | /* We prefix each message with an interal wire type, |
| 1313 | * protocol_batch_element. connectd will eat each message so they don't |
| 1314 | * actually go out to the peer. It's just so connectd can chop up the |
| 1315 | * message batch back out into individual messages. */ |
| 1316 | |
| 1317 | /* We start by calculating the total size */ |
| 1318 | size = 0; |
| 1319 | |
| 1320 | /* Build the `start_batch` msg now so know it's size */ |
| 1321 | tlvs = tlv_start_batch_tlvs_new(tmpctx); |
| 1322 | tlvs->batch_info = tal(tlvs, u16); |
| 1323 | *tlvs->batch_info = WIRE_COMMITMENT_SIGNED; |
| 1324 | batch_msg = towire_start_batch(tmpctx, &peer->channel_id, |
| 1325 | tal_count(msgs), tlvs); |
| 1326 | size += tal_bytelen(batch_msg) + hdr_size; |
| 1327 | |
| 1328 | /* Count the size of all the messages in the batch */ |
| 1329 | for(u32 i = 0; i < tal_count(msgs); i++) |
| 1330 | size += tal_bytelen(msgs[i]) + hdr_size; |
| 1331 | |
| 1332 | /* Now we know the size of our `final_msg` so we allocate */ |
| 1333 | final_msg = tal_arr(tmpctx, u8, size); |
| 1334 | final_msg_ptr = final_msg; |
| 1335 | |
| 1336 | status_debug("proto_batch Building batch with %zu bytes, msgs: %zu", |
| 1337 | size, tal_count(msgs)); |
| 1338 | |
| 1339 | /* Copy the bytes for `start_batch` prefix */ |
| 1340 | memcpy(final_msg_ptr, |
| 1341 | towire_protocol_batch_element(tmpctx, |
| 1342 | &peer->channel_id, |
| 1343 | tal_bytelen(batch_msg)), |
| 1344 | hdr_size); |
| 1345 | final_msg_ptr += hdr_size; |
| 1346 | |
| 1347 | memcpy(final_msg_ptr, batch_msg, tal_bytelen(batch_msg)); |
| 1348 | final_msg_ptr += tal_bytelen(batch_msg); |
| 1349 | |
| 1350 | /* Now copy the bytes from all messages in `msgs` */ |
| 1351 | for(u32 i = 0; i < tal_count(msgs); i++) { |
| 1352 | memcpy(final_msg_ptr, |
no test coverage detected