File: smaskwait.c

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (365 lines) | stat: -rwxr-xr-x 7,153 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Smaskwait.c: this file contains code which allows one to wait until data
 *  is present given a mask
 */
#include <stdio.h>
#include "xtdio.h"
#define  SSLNEEDTIME
#include "sockets.h"

/* ---------------------------------------------------------------------
 * Local Variables:
 */
static int             waitall = 0;   
static fd_set          mask;  			/* mask last-used by Smaskwait, etc		*/
static struct timeval *timeptr = NULL;
static struct timeval  timewait;
static fd_set          rmask;			/* set by Smaskwait						*/
static int             first   = 1;   

/* ---------------------------------------------------------------------
 * Local Prototypes:
 */

/* ---------------------------------------------------------------------
 * Source Code:
 */

/* Smaskwait: this function waits until some Socket specified to the
 * Smaskset() function receives something.
 *
 * Returns: < 0: error
 *          ==0: timeout
 *          > 0: communication data awaiting
 *
 *   NOTE: THE MASK IS NOT RESET TO ZERO - THE MASK IS RETAINED!!!
 */
#ifdef __PROTOTYPE__
int Smaskwait()
#else
int Smaskwait()
#endif
{
fd_set wmask;			/* write mask */
fd_set emask;			/* error mask */
short  result;


FD_ZERO(&wmask);
FD_ZERO(&emask);

/* test if something is available for reading on the socket.  This form
 * will block (sleep) until something arrives
 */
rmask  = mask;
result = select(waitall, &rmask,&wmask,&emask,timeptr);

if(result < 0) {	/* error */
	return result;
	}

/* result >  0: socket has something waiting 
 * result == 0: timeout
 */
return result;
}

/* --------------------------------------------------------------------- */

/* Smaskset: this function sets up the mask
 *  A null skt clears the mask
 */
#ifdef __PROTOTYPE__
void Smaskset(Socket *skt)
#else
void Smaskset(skt)
Socket *skt;
#endif
{

if(first) {
	FD_ZERO(&mask);
	first= 0;
	}

if(skt) {
	FD_SET(skt->skt,&mask);
	if(skt->skt >= waitall) waitall= skt->skt + 1;
	}
else {
	waitall= 0;
	FD_ZERO(&mask);
	}

}

/* --------------------------------------------------------------------- */

/* Smaskfdset: this function sets up the mask with a "file descriptor"
 * which may be associated with devices other than Sockets
 */
#ifdef __PROTOTYPE__
void Smaskfdset(int fd)
#else
void Smaskfdset(fd)
int fd;
#endif
{

if(first) {
	FD_ZERO(&mask);
	first= 0;
	}

if(0 <= fd && fd < FD_SETSIZE) {	/* fd must be a legal file descriptor */
	FD_SET(fd,&mask);
	if(fd >= waitall) waitall= fd + 1;
	}

}

/* --------------------------------------------------------------------- */

/* Smasktime: this function sets up a timeval structure - specifies a
 * time limit for waiting.  Note: if seconds < 0 or useconds < 0,
 * or seconds == useconds == 0, then there will be no timeout waiting.
 */
#ifdef __PROTOTYPE__
void Smasktime(long seconds,long useconds)
#else
void Smasktime(seconds,useconds)
long seconds;			/* time in seconds			*/
long useconds;			/* time in micro-seconds	*/
#endif
{

if(seconds < 0 || useconds < 0 || (seconds == 0 && useconds == 0)) {
	/* no timeout (ie. infinite wait) */
	timewait.tv_sec = 0L;
	timewait.tv_usec= 0L;
	timeptr         = (struct timeval *) NULL;
	}

else { /* set up specified timeout */
	timewait.tv_sec = seconds;
	timewait.tv_usec= useconds;
	timeptr         = &timewait;
	}

}

/* --------------------------------------------------------------------- */

/* Smasktest: this function allows one to test if any Socket in the
 *  mask has something on it
 * Returns: < 0: error
 *          ==0: timeout
 *          > 0: communication data awaiting
 */
int Smasktest()
{
int ret;
struct timeval timewait_hold;
struct timeval *timeptr_hold=NULL;


/* save current time info */
timewait_hold   = timewait;
timeptr_hold    = timeptr;

/* set up for new time info */
timewait.tv_sec = 0L;
timewait.tv_usec= 0L;
timeptr         = &timewait;

ret= Smaskwait();

/* restore time info */
timewait= timewait_hold;
timeptr = timeptr_hold;

return ret;
}

/* --------------------------------------------------------------------- */

/* Smaskunset: this function sets up the mask
 *  A null skt clears the mask
 */
#ifdef __PROTOTYPE__
void Smaskunset(Socket *skt)
#else
void Smaskunset(skt)
Socket *skt;
#endif
{

if(skt) {
	FD_CLR(skt->skt,&mask);
	}
else {
	waitall= 0;
	FD_ZERO(&mask);
	}

}

/* --------------------------------------------------------------------- */

/* Smaskunfdset: this function sets up the mask
 *  A null skt clears the mask
 */
#ifdef __PROTOTYPE__
void Smaskunfdset(int fd)
#else
void Smaskunfdset(fd)
int fd;
#endif
{

if(fd) FD_CLR(fd,&mask);
else {
	waitall= 0;
	FD_ZERO(&mask);
	}

}

/* --------------------------------------------------------------------- */

/* Smaskget: this function returns the current mask */
#ifdef __PROTOTYPE__
Smask Smaskget()
#else
Smask Smaskget()
#endif
{
Smask smask;


smask.mask   = mask;
smask.waitall= waitall;

return smask;
}

/* --------------------------------------------------------------------- */

/* Smaskuse: this function sets up the mask with the provided mask */
#ifdef __PROTOTYPE__
void Smaskuse(Smask usermask)
#else
void Smaskuse(usermask)
Smask usermask;
#endif
{

mask   = usermask.mask;
waitall= usermask.waitall;

}

/* --------------------------------------------------------------------- */

typedef struct MaskStack_str MaskStack;
struct MaskStack_str {
	Smask      mask;
	MaskStack *nxt,*prv;
	};
static MaskStack *mshd=NULL,*mstl=NULL;

/* Smaskpush: this function pushes the current mask down on a stack
 *  ALSO: the current mask is *cleared*!!!
 */
void Smaskpush()
{
MaskStack *m;


/* allocate */
m= (MaskStack *) malloc(sizeof(MaskStack));

/* link */
if(mstl) mstl->nxt= m;
else     mshd     = m;
m->prv= mstl;
mstl  = m;

/* initialize */
mstl->mask.mask   = mask;
mstl->mask.waitall= waitall;

Smaskset((Socket *) NULL);

}

/* --------------------------------------------------------------------- */

/* Smaskpop: this function pops the stack (or, if the stack is empty,
 *  it will set the current mask to null)
 */
void Smaskpop()
{

if(mstl) {
	MaskStack *old=mstl;

	Smaskuse(mstl->mask);

	/* delink current tail */
	if(mstl->prv) mstl->prv->nxt= NULL;
	else          mshd          = NULL;
	mstl= mstl->prv;

	free((char *) old);
	}
else Smaskset((Socket *) NULL);

}

/* --------------------------------------------------------------------- */

/* Smaskisset: this function tests if a specific Socket is read-ready.
 * Assumption: function is called after a Smaskwait() so that the
 * rmask has been set up.
 */
#ifdef __PROTOTYPE__
int Smaskisset(Socket *skt)
#else
int Smaskisset(skt)
Socket *skt;
#endif
{
int ret;


ret= FD_ISSET(skt->skt,&rmask);

return ret;
}

/* --------------------------------------------------------------------- */

/* Smaskfdisset: this function tests if a specific file descriptor is
 * read-ready
 */
#ifdef __PROTOTYPE__
int Smaskfdisset(int fd)
#else
int Smaskfdisset(fd)
int fd;
#endif
{
int ret;


ret= FD_ISSET(fd,&rmask);

return ret;
}

/* --------------------------------------------------------------------- */


/* ---------------------------------------------------------------------
 * vim: ts=4
 */