Debian Codegraph @

COSMIC/Swait.c

raw
1 /* Swait.c: this file contains code which allows one to wait until data
2 * is present at the Socket (this will block)
3 *
4 * Returns: number of bytes of data awaiting perusal
5 * or EOF if unable to "select" the socket
6 *
7 * Authors: Charles E. Campbell, Jr.
8 * Terry McRoberts
9 * Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1
10 * Permission is hereby granted to use and distribute this code,
11 * with or without modifications, provided that this copyright
12 * notice is copied with it. Like anything else that's free,
13 * Swait.c is provided *as is* and comes with no warranty
14 * of any kind, either expressed or implied. By using this
15 * software, you agree that in no event will the copyright
16 * holder be liable for any damages resulting from the use
17 * of this software.
18 * Date: Aug 22, 2005
19 */
20 #include <stdio.h>
21 #define SSLNEEDTIME
22 #include "sockets.h"
23
24 /* ---------------------------------------------------------------------
25 * Source Code:
26 */
27
28 #ifdef __PROTOTYPE__
29 int Swait(Socket *skt)
30 #else
31 int Swait(skt)
32 Socket *skt;
33 #endif
34 {
35 static char buf[PM_BIGBUF];
36 short result;
37 int ret;
38 fd_set emask;
39 fd_set rmask;
40 fd_set wmask;
41
42
43 /* sanity check */
44 if(!skt) {
45 return -1;
46 }
47
48 FD_ZERO(&rmask);
49 FD_SET(skt->skt,&rmask);
50 FD_ZERO(&wmask);
51 FD_ZERO(&emask);
52
53 /* test if something is available for reading on the socket. This form
54 * will block (sleep) until something arrives
55 */
56 #ifdef SSLNOPEEK
57 result = select(skt->skt+1,rmask.fds_bits,wmask.fds_bits,emask.fds_bits,
58 (struct timeval *) NULL);
59 #else
60 result = select(skt->skt+1, &rmask,&wmask,&emask, (struct timeval *) NULL);
61 #endif
62 if(result < 0) {
63 return EOF;
64 }
65
66 /* server sockets return the select result */
67 if(skt->type == PM_SERVER) {
68 return result;
69 }
70
71
72 #ifdef SSLNOPEEK
73
74 return 1;
75
76 #else /* #ifdef SSLNOPEEK ... #else ... #endif */
77
78 /* wait if message available from socket, return qty bytes avail */
79 if(FD_ISSET(skt->skt,&rmask)) {
80 ret= recv(skt->skt,buf,PM_BIGBUF-1,MSG_PEEK);
81 if(result == 1 && ret == 0) ret= EOF;
82 return ret;
83 }
84
85 /* socket is empty */
86 return 0;
87 #endif /* #ifdef SSLNOPEEK ... #else ... #endif */
88 }
89
90 /* ---------------------------------------------------------------------
91 * vim: ts=4
92 */