pollfd in Winsock2.h redefinition issue

I am trying to get struct pollfd to work. I have defined it in poll.h exactly how it is in winsock2.h, changed my int poll() function to int WSApoll() to conform to the declaration in Winsock2, made sure Winsock2 and WS2tcpip come before windows.h and tried EVERY configuration imaginable. I am at a loss. I keep getting pollfd 'struct' type redefinition in my poll.h file and a use of undefined type 'pollfd' in my cpp file. The commented parts are part of my multiple attempts and the common.h used to house Winsock2.h, WS2tcpip.h and Windows.h. Any thoughts? I have tried not putting the struct in poll.h, which then gives me a 2019 linker error, I have tried so many different ways to fix this that I can't even remember them all at this point. Any help at all is appreciated - TIA!

poll.h
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
#ifndef _POLL_H__
#define _POLL_H__
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
//#include "common.h"
typedef unsigned long ndfs_t;

typedef struct pollfd {
	SOCKET fd;
	short events;
	short revents;
}WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;

int WSApoll(WSAPOLLFD *fds, ndfs_t ndfs, int tmo);
#endif
/*#pragma once
#define POLLIN 0x001
#define POLLOUT 0x004
#define POLLERR 0x008
#define POLLHUP 0x010
#define POLLNVAL 0x020

/*typedef struct pollfd {
	SOCKET fd;
	short events;
	short revents;
}WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;

typedef unsigned long ndfs_t;

int WSApoll(WSAPOLLFD *fds, ndfs_t ndfs, int tmo);

*/

poll.cpp
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
#ifndef WIN32
#define Win32
#define Win32_LEAN_AND_MEAN
#endif
#include "poll.h"
#include <iostream>
#include <time.h>
#include <string.h>
//#include <sys/types.h>


int WSApoll(WSAPOLLFD fds[], ndfs_t ndfs, int tmo)
{
	fd_set rdfs, wfds, efds;
	ndfs_t x;
	int maxfd = 0;
	FD_ZERO(&rdfs);
	FD_ZERO(&wfds);
	FD_ZERO(&efds);

	for (ndfs_t x = 0; x < ndfs; x++)
	{
		if (fds[x].events & (POLLIN | POLLOUT))
		{
			if(fds[x].events & POLLIN)
				{
				FD_SET(fds[x].fd, &rdfs);
				}
			if(fds[x].events & POLLOUT)
			{
				FD_SET(fds[x].fd, &wfds);
			}
		}
}
	struct timeval timeout = { timeout.tv_sec = tmo / 1000, timeout.tv_usec = (tmo % 1000) * 1000 };
	struct timeval *tp = &timeout;

	if (tmo == -1)
	{
		tp = NULL;
	}
	int ret = select(maxfd + 1, &rdfs, &wfds, &efds, tp);
	if (ret <= 0)
	{
		return ret;
	}
	for (ndfs_t x = 0; x < ndfs; x++)
	{
		fds[x].revents = 0;
		if (FD_ISSET(fds[x].fd, &rdfs))
		{
			fds[x].revents |= POLLIN;
		}
		if (FD_ISSET(fds[x].fd, &wfds))
		{
			fds[x].revents |= POLLOUT;
		}
		if (FD_ISSET(fds[x].fd, &efds))
		{
			fds[x].revents |= POLLERR;
		}
}
	return ret;
}
Last edited on
https://www.cplusplus.com/articles/jEywvCM9/
Please read, then edit your post.
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Just to clarify - are you saying that pollfd is defined in WinSock2.h, and that you're trying to redefine it in poll.h?
I apologize, I tried repeatedly to get the code tag to work, but it wouldn't yesterday - I thought it was broken. It worked today though, probably some user error on my part - I blame Monday!

2) from MikeyBoy: pollfd is defined and I am trying to use it from Winsock2, but I am getting an error telling me that I am trying to redefine it, but as far as I can tell from all examples and the way it is written in Winsock2.h, I'm not. That's why I am confused.
Look at lines 9 - 13 of your poll.h. You are defining the struct there. And yet it's already defined in WinSock2.h. So, obviously, you're attempting to redefine a struct that's already defined.

EDIT: Also, from your OP:

which then gives me a 2019 linker error

Do you honestly believe any of us has memorized what all the thousands of numerical error codes from Visual Studio mean?

And:

I have tried not putting the struct in poll.h, which then gives me a 2019 linker error

So, you fixed one error, such that your file compiled properly, then you got another error at link-time, and decided to put back the first error so that your code couldn't even compile again.

Fix your compilation error - which you already know how to do - and then we can deal with the linker error.
Last edited on
Again, I apologize - I get 2019's all the time, I just didn't think.

Your notes helped though - all fixed...



Glad I could help.
Registered users can post here. Sign in or register to post.