File: recv.nonblock.c

package info (click to toggle)
mlton 20100608-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 34,980 kB
  • ctags: 69,089
  • sloc: ansic: 18,421; lisp: 2,879; makefile: 1,570; sh: 1,325; pascal: 256; asm: 97
file content (42 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Simulates MSG_DONTWAIT using fcntl() and O_NONBLOCK. */

static void fd_modify(int fd, int flags, int add, int shouldRemove)
{
        int status;
        
        if (flags & MSG_DONTWAIT) {
                status = errno;
                int f = fcntl(fd, F_GETFL);
                fcntl(fd, F_SETFL, (f | add) & ~shouldRemove);
                errno = status;
        }
}

static void set_nonblock(int fd, int flags)
{
        fd_modify(fd, flags, O_NONBLOCK, 0);
}

static void clear_nonblock(int fd, int flags)
{
        fd_modify(fd, flags, 0, O_NONBLOCK);
}

int MLton_recv(int s, void *buf, int len, int flags)
{
        int ret;
        set_nonblock(s, flags);
        ret = recv(s, buf, len, flags & ~MSG_DONTWAIT);
        clear_nonblock(s, flags);
        return ret;
}

int MLton_recvfrom(int s, void *buf, int len, int flags, void *from,
                   socklen_t *fromlen)
{
        int ret;
        set_nonblock(s, flags);
        ret = recvfrom(s, buf, len, flags & ~MSG_DONTWAIT, from, fromlen);
        clear_nonblock(s, flags);
        return ret;
}