~vcs-imports/junkcode/main

« back to all changes in this revision

Viewing changes to messaging/tcp2.c

  • Committer: tridge
  • Date: 2007-05-26 08:29:21 UTC
  • Revision ID: vcs-imports@canonical.com-20070526082921-djzi1qk1jnvaynfx
demo dgram flood problem

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <netinet/tcp.h>
22
22
#include <netdb.h>
23
23
#include <arpa/inet.h>
 
24
#include <sys/epoll.h>
24
25
 
 
26
#define TCP_NODELAY            1
25
27
 
26
28
typedef int BOOL;
27
29
 
285
287
        return accept(fd, &a, &len);
286
288
}
287
289
 
 
290
static void set_nonblocking(int fd)
 
291
{
 
292
        unsigned v = fcntl(fd, F_GETFL, 0);
 
293
        fcntl(fd, F_SETFL, v | O_NONBLOCK);
 
294
}
 
295
 
288
296
static void worker(const char *host, int port1, int port2, int w)
289
297
{
290
298
        int l = tcp_socket_bind(port1);
291
299
        int s2, s1;
292
300
        int count=0;
 
301
        char c[4] = { 0, };
 
302
        int epoll_fd;
 
303
        struct epoll_event event;
293
304
 
294
305
        set_socket_options(l, "SO_REUSEADDR");
295
306
 
303
314
 
304
315
        start_timer();
305
316
 
 
317
        epoll_fd = epoll_create(2);
 
318
 
 
319
        memset(&event, 0, sizeof(event));
 
320
 
 
321
        event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
 
322
        event.data.fd = s1;
 
323
        epoll_ctl(epoll_fd, EPOLL_CTL_ADD, s1, &event);
 
324
 
 
325
        set_nonblocking(s1);
 
326
 
306
327
        while (1) {
307
 
                char c=0;
308
 
                if (write(s2, &c, 1) != 1) {
 
328
                struct epoll_event events[8];
 
329
                int ret;
 
330
                int num_ready;
 
331
 
 
332
                *(int*)c = count;
 
333
                
 
334
                if (write(s2, c, sizeof(c)) != sizeof(c)) {
309
335
                        fatal("write");
310
336
                }
311
 
                if (read(s1, &c, 1) != 1) {
 
337
                ret = epoll_wait(epoll_fd, events, 8, -1);
 
338
                if (ret != 1) fatal("epoll_wait");
 
339
 
 
340
                if (ioctl(s1, FIONREAD, &num_ready) != 0) fatal("FIONREAD");
 
341
 
 
342
                printf("num_ready=%d\n", num_ready);
 
343
 
 
344
//              if (num_ready != sizeof(c)) fatal("num_ready");
 
345
             
 
346
                if (read(s1, c, sizeof(c)) != sizeof(c)) {
312
347
                        fatal("read");
313
348
                }
314
349
                if (w == 1 && (end_timer() > 1.0)) {