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