Mercurial
comparison seobeo/s_network.c @ 251:117c4d53c9a4
[ui] Add HTML-first Web Component system
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Tue, 04 Aug 2026 09:14:57 -0700 |
| parents | b8aa08503378 |
| children | 609d3c6aff4e |
comparison
equal
deleted
inserted
replaced
| 250:745fd127b2a1 | 251:117c4d53c9a4 |
|---|---|
| 39 Seobeo_Handle *Seobeo_Stream_Handle_Server_Create(const char *host, const char* port) | 39 Seobeo_Handle *Seobeo_Stream_Handle_Server_Create(const char *host, const char* port) |
| 40 { | 40 { |
| 41 pthread_once(&g_sigpipe_once, Seobeo_Process_Ignore_Sigpipe); | 41 pthread_once(&g_sigpipe_once, Seobeo_Process_Ignore_Sigpipe); |
| 42 Seobeo_Handle *p_handle; | 42 Seobeo_Handle *p_handle; |
| 43 struct addrinfo hints, *server_infos, *free_server_info; | 43 struct addrinfo hints, *server_infos, *free_server_info; |
| 44 int32 socket_fd, yes = 1; // Need this for setsockopt | 44 int32 socket_fd = -1; |
| 45 int32 yes = 1; | |
| 45 | 46 |
| 46 memset(&hints, 0, sizeof hints); | 47 memset(&hints, 0, sizeof hints); |
| 47 hints.ai_family = AF_UNSPEC; | 48 hints.ai_family = AF_UNSPEC; |
| 48 hints.ai_socktype = SOCK_STREAM; | 49 hints.ai_socktype = SOCK_STREAM; |
| 49 hints.ai_protocol = IPPROTO_TCP; | 50 hints.ai_protocol = IPPROTO_TCP; |
| 68 { perror("socket"); continue; } | 69 { perror("socket"); continue; } |
| 69 Seobeo_Socket_Set_Close_On_Exec(socket_fd); | 70 Seobeo_Socket_Set_Close_On_Exec(socket_fd); |
| 70 Seobeo_Socket_Disable_Sigpipe(socket_fd); | 71 Seobeo_Socket_Disable_Sigpipe(socket_fd); |
| 71 | 72 |
| 72 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) | 73 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) |
| 73 { perror("setsockopt SO_REUSEADDR"); continue; } | 74 { |
| 75 perror("setsockopt SO_REUSEADDR"); | |
| 76 close(socket_fd); | |
| 77 socket_fd = -1; | |
| 78 continue; | |
| 79 } | |
| 74 | 80 |
| 75 #ifdef SO_REUSEPORT | 81 #ifdef SO_REUSEPORT |
| 76 // SO_REUSEPORT allows multiple threads/processes to bind to the same port | 82 // SO_REUSEPORT allows multiple threads/processes to bind to the same port |
| 77 // The kernel will distribute incoming connections among them | 83 // The kernel will distribute incoming connections among them |
| 78 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) == -1) | 84 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) == -1) |
| 79 { perror("setsockopt SO_REUSEPORT"); continue; } | 85 { |
| 86 perror("setsockopt SO_REUSEPORT"); | |
| 87 close(socket_fd); | |
| 88 socket_fd = -1; | |
| 89 continue; | |
| 90 } | |
| 80 #endif | 91 #endif |
| 81 | 92 |
| 82 if (bind(socket_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) | 93 if (bind(socket_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) |
| 83 { perror("v_network: Couldn't make socket non-blocking\n"); continue; } | 94 { |
| 95 perror("bind"); | |
| 96 close(socket_fd); | |
| 97 socket_fd = -1; | |
| 98 continue; | |
| 99 } | |
| 84 | 100 |
| 85 break; | 101 break; |
| 86 } | 102 } |
| 103 freeaddrinfo(server_infos); | |
| 104 | |
| 105 if (socket_fd < 0) | |
| 106 return NULL; | |
| 87 | 107 |
| 88 if (listen(socket_fd, 16) != 0) | 108 if (listen(socket_fd, 16) != 0) |
| 89 { | 109 { |
| 90 Seobeo_Log(SEOBEO_DEBUG, "Closing socket: %d\n", socket_fd); | 110 Seobeo_Log(SEOBEO_DEBUG, "Closing socket: %d\n", socket_fd); |
| 91 perror("listen"); close(socket_fd); return NULL; | 111 perror("listen"); close(socket_fd); return NULL; |
| 92 } | 112 } |
| 93 | 113 |
| 94 int flags = fcntl(socket_fd, F_GETFL, 0); | 114 int flags = fcntl(socket_fd, F_GETFL, 0); |
| 95 if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; } | 115 if (flags < 0 || |
| 96 freeaddrinfo(server_infos); | 116 fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) |
| 97 | 117 { |
| 98 p_handle = malloc(sizeof(*p_handle)); | 118 perror("fcntl"); |
| 119 close(socket_fd); | |
| 120 return NULL; | |
| 121 } | |
| 122 | |
| 123 p_handle = calloc(1, sizeof(*p_handle)); | |
| 124 if (!p_handle) | |
| 125 { | |
| 126 close(socket_fd); | |
| 127 return NULL; | |
| 128 } | |
| 99 p_handle->socket = socket_fd; | 129 p_handle->socket = socket_fd; |
| 100 p_handle->type = SEOBEO_STREAM_TYPE_SERVER; | 130 p_handle->type = SEOBEO_STREAM_TYPE_SERVER; |
| 101 p_handle->connected = FALSE; | 131 p_handle->connected = FALSE; |
| 102 | 132 |
| 103 p_handle->host = host != NULL ? strdup(host) : "localhost"; | 133 p_handle->host = strdup(host != NULL ? host : "localhost"); |
| 104 p_handle->port = strdup(port); | 134 p_handle->port = strdup(port); |
| 105 | 135 |
| 106 p_handle->ssl_ctx = NULL; | 136 p_handle->ssl_ctx = NULL; |
| 107 p_handle->ssl = NULL; | 137 p_handle->ssl = NULL; |
| 108 | 138 |
| 114 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); | 144 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
| 115 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; | 145 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
| 116 p_handle->write_buffer_len = 0; | 146 p_handle->write_buffer_len = 0; |
| 117 | 147 |
| 118 p_handle->destroyed = FALSE; | 148 p_handle->destroyed = FALSE; |
| 149 | |
| 150 if (!p_handle->host || | |
| 151 !p_handle->port || | |
| 152 !p_handle->read_buffer || | |
| 153 !p_handle->write_buffer) | |
| 154 { | |
| 155 Seobeo_Handle_Destroy(p_handle); | |
| 156 return NULL; | |
| 157 } | |
| 119 | 158 |
| 120 return p_handle; | 159 return p_handle; |
| 121 } | 160 } |
| 122 | 161 |
| 123 | 162 |
| 256 if (p_handle->host) Dowa_Free(p_handle->host); | 295 if (p_handle->host) Dowa_Free(p_handle->host); |
| 257 if (p_handle->port) Dowa_Free(p_handle->port); | 296 if (p_handle->port) Dowa_Free(p_handle->port); |
| 258 | 297 |
| 259 Seobeo_SSL_Cleanup(p_handle); | 298 Seobeo_SSL_Cleanup(p_handle); |
| 260 | 299 |
| 261 if (p_handle->socket) { | 300 if (p_handle->socket >= 0) { |
| 262 Seobeo_Log(SEOBEO_DEBUG, "Closing handle socket: %d\n", p_handle->socket); | 301 Seobeo_Log(SEOBEO_DEBUG, "Closing handle socket: %d\n", p_handle->socket); |
| 263 close(p_handle->socket); | 302 close(p_handle->socket); |
| 264 } | 303 } |
| 265 | 304 |
| 266 if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer); | 305 if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer); |