ReactOS  0.4.13-dev-242-g611e6d7
SRecvfrom.c
Go to the documentation of this file.
1 #include "syshdrs.h"
2 
3 #ifndef NO_SIGNALS
4 extern volatile Sjmp_buf gNetTimeoutJmp;
5 extern volatile Sjmp_buf gPipeJmp;
6 #endif
7 
8 #ifndef NO_SIGNALS
9 
10 int
11 SRecvfrom(int sfd, char *const buf, size_t size, int fl, struct sockaddr_in *const fromAddr, int tlen)
12 {
13  int nread, tleft;
14  vsio_sigproc_t sigalrm, sigpipe;
15  time_t done, now;
16  int alen;
17 
18  if (SSetjmp(gNetTimeoutJmp) != 0) {
19  alarm(0);
20  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
21  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
22  errno = ETIMEDOUT;
23  return (kTimeoutErr);
24  }
25 
26  if (SSetjmp(gPipeJmp) != 0) {
27  alarm(0);
28  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
29  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
30  errno = EPIPE;
31  return (kBrokenPipeErr);
32  }
33 
34  sigalrm = (vsio_sigproc_t) SSignal(SIGALRM, SIOHandler);
36 
37  time(&now);
38  done = now + tlen;
39  tleft = (int) (done - now);
40  forever {
41  alen = sizeof(struct sockaddr_in);
42  (void) alarm((unsigned int) tleft);
43  nread = recvfrom(sfd, buf, size, fl,
44  (struct sockaddr *) fromAddr, &alen);
45  (void) alarm(0);
46  if (nread >= 0)
47  break;
48  if (errno != EINTR)
49  break; /* Fatal error. */
50  errno = 0;
51  time(&now);
52  tleft = (int) (done - now);
53  if (tleft < 1) {
54  nread = kTimeoutErr;
55  errno = ETIMEDOUT;
56  break;
57  }
58  }
59 
60  (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
61  (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
62 
63  return (nread);
64 } /* SRecvfrom */
65 
66 #else
67 
68 int
69 SRecvfrom(int sfd, char *const buf, size_t size, int fl, struct sockaddr_in *const fromAddr, int tlen)
70 {
71  int nread, tleft;
72  fd_set ss;
73  struct timeval tv;
74  int result;
75  time_t done, now;
76  int alen;
77 
78  time(&now);
79  done = now + tlen;
80  tleft = (int) (done - now);
81  nread = 0;
82  forever {
83  alen = sizeof(struct sockaddr_in);
84 
85  forever {
86  errno = 0;
87  FD_ZERO(&ss);
88  FD_SET(sfd, &ss);
89  tv.tv_sec = tleft;
90  tv.tv_usec = 0;
92  if (result == 1) {
93  /* ready */
94  break;
95  } else if (result == 0) {
96  /* timeout */
97  errno = ETIMEDOUT;
99  return (kTimeoutErr);
100  } else if (errno != EINTR) {
101  return (-1);
102  }
103  }
104 
105  nread = recvfrom(sfd, buf, size, fl,
106  (struct sockaddr *) fromAddr, &alen);
107 
108  if (nread >= 0)
109  break;
110  if (errno != EINTR)
111  break; /* Fatal error. */
112  errno = 0;
113  time(&now);
114  tleft = (int) (done - now);
115  if (tleft < 1) {
116  nread = kTimeoutErr;
117  errno = ETIMEDOUT;
119  break;
120  }
121  }
122 
123  return (nread);
124 } /* SRecvfrom */
125 
126 #endif
127 
Definition: winsock.h:66
#define EPIPE
Definition: acclib.h:91
#define SELECT_TYPE_ARG234
Definition: wincfg.h:4
INT WSAAPI recvfrom(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags, OUT LPSOCKADDR from, IN OUT INT FAR *fromlen)
Definition: recv.c:87
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
volatile sio_sigproc_t vsio_sigproc_t
Definition: sio.h:124
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define EINTR
Definition: acclib.h:80
unsigned long tv_sec
Definition: linux.h:1738
int errno
__u16 time
Definition: mkdosfs.c:366
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
#define Sjmp_buf
Definition: sio.h:41
_In_ FLONG fl
Definition: winddi.h:1279
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
time_t now
Definition: finger.c:65
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
#define kTimeoutErr
Definition: sio.h:58
GLsizeiptr size
Definition: glext.h:5919
int SRecvfrom(int sfd, char *const buf, size_t size, int fl, struct sockaddr_in *const fromAddr, int tlen)
Definition: SRecvfrom.c:69
#define SIGPIPE
Definition: signal.h:35
#define SSetjmp(a)
Definition: sio.h:39
#define SELECT_TYPE_ARG5
Definition: wincfg.h:7
#define forever
Definition: ncftp.h:73
#define kBrokenPipeErr
Definition: sio.h:59
#define SETWSATIMEOUTERR
Definition: sio.h:120
__kernel_time_t time_t
Definition: linux.h:252
void(*)(int) SSignal(int signum, void(*handler)(int))
Definition: sio.h:237
#define ETIMEDOUT
Definition: errno.h:121
#define ss
Definition: i386-dis.c:432
GLuint64EXT * result
Definition: glext.h:11304
void(* sio_sigproc_t)(int)
Definition: sio.h:123
void SIOHandler(int)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31