comparison seobeo/s_linux_network.c @ 36:84672efec192

[Zenbu] WIP fixing issues regarding to using edge only. I think there is a problem where socket closes before sending back the info.
author MrJuneJune <me@mrjunejune.com>
date Sun, 09 Nov 2025 06:25:16 -0800
parents c0f6c8c7829f
children ea9ef388ab97
comparison
equal deleted inserted replaced
35:33b1caf051cd 36:84672efec192
35 35
36 break; 36 break;
37 } 37 }
38 38
39 if (listen(socket_fd, 16) != 0) 39 if (listen(socket_fd, 16) != 0)
40 { perror("listen"); close(socket_fd); return NULL; } 40 {
41 41 printf("Closing: %d", socket_fd);
42 if(fcntl(socket_fd, F_SETFL, O_NONBLOCK) != 0) { perror("fcntl"); return NULL; } 42 perror("listen"); close(socket_fd); return NULL;
43 }
44
45 int flags = fcntl(socket_fd, F_GETFL, 0);
46 if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; }
43 freeaddrinfo(server_infos); 47 freeaddrinfo(server_infos);
44 48
45 p_handle = malloc(sizeof(*p_handle)); 49 p_handle = malloc(sizeof(*p_handle));
46 p_handle->socket = socket_fd; 50 p_handle->socket = socket_fd;
47 p_handle->type = SEOBEO_STREAM_TYPE_SERVER; 51 p_handle->type = SEOBEO_STREAM_TYPE_SERVER;
60 64
61 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); 65 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY);
62 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; 66 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
63 p_handle->write_buffer_len = 0; 67 p_handle->write_buffer_len = 0;
64 68
65 p_handle->file = NULL;
66 p_handle->text_copy = NULL;
67 p_handle->file_name = NULL;
68 p_handle->destroyed = false; 69 p_handle->destroyed = false;
69 70
70 return p_handle; 71 return p_handle;
71 } 72 }
72 73
133 134
134 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); 135 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY);
135 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; 136 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY;
136 p_handle->write_buffer_len = 0; 137 p_handle->write_buffer_len = 0;
137 138
138 p_handle->file = NULL;
139 p_handle->text_copy = NULL;
140 p_handle->file_name = NULL;
141 p_handle->destroyed = false; 139 p_handle->destroyed = false;
142 140
143 return p_handle; 141 return p_handle;
144 } 142 }
145 143
192 void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle) 190 void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle)
193 { 191 {
194 if (!p_handle) return; 192 if (!p_handle) return;
195 193
196 bool expected = false; 194 bool expected = false;
195 // Need to check
197 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, true)) 196 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, true))
198 { 197 {
199 // Already destroyed by another thread
200 return; 198 return;
201 } 199 }
202 200
203 if (p_handle->host) free(p_handle->host); 201 if (p_handle->host) Dowa_Free(p_handle->host);
204 if (p_handle->port) free(p_handle->port); 202 if (p_handle->port) Dowa_Free(p_handle->port);
205 203
206 if (p_handle->ssl) 204 if (p_handle->ssl)
207 { 205 {
208 SSL_shutdown(p_handle->ssl); 206 SSL_shutdown(p_handle->ssl);
209 SSL_free(p_handle->ssl); 207 SSL_free(p_handle->ssl);
214 { 212 {
215 SSL_CTX_free(p_handle->ssl_ctx); 213 SSL_CTX_free(p_handle->ssl_ctx);
216 p_handle->ssl_ctx = NULL; 214 p_handle->ssl_ctx = NULL;
217 } 215 }
218 216
219 if (p_handle->socket) close(p_handle->socket); 217 if (p_handle->socket) {
220 218 printf("Closing: %d", p_handle->socket);
221 if (p_handle->read_buffer) free(p_handle->read_buffer); 219 close(p_handle->socket);
222 if (p_handle->write_buffer) free(p_handle->write_buffer); 220 }
223 221
224 if (p_handle->text_copy) free(p_handle->text_copy); 222 if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer);
225 if (p_handle->file_name) free(p_handle->file_name); 223 if (p_handle->write_buffer) Dowa_Free(p_handle->write_buffer);
226 224
227 free(p_handle); 225 Dowa_Free(p_handle);
228 } 226 }
229 227
230 228
231 int32 Seobeo_Handle_Flush(Seobeo_PHandle p_handle) 229 int32 Seobeo_Handle_Flush(Seobeo_PHandle p_handle)
232 { 230 {
252 return -1; 250 return -1;
253 } 251 }
254 sent += (uint32)n; 252 sent += (uint32)n;
255 }else 253 }else
256 { 254 {
255 printf("socket: %d\n", p_handle->socket);
257 ssize_t n = write( 256 ssize_t n = write(
258 p_handle->socket, 257 p_handle->socket,
259 p_handle->write_buffer + sent, 258 p_handle->write_buffer + sent,
260 total - sent 259 total - sent
261 ); 260 );
313 // DEBUG 312 // DEBUG
314 printf("\n\nTotal: %d\n", offset); 313 printf("\n\nTotal: %d\n", offset);
315 return 0; 314 return 0;
316 } 315 }
317 316
317 if (!p_handle) {
318 printf("[ERROR] p_handle is NULL before memcpy\n");
319 return;
320 }
321
322 if (!p_handle->write_buffer) {
323 printf("[ERROR] p_handle->write_buffer is NULL (len=%zu, size=%zu)\n",
324 p_handle->write_buffer_len, data_size);
325 return;
326 }
327
328 printf("[DEBUG] memcpy -> dest=%p (write_buffer=%p + offset=%zu), src=%p, size=%zu\n",
329 p_handle->write_buffer + p_handle->write_buffer_len,
330 p_handle->write_buffer,
331 p_handle->write_buffer_len,
332 data,
333 data_size);
334
318 memcpy(p_handle->write_buffer + p_handle->write_buffer_len, 335 memcpy(p_handle->write_buffer + p_handle->write_buffer_len,
319 data, 336 data,
320 data_size); 337 data_size);
321 p_handle->write_buffer_len += data_size; 338 p_handle->write_buffer_len += data_size;
322 // DEBUG
323 printf("\nheader data_size: %d\n\n", data_size);
324 return 0; 339 return 0;
325 } 340 }
326 341
327 int32 Seobeo_Handle_Read(Seobeo_PHandle p_handle) 342 int32 Seobeo_Handle_Read(Seobeo_PHandle p_handle)
328 { 343 {