Important Update:

- CLiteProtocol:
  - Set input string on methods to const char *
  - Simplify checking for verification
  - Ensure Length parameter can be left out on all methods
  - Add quick functions for structured commands: To, From, Command
- CLogCore:
  - Use only "\n", not "\r\n"
- SignalCore:
  - Use std::endl instead of "\r\n"
This commit is contained in:
Charl Wentzel
2017-07-08 22:23:50 +02:00
parent 0c20103c9c
commit 4802452a7c
5 changed files with 94 additions and 41 deletions

View File

@@ -95,7 +95,7 @@ bool CLiteProtocol::UpdateCommandStr()
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CLiteProtocol::VerifyStr( char * pString, int pLength, int &ParamCount, char * pError ) bool CLiteProtocol::VerifyStr( const char * pString, int pLength, int &ParamCount, char * pError )
{ {
char * Mark; char * Mark;
long tmpLen; long tmpLen;
@@ -230,7 +230,7 @@ char* CLiteProtocol::GetCommandStr( char ** pString, int * pLength )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CLiteProtocol::SetCommandStr( char * pString, int pLength, char * Error ) bool CLiteProtocol::SetCommandStr( const char * pString, const int pLength )
{ {
int ParamCount; int ParamCount;
@@ -343,21 +343,24 @@ bool CLiteProtocol::VerifyCommandStr()
bool CLiteProtocol::AppendParam( const char * pString, const int pLength ) bool CLiteProtocol::AppendParam( const char * pString, const int pLength )
{ {
int StringLen;
// Check if valid // Check if valid
if (!pString) if (!pString)
return false; return false;
// Increase string length if required // Increase string length if required
IncCommandStr( Length + pLength+1 ); StringLen = (pLength < 0)? strlen(pString) : pLength;
IncCommandStr( Length + StringLen+1 );
// Copy string // Copy string
memcpy( &CommandStr[CurrPos], (char*)pString, pLength ); memcpy( &CommandStr[CurrPos], (char*)pString, StringLen );
CommandStr[CurrPos+pLength] = Separator; CommandStr[CurrPos+StringLen] = Separator;
Length = Length + pLength + 1; // Allow for separator character Length = Length + StringLen + 1; // Allow for separator character
// Move pointer to end and set end char // Move pointer to end and set end char
CurrParam++; CurrParam++;
CurrPos = CurrPos + pLength + 1; // Allow for separator character CurrPos = CurrPos + StringLen + 1; // Allow for separator character
CommandStr[CurrPos] = EndChar; CommandStr[CurrPos] = EndChar;
// Update references // Update references
@@ -391,8 +394,8 @@ bool CLiteProtocol::AppendParam( double pValue, int pDigits )
char ConvStr[10]; char ConvStr[10];
char TempStr[20]; char TempStr[20];
// Check if command sting exists // Check if command sting valid
if (!CommandStr) if (!Verified)
return false; return false;
// Create string representation // Create string representation
@@ -415,7 +418,7 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
char * Mark = NULL; char * Mark = NULL;
// Check if valid parameter no // Check if valid parameter no
if (!CommandStr || !CommandStr[0] || !Length || (ParamNo < 1) || (ParamNo > NoOfParams)) if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams))
return NULL; return NULL;
// Set Starting point // Set Starting point
@@ -444,8 +447,8 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
bool CLiteProtocol::GotoParam( int ParamNo ) bool CLiteProtocol::GotoParam( int ParamNo )
{ {
// Check if command sting exists // Check if command sting valid
if (!CommandStr || (ParamNo < 1)) if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams+1))
return false; return false;
// Goto Param one // Goto Param one
@@ -464,12 +467,8 @@ bool CLiteProtocol::NextParam()
int ParamLen; int ParamLen;
char * Mark = NULL; char * Mark = NULL;
// Check if command sting exists // Check if command sting valid
if (!CommandStr || !CommandStr[0] || !Length) if (!Verified || (CurrParam == NoOfParams))
return false;
// Check if next param exists
if (CurrParam == NoOfParams)
return false; return false;
// Get Current parameter length // Get Current parameter length
@@ -491,7 +490,7 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
char * Mark = NULL; char * Mark = NULL;
// Check if valid parameter no // Check if valid parameter no
if (!CommandStr || !CommandStr[0] || !Length) if (!Verified)
{ {
if (pString) *pString = NULL; if (pString) *pString = NULL;
if (pLength) pLength = 0; if (pLength) pLength = 0;
@@ -511,3 +510,45 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Create command
bool CLiteProtocol::CreateCommand( char const * From, char const * To, char const * Command )
{
// Create new command
if (!NewCommandStr())
return false;
// Insert Parameters in correct order
if (!AppendParam( From ) || !AppendParam( To ) || !AppendParam( Command ))
{
ClearCommandStr();
return false;
};
// All is well
return true;
}
//---------------------------------------------------------------------------
// Read command
bool CLiteProtocol::ReadCommand( char ** From, char ** To, char ** Command, int * DataParams,
int * FromLen, int * ToLen, int * CommandLen )
{
// Check if parameters available
if (!Verified || (NoOfParams < 3))
return false;
// Read parameters one by one
GotoParam( 1 );
GetParam( From, FromLen );
NextParam();
GetParam( To, ToLen );
NextParam();
GetParam( Command, CommandLen );
if (DataParams)
*DataParams = NoOfParams - 3;
return true;
}
//---------------------------------------------------------------------------

View File

@@ -50,7 +50,7 @@ class CLiteProtocol
// Internal tools // Internal tools
bool IncCommandStr( int pLength ); bool IncCommandStr( int pLength );
bool UpdateCommandStr(); bool UpdateCommandStr();
bool VerifyStr( char * pString, int pLength, int &ParamCount, char * pError ); bool VerifyStr( const char * pString, int pLength, int &ParamCount, char * pError );
public: public:
// Constructor & Destructor // Constructor & Destructor
@@ -58,29 +58,41 @@ class CLiteProtocol
~CLiteProtocol(); ~CLiteProtocol();
// Set Command string // Set Command string
bool ClearCommandStr(); bool ClearCommandStr(); // Set command string to empty
bool NewCommandStr(); bool NewCommandStr(); // Create new empty command string with headers (length, param count)
bool SetCommandStr( char * pString, int pLength, char * Error ); bool SetCommandStr( const char * pString, int pLength ); // Set complete command string (as received)
bool AppendCommandStr( char *pString, int &pLength ); bool AppendCommandStr( char *pString, int &pLength ); // Build up command string in chunks as it is received
bool VerifyCommandStr(); bool VerifyCommandStr(); // Validate received command string
// Get Command String // Get Command String
char* GetCommandStr( char ** pString = NULL, int * pLength = NULL ); char* GetCommandStr( char ** pString = NULL, int * pLength = NULL ); // Return command string
int GetCommandLen() { return Length; }; int GetCommandLen() { return Length; }; // Return length of command string
int GetNoOfParams() { return NoOfParams; }; int GetNoOfParams() { return NoOfParams; }; // Returns total number of parameters
bool isVerified() { return Verified; }; bool isVerified() { return Verified; }; // Returns whether command has been validated
char* GetError() { return Error; }; char* GetError() { return Error; }; // Return reason for not being valid
// Set Parameters // Set Parameters
bool AppendParam( const char * pString, const int pLength ); bool AppendParam( const char * pString, const int pLength = -1 ); // Add string param to command string
bool AppendParam( long pValue ); bool AppendParam( const long pValue ); // Add long param as text to command string
bool AppendParam( double pValue, int pDigits ); bool AppendParam( const double pValue, const int pDigits ); // Add real param as text to command string
// Get Parameters // Get Parameters
char const * GetParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL ); bool GotoParam( int ParamNo ); // Reset pointer to specific parameter
bool GotoParam( int ParamNo ); bool NextParam(); // Move pointer to next parameter
bool NextParam(); char const * GetParam( char ** pString = NULL, int * pLength = NULL ); // Read parameter at pointer (always text)
char const * GetParam( char ** pString = NULL, int * pLength = NULL ); char const * GetParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL ); // Get total no of parameters in command string
// Create & Read Structured Command string
bool CreateCommand( char const * From, char const * To, char const * Command ); // Create new command string with core params: To, From & Command
bool ReadCommand( char ** From, char ** To, char ** Command, int * DataParams,
int * FromLen = NULL, int * ToLen = NULL, int * CommandLen = NULL ); // Read core parameters from structured command: To, From & Command
// Read structured Command string
inline int GetNoOfDataParams() { return (NoOfParams - 3); }; // Return number for data (additional) params
inline bool GotoDataParam( int ParamNo ) { return (GotoParam( ParamNo+3 )); }; // Goto specific data (additional) param
inline char const * GetDataParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL ) { // Read specific data param (by number)
return (GetParamNo( ParamNo+3, pString, pLength ));
};
}; };
#endif /* LITE_PROTOCOL_CORE */ #endif /* LITE_PROTOCOL_CORE */

View File

@@ -50,7 +50,7 @@ bool CLogCore::Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char
// Print formated message // Print formated message
va_start( ArgPtr, Format ); va_start( ArgPtr, Format );
vfprintf( OutputFile, Format, ArgPtr ); vfprintf( OutputFile, Format, ArgPtr );
fprintf( OutputFile, "\r\n" ); fprintf( OutputFile, "\n" );
va_end( ArgPtr ); va_end( ArgPtr );
return true; return true;
} }

View File

@@ -1042,7 +1042,7 @@ bool CSelectableCore::ProcessBuffer( THandle * Handle, bool Force )
{ {
// Show Packet // Show Packet
Len = Handle->InBuffer->Peek( &Data ); Len = Handle->InBuffer->Peek( &Data );
if (Log) Log->Output( DebugLevel, dlHigh, OutputDisplay, Data, Len, "%s: Handle '%s' - IN-F:", Name, Handle->Name ); if (Log) Log->Output( DebugLevel, dlHigh, OutputDisplay, Data, Len, "%s: Handle '%s' - IN-T:", Name, Handle->Name );
// Write buffer to Outputs // Write buffer to Outputs
if (Handle->Type == ctRemoteClient) { if (Handle->Type == ctRemoteClient) {

View File

@@ -83,7 +83,7 @@ void SignalTerminate( int sig )
Terminate = true; Terminate = true;
TermCount++; TermCount++;
std::cerr << "\r\n" << ProcessName << ": ***********************************" << std::endl; std::cerr << std::endl << ProcessName << ": ***********************************" << std::endl;
// Create Log Entry // Create Log Entry
if (Log) if (Log)
@@ -134,7 +134,7 @@ void SignalAbort( int sig )
} }
// Show signal received // Show signal received
std::cerr << "\n" << ProcessName << ": ********************************" << std::endl; std::cerr << std::endl << ProcessName << ": ********************************" << std::endl;
// Create Log Entry - but don't post // Create Log Entry - but don't post
if (Log) { if (Log) {