Files
NE_YuR/network/tcpquiclab/quic_perf_client.c
2025-12-25 15:14:14 +08:00

130 lines
4.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <quiche.h>
#define MAX_DATAGRAM_SIZE 1350
#define TARGET_MB 100
// void debug_log(const char *line, void *argp) {
// fprintf(stderr, "%s\n", line);
// }
int main(int argc, char *argv[]) {
// quiche_enable_debug_logging(debug_log, NULL);
quiche_config *config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
if (config == NULL) return -1;
quiche_config_verify_peer(config, false);
quiche_config_set_application_protos(config, (uint8_t *) "\x0ahq-interop\x05hq-29\x05hq-28\x05hq-27\x08http/0.9", 38);
quiche_config_set_max_idle_timeout(config, 10000);
quiche_config_set_max_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
quiche_config_set_max_send_udp_payload_size(config, MAX_DATAGRAM_SIZE);
quiche_config_set_initial_max_data(config, 1024 * 1024 * 200);
quiche_config_set_initial_max_stream_data_bidi_local(config, 1024 * 1024 * 200);
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1024 * 1024 * 200);
quiche_config_set_initial_max_streams_bidi(config, 100);
quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) return -1;
struct sockaddr_in peer_addr;
memset(&peer_addr, 0, sizeof(peer_addr));
peer_addr.sin_family = AF_INET;
peer_addr.sin_port = htons(8889);
inet_pton(AF_INET, "127.0.0.1", &peer_addr.sin_addr);
if (connect(sock, (struct sockaddr *)&peer_addr, sizeof(peer_addr)) < 0) return -1;
struct sockaddr_in local_addr;
socklen_t local_addr_len = sizeof(local_addr);
if (getsockname(sock, (struct sockaddr *)&local_addr, &local_addr_len) < 0) {
perror("getsockname");
return -1;
}
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
int rng = open("/dev/urandom", O_RDONLY);
if (rng >= 0) {
read(rng, scid, sizeof(scid));
close(rng);
}
quiche_conn *conn = quiche_connect("127.0.0.1", (const uint8_t *)scid, sizeof(scid), (struct sockaddr *)&local_addr, local_addr_len, (struct sockaddr *)&peer_addr, sizeof(peer_addr), config);
if (conn == NULL) {
fprintf(stderr, "quiche_connect failed\n");
return -1;
}
printf("Connecting to QUIC Perf Server and sending %d MB...\n", TARGET_MB);
uint8_t buf[65535];
uint8_t out[MAX_DATAGRAM_SIZE];
uint8_t payload[4096];
memset(payload, 'A', sizeof(payload));
long long bytes_to_send = (long long)TARGET_MB * 1024 * 1024;
long long bytes_sent = 0;
bool finished_sending = false;
while (1) {
ssize_t read_len = recv(sock, buf, sizeof(buf), 0);
if (read_len > 0) {
quiche_conn_recv(conn, buf, read_len, &(quiche_recv_info){
.to = (struct sockaddr *)&local_addr,
.to_len = local_addr_len,
.from = (struct sockaddr *)&peer_addr,
.from_len = sizeof(peer_addr),
});
}
if (quiche_conn_is_closed(conn)) {
printf("Connection closed.\n");
break;
}
if (quiche_conn_is_established(conn)) {
// printf("Connection established.\n");
while (!finished_sending) {
uint64_t err_code = 0;
ssize_t sent = quiche_conn_stream_send(conn, 4, payload, sizeof(payload), false, &err_code);
if (sent > 0) {
bytes_sent += sent;
if (bytes_sent >= bytes_to_send) {
quiche_conn_stream_send(conn, 4, NULL, 0, true, &err_code);
finished_sending = true;
printf("Finished sending data.\n");
}
} else {
break;
}
}
}
while (1) {
quiche_send_info send_info;
ssize_t written = quiche_conn_send(conn, out, sizeof(out), &send_info);
if (written == QUICHE_ERR_DONE) break;
if (written < 0) break;
send(sock, out, written, 0);
}
quiche_conn_on_timeout(conn);
usleep(100);
}
quiche_conn_free(conn);
quiche_config_free(config);
return 0;
}