forked from electronicarts/CnC_Tiberian_Dawn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPXGCONN.CPP
473 lines (425 loc) · 22 KB
/
IPXGCONN.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
** 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\ipxgconn.cpv 1.9 16 Oct 1995 16:51:00 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 : IPXGCONN.CPP *
* *
* Programmer : Bill Randolph *
* *
* Start Date : December 20, 1994 *
* *
* Last Update : July 6, 1995 [BRR] *
*-------------------------------------------------------------------------*
* Functions: *
* IPXGlobalConnClass::IPXGlobalConnClass -- class constructor *
* IPXGlobalConnClass::~IPXGlobalConnClass -- class destructor *
* IPXGlobalConnClass::Send_Packet -- adds a packet to the send queue *
* IPXGlobalConnClass::Receive_Packet -- adds packet to the receive queue*
* IPXGlobalConnClass::Get_Packet -- gets a packet from the receive queue*
* IPXGlobalConnClass::Send -- sends a packet *
* IPXGlobalConnClass::Service_Receive_Queue -- services recieve queue *
* IPXGlobalConnClass::Set_Bridge -- Sets up connection to cross a bridge*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "function.h"
/***************************************************************************
* IPXGlobalConnClass::IPXGlobalConnClass -- class constructor *
* *
* This routine chains to the parent constructor, but it adjusts the size *
* of the packet by the added bytes in the GlobalHeaderType structure. *
* This forces the parent classes to allocate the proper sized PacketBuf *
* for outgoing packets, and to set MaxPacketLen to the proper value. *
* *
* INPUT: *
* numsend desired # of entries for the send queue *
* numreceive desired # of entries for the recieve queue *
* maxlen max length of an application packet *
* product_id unique ID for this product *
* *
* OUTPUT: *
* none. *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
IPXGlobalConnClass::IPXGlobalConnClass (int numsend, int numreceive, int maxlen,
unsigned short product_id) :
IPXConnClass (numsend, numreceive,
maxlen + sizeof(GlobalHeaderType) - sizeof(CommHeaderType),
GLOBAL_MAGICNUM, // magic number for this connection
NULL, // IPX Address (none)
0, // Connection ID
"") // Connection Name
{
ProductID = product_id;
IsBridge = 0;
} /* end of IPXGlobalConnClass */
/***************************************************************************
* IPXGlobalConnClass::Send_Packet -- adds a packet to the send queue *
* *
* This routine prefixes the given buffer with a GlobalHeaderType and *
* queues the resulting packet into the Send Queue. The packet's *
* MagicNumber, Code, PacketID, destination Address and ProductID are set *
* here. *
* *
* INPUT: *
* buf buffer to send *
* buflen length of buffer *
* address address to send the packet to (NULL = Broadcast) *
* ack_req true = ACK is required for this packet; false = isn't *
* *
* OUTPUT: *
* 1 = OK, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int IPXGlobalConnClass::Send_Packet (void * buf, int buflen,
IPXAddressClass *address, int ack_req)
{
/*------------------------------------------------------------------------
Store the packet's Magic Number
------------------------------------------------------------------------*/
((GlobalHeaderType *)PacketBuf)->Header.MagicNumber = MagicNum;
/*------------------------------------------------------------------------
If this is a ACK-required packet, sent to a specific system, mark it as
ACK-required; otherwise, mark as no-ACK-required.
------------------------------------------------------------------------*/
if (ack_req && address != NULL) {
((GlobalHeaderType *)PacketBuf)->Header.Code = PACKET_DATA_ACK;
} else {
((GlobalHeaderType *)PacketBuf)->Header.Code = PACKET_DATA_NOACK;
}
/*------------------------------------------------------------------------
Fill in the packet ID. This will have very limited meaning; it only
allows us to determine if an ACK packet we receive later goes with this
packet; it doesn't let us detect re-sends of other systems' packets.
------------------------------------------------------------------------*/
((GlobalHeaderType *)PacketBuf)->Header.PacketID = Queue->Send_Total();
/*------------------------------------------------------------------------
Set the product ID for this packet.
------------------------------------------------------------------------*/
((GlobalHeaderType *)PacketBuf)->ProductID = ProductID;
/*------------------------------------------------------------------------
Set this packet's destination address. If no address is specified, use
a Broadcast address (which IPXAddressClass's default constructor creates).
------------------------------------------------------------------------*/
if (address != NULL) {
((GlobalHeaderType *)PacketBuf)->Address = (*address);
} else {
((GlobalHeaderType *)PacketBuf)->Address = IPXAddressClass();
}
/*------------------------------------------------------------------------
Copy the application's data
------------------------------------------------------------------------*/
memcpy(PacketBuf + sizeof(GlobalHeaderType), buf, buflen);
/*------------------------------------------------------------------------
Queue it
------------------------------------------------------------------------*/
return(Queue->Queue_Send(PacketBuf,buflen + sizeof(GlobalHeaderType)));
} /* end of Send_Packet */
/***************************************************************************
* IPXGlobalConnClass::Receive_Packet -- adds packet to the receive queue *
* *
* INPUT: *
* buf buffer to process (already includes GlobalHeaderType) *
* buflen length of buffer to process *
* address the address of the sender (the IPX Manager class must *
* extract this from the IPX Header of the received packet.) *
* *
* OUTPUT: *
* 1 = OK, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int IPXGlobalConnClass::Receive_Packet (void * buf, int buflen,
IPXAddressClass *address)
{
GlobalHeaderType *packet; // ptr to this packet
SendQueueType *send_entry; // ptr to send entry header
GlobalHeaderType *entry_data; // ptr to queue entry data
int i;
/*
--------------------------- Check the magic # ----------------------------
*/
packet = (GlobalHeaderType *)buf;
if (packet->Header.MagicNumber!=MagicNum) {
return(false);
}
/*------------------------------------------------------------------------
Process the packet based on its Code
------------------------------------------------------------------------*/
switch (packet->Header.Code) {
/*.....................................................................
DATA: Save the given address in the message buffer (so Get_Message()
can extract it later), and queue this message.
Don't bother checking for a Re-Send; since this queue is receiving data
from multiple systems, the Total_Receive() value for this queue will
have nothing to do with the packet's ID. The application must deal
with this by being able to handle multiple receipts of the same packet.
.....................................................................*/
case PACKET_DATA_ACK:
case PACKET_DATA_NOACK:
packet->Address = (*address);
Queue->Queue_Receive (buf, buflen);
break;
/*.....................................................................
ACK: If this ACK is for any of my packets, mark that packet as
acknowledged, then throw this packet away. Otherwise, ignore the ACK
(if we re-sent before we received the other system's first ACK, this
ACK will be a leftover)
.....................................................................*/
case PACKET_ACK:
for (i = 0; i < Queue->Num_Send(); i++) {
/*
..................... Get queue entry ptr .......................
*/
send_entry = Queue->Get_Send(i);
/*
............. If ptr is valid, get ptr to its data ..............
*/
entry_data = (GlobalHeaderType *)(send_entry->Buffer);
/*
.............. If ACK is for this entry, mark it ................
*/
if (packet->Header.PacketID==entry_data->Header.PacketID &&
entry_data->Header.Code == PACKET_DATA_ACK) {
send_entry->IsACK = 1;
break;
}
}
break;
/*.....................................................................
Default: ignore the packet
.....................................................................*/
default:
break;
} /* end of switch */
return(true);
}
/***************************************************************************
* IPXGlobalConnClass::Get_Packet -- gets a packet from the receive queue *
* *
* INPUT: *
* buf location to store buffer *
* buflen filled in with length of 'buf' *
* address filled in with sender's address *
* product_id filled in with sender's ProductID *
* *
* OUTPUT: *
* 1 = OK, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int IPXGlobalConnClass::Get_Packet (void * buf, int *buflen,
IPXAddressClass *address, unsigned short *product_id)
{
ReceiveQueueType *rec_entry; // ptr to receive entry header
GlobalHeaderType *packet;
int packetlen; // size of received packet
/*
------------------------ Return if nothing to do -------------------------
*/
if (Queue->Num_Receive() == 0) {
return(false);
}
/*
------------------ Get ptr to the next available entry -------------------
*/
rec_entry = Queue->Get_Receive(0);
/*
------------------------ Read it if it's un-read -------------------------
*/
if (rec_entry!=NULL && rec_entry->IsRead==0) {
/*
........................... Mark as read ..............................
*/
rec_entry->IsRead = 1;
/*
.......................... Copy data packet ...........................
*/
packet = (GlobalHeaderType *)(rec_entry->Buffer);
packetlen = rec_entry->BufLen - sizeof(GlobalHeaderType);
if (packetlen > 0)
memcpy(buf, rec_entry->Buffer + sizeof(GlobalHeaderType), packetlen);
(*buflen) = packetlen;
(*address) = packet->Address;
(*product_id) = packet->ProductID;
return(true);
}
return(false);
}
/***************************************************************************
* IPXGlobalConnClass::Send -- sends a packet *
* *
* This routine gets invoked by NonSequencedConn, when it's processing *
* the Send & Receive Queues. The buffer provided will already have the *
* GlobalHeaderType header embedded in it. *
* *
* INPUT: *
* buf buffer to send *
* buflen length of buffer *
* *
* OUTPUT: *
* 1 = OK, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int IPXGlobalConnClass::Send(char *buf, int buflen)
{
IPXAddressClass *addr;
int rc;
/*------------------------------------------------------------------------
Extract the packet's embedded IPX address
------------------------------------------------------------------------*/
addr = &(((GlobalHeaderType *)buf)->Address);
/*------------------------------------------------------------------------
If it's a broadcast address, broadcast it
------------------------------------------------------------------------*/
if (addr->Is_Broadcast()) {
return(Broadcast (buf, buflen));
} else {
/*------------------------------------------------------------------------
Otherwise, send it
------------------------------------------------------------------------*/
if (IsBridge && !memcmp (addr, BridgeNet, 4)) {
rc = Send_To (buf, buflen, &(((GlobalHeaderType *)buf)->Address),
BridgeNode);
} else {
rc = Send_To (buf, buflen, &(((GlobalHeaderType *)buf)->Address), NULL);
}
return (rc);
}
} /* end of Send */
/***************************************************************************
* IPXGlobalConnClass::Service_Receive_Queue -- services the recieve queue *
* *
* This routine is necessary because the Global Connection has to ACK *
* a packet differently from other types of connections; its Send routine *
* assumes that the destination address is embedded within the outgoing *
* packet, so we have to create our ACK Packet using the GlobalHeaderType, *
* not the CommHeaderType. *
* *
* INPUT: *
* none. *
* *
* OUTPUT: *
* 1 = OK, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 12/20/1994 BR : Created. *
*=========================================================================*/
int IPXGlobalConnClass::Service_Receive_Queue (void)
{
GlobalHeaderType ackpacket; // ACK packet to send
ReceiveQueueType *rec_entry; // ptr to receive entry header
GlobalHeaderType *packet_hdr; // packet header
/*------------------------------------------------------------------------
Get a pointer to the next received entry
------------------------------------------------------------------------*/
rec_entry = Queue->Get_Receive(0);
if (rec_entry==NULL)
return(1);
/*------------------------------------------------------------------------
If this packet doesn't require an ACK, mark it as ACK'd.
------------------------------------------------------------------------*/
packet_hdr = (GlobalHeaderType *)(rec_entry->Buffer);
if (packet_hdr->Header.Code==PACKET_DATA_NOACK)
rec_entry->IsACK = 1;
/*------------------------------------------------------------------------
If this packet hasn't been ACK'd, send an ACK:
- Fill in the MagicNum & the Code
- Set the PacketID to the same ID that the sending system used, so the
sending system knows which packet the ACK is for
------------------------------------------------------------------------*/
if (rec_entry->IsACK==0) {
ackpacket.Header.MagicNumber = MagicNum;
ackpacket.Header.Code = PACKET_ACK;
ackpacket.Header.PacketID = packet_hdr->Header.PacketID;
ackpacket.Address = packet_hdr->Address;
ackpacket.ProductID = ProductID;
Send ((char *)&ackpacket, sizeof(GlobalHeaderType));
rec_entry->IsACK = 1;
}
/*------------------------------------------------------------------------
If this packet has been read by the application, and has been ACK'd, and
there is another packet in the queue behind this one, it means the other
system got the ACK we sent for this packet; remove this packet from the
queue.
------------------------------------------------------------------------*/
if (rec_entry!=NULL && rec_entry->IsRead && rec_entry->IsACK &&
Queue->Num_Receive() > 1)
Queue->UnQueue_Receive(NULL,NULL,0);
return(1);
} /* end of Service_Receive_Queue */
/***************************************************************************
* Set_Bridge -- Sets up connection to cross a bridge *
* *
* This routine is designed to prevent the connection from having to *
* call Get_Local_Target, except the minimum number of times, since that *
* routine is buggy & goes away for long periods sometimes. *
* *
* INPUT: *
* bridge network number of the destination bridge *
* *
* OUTPUT: *
* none *
* *
* WARNINGS: *
* none *
* *
* HISTORY: *
* 07/06/1995 BRR : Created. *
*=========================================================================*/
void IPXGlobalConnClass::Set_Bridge(NetNumType bridge)
{
if (Configured) {
memcpy (BridgeNet, bridge, 4);
memset (BridgeNode, 0xff, 6);
if (IPX_Get_Local_Target (BridgeNet, BridgeNode, Socket, BridgeNode)==0) {
IsBridge = 1;
} else {
IsBridge = 0;
}
}
}