2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
30 #include <sys/socket.h>
37 * This write is patient because it restarts if it was incomplete.
40 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
42 const char *bufc
= (const char *) buf
;
46 result
= write(fd
, bufc
, count
);
47 if (result
== -1 && errno
== EINTR
) {
61 return bufc
-(const char *)buf
;
65 * The `struct iovec *iov` is not `const` because we modify it to support
68 ssize_t
patient_writev(int fd
, struct iovec
*iov
, int iovcnt
)
70 ssize_t written
, total_written
= 0;
71 int curr_element_idx
= 0;
74 written
= writev(fd
, iov
+ curr_element_idx
,
75 iovcnt
- curr_element_idx
);
76 if (written
== -1 && errno
== EINTR
) {
83 total_written
+= written
;
86 * If it's not the last element in the vector and we have
87 * written more than the current element size, then increment
88 * the current element index until we reach the element that
89 * was partially written.
91 while (curr_element_idx
< iovcnt
&&
92 written
>= iov
[curr_element_idx
].iov_len
) {
93 written
-= iov
[curr_element_idx
].iov_len
;
97 /* Maybe we are done. */
98 if (curr_element_idx
>= iovcnt
) {
102 /* Update the current element base and size. */
103 iov
[curr_element_idx
].iov_base
+= written
;
104 iov
[curr_element_idx
].iov_len
-= written
;
107 return total_written
;
110 ssize_t
patient_send(int fd
, const void *buf
, size_t count
, int flags
)
112 const char *bufc
= (const char *) buf
;
116 result
= send(fd
, bufc
, count
, flags
);
117 if (result
== -1 && errno
== EINTR
) {
131 return bufc
- (const char *) buf
;
This page took 0.039965 seconds and 4 git commands to generate.