CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

CONNECT.CPP (12154B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header:   F:\projects\c&c\vcs\code\connect.cpv   1.9   16 Oct 1995 16:48:56   JOE_BOSTIC  $ */
     17 /***************************************************************************
     18  **   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        **
     19  ***************************************************************************
     20  *                                                                         *
     21  *                 Project Name : Command & Conquer                        *
     22  *                                                                         *
     23  *                    File Name : CONNECT.CPP                              *
     24  *                                                                         *
     25  *                   Programmer : Bill Randolph                            *
     26  *                                                                         *
     27  *                   Start Date : December 20, 1994                        *
     28  *                                                                         *
     29  *                  Last Update : May 31, 1995 [BRR]								*
     30  *-------------------------------------------------------------------------*
     31  * Functions:    			                                                   *
     32  *   ConnectionClass::ConnectionClass -- class constructor                 *
     33  *   ConnectionClass::~ConnectionClass -- class destructor                 *
     34  *   ConnectionClass::Service -- main polling routine; services packets		*
     35  *   ConnectionClass::Time -- gets current time										*
     36  *   ConnectionClass::Command_Name -- returns name for a packet command		* 
     37  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     38 
     39 #include "function.h"
     40 
     41 #ifdef WWLIB32_H
     42 #include "TIMER.H"
     43 #else
     44 #include <sys\timeb.h>
     45 #endif
     46 
     47 /*
     48 ********************************* Globals ***********************************
     49 */
     50 char *ConnectionClass::Commands[PACKET_COUNT] = {
     51 	"ADATA",
     52 	"NDATA",
     53 	"ACK"
     54 };
     55 
     56 
     57 /***************************************************************************
     58  * ConnectionClass::ConnectionClass -- class constructor                   *
     59  *                                                                         *
     60  * If either max_retries or timeout is -1, that parameter is ignored in		*
     61  * timeout computations.  If both are -1, the connection will just keep		*
     62  * retrying forever.																			*
     63  *                                                                         *
     64  * INPUT:                                                                  *
     65  *		numsend			desired # of entries for the send queue					*
     66  *		numreceive		desired # of entries for the recieve queue				*
     67  *		maxlen			max length of an application packet							*
     68  *		magicnum			the packet "magic number" for this connection			*
     69  *		retry_delta		the time to wait between sends								*
     70  *		max_retries		the max # of retries allowed for a packet					*
     71  *							(-1 means retry forever, based on this parameter)		*
     72  *		timeout			the max amount of time before we give up on a packet	*
     73  *							(-1 means retry forever, based on this parameter)		*
     74  *                                                                         *
     75  * OUTPUT:                                                                 *
     76  *		none.																						*
     77  *                                                                         *
     78  * WARNINGS:                                                               *
     79  *		none.																						*
     80  *                                                                         *
     81  * HISTORY:                                                                *
     82  *   12/20/1994 BR : Created.                                              *
     83  *=========================================================================*/
     84 ConnectionClass::ConnectionClass (int maxlen, unsigned short magicnum, 
     85 	unsigned long retry_delta, unsigned long max_retries, unsigned long timeout)
     86 {
     87 	/*------------------------------------------------------------------------
     88 	Compute our maximum packet length
     89 	------------------------------------------------------------------------*/
     90 	MaxPacketLen = maxlen + sizeof(CommHeaderType);
     91 
     92 	/*------------------------------------------------------------------------
     93 	Assign the magic number
     94 	------------------------------------------------------------------------*/
     95 	MagicNum = magicnum;
     96 
     97 	/*------------------------------------------------------------------------
     98 	Initialize the retry time.  This is the time that t2 - t1 must be greater
     99 	than before a retry will occur.
    100 	------------------------------------------------------------------------*/
    101 	RetryDelta = retry_delta;
    102 
    103 	/*------------------------------------------------------------------------
    104 	Set the maximum allowable retries.
    105 	------------------------------------------------------------------------*/
    106 	MaxRetries = max_retries;
    107 
    108 	/*------------------------------------------------------------------------
    109 	Set the timeout for this connection.
    110 	------------------------------------------------------------------------*/
    111 	Timeout = timeout;
    112 
    113 	/*------------------------------------------------------------------------
    114 	Allocate the packet staging buffer.  This will be used to
    115 	------------------------------------------------------------------------*/
    116 	PacketBuf = new char[ MaxPacketLen ];
    117 
    118 }	/* end of ConnectionClass */
    119 
    120 
    121 /***************************************************************************
    122  * ConnectionClass::~ConnectionClass -- class destructor                   *
    123  *                                                                         *
    124  * INPUT:                                                                  *
    125  *		none.																						*
    126  *                                                                         *
    127  * OUTPUT:                                                                 *
    128  *		none.																						*
    129  *                                                                         *
    130  * WARNINGS:                                                               *
    131  *		none.																						*
    132  *                                                                         *
    133  * HISTORY:                                                                *
    134  *   12/20/1994 BR : Created.                                              *
    135  *=========================================================================*/
    136 ConnectionClass::~ConnectionClass ()
    137 {
    138 	/*------------------------------------------------------------------------
    139 	Free memory.
    140 	------------------------------------------------------------------------*/
    141 	delete [] PacketBuf;
    142 
    143 }	/* end of ~ConnectionClass */
    144 
    145 
    146 /***************************************************************************
    147  * ConnectionClass::Service -- main polling routine; services packets		*
    148  *                                                                         *
    149  * INPUT:                                                                  *
    150  *		none.																						*
    151  *                                                                         *
    152  * OUTPUT:                                                                 *
    153  *		1 = OK, 0 = error (connection is broken!)										*
    154  *                                                                         *
    155  * WARNINGS:                                                               *
    156  *		none.																						*
    157  *                                                                         *
    158  * HISTORY:                                                                *
    159  *   12/20/1994 BR : Created.                                              *
    160  *=========================================================================*/
    161 int ConnectionClass::Service (void)
    162 {
    163 	/*------------------------------------------------------------------------
    164 	Service the Send Queue.  This [re]sends packets in the Send Queue which
    165 	haven't been ACK'd yet, and if their retry timeout has expired, and 
    166 	updates the FirstTime, LastTime & SendCount values in the Queue entry.
    167 	Entries that have been ACK'd should be removed.
    168 	------------------------------------------------------------------------*/
    169 //	if (!Service_Send_Queue())
    170 //		return(0);
    171 
    172 	/*------------------------------------------------------------------------
    173 	Service the Receive Queue.  This sends ACKs for packets that haven't
    174 	been ACK'd yet.  Entries that the app has read, and have been ACK'd,
    175 	should be removed.
    176 	------------------------------------------------------------------------*/
    177 //	if (!Service_Receive_Queue())
    178 //		return(0);
    179 
    180 //	return(1);
    181 
    182 	if ( Service_Send_Queue() && Service_Receive_Queue() ) {
    183 		return(1);
    184 	} else {
    185 		return(0);
    186 	}
    187 
    188 }	/* end of Service */
    189 
    190 // ST = 12/17/2018 5:44PM
    191 #ifndef TickCount
    192 extern TimerClass					TickCount;
    193 #endif
    194 
    195 /***************************************************************************
    196  * ConnectionClass::Time -- gets current time										*
    197  *                                                                         *
    198  * INPUT:                                                                  *
    199  *                                                                         *
    200  * OUTPUT:                                                                 *
    201  *		none.																						*
    202  *                                                                         *
    203  * WARNINGS:                                                               *
    204  *		none.																						*
    205  *                                                                         *
    206  * HISTORY:                                                                *
    207  *   12/20/1994 BR : Created.                                              *
    208  *=========================================================================*/
    209 unsigned long ConnectionClass::Time (void)
    210 {
    211 #ifdef WWLIB32_H
    212 	return(TickCount.Time());				// Westwood Library time
    213 #else
    214 	static struct timeb mytime;			// DOS time
    215 	unsigned long msec;
    216 
    217 	ftime(&mytime);
    218 	msec = (unsigned long)mytime.time * 1000L + (unsigned long)mytime.millitm;
    219 	return((msec / 100) * 6);
    220 #endif
    221 
    222 }	/* end of Time */
    223 
    224 
    225 /*************************************************************************** 
    226  * ConnectionClass::Command_Name -- returns name for given packet command  * 
    227  *                                                                         * 
    228  * INPUT:                                                                  * 
    229  *		command		packet Command value to get name for							*
    230  *                                                                         * 
    231  * OUTPUT:                                                                 * 
    232  *		ptr to command name, NULL if invalid											*
    233  *                                                                         * 
    234  * WARNINGS:                                                               * 
    235  *		none.																						*
    236  *                                                                         * 
    237  * HISTORY:                                                                * 
    238  *   05/31/1995 BRR : Created.                                             * 
    239  *=========================================================================*/
    240 char *ConnectionClass::Command_Name(int command)
    241 {
    242 	if (command >= 0 && command < PACKET_COUNT) {
    243 		return(Commands[command]);
    244 	} else {
    245 		return(NULL);
    246 	}
    247 }
    248