1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #include <sys/epoll.h>
#define MAX_EVENTS 1000 #define BUFFER_SIZE 8192
typedef struct connection { int fd; char *read_buffer; char *write_buffer; size_t read_pos; size_t write_pos; size_t write_len; time_t last_activity; } connection_t;
typedef struct epoll_server { int listen_fd; int epoll_fd; struct epoll_event *events; connection_t *connections; size_t max_connections; void (*on_accept)(struct epoll_server *server, int client_fd); void (*on_read)(struct epoll_server *server, connection_t *conn); void (*on_write)(struct epoll_server *server, connection_t *conn); void (*on_error)(struct epoll_server *server, connection_t *conn); } epoll_server_t;
epoll_server_t* epoll_server_create(int port, size_t max_connections) { epoll_server_t *server = calloc(1, sizeof(epoll_server_t)); if (!server) return NULL; server->max_connections = max_connections; server->listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (server->listen_fd < 0) { free(server); return NULL; } set_reuseaddr(server->listen_fd); set_nonblocking(server->listen_fd); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if (bind(server->listen_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0 || listen(server->listen_fd, SOMAXCONN) < 0) { close(server->listen_fd); free(server); return NULL; } server->epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (server->epoll_fd < 0) { close(server->listen_fd); free(server); return NULL; } server->events = calloc(MAX_EVENTS, sizeof(struct epoll_event)); server->connections = calloc(max_connections, sizeof(connection_t)); if (!server->events || !server->connections) { epoll_server_destroy(server); return NULL; } struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = server->listen_fd; if (epoll_ctl(server->epoll_fd, EPOLL_CTL_ADD, server->listen_fd, &ev) < 0) { epoll_server_destroy(server); return NULL; } return server; }
static int add_connection(epoll_server_t *server, int client_fd) { for (size_t i = 0; i < server->max_connections; i++) { if (server->connections[i].fd == 0) { connection_t *conn = &server->connections[i]; conn->fd = client_fd; conn->read_buffer = malloc(BUFFER_SIZE); conn->write_buffer = malloc(BUFFER_SIZE); conn->read_pos = 0; conn->write_pos = 0; conn->write_len = 0; conn->last_activity = time(NULL); if (!conn->read_buffer || !conn->write_buffer) { return -1; } struct epoll_event ev; ev.events = EPOLLIN | EPOLLET; ev.data.ptr = conn; if (epoll_ctl(server->epoll_fd, EPOLL_CTL_ADD, client_fd, &ev) < 0) { free(conn->read_buffer); free(conn->write_buffer); conn->fd = 0; return -1; } return i; } } return -1; }
int epoll_server_run(epoll_server_t *server) { if (!server) return -1; while (1) { int nfds = epoll_wait(server->epoll_fd, server->events, MAX_EVENTS, 1000); if (nfds < 0) { if (errno == EINTR) continue; return -1; } for (int i = 0; i < nfds; i++) { struct epoll_event *ev = &server->events[i]; if (ev->data.fd == server->listen_fd) { while (1) { struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); int client_fd = accept(server->listen_fd, (struct sockaddr*)&client_addr, &addr_len); if (client_fd < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) break; continue; } set_nonblocking(client_fd); if (add_connection(server, client_fd) >= 0) { if (server->on_accept) { server->on_accept(server, client_fd); } } else { close(client_fd); } } } else { connection_t *conn = (connection_t*)ev->data.ptr; if (ev->events & (EPOLLERR | EPOLLHUP)) { if (server->on_error) { server->on_error(server, conn); } epoll_ctl(server->epoll_fd, EPOLL_CTL_DEL, conn->fd, NULL); close(conn->fd); free(conn->read_buffer); free(conn->write_buffer); conn->fd = 0; } else if (ev->events & EPOLLIN) { while (1) { ssize_t bytes_read = read(conn->fd, conn->read_buffer + conn->read_pos, BUFFER_SIZE - conn->read_pos - 1); if (bytes_read > 0) { conn->read_pos += bytes_read; conn->last_activity = time(NULL); conn->read_buffer[conn->read_pos] = '\0'; if (server->on_read) { server->on_read(server, conn); } conn->read_pos = 0; } else if (bytes_read == 0) { break; } else { if (errno == EAGAIN || errno == EWOULDBLOCK) break; break; } } } } } } return 0; }
int epoll_server_send(epoll_server_t *server, connection_t *conn, const char *data, size_t len) { if (!server || !conn || !data || len == 0) return -1; if (len > BUFFER_SIZE) len = BUFFER_SIZE; memcpy(conn->write_buffer, data, len); conn->write_len = len; conn->write_pos = 0; ssize_t sent = write(conn->fd, data, len); if (sent == len) { return 0; } else if (sent > 0) { conn->write_pos = sent; } struct epoll_event ev; ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.ptr = conn; return epoll_ctl(server->epoll_fd, EPOLL_CTL_MOD, conn->fd, &ev); }
|