|
| 1 | +/* |
| 2 | + * F-Stack TCP client for issue #842 — ff_epoll version. |
| 3 | + * Uses ff_epoll (EPOLLOUT → EPOLLIN via MOD) instead of kqueue native API. |
| 4 | + */ |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <string.h> |
| 9 | +#include <strings.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <sys/socket.h> |
| 12 | +#include <sys/epoll.h> |
| 13 | +#include <sys/ioctl.h> |
| 14 | +#include <arpa/inet.h> |
| 15 | +#include <netdb.h> |
| 16 | +#include <errno.h> |
| 17 | +#include <unistd.h> |
| 18 | +#include <time.h> |
| 19 | + |
| 20 | +#include "ff_config.h" |
| 21 | +#include "ff_api.h" |
| 22 | +#include "ff_event.h" |
| 23 | +#include "ff_epoll.h" |
| 24 | + |
| 25 | +#define MAX_EVENTS 10 |
| 26 | +#define BUFFER_SIZE 4096 |
| 27 | + |
| 28 | +static int epfd; |
| 29 | +static int sockfd; |
| 30 | +static int request_sent = 0; |
| 31 | + |
| 32 | +static int64_t br = 0; |
| 33 | +static int64_t buffer_reads = 0; |
| 34 | +static int64_t last_latency = 0; |
| 35 | +static int64_t first_latency = 0; |
| 36 | +static struct timespec t0; |
| 37 | + |
| 38 | +static int64_t get_time_difference(const char *buffer, size_t bytes_read) |
| 39 | +{ |
| 40 | + if (bytes_read < 20) |
| 41 | + return -1; |
| 42 | + int64_t d = strtoll(buffer + (bytes_read - 20), NULL, 10); |
| 43 | + struct timespec ts; |
| 44 | + clock_gettime(CLOCK_REALTIME, &ts); |
| 45 | + int64_t now = (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; |
| 46 | + return now - d; |
| 47 | +} |
| 48 | + |
| 49 | +static int loop(void *arg) |
| 50 | +{ |
| 51 | + struct epoll_event events[MAX_EVENTS]; |
| 52 | + int n = ff_epoll_wait(epfd, events, MAX_EVENTS, 0); |
| 53 | + |
| 54 | + for (int i = 0; i < n; i++) { |
| 55 | + if ((events[i].events & EPOLLOUT) && !request_sent) { |
| 56 | + int sent = ff_send(sockfd, |
| 57 | + "GET / HTTP/1.1\r\nHost: 9.134.211.87\r\nConnection: keep-alive\r\n\r\n", 62, 0); |
| 58 | + if (sent > 0) { |
| 59 | + request_sent = 1; |
| 60 | + struct epoll_event ev; |
| 61 | + ev.events = EPOLLIN | EPOLLET; |
| 62 | + ev.data.fd = sockfd; |
| 63 | + ff_epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev); |
| 64 | + } |
| 65 | + } |
| 66 | + if ((events[i].events & EPOLLIN) && request_sent) { |
| 67 | + char buffer[BUFFER_SIZE]; |
| 68 | + int bytes_read; |
| 69 | + while ((bytes_read = ff_recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) { |
| 70 | + br += bytes_read; |
| 71 | + ++buffer_reads; |
| 72 | + buffer[bytes_read] = '\0'; |
| 73 | + last_latency = get_time_difference(buffer, bytes_read); |
| 74 | + if (first_latency < 10 || first_latency > 10000000000LL) |
| 75 | + first_latency = last_latency; |
| 76 | + } |
| 77 | + if (bytes_read == 0) { |
| 78 | + struct timespec t1; |
| 79 | + clock_gettime(CLOCK_MONOTONIC, &t1); |
| 80 | + double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9; |
| 81 | + float avg_bytes = buffer_reads > 0 ? (float)br / (float)buffer_reads : 0; |
| 82 | + printf("avg_bytes: %.2f\n", avg_bytes); |
| 83 | + printf("buffer_reads: %ld\n", buffer_reads); |
| 84 | + printf("bytes_read: %ld\n", br); |
| 85 | + printf("first_latency_ns: %ld\n", first_latency); |
| 86 | + printf("last_latency_ns: %ld\n", last_latency); |
| 87 | + printf("lat_diff_ns: %ld\n", last_latency - first_latency); |
| 88 | + printf("total_time_s: %.3f\n", elapsed); |
| 89 | + fflush(stdout); |
| 90 | + ff_close(sockfd); |
| 91 | + ff_close(epfd); |
| 92 | + exit(0); |
| 93 | + } else if (bytes_read == -1 && errno != EAGAIN) { |
| 94 | + struct timespec t1; |
| 95 | + clock_gettime(CLOCK_MONOTONIC, &t1); |
| 96 | + double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9; |
| 97 | + float avg_bytes = buffer_reads > 0 ? (float)br / (float)buffer_reads : 0; |
| 98 | + printf("avg_bytes: %.2f\n", avg_bytes); |
| 99 | + printf("buffer_reads: %ld\n", buffer_reads); |
| 100 | + printf("bytes_read: %ld\n", br); |
| 101 | + printf("first_latency_ns: %ld\n", first_latency); |
| 102 | + printf("last_latency_ns: %ld\n", last_latency); |
| 103 | + printf("lat_diff_ns: %ld\n", last_latency - first_latency); |
| 104 | + printf("total_time_s: %.3f\n", elapsed); |
| 105 | + printf("recv_error: %s\n", strerror(errno)); |
| 106 | + fflush(stdout); |
| 107 | + ff_close(sockfd); |
| 108 | + ff_close(epfd); |
| 109 | + exit(1); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +int main(int argc, char *argv[]) |
| 117 | +{ |
| 118 | + ff_init(argc, argv); |
| 119 | + |
| 120 | + clock_gettime(CLOCK_MONOTONIC, &t0); |
| 121 | + |
| 122 | + sockfd = ff_socket(AF_INET, SOCK_STREAM, 0); |
| 123 | + if (sockfd < 0) { perror("ff_socket"); exit(EXIT_FAILURE); } |
| 124 | + |
| 125 | + int opt = 1; |
| 126 | + ff_ioctl(sockfd, FIONBIO, &opt); |
| 127 | + |
| 128 | + epfd = ff_epoll_create(10); |
| 129 | + |
| 130 | + struct epoll_event ev; |
| 131 | + ev.events = EPOLLOUT | EPOLLET; |
| 132 | + ev.data.fd = sockfd; |
| 133 | + ff_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev); |
| 134 | + |
| 135 | + struct addrinfo hints, *res; |
| 136 | + memset(&hints, 0, sizeof(hints)); |
| 137 | + hints.ai_family = AF_INET; |
| 138 | + hints.ai_socktype = SOCK_STREAM; |
| 139 | + if (getaddrinfo("9.134.211.87", "12373", &hints, &res) != 0) { |
| 140 | + perror("getaddrinfo"); exit(EXIT_FAILURE); |
| 141 | + } |
| 142 | + |
| 143 | + int rc = ff_connect(sockfd, (struct linux_sockaddr *)res->ai_addr, res->ai_addrlen); |
| 144 | + if (rc == -1 && errno != EINPROGRESS) { |
| 145 | + perror("ff_connect"); exit(EXIT_FAILURE); |
| 146 | + } |
| 147 | + freeaddrinfo(res); |
| 148 | + |
| 149 | + ff_run(loop, NULL); |
| 150 | + return 0; |
| 151 | +} |
0 commit comments