CnC_Remastered_Collection

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

NULLCONN.CPP (13110B)


      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: /CounterStrike/NULLCONN.CPP 1     3/03/97 10:25a 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 : NULLCONN.CPP                             *
     24  *                                                                         *
     25  *                   Programmer : Bill Randolph                            *
     26  *                                                                         *
     27  *                   Start Date : April 5, 1995		                        *
     28  *                                                                         *
     29  *                  Last Update : April 20, 1995   [DRD]                   *
     30  *                                                                         *
     31  *-------------------------------------------------------------------------*
     32  * Functions:    			                                                   *
     33  *   NullModemConnClass::NullModemConnClass -- class constructor           *
     34  *   NullModemConnClass::~NullModemConnClass -- class destructor           *
     35  *   NullModemConnClass::Init -- hardware-dependent initialization			*
     36  *   NullModemConnClass::Send -- hardware-dependent packet sending			*
     37  *   NullModemConnClass::Compute_CRC -- computes CRC for given buffer		*
     38  *   NullModemConnClass::Packet_Overhead_Size -- number of extra bytes     *
     39  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     40 
     41 #include	"function.h"
     42 #ifdef WIN32
     43 #include "wincomm.h"
     44 #endif	//WIN32
     45 #include <stdio.h>
     46 //#include <mem.h>
     47 #include "nullconn.h"
     48 
     49 
     50 /***************************************************************************
     51  * NullModemConnClass::NullModemConnClass -- class constructor             *
     52  *                                                                         *
     53  * INPUT:                                                                  *
     54  *		numsend			desired # send queue entries									*
     55  *		numreceive		desired # send receive entries								*
     56  *		maxlen			max length of application's packets							*
     57  *		magicnum			application-defined magic # for the packets				*
     58  *                                                                         *
     59  * OUTPUT:                                                                 *
     60  *		none.																						*
     61  *                                                                         *
     62  * WARNINGS:                                                               *
     63  *		none.																						*
     64  *                                                                         *
     65  * HISTORY:                                                                *
     66  *   12/20/1994 BR : Created.                                              *
     67  *=========================================================================*/
     68 NullModemConnClass::NullModemConnClass (int numsend, int numreceive,
     69 	int maxlen, unsigned short magicnum) :
     70 	ConnectionClass (numsend, numreceive, maxlen, magicnum,
     71 		60, 			// Retry Delta Time
     72 		-1, 			// Max Retries (-1 means ignore this timeout parameter)
     73 		1200)			// Timeout: 20 seconds
     74 {
     75 	/*------------------------------------------------------------------------
     76 	Pre-set the port value to NULL, so Send won't send until we've been Init'd
     77 	------------------------------------------------------------------------*/
     78 #ifdef WIN32
     79 	PortHandle = NULL;
     80 #else	//WIN32
     81 	Port = NULL;
     82 #endif	//WIN32
     83 	/*------------------------------------------------------------------------
     84 	Allocate the Send Buffer; the parent constructor has set MaxPacketLen,
     85 	so we can use it in our computation.
     86 	------------------------------------------------------------------------*/
     87 	SendBuf = new char [ Actual_Max_Packet() ];
     88 
     89 }	/* end of NullModemConnClass */
     90 
     91 
     92 /***************************************************************************
     93  * NullModemConnClass::~NullModemConnClass -- class destructor             *
     94  *                                                                         *
     95  * INPUT:                                                                  *
     96  *                                                                         *
     97  * OUTPUT:                                                                 *
     98  *		none.																						*
     99  *                                                                         *
    100  * WARNINGS:                                                               *
    101  *		none.																						*
    102  *                                                                         *
    103  * HISTORY:                                                                *
    104  *   12/20/1994 BR : Created.                                              *
    105  *=========================================================================*/
    106 NullModemConnClass::~NullModemConnClass ()
    107 {
    108 	delete [] SendBuf;
    109 
    110 }	/* end of ~NullModemConnClass */
    111 
    112 
    113 /***************************************************************************
    114  * NullModemConnClass::Init -- hardware-dependent initialization				*
    115  *                                                                         *
    116  * INPUT:                                                                  *
    117  *		port		GreenLeaf port handle													*
    118  *                                                                         *
    119  * OUTPUT:                                                                 *
    120  *		none.																						*
    121  *                                                                         *
    122  * WARNINGS:                                                               *
    123  *		none.																						*
    124  *                                                                         *
    125  * HISTORY:                                                                *
    126  *   12/20/1994 BR : Created.                                              *
    127  *=========================================================================*/
    128 #ifdef WIN32
    129 void NullModemConnClass::Init (HANDLE port_handle)
    130 {
    131 	ConnectionClass::Init();
    132 	PortHandle = port_handle;
    133 }
    134 #else	//WIN32
    135 void NullModemConnClass::Init (PORT *port)
    136 {
    137 	ConnectionClass::Init();
    138 	Port = port;
    139 }	/* end of Init */
    140 #endif	//WIN32
    141 
    142 /***************************************************************************
    143  * NullModemConnClass::Send -- hardware-dependent packet sending				*
    144  *                                                                         *
    145  * INPUT:                                                                  *
    146  *		buf			buffer to send															*
    147  *		buflen		length of buffer to send											*
    148  *		extrabuf		(not used by this class)											*
    149  *		extralen		(not used by this class)											*
    150  *                                                                         *
    151  * OUTPUT:                                                                 *
    152  *		none.																						*
    153  *                                                                         *
    154  * WARNINGS:                                                               *
    155  *		1 = OK, 0 = error																		*
    156  *                                                                         *
    157  * HISTORY:                                                                *
    158  *   12/20/1994 BR : Created.                                              *
    159  *=========================================================================*/
    160 int NullModemConnClass::Send (char *buf, int buflen, void *, int )
    161 {
    162 	return 0;
    163 #if (0)//PG
    164 	int *ibuf;
    165 	SerialHeaderType *header;
    166 	unsigned long sendlen;
    167 
    168 	/*------------------------------------------------------------------------
    169 	Error if we haven't been properly initialized
    170 	------------------------------------------------------------------------*/
    171 #ifdef WIN32
    172 	if ( PortHandle == NULL ) return(false);
    173 
    174 #else	//WIN32
    175 	int status;
    176 	if ( Port == NULL ) {
    177 		return(0);
    178 	}
    179 #endif	//WIN32
    180 
    181 	/*------------------------------------------------------------------------
    182 	Package the data into the Send Buffer
    183 	------------------------------------------------------------------------*/
    184 	header = (SerialHeaderType *) SendBuf;
    185 	header->MagicNumber	= PACKET_SERIAL_START;
    186 	header->Length			= (short) buflen;
    187 	header->MagicNumber2	= PACKET_SERIAL_VERIFY;
    188 
    189 	sendlen = sizeof( SerialHeaderType );
    190 	memcpy (SendBuf + sendlen, buf, buflen);
    191 	sendlen += buflen;
    192 	ibuf = (int *)(SendBuf + sendlen);
    193 	*ibuf = Compute_CRC( buf, buflen );
    194 	sendlen += sizeof( int );
    195 
    196 	*(SendBuf + sendlen) = '\r';
    197 	sendlen += 1;
    198 
    199 	/*------------------------------------------------------------------------
    200 	Send the data
    201 	------------------------------------------------------------------------*/
    202 #ifdef WIN32
    203 		SerialPort->Write_To_Serial_Port((unsigned char *)SendBuf, (int)sendlen );
    204 		return (true);
    205 
    206 #else	//WIN32
    207 	status = WriteBuffer( Port, SendBuf, sendlen );
    208 	if ( status == ASSUCCESS ) {
    209 		return(1);
    210 	}
    211 	else {
    212 		return(0);
    213 	}
    214 #endif	//WIN32
    215 #endif//PG
    216 }	/* end of Send */
    217 
    218 
    219 /***************************************************************************
    220  * NullModemConnClass::Compute_CRC -- computes CRC for given buffer			*
    221  *                                                                         *
    222  * INPUT:                                                                  *
    223  *		buf		buffer to compute CRC for												*
    224  *		buflen	length of buffer in bytes												*
    225  *                                                                         *
    226  * OUTPUT:                                                                 *
    227  *		none.																						*
    228  *                                                                         *
    229  * WARNINGS:                                                               *
    230  *		none.																						*
    231  *                                                                         *
    232  * HISTORY:                                                                *
    233  *   12/20/1994 BR : Created.                                              *
    234  *=========================================================================*/
    235 int NullModemConnClass::Compute_CRC (char *buf, int buflen)
    236 {
    237 	unsigned int sum, hibit;
    238 
    239 	sum = 0;
    240 	for (int i = 0; i < buflen; i++) {
    241 		if ( sum & 0x80000000 ) {		// check hi bit to rotate into low bit
    242 			hibit = 1;
    243 		}
    244 		else {
    245 			hibit = 0;
    246 		}
    247 
    248 		sum <<= 1;
    249 		sum += (hibit + (unsigned char)buf[i]);
    250 	}
    251 
    252 	return((int)sum);
    253 
    254 }	/* end of Compute_CRC */
    255 
    256 
    257 /***************************************************************************
    258  * NullModemConnClass::Packet_Overhead_Size -- number of extra bytes       *
    259  *                                                                         *
    260  * INPUT:                                                                  *
    261  *		none.																						*
    262  *                                                                         *
    263  * OUTPUT:                                                                 *
    264  *		number of bytes used for communications only.								*
    265  *                                                                         *
    266  * WARNINGS:                                                               *
    267  *		none.																						*
    268  *                                                                         *
    269  * HISTORY:                                                                *
    270  *   04/20/1995 DRD : Created.                                             *
    271  *=========================================================================*/
    272 int NullModemConnClass::Packet_Overhead_Size ( void )
    273 {
    274 	/*------------------------------------------------------------------------
    275 	short for Null Modem Magic Number
    276 	short for Null Modem length of packet
    277 	int for Null Modem CRC check
    278 	CommHeaderType for Queued packets
    279 	------------------------------------------------------------------------*/
    280 	return( (PACKET_SERIAL_OVERHEAD_SIZE + sizeof(CommHeaderType)) );
    281 
    282 }	/* end of Packet_Overhead_Size */
    283 
    284 /************************** end of nullconn.cpp ****************************/