1
1

scp_write_nonblock: use select() instead of busyloop

Make this example nicer by not busylooping.
Этот коммит содержится в:
Daniel Stenberg 2010-10-22 12:27:44 +02:00
родитель c49061e708
Коммит d9cdd8c0a7

Просмотреть файл

@ -34,6 +34,36 @@
#include <ctype.h> #include <ctype.h>
#include <time.h> #include <time.h>
static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
{
struct timeval timeout;
int rc;
fd_set fd;
fd_set *writefd = NULL;
fd_set *readfd = NULL;
int dir;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(socket_fd, &fd);
/* now make sure we wait in the correct direction */
dir = libssh2_session_block_directions(session);
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
readfd = &fd;
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
writefd = &fd;
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
return rc;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned long hostaddr; unsigned long hostaddr;
@ -58,6 +88,7 @@ int main(int argc, char *argv[])
time_t start; time_t start;
long total = 0; long total = 0;
int duration; int duration;
size_t prev;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
@ -190,15 +221,23 @@ int main(int argc, char *argv[])
total += nread; total += nread;
prev = 0;
do { do {
/* write the same data over and over, until error or completion */ while ((rc = libssh2_channel_write(channel, ptr, nread)) ==
rc = libssh2_channel_write(channel, ptr, nread); LIBSSH2_ERROR_EAGAIN) {
if (LIBSSH2_ERROR_EAGAIN == rc) { /* must loop around */ waitsocket(sock, session);
continue; prev = 0;
} else if (rc < 0) { }
fprintf(stderr, "ERROR %d\n", rc); if (rc < 0) {
fprintf(stderr, "ERROR %d total %ld / %d prev %d\n", rc,
total, (int)nread, (int)prev);
break; break;
} else { }
else {
prev = nread;
if(rc > nread) {
fprintf(stderr, "MOO %d > %d\n", (int)rc, (int)nread);
}
/* rc indicates how many bytes were written this time */ /* rc indicates how many bytes were written this time */
nread -= rc; nread -= rc;
ptr += rc; ptr += rc;