Sponsored Content
Top Forums Programming Event driven programming / epoll / typedef union / session data array Post 302917602 by John S. on Wednesday 17th of September 2014 05:15:32 PM
Old 09-17-2014
Event driven programming / epoll / typedef union / session data array

Sorry for the “word salad” subject, but I wanted to cast a wide net for help.

I've created an IP (Internet Protocol) server which serves HTTP, SMTP, and FTP requests.

As you probably know, they all require creating a socket, listening on it, accepting connections, and then having a short “conversation” as specified by the appropriate RFC (Request For Comment).

I've also created an automated HTTP client which reads web pages.

During testing of the HTTP client I think I encountered some performance issues possibly those in the C10K (Customer 10,000) problem - creating new threads for each connection became very slow.

So I decided to try event driven programming which purportedly solves the C10K problem.

I'm working with C on Linux. Epoll was added to Linux over 10 years ago to solve the C10K problem. It reduces the problem order from N squared (where N is the number of connections) to constant time for each event or something like that.

So I've struggled and gotten more and more confused but then found some most excellent code here...
epoll_wait: Linux sockets example

I think Lrrr (blogspot poster and probable code author) has provided a nearly perfect bit of self documented code! Lrrr has used print statements in nearly every block which make clear what is going on. Even the code formatting is the same as mine so I found the example clear and useful.

With virtually no modification I was able to incorporate Lrrr's code into my program in a test mode. When it runs, I'm able to open multiple telnet sessions and see the code in action in both the console and in the telnet sessions.

So far so good.

I've even found the two points in the code where I'd need to somehow modify it so it follows the RFC for a web-server for example.

Code:
rc = snprintf(buffer, sizeof(buffer), "Hello socket %d from server socket %d!\n", fd, sd);

and
Code:
printf("Received '");
sprint_buffer(buffer, rc);
printf("' from socket with sd %d\n", fd);

So I know where I need to integrate my old code and I know what structure I have to use.

epoll_create, epoll_ctl, and epoll_wait all use the epoll_event structure which in turn uses the “typedef union epoll_data”

Code:
typedef union epoll_data
{
  void        *ptr;
  int          fd;
  __uint32_t   u32;
  __uint64_t   u64;
} epoll_data_t;

struct epoll_event
{
  __uint32_t   events; /* Epoll events */
  epoll_data_t data;   /* User data variable */
};

I've never used a union before and this is where extra confusion starts. My vague understanding is that a union can hold any of the defined members - but only one-at-a-time.

The void pointer looks like it might be what I want to use, but the fd member is in-use so if I assigned to ptr, fd would be corrupted.

What I'd like is to somehow associate epoll_event.data with one member of a session data array. I envision this array to be comprised of structures which for pilot purposes could be simply 4 integers, say 1,2,3,4. Where data is written to the client, the first integer is printed and others shifted around so the second time data is written to the client the second integer is printed and so on.

I'll gladly entertain any thoughts or suggestions at this time.
 

We Also Found This Discussion For You

1. Web Development

Intersection and union of array by hash

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)
Discussion started by: yifangt
3 Replies
All times are GMT -4. The time now is 05:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy