-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
165 lines (121 loc) · 2.7 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
*
* common routines.
*/
#pragma noroot
#pragma optimize 79
#pragma debug 0x8000
#pragma lint -1
#include <Memory.h>
#include <MiscTool.h>
#include <tcpip.h>
#include <stdio.h>
#include "options.h"
#include "prototypes.h"
#include "connection.h"
int read_binary(Word ipid, FILE *file, ReadBlock *dcb)
{
Word rv = 0;
if (dcb) dcb->transferCount = 0;
IncBusy();
TCPIPPoll();
DecBusy();
for(;;)
{
static char buffer[512];
rrBuff rb;
Word count;
Word tcount;
IncBusy();
rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb);
DecBusy();
count = rb.rrBuffCount;
if (!count)
{
if (rv) break;
IncBusy();
TCPIPPoll();
DecBusy();
continue;
}
tcount = fwrite(buffer, 1, count, file);
if (dcb) dcb->transferCount += tcount;
if (tcount != count) return -1;
}
return 0;
}
// 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;
if (!count)
{
if (rv) return -1;
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;
}
int ConnectLoop(char *host, Word port, Connection *connection)
{
LongWord qtick;
ConnectionInit(connection, MMStartUp(), flags._v ? DisplayCallback : NULL);
ConnectionOpenC(connection, host, port);
// 30 second timeout.
qtick = GetTick() + 30 * 60;
while (!ConnectionPoll(connection))
{
if (GetTick() >= qtick)
{
fprintf(stderr, "Connection timed out.\n");
IncBusy();
TCPIPAbortTCP(connection->ipid);
TCPIPLogout(connection->ipid);
DecBusy();
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;
}