ReactOS 0.4.16-dev-257-g6aa11ac
SRecv.c
Go to the documentation of this file.
1#include "syshdrs.h"
2
3#ifndef NO_SIGNALS
4extern volatile Sjmp_buf gNetTimeoutJmp;
5extern volatile Sjmp_buf gPipeJmp;
6#endif
7
8#ifndef NO_SIGNALS
9
10int
11SRecv(int sfd, char *const buf0, size_t size, int fl, int tlen, int retry)
12{
13 int nread;
14 volatile int nleft;
15 char *volatile buf = buf0;
16 int tleft;
17 vsio_sigproc_t sigalrm, sigpipe;
18 time_t done, now;
19
20 if (SSetjmp(gNetTimeoutJmp) != 0) {
21 alarm(0);
22 (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
23 (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
24 nread = size - nleft;
25 if ((nread > 0) && (retry == kFullBufferNotRequired))
26 return (nread);
28 return (kTimeoutErr);
29 }
30
31 if (SSetjmp(gPipeJmp) != 0) {
32 alarm(0);
33 (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
34 (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
35 nread = size - nleft;
36 if ((nread > 0) && (retry == kFullBufferNotRequired))
37 return (nread);
38 errno = EPIPE;
39 return (kBrokenPipeErr);
40 }
41
42 sigalrm = (vsio_sigproc_t) SSignal(SIGALRM, SIOHandler);
44 errno = 0;
45
46 nleft = (int) size;
47 time(&now);
48 done = now + tlen;
49 forever {
50 tleft = (int) (done - now);
51 if (tleft < 1) {
52 nread = size - nleft;
53 if ((nread == 0) || (retry == kFullBufferRequired)) {
54 nread = kTimeoutErr;
56 }
57 goto done;
58 }
59 (void) alarm((unsigned int) tleft);
60 nread = recv(sfd, (char *) buf, nleft, fl);
61 (void) alarm(0);
62 if (nread <= 0) {
63 if (nread == 0) {
64 /* EOF */
66 nread = size - nleft;
67 goto done;
68 } else if (errno != EINTR) {
69 nread = size - nleft;
70 if (nread == 0)
71 nread = -1;
72 goto done;
73 } else {
74 errno = 0;
75 nread = 0;
76 /* Try again. */
77 }
78 }
79 nleft -= nread;
80 if ((nleft <= 0) || ((retry == 0) && (nleft != (int) size)))
81 break;
82 buf += nread;
83 time(&now);
84 }
85 nread = size - nleft;
86
87done:
88 (void) SSignal(SIGALRM, (sio_sigproc_t) sigalrm);
89 (void) SSignal(SIGPIPE, (sio_sigproc_t) sigpipe);
90
91 return (nread);
92} /* SRecv */
93
94#else
95
96int
97SRecv(int sfd, char *const buf0, size_t size, int fl, int tlen, int retry)
98{
99 int nread;
100 int nleft;
101 char *buf = buf0;
102 int tleft;
103 time_t done, now;
104 fd_set ss;
105 struct timeval tv;
106 int result;
107
108 errno = 0;
109
110 nleft = (int) size;
111 time(&now);
112 done = now + tlen;
113 forever {
114 tleft = (int) (done - now);
115 if (tleft < 1) {
116 nread = size - nleft;
117 if ((nread == 0) || (retry == kFullBufferRequired)) {
118 nread = kTimeoutErr;
121 }
122 goto done;
123 }
124
125 forever {
126 errno = 0;
127 FD_ZERO(&ss);
128 FD_SET(sfd, &ss);
129 tv.tv_sec = tlen;
130 tv.tv_usec = 0;
132 if (result == 1) {
133 /* ready */
134 break;
135 } else if (result == 0) {
136 /* timeout */
137 nread = size - nleft;
138 if ((nread > 0) && (retry == kFullBufferNotRequired))
139 return (nread);
142 return (kTimeoutErr);
143 } else if (errno != EINTR) {
144 return (-1);
145 }
146 }
147
148#if defined(WIN32) || defined(_WINDOWS)
149 nread = recv(sfd, (char *) buf, nleft, fl);
150#else
151 nread = recv(sfd, (char *) buf, nleft, fl);
152#endif
153
154 if (nread <= 0) {
155 if (nread == 0) {
156 /* EOF */
158 nread = size - nleft;
159 goto done;
160 } else if (errno != EINTR) {
161 nread = size - nleft;
162 if (nread == 0)
163 nread = -1;
164 goto done;
165 } else {
166 errno = 0;
167 nread = 0;
168 /* Try again. */
169 }
170 }
171 nleft -= nread;
172 if ((nleft <= 0) || ((retry == 0) && (nleft != (int) size)))
173 break;
174 buf += nread;
175 time(&now);
176 }
177 nread = size - nleft;
178
179done:
180 return (nread);
181} /* SRecv */
182
183#endif
184
int SRecv(int sfd, char *const buf0, size_t size, int fl, int tlen, int retry)
Definition: SRecv.c:97
#define EINTR
Definition: acclib.h:80
#define EPIPE
Definition: acclib.h:91
#define SIGPIPE
Definition: signal.h:35
#define NULL
Definition: types.h:112
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
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
#define ETIMEDOUT
Definition: errno.h:121
__kernel_time_t time_t
Definition: linux.h:252
time_t now
Definition: finger.c:65
GLsizeiptr size
Definition: glext.h:5919
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint64EXT * result
Definition: glext.h:11304
#define ss
Definition: i386-dis.c:441
#define SELECT_TYPE_ARG234
Definition: wincfg.h:4
#define SELECT_TYPE_ARG5
Definition: wincfg.h:7
__u16 time
Definition: mkdosfs.c:8
#define kBrokenPipeErr
Definition: sio.h:59
void SIOHandler(int)
#define kFullBufferRequired
Definition: sio.h:50
#define SETWSATIMEOUTERR
Definition: sio.h:120
#define SSetjmp(a)
Definition: sio.h:39
#define kFullBufferRequiredExceptLast
Definition: sio.h:51
volatile sio_sigproc_t vsio_sigproc_t
Definition: sio.h:124
void(*)(int) SSignal(int signum, void(*handler)(int))
Definition: sio.h:237
#define kFullBufferNotRequired
Definition: sio.h:49
#define Sjmp_buf
Definition: sio.h:41
#define kTimeoutErr
Definition: sio.h:58
void(* sio_sigproc_t)(int)
Definition: sio.h:123
#define forever
Definition: ncftp.h:73
#define errno
Definition: errno.h:18
Definition: winsock.h:66
unsigned long tv_sec
Definition: linux.h:1738
unsigned long tv_usec
Definition: linux.h:1739
_In_ FLONG fl
Definition: winddi.h:1279
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89