gopher/common.c

166 lines
2.7 KiB
C
Raw Normal View History

2012-04-28 01:18:46 +00:00
/*
*
* common routines.
*/
#pragma noroot
#pragma optimize 79
#pragma debug 0x8000
#pragma lint -1
2012-04-28 01:18:46 +00:00
#include <Memory.h>
#include <MiscTool.h>
#include <tcpip.h>
#include <stdio.h>
#include "options.h"
2012-09-11 00:50:51 +00:00
#include "prototypes.h"
#include "connection.h"
2012-04-28 01:18:46 +00:00
2012-09-11 00:50:51 +00:00
int read_binary(Word ipid, FILE *file, ReadBlock *dcb)
2012-04-28 01:18:46 +00:00
{
Word rv = 0;
2012-09-11 00:50:51 +00:00
if (dcb) dcb->transferCount = 0;
IncBusy();
2012-04-28 01:18:46 +00:00
TCPIPPoll();
2012-09-11 00:50:51 +00:00
DecBusy();
2012-04-28 01:18:46 +00:00
for(;;)
{
static char buffer[512];
rrBuff rb;
Word count;
2012-09-11 00:50:51 +00:00
Word tcount;
2012-04-28 01:18:46 +00:00
2012-08-26 19:51:46 +00:00
IncBusy();
2012-04-28 01:18:46 +00:00
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb);
2012-08-26 19:51:46 +00:00
DecBusy();
2012-04-28 01:18:46 +00:00
count = rb.rrBuffCount;
2012-04-28 01:18:46 +00:00
if (!count)
{
if (rv) break;
2012-08-26 19:51:46 +00:00
IncBusy();
2012-04-28 01:18:46 +00:00
TCPIPPoll();
2012-08-26 19:51:46 +00:00
DecBusy();
2012-04-28 01:18:46 +00:00
continue;
}
2012-09-11 00:50:51 +00:00
tcount = fwrite(buffer, 1, count, file);
if (dcb) dcb->transferCount += tcount;
if (tcount != count) return -1;
2012-04-28 01:18:46 +00:00
}
2012-09-11 00:50:51 +00:00
return 0;
2012-04-28 01:18:46 +00:00
}
2012-09-11 00:50:51 +00:00
// ReadTCP will only return if the entire request is fulfilled or the connection closes,
// so it could just do that. For a large request, this is probably nicer.
int read_binary_size(Word ipid, FILE *file, ReadBlock *dcb)
{
Word rv = 0;
LongWord size;
if (!dcb) return -1;
dcb->transferCount = 0;
size = dcb->requestCount;
if (!size) return 0;
IncBusy();
TCPIPPoll();
DecBusy();
for(;;)
{
static char buffer[512];
rrBuff rb;
Word count;
Word tcount;
count = 512;
if (count > size) count = size;
IncBusy();
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, count, &rb);
DecBusy();
count = rb.rrBuffCount;
2012-09-11 00:50:51 +00:00
if (!count)
{
if (rv) return -1;
2012-09-11 00:50:51 +00:00
IncBusy();
TCPIPPoll();
DecBusy();
continue;
}
tcount = fwrite(buffer, 1, count, file);
dcb->transferCount += tcount;
size -= tcount;
if (tcount != count) return -1;
if (!size) return 0;
}
return 0;
}
2012-04-28 01:18:46 +00:00
int ConnectLoop(char *host, Word port, Connection *connection)
{
LongWord qtick;
2012-04-28 01:18:46 +00:00
ConnectionInit(connection, MMStartUp(), flags._v ? DisplayCallback : NULL);
ConnectionOpenC(connection, host, port);
2012-04-28 01:18:46 +00:00
// 30 second timeout.
qtick = GetTick() + 30 * 60;
while (!ConnectionPoll(connection))
{
if (GetTick() >= qtick)
{
fprintf(stderr, "Connection timed out.\n");
2012-08-26 19:51:46 +00:00
IncBusy();
2012-04-28 01:18:46 +00:00
TCPIPAbortTCP(connection->ipid);
TCPIPLogout(connection->ipid);
2012-08-26 19:51:46 +00:00
DecBusy();
2012-04-28 01:18:46 +00:00
return 0;
}
}
if (connection->state != kConnectionStateConnected)
{
fprintf(stderr, "Unable to open host: %s:%u\n",
host,
port);
return 0;
}
return 1;
}
int CloseLoop(Connection *connection)
{
ConnectionClose(connection);
while (!ConnectionPoll(connection)) ; // wait for it to close.
return 1;
}