📄 msgcommain.pas
字号:
{******************************************************************************}
{ }
{ Main definitions, global procedures and functions }
{ }
{******************************************************************************}
unit MsgComMain;
interface
{$I MsgVer.inc}
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
{$IFDEF LINUX}
Libc,
{$ENDIF}
SysUtils, Classes, IniFiles,
////////////////////////////////////////////////////////////////////////////////
//
// MsgCommunicator units
//
////////////////////////////////////////////////////////////////////////////////
MsgComBase,
MsgCrypto,
MsgCompression,
MsgTypes,
MsgExcept,
MsgConst,
{$IFDEF DEBUG_LOG}
MsgDebug,
{$ENDIF}
MsgMemory; // UNIT MsgMemory MUST BE LAST !!!
type
{$IFDEF LINUX}
// Delphi7 Controls.pas
TDate = type TDateTime;
TTime = type TDateTime;
{$ENDIF}
TMsgCompressionAlgorithm = (caNone,caZLIB,caBZIP,caPPM);
TMsgCryptoAlgorithm = (
craNone
{$IFDEF ENCRYPTION_ON}
,
craRijndael_128,
craRijndael_256,
craBlowfish,
craTwofish_128,
craTwofish_256,
craSquare,
craDES_Single_8,
craDES_Double_8,
craDES_Double_16,
craDES_Triple_8,
craDES_Triple_16,
craDES_Triple_24
{$ENDIF}
);
TMsgCryptoMode = (acmCTS,acmCBC,acmCFB,acmOFB);
////////////////////////////////////////////////////////////////////////////////
//
// TMsgCryptoParamsEditor
//
////////////////////////////////////////////////////////////////////////////////
TMsgCryptoParamsEditor = class (TPersistent)
private
FKeyInfo: TMsgCryptoKey;
FInitVector: array [0..Msg_MAX_VECTOR] of Byte;
FPassword: string; // MsgDefaultPassword by default
FCryptoAlgorithm: TMsgCryptoAlgorithm; // msg_Cipher_None by Default
FCryptoMode: TMsgCryptoMode; // msg_CTS by Default
FUseInitVector: Boolean; // False by default
public
constructor Create;
procedure SetCryptoParams(Params: TMsgCryptoParams);
function GetCryptoParams: TMsgCryptoParams;
protected
function GetInitVectorValue(Index: Integer): Byte;
procedure SetInitVectorValue(Index: Integer; Value: Byte);
function GetVectorSize: Integer;
function GetKeyValue(Index: Integer): Byte;
procedure SetKeyValue(Index: Integer; Value: Byte);
function GetKeySize: Integer;
procedure SetKeySize(Value: Integer);
function GetMaxKeySize: Integer;
public
procedure SetKey(Key: Pointer; KeySize: Integer);
function GetKey: Pointer;
procedure MakeRandomKey(KeySize: Integer);
procedure MakeRandomInitVector;
procedure SetInitVector(Vector: Pointer);
function GetInitVector: Pointer;
procedure Assign(Source: TPersistent); override;
public
property InitVector[Index: Integer]: Byte read GetInitVectorValue write SetInitVectorValue;
property MaxInitVectorSize: Integer read GetVectorSize;
property Key[Index: Integer]: Byte read GetKeyValue write SetKeyValue;
property MaxKeySize: Integer read GetMaxKeySize;
published
property CryptoAlgorithm: TMsgCryptoAlgorithm read FCryptoAlgorithm write FCryptoAlgorithm;
property CryptoMode:TMsgCryptoMode read FCryptoMode write FCryptoMode;
property KeySize: Integer read GetKeySize write SetKeySize;
property Password: string read FPassword write FPassword;
property UseInitVector: Boolean read FUseInitVector write FUseInitVector;
end;// TMsgCryptoParamsEditor
////////////////////////////////////////////////////////////////////////////////
//
// TMsgNetworkSettingsEditor
//
////////////////////////////////////////////////////////////////////////////////
TMsgNetworkSettingsEditor = class (TPersistent)
private
FPacketSize: Integer;
FMaxThreadCount: Integer;
FConnectionParamsTunning: Boolean;
FTestPacketCount: Integer;
FDisconnectRetryCount: Integer;
FDisconnectDelay: Integer;
protected
procedure SetPacketSize(Value: Integer);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure CopySettingsToConnectParams(var ConnectParams: TMsgConnectParams); virtual;
published
property PacketSize: Integer read FPacketSize write SetPacketSize;
property MaxThreadCount: Integer read FMaxThreadCount write FMaxThreadCount;
property ConnectionParamsTunning: Boolean read FConnectionParamsTunning write FConnectionParamsTunning;
property TestPacketCount: Integer read FTestPacketCount write FTestPacketCount;
property DisconnectRetryCount: Integer read FDisconnectRetryCount write FDisconnectRetryCount;
property DisconnectDelay: Integer read FDisconnectDelay write FDisconnectDelay;
end; // TMsgNetworkSettingsEditor
////////////////////////////////////////////////////////////////////////////////
//
// TMsgConnectionParamsEditor
//
////////////////////////////////////////////////////////////////////////////////
TMsgConnectionParamsEditor = class (TPersistent)
private
FLocalHost: String;
FLocalPort: Cardinal;
FCryptoParamsEditor: TMsgCryptoParamsEditor;
protected
// procedure SetResendRequestDelay(Value: Integer);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function GetConnectParams: TMsgConnectParams; virtual;
property ConnectParams: TMsgConnectParams read GetConnectParams;
published
property LocalHost: String read FLocalHost write FLocalHost;
property LocalPort: Cardinal read FLocalPort write FLocalPort;
property CryptoParams: TMsgCryptoParamsEditor read FCryptoParamsEditor write FCryptoParamsEditor;
end;
////////////////////////////////////////////////////////////////////////////////
//
// Global functions
//
////////////////////////////////////////////////////////////////////////////////
// compression algorithm
function ConverTMsgCompressionAlgorithmToMsgCompressionAlgorithm(
CompressionAlgorithm: TMsgCompressionAlgorithm
): TMsgCompressionAlgorithm1;
// compression algorithm
function ConverTMsgCompressionAlgorithm1ToCompressionAlgorithm(
CompressionAlgorithm: TMsgCompressionAlgorithm1
): TMsgCompressionAlgorithm;
implementation
////////////////////////////////////////////////////////////////////////////////
//
// TMsgCryptoParamsEditor
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
constructor TMsgCryptoParamsEditor.Create;
begin
inherited;
FPassword := MsgDefaultPassword;
FKeyInfo.KeySize := Msg_MAX_KEY+1;
FillChar(FInitVector,MaxInitVectorSize,$00);
FillChar(FKeyInfo.Key,MaxKeySize,$00);
FCryptoAlgorithm := craNone;
FCryptoMode := acmCTS;
FUseInitVector := False;
end;//Create
//------------------------------------------------------------------------------
// set CryptoParams
//------------------------------------------------------------------------------
procedure TMsgCryptoParamsEditor.SetCryptoParams(Params: TMsgCryptoParams);
begin
FUseInitVector := Params.UseInitVector;
FKeyInfo := Params.KeyInfo;
Move(Params.InitVector[0],FInitVector[0],MaxInitVectorSize);
FPassword := Params.Password;
case Params.CryptoAlgorithm of
Msg_Cipher_None: FCryptoAlgorithm := craNone;
Msg_Cipher_Rijndael_128: FCryptoAlgorithm := craRijndael_128;
Msg_Cipher_Rijndael_256: FCryptoAlgorithm := craRijndael_256;
Msg_Cipher_Blowfish: FCryptoAlgorithm := craBlowfish;
Msg_Cipher_Twofish_128: FCryptoAlgorithm := craTwofish_128;
Msg_Cipher_Twofish_256: FCryptoAlgorithm := craTwofish_256;
Msg_Cipher_Square: FCryptoAlgorithm := craSquare;
Msg_Cipher_Des_Single_8: FCryptoAlgorithm := craDES_Single_8;
Msg_Cipher_Des_Double_8: FCryptoAlgorithm := craDES_Double_8;
Msg_Cipher_Des_Double_16: FCryptoAlgorithm := craDES_Double_16;
Msg_Cipher_Des_Triple_8: FCryptoAlgorithm := craDES_Triple_8;
Msg_Cipher_Des_Triple_16: FCryptoAlgorithm := craDES_Triple_16;
Msg_Cipher_Des_Triple_24: FCryptoAlgorithm := craDES_Triple_24;
end;
case Params.CryptoMode of
Msg_Cipher_Mode_CTS: FCryptoMode := acmCTS;
Msg_Cipher_Mode_CBC: FCryptoMode := acmCBC;
Msg_Cipher_Mode_CFB: FCryptoMode := acmCFB;
Msg_Cipher_Mode_OFB: FCryptoMode := acmOFB;
end;
end; // SetCryptoParams
//------------------------------------------------------------------------------
// GetCryptoParams
//------------------------------------------------------------------------------
function TMsgCryptoParamsEditor.GetCryptoParams: TMsgCryptoParams;
begin
Result.UseInitVector := FUseInitVector;
Result.KeyInfo := FKeyInfo;
Move(FInitVector[0],Result.InitVector[0],MaxInitVectorSize);
Result.Password := FPassword;
case FCryptoAlgorithm of
craNone: Result.CryptoAlgorithm := Msg_Cipher_None;
craRijndael_128: Result.CryptoAlgorithm := Msg_Cipher_Rijndael_128;
craRijndael_256: Result.CryptoAlgorithm := Msg_Cipher_Rijndael_256;
craBlowfish: Result.CryptoAlgorithm := Msg_Cipher_Blowfish;
craTwofish_128: Result.CryptoAlgorithm := Msg_Cipher_Twofish_128;
craTwofish_256: Result.CryptoAlgorithm := Msg_Cipher_Twofish_256;
craSquare: Result.CryptoAlgorithm := Msg_Cipher_Square;
craDES_Single_8: Result.CryptoAlgorithm := Msg_Cipher_Des_Single_8;
craDES_Double_8: Result.CryptoAlgorithm := Msg_Cipher_Des_Double_8;
craDES_Double_16: Result.CryptoAlgorithm := Msg_Cipher_Des_Double_16;
craDES_Triple_8: Result.CryptoAlgorithm := Msg_Cipher_Des_Triple_8;
craDES_Triple_16: Result.CryptoAlgorithm := Msg_Cipher_Des_Triple_16;
craDES_Triple_24: Result.CryptoAlgorithm := Msg_Cipher_Des_Triple_24;
end;
case FCryptoMode of
acmCTS: Result.CryptoMode := Msg_Cipher_Mode_CTS;
acmCBC: Result.CryptoMode := Msg_Cipher_Mode_CBC;
acmCFB: Result.CryptoMode := Msg_Cipher_Mode_CFB;
acmOFB: Result.CryptoMode := Msg_Cipher_Mode_OFB;
end;
end;// GetCryptoParams
function TMsgCryptoParamsEditor.GetInitVectorValue(Index: Integer): Byte;
begin
if (Index < 0) or (Index >= MaxInitVectorSize) then
raise EMsgException.Create(10717,ErrorLInvalidVectorIndex,[Index,MaxInitVectorSize]);
Result := FInitVector[Index];
end; // FInitVector
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -