3 #include "network_backends.h"
8 #include "sys-socket.h"
15 int network_write_mem_chunk(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t
*p_max_bytes
) {
16 chunk
* const c
= cq
->first
;
21 force_assert(NULL
!= c
);
22 force_assert(MEM_CHUNK
== c
->type
);
23 force_assert(c
->offset
>= 0 && c
->offset
<= (off_t
)buffer_string_length(c
->mem
));
25 c_len
= buffer_string_length(c
->mem
) - c
->offset
;
26 if (c_len
> *p_max_bytes
) c_len
= *p_max_bytes
;
29 chunkqueue_remove_finished_chunks(cq
);
34 if ((r
= send(fd
, c
->mem
->ptr
+ c
->offset
, c_len
, 0)) < 0) {
35 int lastError
= WSAGetLastError();
45 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
46 "send failed: ", lastError
, fd
);
51 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, c_len
)) < 0) {
60 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
61 "write failed:", strerror(errno
), fd
);
69 chunkqueue_mark_written(cq
, r
);
72 return (r
> 0 && r
== c_len
) ? 0 : -3;
75 int network_write_chunkqueue_write(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t max_bytes
) {
76 while (max_bytes
> 0 && NULL
!= cq
->first
) {
79 switch (cq
->first
->type
) {
81 r
= network_write_mem_chunk(srv
, con
, fd
, cq
, &max_bytes
);
84 r
= network_write_file_chunk_mmap(srv
, con
, fd
, cq
, &max_bytes
);
88 if (-3 == r
) return 0;
95 int network_write_chunkqueue_sendfile(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t max_bytes
) {
96 while (max_bytes
> 0 && NULL
!= cq
->first
) {
99 switch (cq
->first
->type
) {
101 r
= network_writev_mem_chunks(srv
, con
, fd
, cq
, &max_bytes
);
104 r
= network_write_file_chunk_sendfile(srv
, con
, fd
, cq
, &max_bytes
);
108 if (-3 == r
) return 0;
109 if (0 != r
) return r
;