Mercurial
comparison seobeo/s_linux_network.c @ 17:d97ec3ded2ae
[Seobeo] Few changes...
- Fixed seobeo edge for macos
- Updated so that socket creation can be used for both client and server
- Started on a cutelient library for making connection to the server.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 04 Oct 2025 07:53:12 -0700 |
| parents | fb2cff495a60 |
| children | fa2b8af609d9 |
comparison
equal
deleted
inserted
replaced
| 16:fb2cff495a60 | 17:d97ec3ded2ae |
|---|---|
| 1 #include "seobeo/seobeo.h" | 1 #include "seobeo/seobeo.h" |
| 2 | 2 |
| 3 int Seobeo_CreateSocket(int32 stream, const char *host, const char* port, int32 backlog) | 3 int Seobeo_CreateSocket(int32 stream, const char *host, const char* port) |
| 4 { | 4 { |
| 5 struct addrinfo hints = {0}, *server_infos, *free_server_info; | 5 struct addrinfo hints = {0}, *server_infos, *free_server_info; |
| 6 int32 sock_fd, yes = 1; // Need this for setsockopt | 6 int32 sock_fd, yes = 1; // Need this for setsockopt |
| 7 | 7 |
| 8 hints.ai_family = AF_INET; | 8 hints.ai_family = AF_INET; |
| 39 { | 39 { |
| 40 perror("setsockopt"); | 40 perror("setsockopt"); |
| 41 continue; | 41 continue; |
| 42 } | 42 } |
| 43 | 43 |
| 44 if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) | 44 if (host != NULL) |
| 45 { | 45 { |
| 46 close(sock_fd); | 46 if (connect(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) |
| 47 perror("setsockopt"); | 47 { |
| 48 continue; | 48 perror("connect"); |
| 49 continue; | |
| 50 } | |
| 51 | |
| 49 } | 52 } |
| 50 | 53 else |
| 51 // UDP should be non blocking | |
| 52 if(!stream) | |
| 53 { | 54 { |
| 54 if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0) | 55 if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) |
| 55 { | 56 { |
| 56 close(sock_fd); | |
| 57 perror("v_network: Couldn't make socket non-blocking\n"); | 57 perror("v_network: Couldn't make socket non-blocking\n"); |
| 58 return -1; | 58 continue; |
| 59 } | |
| 60 | |
| 61 // UDP should be non blocking | |
| 62 if(!stream) | |
| 63 { | |
| 64 if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) != 0) | |
| 65 { | |
| 66 close(sock_fd); | |
| 67 perror("v_network: Couldn't make socket non-blocking\n"); | |
| 68 return -1; | |
| 69 } | |
| 59 } | 70 } |
| 60 } | 71 } |
| 61 | |
| 62 // binded to a open server infos; | 72 // binded to a open server infos; |
| 63 break; | 73 break; |
| 64 } | 74 } |
| 65 | 75 |
| 66 // No longer need these values | 76 // No longer need these values |
| 70 { | 80 { |
| 71 perror("No free server"); | 81 perror("No free server"); |
| 72 return -1; | 82 return -1; |
| 73 } | 83 } |
| 74 | 84 |
| 75 // TCP listen | 85 if (host == NULL) |
| 76 if(stream) | 86 { |
| 77 { | 87 if (listen(sock_fd, 16) != 0) |
| 78 if (listen(sock_fd, backlog) != 0) | |
| 79 { | 88 { |
| 80 perror("listen"); | 89 perror("listen"); |
| 81 close(sock_fd); | 90 close(sock_fd); |
| 82 return -1; | 91 return -1; |
| 83 } | 92 } |
| 89 Seobeo_PHandle Seobeo_Stream_Handle_Create(const char *host, const char* port) | 98 Seobeo_PHandle Seobeo_Stream_Handle_Create(const char *host, const char* port) |
| 90 { | 99 { |
| 91 Seobeo_PHandle p_handle; | 100 Seobeo_PHandle p_handle; |
| 92 p_handle = malloc(sizeof(*p_handle)); | 101 p_handle = malloc(sizeof(*p_handle)); |
| 93 | 102 |
| 94 p_handle->socket = Seobeo_CreateSocket(1, host, port, 10); // socke fd | 103 p_handle->socket = Seobeo_CreateSocket(1, host, port); // socke fd |
| 104 if (!p_handle->socket) | |
| 105 { | |
| 106 perror("Seobeo_CreateSocket"); | |
| 107 } | |
| 95 p_handle->host = host != NULL ? strdup(host) : "localhost"; | 108 p_handle->host = host != NULL ? strdup(host) : "localhost"; |
| 96 p_handle->port = strdup(port); | 109 p_handle->port = strdup(port); |
| 97 | 110 |
| 98 p_handle->read_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); | 111 p_handle->read_buffer = malloc(sizeof(*p_handle->read_buffer) * INITIAL_BUFFER_CAPACITY); |
| 99 p_handle->read_buffer_capacity = INITIAL_BUFFER_CAPACITY; | 112 p_handle->read_buffer_capacity = INITIAL_BUFFER_CAPACITY; |
| 190 | 203 |
| 191 p_handle->write_buffer_len = 0; | 204 p_handle->write_buffer_len = 0; |
| 192 return 0; | 205 return 0; |
| 193 } | 206 } |
| 194 | 207 |
| 195 int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size) | 208 int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8 *data, uint32 data_size) |
| 196 { | 209 { |
| 197 if (p_handle->write_buffer_len + data_size > p_handle->write_buffer_capacity) | 210 if (p_handle->write_buffer_len + data_size > p_handle->write_buffer_capacity) |
| 198 { | 211 { |
| 199 int rc = Seobeo_Handle_Flush(p_handle); | 212 int rc = Seobeo_Handle_Flush(p_handle); |
| 200 if (rc < 0) return -1; | 213 if (rc < 0) return -1; |
| 201 if (rc > 0) return 1; | 214 if (rc > 0) return 1; |
| 202 } | 215 } |
| 203 | 216 |
| 204 if (data_size > p_handle->write_buffer_capacity) | 217 if (data_size > p_handle->write_buffer_capacity) |
| 205 { | 218 { |
| 206 uint32_t offset = 0; | 219 uint32 offset = 0; |
| 207 while (offset < data_size) | 220 while (offset < data_size) |
| 208 { | 221 { |
| 209 ssize_t n = write(p_handle->socket, | 222 ssize_t n = write(p_handle->socket, |
| 210 data + offset, | 223 data + offset, |
| 211 data_size - offset); | 224 data_size - offset); |
| 213 { | 226 { |
| 214 if (errno == EINTR) continue; | 227 if (errno == EINTR) continue; |
| 215 if (errno == EAGAIN) return 1; | 228 if (errno == EAGAIN) return 1; |
| 216 return -1; | 229 return -1; |
| 217 } | 230 } |
| 218 offset += (uint32_t)n; | 231 offset += (uint32)n; |
| 219 } | 232 } |
| 220 return 0; | 233 return 0; |
| 221 } | 234 } |
| 222 | 235 |
| 223 memcpy(p_handle->write_buffer + p_handle->write_buffer_len, | 236 memcpy(p_handle->write_buffer + p_handle->write_buffer_len, |