version 1.1, 2005/03/01 22:28:18
|
version 1.2, 2005/03/03 03:26:04
|
|
|
#include <winerror.h> | #include <winerror.h> |
| |
#define MAX_CLIENTS 4 /* Max number of clients */ | #define MAX_CLIENTS 4 /* Max number of clients */ |
#define NUM_TESTS 2 /* Number of tests performed */ |
#define NUM_TESTS 3 /* Number of tests performed */ |
#define FIRST_CHAR 'A' /* First character in transferred pattern */ | #define FIRST_CHAR 'A' /* First character in transferred pattern */ |
#define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */ | #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */ |
#define BIND_TRIES 6 /* Number of bind() attempts */ | #define BIND_TRIES 6 /* Number of bind() attempts */ |
|
|
return p - buf; | return p - buf; |
} | } |
| |
|
static int do_nonblocking_send ( int id, SOCKET s, char *buf, int buflen, int sendlen ) |
|
{ |
|
char *last = buf + buflen, *p[MAX_CLIENTS] = NULL; |
|
int n; |
|
|
|
if ( p[id] == NULL ) { |
|
p[id] = buf; |
|
} |
|
|
|
n = send ( s, p[id], min ( sendlen, last - p[id] ), 0 ); |
|
p[id] += n; |
|
|
|
//wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" ); |
|
return p[id] - buf; |
|
} |
|
|
|
static int do_nonblocking_recv ( int id, SOCKET s, char *buf, int buflen, int recvlen ) |
|
{ |
|
char *last = buf + buflen, *p[MAX_CLIENTS] = NULL; |
|
int n; |
|
|
|
if ( p[id] == NULL ) { |
|
p[id] = buf; |
|
} |
|
|
|
n = recv ( s, p[id], min ( recvlen, last - p[id] ), 0 ); |
|
p[id] += n; |
|
|
|
//wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" ); |
|
return p[id] - buf; |
|
} |
|
|
/* | /* |
* Call this routine right after thread startup. | * Call this routine right after thread startup. |
* SO_OPENTYPE must by 0, regardless what the server did. | * SO_OPENTYPE must by 0, regardless what the server did. |
|
|
server_stop (); | server_stop (); |
} | } |
| |
|
/* |
|
* select_server: A non-blocking server. |
|
*/ |
|
static VOID WINAPI select_server ( server_params *par ) |
|
{ |
|
test_params *gen = par->general; |
|
server_memory *mem; |
|
int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i, |
|
id = GetCurrentThreadId(), n_connections = 0; |
|
char *p; |
|
struct timeval zerotime = {0,0}; |
|
fd_set fds_read, fds_write, fds_open; |
|
|
|
trace ( "select_server (%x) starting\n", id ); |
|
|
|
set_so_opentype ( FALSE ); /* non-overlapped */ |
|
server_start ( par ); |
|
mem = TlsGetValue ( tls ); |
|
|
|
wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n"); |
|
wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n"); |
|
|
|
trace ( "select_server (%x) ready\n", id ); |
|
SetEvent ( server_ready ); /* notify clients */ |
|
|
|
FD_ZERO ( &fds_openread ); |
|
FD_ZERO ( &fds_read ); |
|
FD_ZERO ( &fds_write ); |
|
|
|
FD_SET ( mem->s, &fds_open ); |
|
|
|
while(1) |
|
{ |
|
fds_read = fds_write = fds_open; |
|
|
|
select ( 0, &fds_read, &fds_write, NULL, &zerotime ); |
|
|
|
/* check for incoming requests */ |
|
if ( FD_ISSET ( mem->s, &fds_read ) ) { |
|
trace ( "select_server (%x): accepting client connection\n", id ); |
|
|
|
/* accept a single connection */ |
|
tmp = sizeof ( mem->sock[i].peer ); |
|
mem->sock[i].s = accept ( mem->s, (struct sockaddr*) &mem->sock[i].peer, &tmp ); |
|
wsa_ok ( mem->sock[i].s, INVALID_SOCKET !=, "select_server (%lx): accept failed: %d\n" ); |
|
|
|
ok ( mem->sock[i].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ), |
|
"select_server (%x): strange peer address\n", id ); |
|
|
|
/* add to list of open connections */ |
|
FD_SET ( mem->sock[i].s, &fds_open ); |
|
|
|
n_connections += 1; |
|
} |
|
|
|
/* handle open requests */ |
|
|
|
for ( i = 0; i < n_connections; i++ ) |
|
{ |
|
if ( (mem->sock[i].nread < n_expected) |
|
&& FD_ISSET( mem->sock[i].s, &fds_read ) ) { |
|
/* more data to receive */ |
|
|
|
/* Receive data & check it */ |
|
n_recvd = do_nonblocking_recv ( i, mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen ); |
|
//ok ( n_recvd == n_expected, |
|
// "select_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected ); |
|
|
|
if ( n_recvd == n_expected ) { |
|
p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks ); |
|
ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf); |
|
} |
|
|
|
} else if ( mem->sock[i].nread == n_expected |
|
&& FD_ISSET ( mem->sock[i].s, &fds_write ) ) { |
|
/* send data */ |
|
|
|
/* Echo data back */ |
|
n_sent = do_nonblocking_send ( i, mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen ); |
|
ok ( n_sent == n_expected, |
|
"select_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected ); |
|
|
|
|
|
|
|
} else { |
|
ok ( 0, "select_server (%x): too many bytes read\n", id ); |
|
} |
|
} |
|
} |
|
|
|
for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ ) |
|
{ |
|
/* cleanup */ |
|
read_zero_bytes ( mem->sock[i].s ); |
|
wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%lx): closesocket error: %d\n" ); |
|
mem->sock[i].s = INVALID_SOCKET; |
|
} |
|
|
|
trace ( "select_server (%x) exiting\n", id ); |
|
server_stop (); |
|
} |
|
|
/**************** Clients ***************/ | /**************** Clients ***************/ |
| |
/* | /* |
|
|
{ | { |
NULL, | NULL, |
0, | 0, |
|
64 |
|
}, |
|
event_client, |
|
{ |
|
NULL, |
|
WSA_FLAG_OVERLAPPED, |
|
128 |
|
} |
|
}, |
|
/* Test 2: event-driven client, non-blocking server via select() */ |
|
{ |
|
{ |
|
STD_STREAM_SOCKET, |
|
2048, |
|
16, |
|
2 |
|
}, |
|
select_server, |
|
{ |
|
NULL, |
|
0, |
64 | 64 |
}, | }, |
event_client, | event_client, |