/*
 *  GET.C
 *
 *  GET port
 *
 *  receives data from port and prints results, massive receive w/ multiple
 *  requests queued.
 */

#include "defs.h"

#define REQS	8

typedef struct IORequest IOR;

Iob iob[REQS];
char Buf[8192];

int
brk(void)
{
    return(0);
}

void
main(ac, av)
int ac;
char *av[];
{
    PORT *port = CreatePort(NULL, 0);
    long bytes = 0;
    short i;
    long j;

    onbreak(brk);
    iob[0].io_Message.mn_ReplyPort = port;
    iob[0].io_Port = atoi(av[1]);
    iob[0].io_Flags= PRO_DGRAM;

    if (OpenDevice("parnet.device", 0, (IOR *)&iob[0], 0)) {
	printf("Unable to open parnet.device, error %d %d\n", iob[0].io_Error, iob[0].io_Actual);
	exit(1);
    }
    printf("Device $%08lx Unit $%08lx\n", iob[0].io_Device, iob[0].io_Unit);

    iob[0].io_Command = CMD_READ;
    iob[0].io_Data    = (APTR)Buf;
    iob[0].io_Length  = sizeof(Buf);

    for (i = 1; i < REQS; ++i)
	iob[i] = iob[0];
    for (i = 0; i < REQS; ++i)
	SendIO((IOR *)&iob[i]);

    j = 0;

    for (;;) {
	long mask;
	Iob *io;

	mask = Wait(SIGBREAKF_CTRL_E | (1 << port->mp_SigBit));

	if (mask & SIGBREAKF_CTRL_E)
	    break;

	while (io = (Iob *)GetMsg(port)) {
	    bytes += io->io_Actual;
	    SendIO((IOR *)io);
	    ++j;
	    if ((j & 15) == 0)
		printf("Got %3ld %dK\n", j, bytes >> 10);
	}
    }
    printf("Aborting...\n");
    for (i = 0; i < REQS; ++i) {
	AbortIO((IOR *)&iob[i]);
	WaitIO((IOR *)&iob[i]);
    }
    printf("Closing...\n");
    CloseDevice((IOR *)&iob[0]);
    DeletePort(port);
}

