Skip to content

Commit e6ea7b3

Browse files
committed
For #12, refine API for sendmmsg
1 parent 3aa0013 commit e6ea7b3

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

io.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,9 @@ int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t
742742
return n;
743743
}
744744

745-
746-
int st_sendmmsg(st_netfd_t fd, void *msgvec, unsigned int vlen, int flags, st_utime_t timeout)
745+
int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout)
747746
{
748-
#if !defined(MD_HAVE_SENDMMSG) || !defined(_GNU_SOURCE)
749-
return -1;
750-
#else
747+
#if defined(MD_HAVE_SENDMMSG) && defined(_GNU_SOURCE)
751748
int n;
752749
int left;
753750
struct mmsghdr *p;
@@ -774,6 +771,28 @@ int st_sendmmsg(st_netfd_t fd, void *msgvec, unsigned int vlen, int flags, st_ut
774771
return n;
775772
}
776773
return (int)vlen - left;
774+
#else
775+
struct st_mmsghdr *p;
776+
int i, n;
777+
778+
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
779+
for (i = 0; i < (int)vlen; ++i) {
780+
p = msgvec + i;
781+
n = st_sendmsg(fd, &p->msg_hdr, flags, timeout);
782+
if (n < 0) {
783+
// An error is returned only if no datagrams could be sent.
784+
if (i == 0) {
785+
return n;
786+
}
787+
return i + 1;
788+
}
789+
790+
p->msg_len = n;
791+
}
792+
793+
// Returns the number of messages sent from msgvec; if this is less than vlen, the caller can retry with a
794+
// further sendmmsg() call to send the remaining messages.
795+
return vlen;
777796
#endif
778797
}
779798

public.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ extern int st_recvfrom(st_netfd_t fd, void *buf, int len, struct sockaddr *from,
151151
extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct sockaddr *to, int tolen, st_utime_t timeout);
152152
extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout);
153153
extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout);
154-
extern int st_sendmmsg(st_netfd_t fd, void *msgvec, unsigned int vlen, int flags, st_utime_t timeout);
154+
155+
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
156+
#include <sys/socket.h>
157+
struct st_mmsghdr {
158+
struct msghdr msg_hdr; /* Message header */
159+
unsigned int msg_len; /* Number of bytes transmitted */
160+
};
161+
extern int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout);
162+
155163
extern st_netfd_t st_open(const char *path, int oflags, mode_t mode);
156164

157165
#ifdef DEBUG

0 commit comments

Comments
 (0)