Mercurial
comparison seobeo/s_linux_network.c @ 96:70401cf61e97
[Seobeo] Added logging.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Fri, 02 Jan 2026 19:16:17 -0800 |
| parents | 75de5903355c |
| children |
comparison
equal
deleted
inserted
replaced
| 95:b51f8cce9170 | 96:70401cf61e97 |
|---|---|
| 43 break; | 43 break; |
| 44 } | 44 } |
| 45 | 45 |
| 46 if (listen(socket_fd, 16) != 0) | 46 if (listen(socket_fd, 16) != 0) |
| 47 { | 47 { |
| 48 printf("Closing: %d\n", socket_fd); | 48 Seobeo_Log(SEOBEO_DEBUG, "Closing socket: %d\n", socket_fd); |
| 49 perror("listen"); close(socket_fd); return NULL; | 49 perror("listen"); close(socket_fd); return NULL; |
| 50 } | 50 } |
| 51 | 51 |
| 52 int flags = fcntl(socket_fd, F_GETFL, 0); | 52 int flags = fcntl(socket_fd, F_GETFL, 0); |
| 53 if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; } | 53 if(fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK) != 0) { perror("fcntl"); return NULL; } |
| 71 | 71 |
| 72 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); | 72 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
| 73 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; | 73 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
| 74 p_handle->write_buffer_len = 0; | 74 p_handle->write_buffer_len = 0; |
| 75 | 75 |
| 76 p_handle->destroyed = false; | 76 p_handle->destroyed = FALSE; |
| 77 | 77 |
| 78 return p_handle; | 78 return p_handle; |
| 79 } | 79 } |
| 80 | 80 |
| 81 | 81 |
| 115 { | 115 { |
| 116 free(p_handle); | 116 free(p_handle); |
| 117 return NULL; | 117 return NULL; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 p_handle->connected = true; | 120 p_handle->connected = TRUE; |
| 121 | 121 |
| 122 p_handle->host = host != NULL ? strdup(host) : "localhost"; | 122 p_handle->host = host != NULL ? strdup(host) : "localhost"; |
| 123 p_handle->port = strdup(port); | 123 p_handle->port = strdup(port); |
| 124 | 124 |
| 125 p_handle->read_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); | 125 p_handle->read_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
| 128 | 128 |
| 129 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); | 129 p_handle->write_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
| 130 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; | 130 p_handle->write_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
| 131 p_handle->write_buffer_len = 0; | 131 p_handle->write_buffer_len = 0; |
| 132 | 132 |
| 133 p_handle->destroyed = false; | 133 p_handle->destroyed = FALSE; |
| 134 | 134 |
| 135 return p_handle; | 135 return p_handle; |
| 136 } | 136 } |
| 137 | 137 |
| 138 Seobeo_Handle *Seobeo_Stream_Handle_Server_Accept(Seobeo_Handle *p_server_handle) | 138 Seobeo_Handle *Seobeo_Stream_Handle_Server_Accept(Seobeo_Handle *p_server_handle) |
| 157 | 157 |
| 158 Seobeo_Handle *p_client_handle = malloc(sizeof *p_client_handle); | 158 Seobeo_Handle *p_client_handle = malloc(sizeof *p_client_handle); |
| 159 | 159 |
| 160 p_client_handle->socket = client_fd; | 160 p_client_handle->socket = client_fd; |
| 161 p_client_handle->type = SEOBEO_STREAM_TYPE_CLIENT; | 161 p_client_handle->type = SEOBEO_STREAM_TYPE_CLIENT; |
| 162 p_client_handle->connected = true; | 162 p_client_handle->connected = TRUE; |
| 163 | 163 |
| 164 // TODO: support SSL in the future. | 164 // TODO: support SSL in the future. |
| 165 p_client_handle->ssl_ctx = NULL; | 165 p_client_handle->ssl_ctx = NULL; |
| 166 p_client_handle->ssl = NULL; | 166 p_client_handle->ssl = NULL; |
| 167 | 167 |
| 178 | 178 |
| 179 p_client_handle->read_buffer_used = 0; | 179 p_client_handle->read_buffer_used = 0; |
| 180 p_client_handle->file = NULL; | 180 p_client_handle->file = NULL; |
| 181 p_client_handle->text_copy = NULL; | 181 p_client_handle->text_copy = NULL; |
| 182 p_client_handle->file_name = NULL; | 182 p_client_handle->file_name = NULL; |
| 183 p_client_handle->destroyed = false; | 183 p_client_handle->destroyed = FALSE; |
| 184 | 184 |
| 185 return p_client_handle; | 185 return p_client_handle; |
| 186 } | 186 } |
| 187 | 187 |
| 188 void Seobeo_Handle_Destroy(Seobeo_Handle *p_handle) | 188 void Seobeo_Handle_Destroy(Seobeo_Handle *p_handle) |
| 189 { | 189 { |
| 190 if (!p_handle) return; | 190 if (!p_handle) return; |
| 191 | 191 |
| 192 bool expected = false; | 192 boolean expected = FALSE; |
| 193 // Need to check | 193 // Need to check |
| 194 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, true)) | 194 if (!atomic_compare_exchange_strong(&p_handle->destroyed, &expected, TRUE)) |
| 195 { | 195 { |
| 196 return; | 196 return; |
| 197 } | 197 } |
| 198 | 198 |
| 199 if (p_handle->host) Dowa_Free(p_handle->host); | 199 if (p_handle->host) Dowa_Free(p_handle->host); |
| 200 if (p_handle->port) Dowa_Free(p_handle->port); | 200 if (p_handle->port) Dowa_Free(p_handle->port); |
| 201 | 201 |
| 202 Seobeo_SSL_Cleanup(p_handle); | 202 Seobeo_SSL_Cleanup(p_handle); |
| 203 | 203 |
| 204 if (p_handle->socket) { | 204 if (p_handle->socket) { |
| 205 printf("Closing: %d\n", p_handle->socket); | 205 Seobeo_Log(SEOBEO_DEBUG, "Closing handle socket: %d\n", p_handle->socket); |
| 206 close(p_handle->socket); | 206 close(p_handle->socket); |
| 207 } | 207 } |
| 208 | 208 |
| 209 if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer); | 209 if (p_handle->read_buffer) Dowa_Free(p_handle->read_buffer); |
| 210 if (p_handle->write_buffer) Dowa_Free(p_handle->write_buffer); | 210 if (p_handle->write_buffer) Dowa_Free(p_handle->write_buffer); |
| 216 int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle) | 216 int32 Seobeo_Handle_Flush(Seobeo_Handle *p_handle) |
| 217 { | 217 { |
| 218 uint32 total = p_handle->write_buffer_len; | 218 uint32 total = p_handle->write_buffer_len; |
| 219 uint32 sent = 0; | 219 uint32 sent = 0; |
| 220 | 220 |
| 221 printf("Total: %d\n\n", p_handle->write_buffer_len); | 221 Seobeo_Log(SEOBEO_DEBUG, "Write buffer total: %d\n", p_handle->write_buffer_len); |
| 222 | 222 |
| 223 while (sent < total) | 223 while (sent < total) |
| 224 { | 224 { |
| 225 if (p_handle->ssl) | 225 if (p_handle->ssl) |
| 226 { | 226 { |
| 228 if (n < 0) return -1; | 228 if (n < 0) return -1; |
| 229 if (n == 0) return 0; // would block | 229 if (n == 0) return 0; // would block |
| 230 sent += (uint32)n; | 230 sent += (uint32)n; |
| 231 }else | 231 }else |
| 232 { | 232 { |
| 233 printf("socket: %d\n", p_handle->socket); | 233 Seobeo_Log(SEOBEO_DEBUG, "Flushing socket: %d\n", p_handle->socket); |
| 234 ssize_t n = write( | 234 ssize_t n = write( |
| 235 p_handle->socket, | 235 p_handle->socket, |
| 236 p_handle->write_buffer + sent, | 236 p_handle->write_buffer + sent, |
| 237 total - sent | 237 total - sent |
| 238 ); | 238 ); |
| 267 data + offset, | 267 data + offset, |
| 268 data_size - offset); | 268 data_size - offset); |
| 269 if (n==0) | 269 if (n==0) |
| 270 { | 270 { |
| 271 // DEBUG | 271 // DEBUG |
| 272 printf("NONE %d\n", offset); | 272 Seobeo_Log(SEOBEO_DEBUG, "Write offset: %d\n", offset); |
| 273 break; | 273 break; |
| 274 } | 274 } |
| 275 if (n < 0) | 275 if (n < 0) |
| 276 { | 276 { |
| 277 if (errno == EINTR || errno == EAGAIN) | 277 if (errno == EINTR || errno == EAGAIN) |
| 283 if (errno == EAGAIN) return 1; | 283 if (errno == EAGAIN) return 1; |
| 284 return -1; | 284 return -1; |
| 285 } | 285 } |
| 286 offset += (uint32)n; | 286 offset += (uint32)n; |
| 287 // DEBUG | 287 // DEBUG |
| 288 printf("\n\noffset: %d data_size: %d\n\n", offset, data_size); | 288 Seobeo_Log(SEOBEO_DEBUG, "Write completed - offset: %d, data_size: %d\n", offset, data_size); |
| 289 } | 289 } |
| 290 // DEBUG | 290 // DEBUG |
| 291 printf("\n\nTotal: %d\n", offset); | 291 Seobeo_Log(SEOBEO_DEBUG, "Total bytes written: %d\n", offset); |
| 292 return 0; | 292 return 0; |
| 293 } | 293 } |
| 294 | 294 |
| 295 if (!p_handle) | 295 if (!p_handle) |
| 296 { | 296 { |
| 297 printf("[ERROR] p_handle is NULL before memcpy\n"); | 297 Seobeo_Log(SEOBEO_ERROR, "p_handle is NULL before memcpy\n"); |
| 298 return -1; | 298 return -1; |
| 299 } | 299 } |
| 300 | 300 |
| 301 if (!p_handle->write_buffer) | 301 if (!p_handle->write_buffer) |
| 302 { | 302 { |
| 303 printf("[ERROR] p_handle->write_buffer is NULL (len=%zu, size=%zu)\n", | 303 Seobeo_Log(SEOBEO_ERROR, "p_handle->write_buffer is NULL (len=%u, size=%u)\n", |
| 304 p_handle->write_buffer_len, data_size); | 304 p_handle->write_buffer_len, data_size); |
| 305 return -1; | 305 return -1; |
| 306 } | 306 } |
| 307 | 307 |
| 308 printf("[DEBUG] memcpy -> dest=%p (write_buffer=%p + offset=%zu), src=%p, size=%zu\n", | 308 Seobeo_Log(SEOBEO_DEBUG, "memcpy -> dest=%p (write_buffer=%p + offset=%u), src=%p, size=%u\n", |
| 309 p_handle->write_buffer + p_handle->write_buffer_len, | 309 p_handle->write_buffer + p_handle->write_buffer_len, |
| 310 p_handle->write_buffer, | 310 p_handle->write_buffer, |
| 311 p_handle->write_buffer_len, | 311 p_handle->write_buffer_len, |
| 312 data, | 312 data, |
| 313 data_size); | 313 data_size); |