tcp/quic lab and switch lab added
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <time.h>
|
||||
#include <quiche.h>
|
||||
|
||||
#define MAX_DATAGRAM_SIZE 1350
|
||||
@@ -78,7 +79,7 @@ int main(int argc, char *argv[]) {
|
||||
bool finished_sending = false;
|
||||
|
||||
while (1) {
|
||||
ssize_t read_len = recv(sock, buf, sizeof(buf), 0);
|
||||
ssize_t read_len = recv(sock, buf, sizeof(buf), MSG_DONTWAIT);
|
||||
if (read_len > 0) {
|
||||
quiche_conn_recv(conn, buf, read_len, &(quiche_recv_info){
|
||||
.to = (struct sockaddr *)&local_addr,
|
||||
@@ -87,12 +88,12 @@ int main(int argc, char *argv[]) {
|
||||
.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) {
|
||||
@@ -101,26 +102,36 @@ int main(int argc, char *argv[]) {
|
||||
if (sent > 0) {
|
||||
bytes_sent += sent;
|
||||
if (bytes_sent >= bytes_to_send) {
|
||||
quiche_conn_stream_send(conn, 4, NULL, 0, true, &err_code);
|
||||
quiche_conn_stream_send(conn, 4, NULL, 0, true, &err_code);
|
||||
finished_sending = true;
|
||||
printf("Finished sending data.\n");
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
} else if (sent == QUICHE_ERR_DONE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool has_outgoing = false;
|
||||
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);
|
||||
has_outgoing = true;
|
||||
}
|
||||
|
||||
|
||||
quiche_conn_on_timeout(conn);
|
||||
usleep(100);
|
||||
|
||||
// 使用更短的轮询间隔以提高响应速度
|
||||
if (!has_outgoing && !finished_sending) {
|
||||
usleep(100); // 100微秒轮询
|
||||
}
|
||||
|
||||
if (finished_sending && !has_outgoing) {
|
||||
usleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
quiche_conn_free(conn);
|
||||
|
||||
Reference in New Issue
Block a user