Commit 7e04141b authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(reading_from_server): New variable.

(request_limit): New variable.
(start_writing): New static function.
(start_reading): Ditto.
(stop_reading): Ditto.
(read_stdin): New command: start-reading.
(read_server): If do_write_only, just return OOP_HALT when eof on
	the server is seen--don't print anything.  Print a message if
	more than request_limit replies are received.
(write_server): Made a log message unique.
(main): Use start_reading(), start_writing() and stop_reading() to
	simplify the code.  Don't do a final write; different Unixes
	handles this in various strange ways, and we don't want to
	test *them*.
parent c37072b7
......@@ -74,11 +74,13 @@ static long rcvd_bytes = 0;
static int server;
static int writing_to_server;
static int reading_from_server;
static long last_progress_status = 0;
static int abort_pending = 0;
static int do_write_only = 0;
static long request_limit = -1;
static oop_call_fd read_stdin;
static oop_call_fd read_server;
......@@ -95,6 +97,45 @@ fail(const char *reason)
exit(1);
}
static void
start_writing(oop_source *oop)
{
if (!writing_to_server)
{
oop->on_fd(oop, server, OOP_WRITE, write_server, NULL);
writing_to_server = 1;
}
}
static void
stop_writing(oop_source *oop)
{
if (writing_to_server)
{
oop->cancel_fd(oop, server, OOP_WRITE);
writing_to_server = 0;
}
}
static void
start_reading(oop_source *oop)
{
if (!reading_from_server)
{
oop->on_fd(oop, server, OOP_READ, read_server, NULL);
reading_from_server = 1;
}
}
static void
stop_reading(oop_source *oop)
{
if (reading_from_server)
{
oop->cancel_fd(oop, server, OOP_READ);
reading_from_server = 0;
}
}
static int
tcp_connect(const char *host,
......@@ -116,7 +157,7 @@ tcp_connect(const char *host,
}
static void *
read_stdin(oop_source *UNUSED(source),
read_stdin(oop_source *source,
int fd,
oop_event UNUSED(event),
void *UNUSED(user))
......@@ -156,6 +197,17 @@ read_stdin(oop_source *UNUSED(source),
return OOP_CONTINUE;
}
if (!strcmp(readbuf, "start-reading"))
{
start_reading(source);
if (do_write_only)
request_limit = sent_requests;
puts("ok");
fflush(stdout);
return OOP_CONTINUE;
}
fprintf(stderr, "unknown command on stdin: %s.\n", readbuf);
return OOP_HALT;
}
......@@ -281,6 +333,8 @@ read_server(oop_source *UNUSED(source),
}
else if (rv == 0)
{
if (do_write_only)
return OOP_HALT;
fprintf(stderr, "eof on server");
exit(1);
}
......@@ -297,6 +351,13 @@ read_server(oop_source *UNUSED(source),
for (pos = readbuf; src < end; )
*pos++ = *src++;
if (request_limit != -1 && rcvd_requests >= request_limit)
{
fprintf(stderr, "Only expected %ld replies, but got at least %ld.\n",
request_limit, rcvd_requests);
request_limit = -1;
}
if (writing_to_server)
return OOP_CONTINUE;
if (rcvd_requests < sent_requests)
......@@ -304,16 +365,6 @@ read_server(oop_source *UNUSED(source),
return OOP_HALT;
}
static void
stop_writing(oop_source *oop)
{
if (writing_to_server)
{
oop->cancel_fd(oop, server, OOP_WRITE);
writing_to_server = 0;
}
}
static void *
write_server(oop_source *source,
int fd,
......@@ -354,7 +405,7 @@ write_server(oop_source *source,
{
if (do_write_only)
return OOP_HALT;
fprintf(stderr, "eof on server");
fprintf(stderr, "eof on server (write)");
exit(1);
}
else
......@@ -470,8 +521,6 @@ main(int argc,
struct timeval report_timer;
struct timeval end_timer;
char buf[8192];
int retry;
int again;
/* What should we do? */
int do_limit_time = 0;
......@@ -547,10 +596,8 @@ main(int argc,
oop->on_fd(oop, 0, OOP_READ, read_stdin, NULL);
if (!do_write_only)
oop->on_fd(oop, server, OOP_READ, read_server, NULL);
oop->on_fd(oop, server, OOP_WRITE, write_server, NULL);
writing_to_server = 1;
start_reading(oop);
start_writing(oop);
if (verbose)
arm_timer(oop, 0, 0, report_stats, &report_timer);
......@@ -566,45 +613,6 @@ main(int argc,
oop_sys_run(sys);
if (do_write_only)
{
again = 1;
for (retry = 3; again && retry > 0; retry--)
{
again = 0;
memset(buf, '\n', sizeof(buf));
rv = write(server, buf, sizeof(buf));
if (rv == -1)
{
if (retry > 1 && (errno == EAGAIN || errno == EWOULDBLOCK
|| errno == EINTR))
{
again = 1;
sleep(1);
continue;
}
/* We expect EPIPE or ECONNRESET. */
if (errno != EPIPE && errno != ECONNRESET)
fprintf(stderr, "final write (retry==%d): %s\n",
retry, strerror(errno));
}
if (rv == 0)
fprintf(stderr, "final write returned 0\n");
if (rv > 0)
{
if (retry > 1)
{
again = 1;
sleep(1);
continue;
}
fprintf(stderr, "ERROR: final write succeeded: %d\n", (int)rv);
}
}
}
if (verbose)
oop->cancel_time(oop, report_timer, report_stats, &report_timer);
......@@ -617,8 +625,7 @@ main(int argc,
do_report_stats();
oop->cancel_fd(oop, 0, OOP_READ);
if (!do_write_only)
oop->cancel_fd(oop, server, OOP_READ);
stop_reading(oop);
stop_writing(oop);
oop_sys_delete(sys);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment