Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Connection management functions |
| 3 | * |
| 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/compat.h> |
| 14 | #include <common/config.h> |
| 15 | |
| 16 | #include <types/connection.h> |
| 17 | #include <types/stream_interface.h> |
| 18 | |
| 19 | /* I/O callback for fd-based connections. It calls the read/write handlers |
| 20 | * provided by the connection's sock_ops, which must be valid. It returns |
| 21 | * FD_WAIT_*. |
| 22 | */ |
| 23 | int conn_fd_handler(int fd) |
| 24 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 25 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 26 | int ret = 0; |
| 27 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 28 | if (!conn) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 29 | return ret; |
| 30 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 31 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 32 | return ret; |
| 33 | |
| 34 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 35 | if (!conn->data->read(fd)) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 36 | ret |= FD_WAIT_READ; |
| 37 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 38 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 39 | return ret; |
| 40 | |
| 41 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 42 | if (!conn->data->write(fd)) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 43 | ret |= FD_WAIT_WRITE; |
| 44 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame^] | 45 | /* remove the events before leaving */ |
| 46 | fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR); |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 47 | return ret; |
| 48 | } |