common code/prototypes

This commit is contained in:
Kelvin Sherlock 2012-04-27 21:18:46 -04:00
parent 20ad89176f
commit 86b60c74e9
2 changed files with 111 additions and 0 deletions

88
common.c Normal file
View File

@ -0,0 +1,88 @@
/*
*
* 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;
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb);
count = rb.rrBuffCount;
if (!count)
{
if (rv) break;
TCPIPPoll();
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");
TCPIPAbortTCP(connection->ipid);
TCPIPLogout(connection->ipid);
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;
}

23
prototypes.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef __prototypes_h__
#define __prototypes_h__
#include <stdio.h>
int read_binary(unsigned ipid, FILE *file);
int setfiletype(const char *filename);
#ifdef __CONNECTION_H__
int ConnectLoop(char *host, Word port, Connection *connection);
int CloseLoop(Connection *connection);
#endif
#ifdef __url_h__
int do_gopher(const char *url, URLComponents *components, FILE *file);
int do_http_0_9(const char *url, URLComponents *components, FILE *file);
int do_http_1_0(const char *url, URLComponents *components, FILE *file);
int do_http_1_1(const char *url, URLComponents *components, FILE *file);
#endif
#endif