|
13 | 13 | */
|
14 | 14 | #include "fossil/io/network.h"
|
15 | 15 | #include <stdio.h>
|
| 16 | +#include <stdlib.h> |
16 | 17 | #include <string.h>
|
17 | 18 | #include <sys/select.h> // For fd_set and select
|
18 | 19 | #include <sys/time.h> // For struct timeval
|
@@ -143,77 +144,6 @@ void fossil_nstream_close(fossil_nstream_t *ns) {
|
143 | 144 | free(ns);
|
144 | 145 | }
|
145 | 146 |
|
146 |
| -// Set socket to non-blocking mode |
147 |
| -int fossil_nstream_set_nonblocking(fossil_nstream_t *ns, int enable) { |
148 |
| -#ifdef _WIN32 |
149 |
| - u_long mode = enable ? 1 : 0; |
150 |
| - return ioctlsocket(ns->socket, FIONBIO, &mode); |
151 |
| -#else |
152 |
| - int flags = fcntl(ns->socket, F_GETFL, 0); |
153 |
| - if (flags == -1) { |
154 |
| - return -1; |
155 |
| - } |
156 |
| - |
157 |
| - if (enable) { |
158 |
| - flags |= O_NONBLOCK; |
159 |
| - } else { |
160 |
| - flags &= ~O_NONBLOCK; |
161 |
| - } |
162 |
| - |
163 |
| - return fcntl(ns->socket, F_SETFL, flags); |
164 |
| -#endif |
165 |
| -} |
166 |
| - |
167 |
| -// Send a line of text (appends \r\n) |
168 |
| -ssize_t fossil_nstream_send_line(fossil_nstream_t *ns, const char *line) { |
169 |
| - size_t len = strlen(line); |
170 |
| - char *buffer = malloc(len + 3); |
171 |
| - if (!buffer) return -1; |
172 |
| - |
173 |
| - strcpy(buffer, line); |
174 |
| - buffer[len] = '\r'; |
175 |
| - buffer[len + 1] = '\n'; |
176 |
| - buffer[len + 2] = '\0'; |
177 |
| - |
178 |
| - ssize_t result = fossil_nstream_send(ns, buffer, len + 2); |
179 |
| - free(buffer); |
180 |
| - return result; |
181 |
| -} |
182 |
| - |
183 |
| -// Receive a line (stops at \r\n or max_len) |
184 |
| -ssize_t fossil_nstream_recv_line(fossil_nstream_t *ns, char *buf, size_t max_len) { |
185 |
| - ssize_t total_received = 0; |
186 |
| - char c; |
187 |
| - size_t i = 0; |
188 |
| - |
189 |
| - while (i < max_len - 1) { |
190 |
| - ssize_t result = fossil_nstream_recv(ns, &c, 1); |
191 |
| - if (result <= 0) { |
192 |
| -#ifdef _WIN32 |
193 |
| - if (WSAGetLastError() == WSAEWOULDBLOCK) { |
194 |
| - continue; // Non-blocking mode, retry |
195 |
| - } |
196 |
| -#else |
197 |
| - if (errno == EAGAIN || errno == EWOULDBLOCK) { |
198 |
| - continue; // Non-blocking mode, retry |
199 |
| - } |
200 |
| -#endif |
201 |
| - return result; |
202 |
| - } |
203 |
| - |
204 |
| - buf[i++] = c; |
205 |
| - total_received++; |
206 |
| - |
207 |
| - // Stop when \r\n is found |
208 |
| - if (i >= 2 && buf[i-1] == '\n' && buf[i-2] == '\r') { |
209 |
| - break; |
210 |
| - } |
211 |
| - } |
212 |
| - |
213 |
| - buf[i] = '\0'; // Null-terminate the buffer |
214 |
| - return total_received; |
215 |
| -} |
216 |
| - |
217 | 147 | // Set socket to non-blocking mode
|
218 | 148 | int fossil_nstream_set_nonblocking(fossil_nstream_t *ns, int enable) {
|
219 | 149 | #ifdef _WIN32
|
@@ -326,7 +256,7 @@ ssize_t fossil_nstream_send_line(fossil_nstream_t *ns, const char *line) {
|
326 | 256 | ssize_t fossil_nstream_recv_line(fossil_nstream_t *ns, char *buf, size_t max_len) {
|
327 | 257 | ssize_t bytes_received = 0;
|
328 | 258 | char ch;
|
329 |
| - while (bytes_received < max_len - 1) { |
| 259 | + while ((size_t)bytes_received < max_len - 1) { |
330 | 260 | if (fossil_nstream_recv(ns, &ch, 1) <= 0) return -1;
|
331 | 261 |
|
332 | 262 | buf[bytes_received++] = ch;
|
|
0 commit comments