-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollLib.c
More file actions
executable file
·127 lines (101 loc) · 3.29 KB
/
pollLib.c
File metadata and controls
executable file
·127 lines (101 loc) · 3.29 KB
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
//
// Written Hugh Smith, Updated: April 2022
// Use at your own risk. Feel free to copy, just leave my name in it.
//
// Note this is not a robust implementation
// 1. It is about as un-thread safe as you can write code. If you
// are using pthreads do NOT use this code.
// 2. pollCall() always returns the lowest available file descriptor
// which could cause higher file descriptors to never be processed
//
// This is for student projects so I don't intend on improving this.
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include "safeUtil.h"
#include "pollLib.h"
// Poll global variables
static struct pollfd * pollFileDescriptors;
static int maxFileDescriptor = 0;
static int currentPollSetSize = 0;
static void growPollSet(int newSetSize);
// Poll functions (setup, add, remove, call)
void setupPollSet()
{
currentPollSetSize = POLL_SET_SIZE;
pollFileDescriptors = (struct pollfd *) sCalloc(POLL_SET_SIZE, sizeof(struct pollfd));
}
void addToPollSet(int socketNumber)
{
if (socketNumber >= currentPollSetSize)
{
// needs to increase off of the biggest socket number since
// the file desc. may grow with files open or sockets
// so socketNumber could be much bigger than currentPollSetSize
growPollSet(socketNumber + POLL_SET_SIZE);
}
if (socketNumber + 1 >= maxFileDescriptor)
{
maxFileDescriptor = socketNumber + 1;
}
pollFileDescriptors[socketNumber].fd = socketNumber;
pollFileDescriptors[socketNumber].events = POLLIN;
}
void removeFromPollSet(int socketNumber)
{
pollFileDescriptors[socketNumber].fd = 0;
pollFileDescriptors[socketNumber].events = 0;
}
int pollCall(int timeInMilliSeconds)
{
// returns the socket number if one is ready for read
// returns -1 if timeout occurred
// if timeInMilliSeconds == -1 blocks forever (until a socket ready)
// (this -1 is a feature of poll)
// If timeInMilliSeconds == 0 it will return immediately after looking at the poll set
int i = 0;
int returnValue = -1;
int pollValue = 0;
if ((pollValue = poll(pollFileDescriptors, maxFileDescriptor, timeInMilliSeconds)) < 0)
{
perror("pollCall");
exit(-1);
}
// check to see if timeout occurred (poll returned 0)
if (pollValue > 0)
{
// see which socket is ready
for (i = 0; i < maxFileDescriptor; i++)
{
//if(pollFileDescriptors[i].revents & (POLLIN|POLLHUP|POLLNVAL))
//Could just check for specific revents, but want to catch all of them
//Otherwise, this could hide an error (eat the error condition)
if(pollFileDescriptors[i].revents > 0)
{
returnValue = i;
break;
}
}
}
// Ready socket # or -1 if timeout/none
return returnValue;
}
static void growPollSet(int newSetSize)
{
int i = 0;
// just check to see if someone screwed up
if (newSetSize <= currentPollSetSize)
{
printf("Error - current poll set size: %d newSetSize is not greater: %d\n",
currentPollSetSize, newSetSize);
exit(-1);
}
pollFileDescriptors = srealloc(pollFileDescriptors, newSetSize * sizeof(struct pollfd));
// zero out the new poll set elements
for (i = currentPollSetSize; i < newSetSize; i++)
{
pollFileDescriptors[i].fd = 0;
pollFileDescriptors[i].events = 0;
}
currentPollSetSize = newSetSize;
}