gopher/common.c

94 lines
1.4 KiB
C
Raw Normal View History

2012-04-28 01:18:46 +00:00
/*
*
* common routines.
*/
#pragma noroot
#pragma optimize 79
#include <Memory.h>
#include <MiscTool.h>
#include <tcpip.h>
#include <stdio.h>
#include "connection.h"
int read_binary(Word ipid, FILE *file)
{
Word rv = 0;
TCPIPPoll();
for(;;)
{
static char buffer[512];
rrBuff rb;
Word count;
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;
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;
}
fwrite(buffer, 1, count, file);
}
return rv;
}
int ConnectLoop(char *host, Word port, Connection *connection)
{
LongWord qtick;
ConnectionInit(connection, MMStartUp());
ConnectionOpenC(connection, host, port);
// 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;
}