summaryrefslogtreecommitdiffabout
path: root/tests/nt.c
Side-by-side diff
Diffstat (limited to 'tests/nt.c') (more/less context) (ignore whitespace changes)
-rw-r--r--tests/nt.c62
1 files changed, 36 insertions, 26 deletions
diff --git a/tests/nt.c b/tests/nt.c
index 7501586..d30e447 100644
--- a/tests/nt.c
+++ b/tests/nt.c
@@ -107,29 +107,49 @@ stdin_reader (same as net_reader)
net_writer (peer -> stdin_reader) same as stdout_writer
*/
+static inline int
+peer_is_state (netcat_server_t *srv, int state)
+{
+ return srv->peer && (srv->peer->state & state);
+}
+
+static void
+netcat_stream_disconnect (netcat_server_t *srv, int mask)
+{
+ srv->disconnect (srv);
+ srv->state &= ~mask;
+ srv->pollfd->events &= ~mask;
+ if (srv->pollfd->events == 0)
+ srv->pollfd->fd = -1;
+}
+
ssize_t
netcat_stream_read (netcat_server_t *srv)
{
ssize_t n;
-
+
if (iobuf_avail_size (&srv->buf[IN]))
{
n = iobuf_fill (&srv->buf[IN], srv->pollfd->fd);
if (n == -1)
{
+ if (errno == EINTR)
+ return 0;
grecs_error (NULL, errno, "%s: read", srv->id);
srv->pollfd->events &= ~POLLIN;
return -1;
}
- if (n == 0)
+ if (n == 0 || !peer_is_state (srv, POLLOUT))
{
- /* No more input is expected */
- srv->disconnect (srv);
- srv->state &= ~POLLIN;
- srv->pollfd->events &= ~POLLIN;
- if (srv->pollfd->events == 0)
- srv->pollfd->fd = -1;
+ /* No more input is expected || needed */
+ netcat_stream_disconnect (srv, POLLIN);
+
+ if (srv->peer)
+ netcat_stream_disconnect (srv->peer, POLLOUT);
+ return 0;
}
+ else
+ srv->peer->pollfd->events |= POLLOUT;
}
else
{
@@ -139,12 +159,6 @@ netcat_stream_read (netcat_server_t *srv)
return n;
}
-static inline int
-peer_is_state (netcat_server_t *srv, int state)
-{
- return srv->peer && (srv->peer->state & state);
-}
-
ssize_t
netcat_stream_write (netcat_server_t *srv)
{
@@ -155,22 +169,21 @@ netcat_stream_write (netcat_server_t *srv)
if (!peer_is_state (srv, POLLIN))
{
// shutdown write end
- srv->disconnect (srv);
- srv->state &= ~POLLOUT;
- srv->pollfd->events &= ~POLLOUT;
- if (srv->pollfd->events == 0)
- srv->pollfd->fd = -1;
+ netcat_stream_disconnect (srv, POLLOUT);
return -1;
}
if (iobuf_copy (&srv->buf[OUT], &srv->peer->buf[IN]) == 0)
{
srv->peer->pollfd->events |= POLLIN;
+ srv->pollfd->events &= ~POLLOUT;
return 0;
}
}
n = iobuf_flush (&srv->buf[OUT], srv->pollfd->fd);
if (n == -1)
{
+ if (errno == EINTR)
+ return 0;
grecs_error (NULL, errno, "%s: write", srv->id);
return -1;
}
@@ -253,19 +266,16 @@ netcat (char const *urlstr)
{
netcat_server_t *next = srv->next;
int events;
-
+
events = (srv->pollfd->events|POLLHUP)
- & (srv->pollfd->revents & (srv->state|POLLHUP));
+ & (srv->pollfd->revents
+ & (srv->state | ((srv->state & POLLOUT) ? POLLHUP : 0)));
if (events)
{
if (events & POLLHUP)
{
//grecs_error (NULL, 0, "HUP on %s", srv->id);
- srv->disconnect (srv);
- srv->pollfd->events &= ~srv->state;
- if (srv->pollfd->events == 0)
- srv->pollfd->fd = -1;
- srv->state = 0;
+ netcat_stream_disconnect (srv, srv->state);
}
else if (events & POLLIN)
netcat_stream_read (srv);

Return to:

Send suggestions and report system problems to the System administrator.