diff --git a/common.c b/common.c new file mode 100644 index 0000000..c93440e --- /dev/null +++ b/common.c @@ -0,0 +1,88 @@ +/* + * + * common routines. + */ +#pragma noroot +#pragma optimize 79 + +#include +#include +#include + + +#include + +#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; +} \ No newline at end of file diff --git a/prototypes.h b/prototypes.h new file mode 100644 index 0000000..cc0e452 --- /dev/null +++ b/prototypes.h @@ -0,0 +1,23 @@ +#ifndef __prototypes_h__ +#define __prototypes_h__ + +#include + +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