forked from electronicarts/CnC_Tiberian_Dawn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNULLCONN.CPP
265 lines (235 loc) · 12.3 KB
/
NULLCONN.CPP
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
** Command & Conquer(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Header: F:\projects\c&c\vcs\code\nullconn.cpv 1.10 16 Oct 1995 16:51:36 JOE_BOSTIC $ */
/***************************************************************************
** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
***************************************************************************
* *
* Project Name : Command & Conquer *
* *
* File Name : NULLCONN.CPP *
* *
* Programmer : Bill Randolph *
* *
* Start Date : April 5, 1995 *
* *
* Last Update : April 20, 1995 [DRD] *
* *
*-------------------------------------------------------------------------*
* Functions: *
* NullModemConnClass::NullModemConnClass -- class constructor *
* NullModemConnClass::~NullModemConnClass -- class destructor *
* NullModemConnClass::Init -- hardware-dependent initialization *
* NullModemConnClass::Send -- hardware-dependent packet sending *
* NullModemConnClass::Compute_CRC -- computes CRC for given buffer *
* NullModemConnClass::Packet_Overhead_Size -- number of extra bytes *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "function.h"
#include "wincomm.h"
#include "tcpip.h"
/***************************************************************************
* NullModemConnClass::NullModemConnClass -- class constructor *
* *
* INPUT: *
* numsend desired # send queue entries *
* numreceive desired # send receive entries *
* maxlen max length of application's packets *
* magicnum application-defined magic # for the packets *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
NullModemConnClass::NullModemConnClass (int numsend, int numreceive,
int maxlen, unsigned short magicnum) :
NonSequencedConnClass (numsend, numreceive, maxlen, magicnum,
60, // Retry Delta Time
-1, // Max Retries (-1 means ignore this timeout parameter)
1200) // Timeout: 20 seconds
{
/*------------------------------------------------------------------------
Pre-set the port value to NULL, so Send won't send until we've been Init'd
------------------------------------------------------------------------*/
PortHandle = NULL;
/*------------------------------------------------------------------------
Allocate the Send Buffer; the parent constructor has set MaxPacketLen,
so we can use it in our computation.
------------------------------------------------------------------------*/
// SendBuf = new char [MaxPacketLen + sizeof(int) * 3];
SendBuf = new char [ Actual_Max_Packet() ];
} /* end of NullModemConnClass */
/***************************************************************************
* NullModemConnClass::~NullModemConnClass -- class destructor *
* *
* INPUT: *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
NullModemConnClass::~NullModemConnClass ()
{
delete [] SendBuf;
} /* end of ~NullModemConnClass */
/***************************************************************************
* NullModemConnClass::Init -- hardware-dependent initialization *
* *
* INPUT: *
* port GreenLeaf port handle *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
void NullModemConnClass::Init (HANDLE port_handle)
{
NonSequencedConnClass::Init();
PortHandle = port_handle;
} /* end of Init */
/***************************************************************************
* NullModemConnClass::Send -- hardware-dependent packet sending *
* *
* INPUT: *
* port GreenLeaf port handle *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* 1 = OK, 0 = error *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int NullModemConnClass::Send (char *buf, int buflen)
{
//int status;
int *ibuf;
SerialHeaderType *header;
unsigned long sendlen;
/*------------------------------------------------------------------------
Error if we haven't been properly initialized
------------------------------------------------------------------------*/
if ( PortHandle == NULL )
return(false);
/*------------------------------------------------------------------------
Package the data into the Send Buffer
------------------------------------------------------------------------*/
header = (SerialHeaderType *) SendBuf;
header->MagicNumber = PACKET_SERIAL_START;
header->Length = (short) buflen;
header->MagicNumber2 = PACKET_SERIAL_VERIFY;
sendlen = sizeof( SerialHeaderType );
memcpy (SendBuf + sendlen, buf, buflen);
sendlen += buflen;
ibuf = (int *)(SendBuf + sendlen);
*ibuf = Compute_CRC( buf, buflen );
sendlen += sizeof( int );
*(SendBuf + sendlen) = '\r';
sendlen += 1;
/*------------------------------------------------------------------------
Send the data
------------------------------------------------------------------------*/
//status =
#ifdef FORCE_WINSOCK
if (Winsock.Get_Connected() || GameToPlay == GAME_INTERNET){
Winsock.Write(SendBuf, (int)sendlen);
}else{
SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
}
#else
SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
#endif //WINSOCK
//if ( status == ASSUCCESS ) {
return(true);
//} else {
// Smart_Printf( "Write Buffer status %d, Port->status %d, sendlen %d \n", status, Port->status, sendlen );
// return(false);
//}
}
/***************************************************************************
* NullModemConnClass::Compute_CRC -- computes CRC for given buffer *
* *
* INPUT: *
* buf buffer to compute CRC for *
* buflen length of buffer in bytes *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int NullModemConnClass::Compute_CRC (char *buf, int buflen)
{
unsigned int sum, hibit;
sum = 0;
for (int i = 0; i < buflen; i++) {
if ( sum & 0x80000000 ) { // check hi bit to rotate into low bit
hibit = 1;
} else {
hibit = 0;
}
sum <<= 1;
sum += (hibit + (unsigned char)buf[i]);
}
return((int)sum);
}
/***************************************************************************
* NullModemConnClass::Packet_Overhead_Size -- number of extra bytes *
* *
* INPUT: *
* none. *
* *
* OUTPUT: *
* number of bytes used for communications only. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 04/20/1995 DRD : Created. *
*=========================================================================*/
int NullModemConnClass::Packet_Overhead_Size ( void )
{
//
// short for Null Modem Magic Number
// short for Null Modem length of packet
// int for Null Modem CRC check
// CommHeaderType for Queued packets
//
return( (PACKET_SERIAL_OVERHEAD_SIZE + sizeof(CommHeaderType)) );
} /* end of Packet_Overhead_Size */