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:
@@ -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;
|
||||
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;
|
||||
|
||||
@@ -343,21 +343,24 @@ bool CLiteProtocol::VerifyCommandStr()
|
||||
|
||||
bool CLiteProtocol::AppendParam( const char * pString, const int pLength )
|
||||
{
|
||||
int StringLen;
|
||||
|
||||
// Check if valid
|
||||
if (!pString)
|
||||
return false;
|
||||
|
||||
// Increase string length if required
|
||||
IncCommandStr( Length + pLength+1 );
|
||||
StringLen = (pLength < 0)? strlen(pString) : pLength;
|
||||
IncCommandStr( Length + StringLen+1 );
|
||||
|
||||
// Copy string
|
||||
memcpy( &CommandStr[CurrPos], (char*)pString, pLength );
|
||||
CommandStr[CurrPos+pLength] = Separator;
|
||||
Length = Length + pLength + 1; // Allow for separator character
|
||||
memcpy( &CommandStr[CurrPos], (char*)pString, StringLen );
|
||||
CommandStr[CurrPos+StringLen] = Separator;
|
||||
Length = Length + StringLen + 1; // Allow for separator character
|
||||
|
||||
// Move pointer to end and set end char
|
||||
CurrParam++;
|
||||
CurrPos = CurrPos + pLength + 1; // Allow for separator character
|
||||
CurrPos = CurrPos + StringLen + 1; // Allow for separator character
|
||||
CommandStr[CurrPos] = EndChar;
|
||||
|
||||
// Update references
|
||||
@@ -391,8 +394,8 @@ bool CLiteProtocol::AppendParam( double pValue, int pDigits )
|
||||
char ConvStr[10];
|
||||
char TempStr[20];
|
||||
|
||||
// Check if command sting exists
|
||||
if (!CommandStr)
|
||||
// Check if command sting valid
|
||||
if (!Verified)
|
||||
return false;
|
||||
|
||||
// Create string representation
|
||||
@@ -415,7 +418,7 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if valid parameter no
|
||||
if (!CommandStr || !CommandStr[0] || !Length || (ParamNo < 1) || (ParamNo > NoOfParams))
|
||||
if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams))
|
||||
return NULL;
|
||||
|
||||
// Set Starting point
|
||||
@@ -444,8 +447,8 @@ char const * CLiteProtocol::GetParamNo( int ParamNo, char ** pString, int * pLen
|
||||
|
||||
bool CLiteProtocol::GotoParam( int ParamNo )
|
||||
{
|
||||
// Check if command sting exists
|
||||
if (!CommandStr || (ParamNo < 1))
|
||||
// Check if command sting valid
|
||||
if (!Verified || (ParamNo < 1) || (ParamNo > NoOfParams+1))
|
||||
return false;
|
||||
|
||||
// Goto Param one
|
||||
@@ -464,12 +467,8 @@ bool CLiteProtocol::NextParam()
|
||||
int ParamLen;
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if command sting exists
|
||||
if (!CommandStr || !CommandStr[0] || !Length)
|
||||
return false;
|
||||
|
||||
// Check if next param exists
|
||||
if (CurrParam == NoOfParams)
|
||||
// Check if command sting valid
|
||||
if (!Verified || (CurrParam == NoOfParams))
|
||||
return false;
|
||||
|
||||
// Get Current parameter length
|
||||
@@ -491,7 +490,7 @@ char const * CLiteProtocol::GetParam( char ** pString, int * pLength )
|
||||
char * Mark = NULL;
|
||||
|
||||
// Check if valid parameter no
|
||||
if (!CommandStr || !CommandStr[0] || !Length)
|
||||
if (!Verified)
|
||||
{
|
||||
if (pString) *pString = NULL;
|
||||
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;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class CLiteProtocol
|
||||
// Internal tools
|
||||
bool IncCommandStr( int pLength );
|
||||
bool UpdateCommandStr();
|
||||
bool VerifyStr( char * pString, int pLength, int &ParamCount, char * pError );
|
||||
bool VerifyStr( const char * pString, int pLength, int &ParamCount, char * pError );
|
||||
|
||||
public:
|
||||
// Constructor & Destructor
|
||||
@@ -58,29 +58,41 @@ class CLiteProtocol
|
||||
~CLiteProtocol();
|
||||
|
||||
// Set Command string
|
||||
bool ClearCommandStr();
|
||||
bool NewCommandStr();
|
||||
bool SetCommandStr( char * pString, int pLength, char * Error );
|
||||
bool AppendCommandStr( char *pString, int &pLength );
|
||||
bool VerifyCommandStr();
|
||||
bool ClearCommandStr(); // Set command string to empty
|
||||
bool NewCommandStr(); // Create new empty command string with headers (length, param count)
|
||||
bool SetCommandStr( const char * pString, int pLength ); // Set complete command string (as received)
|
||||
bool AppendCommandStr( char *pString, int &pLength ); // Build up command string in chunks as it is received
|
||||
bool VerifyCommandStr(); // Validate received command string
|
||||
|
||||
// Get Command String
|
||||
char* GetCommandStr( char ** pString = NULL, int * pLength = NULL );
|
||||
int GetCommandLen() { return Length; };
|
||||
int GetNoOfParams() { return NoOfParams; };
|
||||
bool isVerified() { return Verified; };
|
||||
char* GetError() { return Error; };
|
||||
char* GetCommandStr( char ** pString = NULL, int * pLength = NULL ); // Return command string
|
||||
int GetCommandLen() { return Length; }; // Return length of command string
|
||||
int GetNoOfParams() { return NoOfParams; }; // Returns total number of parameters
|
||||
bool isVerified() { return Verified; }; // Returns whether command has been validated
|
||||
char* GetError() { return Error; }; // Return reason for not being valid
|
||||
|
||||
// Set Parameters
|
||||
bool AppendParam( const char * pString, const int pLength );
|
||||
bool AppendParam( long pValue );
|
||||
bool AppendParam( double pValue, int pDigits );
|
||||
bool AppendParam( const char * pString, const int pLength = -1 ); // Add string param to command string
|
||||
bool AppendParam( const long pValue ); // Add long param as text to command string
|
||||
bool AppendParam( const double pValue, const int pDigits ); // Add real param as text to command string
|
||||
|
||||
// Get Parameters
|
||||
char const * GetParamNo( int ParamNo, char ** pString = NULL, int * pLength = NULL );
|
||||
bool GotoParam( int ParamNo );
|
||||
bool NextParam();
|
||||
char const * GetParam( char ** pString = NULL, int * pLength = NULL );
|
||||
bool GotoParam( int ParamNo ); // Reset pointer to specific parameter
|
||||
bool NextParam(); // Move pointer to next parameter
|
||||
char const * GetParam( char ** pString = NULL, int * pLength = NULL ); // Read parameter at pointer (always text)
|
||||
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 */
|
||||
|
||||
@@ -50,7 +50,7 @@ bool CLogCore::Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char
|
||||
// Print formated message
|
||||
va_start( ArgPtr, Format );
|
||||
vfprintf( OutputFile, Format, ArgPtr );
|
||||
fprintf( OutputFile, "\r\n" );
|
||||
fprintf( OutputFile, "\n" );
|
||||
va_end( ArgPtr );
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1042,7 +1042,7 @@ bool CSelectableCore::ProcessBuffer( THandle * Handle, bool Force )
|
||||
{
|
||||
// Show Packet
|
||||
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
|
||||
if (Handle->Type == ctRemoteClient) {
|
||||
|
||||
@@ -83,7 +83,7 @@ void SignalTerminate( int sig )
|
||||
Terminate = true;
|
||||
TermCount++;
|
||||
|
||||
std::cerr << "\r\n" << ProcessName << ": ***********************************" << std::endl;
|
||||
std::cerr << std::endl << ProcessName << ": ***********************************" << std::endl;
|
||||
|
||||
// Create Log Entry
|
||||
if (Log)
|
||||
@@ -134,7 +134,7 @@ void SignalAbort( int sig )
|
||||
}
|
||||
|
||||
// Show signal received
|
||||
std::cerr << "\n" << ProcessName << ": ********************************" << std::endl;
|
||||
std::cerr << std::endl << ProcessName << ": ********************************" << std::endl;
|
||||
|
||||
// Create Log Entry - but don't post
|
||||
if (Log) {
|
||||
|
||||
Reference in New Issue
Block a user