Mercurial
comparison seobeo/os/s_linux_edge.c @ 71:75de5903355c
Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sun, 28 Dec 2025 20:34:22 -0800 |
| parents | a0f0ad5e42eb |
| children | 48f260576059 |
comparison
equal
deleted
inserted
replaced
| 70:4bc56e88e1f3 | 71:75de5903355c |
|---|---|
| 68 } | 68 } |
| 69 | 69 |
| 70 void Seobeo_Web_Edge( | 70 void Seobeo_Web_Edge( |
| 71 Seobeo_Handle *p_server_handle, | 71 Seobeo_Handle *p_server_handle, |
| 72 int thread_count, | 72 int thread_count, |
| 73 Dowa_HashMap *p_html_cache) | 73 Seobeo_Cache_Entry *p_html_cache) |
| 74 { | 74 { |
| 75 pthread_attr_t attr; | 75 pthread_attr_t attr; |
| 76 pthread_attr_init(&attr); | 76 pthread_attr_init(&attr); |
| 77 pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB | 77 pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB |
| 78 | 78 |
| 89 pthread_join(threads[i], NULL); | 89 pthread_join(threads[i], NULL); |
| 90 } | 90 } |
| 91 | 91 |
| 92 pthread_attr_destroy(&attr); | 92 pthread_attr_destroy(&attr); |
| 93 } | 93 } |
| 94 | |
| 95 | |
| 96 void Seobeo_Web_Edge_2(Seobeo_Handle *p_handle_server, Dowa_HashMap *cache) | |
| 97 { | |
| 98 const int MAX_EVENTS = 1024; | |
| 99 struct epoll_event events[MAX_EVENTS]; | |
| 100 char keybuf[32]; | |
| 101 | |
| 102 int epfd = epoll_create1(0); | |
| 103 if (epfd < 0) | |
| 104 { | |
| 105 perror("epoll_create1"); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 struct epoll_event ev = { | |
| 110 .events = EPOLLIN | EPOLLET, | |
| 111 .data.fd = p_handle_server->socket | |
| 112 }; | |
| 113 if (epoll_ctl(epfd, EPOLL_CTL_ADD, p_handle_server->socket, &ev) < 0) | |
| 114 { | |
| 115 perror("epoll_ctl ADD server"); | |
| 116 close(epfd); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 Dowa_HashMap *handles = Dowa_HashMap_Create(1024); | |
| 121 snprintf(keybuf, sizeof(keybuf), "%d", p_handle_server->socket); | |
| 122 Dowa_HashMap_Push_Value(handles, keybuf, p_handle_server, sizeof(p_handle_server)); | |
| 123 | |
| 124 while (1) { | |
| 125 int n = epoll_wait(epfd, events, MAX_EVENTS, -1); | |
| 126 if (n < 0) | |
| 127 { | |
| 128 if (errno == EINTR) continue; | |
| 129 perror("epoll_wait"); | |
| 130 break; | |
| 131 } | |
| 132 | |
| 133 for (int i = 0; i < n; i++) | |
| 134 { | |
| 135 int fd = events[i].data.fd; | |
| 136 | |
| 137 if (fd == p_handle_server->socket) | |
| 138 { | |
| 139 while (1) | |
| 140 { | |
| 141 Seobeo_Handle *p_handle_client = Seobeo_Stream_Handle_Server_Accept(p_handle_server); | |
| 142 if (!p_handle_client) break; | |
| 143 | |
| 144 struct epoll_event client_ev = { | |
| 145 .events = EPOLLIN | EPOLLET, | |
| 146 .data.fd = p_handle_client->socket | |
| 147 }; | |
| 148 if (epoll_ctl(epfd, EPOLL_CTL_ADD, p_handle_client->socket, &client_ev) < 0) | |
| 149 { | |
| 150 perror("epoll_ctl ADD client"); | |
| 151 Seobeo_Handle_Destroy(p_handle_client); | |
| 152 continue; | |
| 153 } | |
| 154 | |
| 155 snprintf(keybuf, sizeof(keybuf), "%d", p_handle_client->socket); | |
| 156 if (p_handle_client) | |
| 157 Dowa_HashMap_Push_Value_With_Type_NoCopy(handles, keybuf, p_handle_client, | |
| 158 sizeof(p_handle_client), DOWA_HASH_MAP_TYPE_HASHMAP); | |
| 159 } | |
| 160 continue; | |
| 161 } | |
| 162 | |
| 163 snprintf(keybuf, sizeof(keybuf), "%d", fd); | |
| 164 Seobeo_Handle *p_handle_client = Dowa_HashMap_Get(handles, keybuf); | |
| 165 if (!p_handle_client) | |
| 166 { | |
| 167 // might happen if client closed between event and lookup | |
| 168 epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); | |
| 169 continue; | |
| 170 } | |
| 171 | |
| 172 // Remove from epoll | |
| 173 epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); | |
| 174 | |
| 175 // Handle request (this function destroys the handle internally) | |
| 176 Seobeo_Web_HandleClientRequest(p_handle_client, cache); | |
| 177 | |
| 178 // Remove from hashmap (handle is already destroyed by HandleClientRequest) | |
| 179 Dowa_HashMap_Pop_Key(handles, keybuf); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 close(epfd); | |
| 184 Dowa_HashMap_Destroy(handles); | |
| 185 } | |
| 186 |