which pointed out..
"The void *ptr and int fd both are inside a union inside the struct epoll_event. You should use either of them not both. Hence in your struct, add the field for fd as well and only link pointer to the struct in the ptr field of the epoll_event. This way when you get back your pointer then get the fd from it for further use."
In retrospect, it's all perfectly obvious, isn't it?
Here's what I added and edited...
I created a SessionData structure
I ended up incrementing Data1 rather than rotating them to confirm operation as I desired. It's all good as they say.
There are six places where I modified Lrrr's original code.
I commented out the original code to set the data value to the file descriptor since I wanted to use the pointer value. I don't need the data 1, 2, and 3 values here because this is the SERVER socket. Indeed there is NO session data for the server socket, but since the event loop treats all sockets the same, I had to use a session data record.
Here is the start of the event handling loop. I now get the fd value from the session data structure.
Here, for some not-yet-clear reason, Lrrr moves fd BACK to where it had recently been taken FROM. I have dutifully modified it to use the session data structure.
Here is where a new client has connected. This is where the SessionData is needed. A new structure is malloc'd. I have yet to free this so I currently have a memory leak and will fix that next.
Here again, fd is being copied back to where it came from a few moments earlier. I still do not see the need for this.
Here I appended the output to expose the changing session data. Opening two telnet sessions showed the Data1 value incrementing and doing so independently for either session.
I also implemented a 10,000 msec (10 sec) timeout.
I guess I am missing something to rationalize the use of the union type versus a structure so if anyone has an explanation regarding that, I'd be keen to hear it.
Cheers!
Last edited by John S.; 09-21-2014 at 02:34 PM..
Reason: Pasted wrong code.
Hi,
A piece of script from Perl-cookbook I do not understand, and post here for explanation.
The purpose is to find the element in either array (union), and in both array (intersection). Thank you in advance.
@a=qw(1 3 5 6 7 8);
@b=qw(2 3 5 7 9);
foreach $e (@a, @b) {$union{$e}++ &&... (3 Replies)