comparison seobeo/s_linux_network.c @ 6:1e61008b9980

[Seobeo] Updated seobeo so that API is more opinionated. Added my webpage./build.sh
author June Park <parkjune1995@gmail.com>
date Mon, 29 Sep 2025 16:00:44 -0700
parents 0b3b4f5887bb
children fb2cff495a60
comparison
equal deleted inserted replaced
5:3e12bf044589 6:1e61008b9980
108 p_handle->file_name = NULL; 108 p_handle->file_name = NULL;
109 109
110 return p_handle; 110 return p_handle;
111 } 111 }
112 112
113 Seobeo_PHandle Seobeo_Stream_Handle_Accept(Seobeo_PHandle server_h) 113 Seobeo_PHandle Seobeo_Stream_Handle_Accept(Seobeo_PHandle p_server_handle)
114 { 114 {
115 struct sockaddr_storage addr; 115 struct sockaddr_storage addr;
116 socklen_t addrlen = sizeof addr; 116 socklen_t addrlen = sizeof addr;
117 char client_inet_addr[INET6_ADDRSTRLEN]; 117 char client_inet_addr[INET6_ADDRSTRLEN];
118 int client_fd = accept(server_h->socket, 118 int client_fd = accept(p_server_handle->socket,
119 (struct sockaddr*)&addr, 119 (struct sockaddr*)&addr,
120 &addrlen); 120 &addrlen);
121 inet_ntop( 121 inet_ntop(
122 addr.ss_family, 122 addr.ss_family,
123 Seobeo_GetIP4OrIP6((struct sockaddr *)&addr), 123 Seobeo_GetIP4OrIP6((struct sockaddr *)&addr),
126 if (client_fd == -1) 126 if (client_fd == -1)
127 { 127 {
128 return NULL; 128 return NULL;
129 } 129 }
130 130
131 Seobeo_PHandle h = malloc(sizeof *h); 131 Seobeo_PHandle p_client_handle = malloc(sizeof *p_client_handle);
132 132
133 h->socket = client_fd; 133 p_client_handle->socket = client_fd;
134 h->host = strdup(client_inet_addr); 134 p_client_handle->host = strdup(client_inet_addr);
135 h->port = NULL; 135 p_client_handle->port = NULL;
136 136
137 h->read_buffer_capacity = server_h->read_buffer_capacity; 137 p_client_handle->read_buffer_capacity = p_server_handle->read_buffer_capacity;
138 h->read_buffer_len = 0; 138 p_client_handle->read_buffer_len = 0;
139 h->read_buffer = malloc(h->read_buffer_capacity); 139 p_client_handle->read_buffer = malloc(p_client_handle->read_buffer_capacity);
140 140
141 h->write_buffer_capacity = server_h->write_buffer_capacity; 141 p_client_handle->write_buffer_capacity = p_server_handle->write_buffer_capacity;
142 h->write_buffer_len = 0; 142 p_client_handle->write_buffer_len = 0;
143 h->write_buffer = malloc(h->write_buffer_capacity); 143 p_client_handle->write_buffer = malloc(p_client_handle->write_buffer_capacity);
144 144
145 return h; 145 return p_client_handle;
146 } 146 }
147 147
148 void Seobeo_Handle_Destroy(Seobeo_PHandle h) 148 void Seobeo_Handle_Destroy(Seobeo_PHandle p_handle)
149 { 149 {
150 close(h->socket); 150 close(p_handle->socket);
151 free(h->host); 151 free(p_handle->host);
152 free(h->port); 152 free(p_handle->port);
153 free(h->read_buffer); 153 free(p_handle->read_buffer);
154 free(h->write_buffer); 154 free(p_handle->write_buffer);
155 free(h->file); 155 free(p_handle->file);
156 free(h->text_copy); 156 free(p_handle->text_copy);
157 free(h->file_name); 157 free(p_handle->file_name);
158 free(h); 158 free(p_handle);
159 } 159 }
160 160
161 int Seobeo_Handle_Flush(Seobeo_PHandle h) 161 int Seobeo_Handle_Flush(Seobeo_PHandle p_handle)
162 { 162 {
163 uint32 total = h->write_buffer_len; 163 uint32 total = p_handle->write_buffer_len;
164 uint32 sent = 0; 164 uint32 sent = 0;
165 165
166 while (sent < total) { 166 while (sent < total)
167 {
167 ssize_t n = write( 168 ssize_t n = write(
168 h->socket, 169 p_handle->socket,
169 h->write_buffer + sent, 170 p_handle->write_buffer + sent,
170 total - sent 171 total - sent
171 ); 172 );
172 if (n < 0) { 173 if (n < 0) {
173 if (errno == EINTR) continue; 174 if (errno == EINTR) continue;
174 if (errno == EAGAIN) return 1; 175 if (errno == EAGAIN) return 1;
175 return -1; 176 return -1;
176 } 177 }
177 sent += (uint32)n; 178 sent += (uint32)n;
178 } 179 }
179 180
180 h->write_buffer_len = 0; 181 p_handle->write_buffer_len = 0;
181 return 0; 182 return 0;
182 } 183 }
183 184
184 185 int Seobeo_Handle_Queue(Seobeo_PHandle p_handle, const uint8_t *data, uint32_t data_size)
185 int Seobeo_Handle_QueueData(Seobeo_PHandle h, const uint8_t *data, uint32_t data_size) 186 {
186 { 187 if (p_handle->write_buffer_len + data_size > p_handle->write_buffer_capacity)
187 if (h->write_buffer_len + data_size > h->write_buffer_capacity) { 188 {
188 int rc = Seobeo_Handle_Flush(h); 189 int rc = Seobeo_Handle_Flush(p_handle);
189 if (rc < 0) return -1; 190 if (rc < 0) return -1;
190 if (rc > 0) return 1; 191 if (rc > 0) return 1;
191 } 192 }
192 193
193 if (data_size > h->write_buffer_capacity) 194 if (data_size > p_handle->write_buffer_capacity)
194 { 195 {
195 uint32_t offset = 0; 196 uint32_t offset = 0;
196 while (offset < data_size) 197 while (offset < data_size)
197 { 198 {
198 ssize_t n = write(h->socket, 199 ssize_t n = write(p_handle->socket,
199 data + offset, 200 data + offset,
200 data_size - offset); 201 data_size - offset);
201 if (n < 0) { 202 if (n < 0)
203 {
202 if (errno == EINTR) continue; 204 if (errno == EINTR) continue;
203 if (errno == EAGAIN) return 1; 205 if (errno == EAGAIN) return 1;
204 return -1; 206 return -1;
205 } 207 }
206 offset += (uint32_t)n; 208 offset += (uint32_t)n;
207 } 209 }
208 return 0; 210 return 0;
209 } 211 }
210 212
211 memcpy(h->write_buffer + h->write_buffer_len, 213 memcpy(p_handle->write_buffer + p_handle->write_buffer_len,
212 data, 214 data,
213 data_size); 215 data_size);
214 h->write_buffer_len += data_size; 216 p_handle->write_buffer_len += data_size;
215 return 0; 217 return 0;
216 } 218 }
217 219
220 int Seobeo_Handle_Read(Seobeo_PHandle p_handle)
221 {
222 // How many bytes we can still read into the buffer
223 uint32 free_space = p_handle->read_buffer_capacity - p_handle->read_buffer_len;
224 if (free_space == 0)
225 return -1;
226
227 ssize_t n = read(
228 p_handle->socket,
229 p_handle->read_buffer + p_handle->read_buffer_len,
230 free_space
231 );
232 if (n < 0) {
233 if (errno == EINTR) return Seobeo_Handle_Read(p_handle);
234 if (errno == EAGAIN) return 0; // no data available right now
235 return -1; // fatal error
236 }
237 if (n == 0) {
238 return -2; // peer closed the connection
239 }
240
241 p_handle->read_buffer_len += (uint32)n;
242 return (int)n; // number of bytes read
243 }
244
245 void Seobeo_Handle_Consume(Seobeo_PHandle p_handle, uint32 consumed)
246 {
247 if (consumed >= p_handle->read_buffer_len)
248 {
249 p_handle->read_buffer_len = 0;
250 return;
251 }
252
253 // Slide remaining bytes to the front
254 memmove(
255 p_handle->read_buffer,
256 p_handle->read_buffer + consumed,
257 p_handle->read_buffer_len - consumed
258 );
259 p_handle->read_buffer_len -= consumed;
260 }
261
218 void *Seobeo_GetIP4OrIP6(struct sockaddr *sa) 262 void *Seobeo_GetIP4OrIP6(struct sockaddr *sa)
219 { 263 {
220 if (sa->sa_family == AF_INET) 264 if (sa->sa_family == AF_INET)
221 { 265 {
222 return &(((struct sockaddr_in*)sa)->sin_addr); 266 return &(((struct sockaddr_in*)sa)->sin_addr);