-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpconnection.c
60 lines (52 loc) · 1.42 KB
/
tcpconnection.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
#pragma noroot
#include <tcpip.h>
#include <stdlib.h>
#include <orca.h>
#include <misctool.h>
#include "session.h"
#include "tcpconnection.h"
/* Make a TCP connection for a session.
*
* On success, returns 0 and sets sess->ipid.
* On failure, returns an error code.
*/
enum NetDiskError StartTCPConnection(Session *sess) {
Word tcperr;
srBuff mySRBuff;
LongWord initialTime;
/* End any existing TCP connection */
EndTCPConnection(sess);
sess->ipid =
TCPIPLogin(userid(), sess->ipAddr, sess->port, 0, 0x40);
if (toolerror())
return NETWORK_ERROR;
tcperr = TCPIPOpenTCP(sess->ipid);
if (toolerror()) {
TCPIPLogout(sess->ipid);
return NETWORK_ERROR;
} else if (tcperr != tcperrOK) {
TCPIPLogout(sess->ipid);
return NO_RESPONSE;
}
initialTime = GetTick();
do {
TCPIPPoll();
TCPIPStatusTCP(sess->ipid, &mySRBuff);
} while (mySRBuff.srState == TCPSSYNSENT && GetTick()-initialTime < 15*60);
if (mySRBuff.srState != TCPSESTABLISHED) {
TCPIPAbortTCP(sess->ipid);
TCPIPLogout(sess->ipid);
return NO_RESPONSE;
}
sess->tcpLoggedIn = TRUE;
return 0;
}
void EndTCPConnection(Session *sess) {
if (sess->tcpLoggedIn) {
TCPIPPoll();
TCPIPAbortTCP(sess->ipid);
TCPIPLogout(sess->ipid);
sess->ipid = 0;
sess->tcpLoggedIn = FALSE;
}
}