Skip to content

Commit

Permalink
一些小改进
Browse files Browse the repository at this point in the history
  • Loading branch information
winddriver committed May 5, 2020
1 parent fa3ce86 commit 601f7f9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions Net/Net.CrossHttpServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3693,6 +3693,7 @@ function TCrossHttpRequest.ParseRequestData: Boolean;
FRawParamsText := FRawPathAndParams.Substring(J + 1);
end;
FPath := TNetEncoding.URL.Decode(FRawPath);

FQuery.Decode(FRawParamsText);

// HTTP协议版本
Expand Down
39 changes: 19 additions & 20 deletions Net/Net.CrossSocket.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// 否则会导致两块数据被分成小块后出现交错
{.$DEFINE __LITTLE_PIECE__}

{$IF defined(DEBUG) or defined(madExcept)}
{$DEFINE __DEBUG__}
{$ENDIF}

interface

uses
Expand Down Expand Up @@ -364,7 +368,7 @@ TIoEventThread = class;
procedure StopLoop;

/// <summary>
/// 处理IO事件
/// 处理IO事件(内部使用)
/// </summary>
function ProcessIoEvent: Boolean;

Expand Down Expand Up @@ -537,12 +541,12 @@ TIoEventThread = class;
{$endregion}

/// <summary>
/// IO线程开始时触发
/// IO线程开始时触发(内部使用)
/// </summary>
procedure TriggerIoThreadBegin(AIoThread: TIoEventThread);

/// <summary>
/// IO线程结束时触发
/// IO线程结束时触发(内部使用)
/// </summary>
procedure TriggerIoThreadEnd(AIoThread: TIoEventThread);

Expand Down Expand Up @@ -901,13 +905,13 @@ procedure _Log(const Fmt: string; const Args: array of const); overload;
end;

procedure _LogLastOsError(const ATag: string);
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
var
LError: Integer;
LErrMsg: string;
{$ENDIF}
begin
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
LError := GetLastError;
if (ATag <> '') then
LErrMsg := ATag + ' : '
Expand All @@ -930,33 +934,33 @@ constructor TIoEventThread.Create(ACrossSocket: ICrossSocket);

procedure TIoEventThread.Execute;
var
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
LRunCount: Int64;
{$ENDIF}
LCrossSocketObj: TAbstractCrossSocket;
begin
LCrossSocketObj := FCrossSocket as TAbstractCrossSocket;
try
LCrossSocketObj.TriggerIoThreadBegin(Self);
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
LRunCount := 0;
{$ENDIF}
while not Terminated do
begin
try
if not LCrossSocketObj.ProcessIoEvent then Break;
except
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
on e: Exception do
_Log('%s Io线程ID %d, 异常 %s, %s', [TAbstractCrossSocket(FCrossSocket).ClassName, Self.ThreadID, e.ClassName, e.Message]);
_Log('%s Io线程ID %d, 异常 %s, %s', [LCrossSocketObj.ClassName, Self.ThreadID, e.ClassName, e.Message]);
{$ENDIF}
end;
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
Inc(LRunCount)
{$ENDIF};
end;
{$IFDEF DEBUG}
// _Log('%s Io线程ID %d, 被调用了 %d 次', [TAbstractCrossSocket(FCrossSocket).ClassName, Self.ThreadID, LRunCount]);
{$IFDEF __DEBUG__}
// _Log('%s Io线程ID %d, 被调用了 %d 次', [LCrossSocketObj.ClassName, Self.ThreadID, LRunCount]);
{$ENDIF}
finally
LCrossSocketObj.TriggerIoThreadEnd(Self);
Expand Down Expand Up @@ -1198,10 +1202,8 @@ procedure TAbstractCrossSocket.TriggerConnecting(AConnection: ICrossConnection);

_LockConnections;
try
if not FConnections.ContainsKey(AConnection.UID) then
Inc(FConnectionsCount);

FConnections.AddOrSetValue(AConnection.UID, AConnection);
FConnectionsCount := FConnections.Count;
finally
_UnlockConnections;
end;
Expand All @@ -1224,10 +1226,8 @@ procedure TAbstractCrossSocket.TriggerDisconnected(AConnection: ICrossConnection

_LockConnections;
try
if not FConnections.ContainsKey(AConnection.UID) then Exit;

FConnections.Remove(AConnection.UID);
Dec(FConnectionsCount);
FConnectionsCount := FConnections.Count;
finally
_UnlockConnections;
end;
Expand Down Expand Up @@ -1270,7 +1270,6 @@ procedure TAbstractCrossSocket.TriggerListenEnd(AListen: ICrossListen);
begin
_LockListens;
try
if not FListens.ContainsKey(AListen.UID) then Exit;
FListens.Remove(AListen.UID);
FListensCount := FListens.Count;
finally
Expand Down Expand Up @@ -1350,7 +1349,7 @@ destructor TCrossData.Destroy;
if (FSocket <> INVALID_HANDLE_VALUE) then
begin
TSocketAPI.CloseSocket(FSocket);
{$IFDEF DEBUG}
{$IFDEF __DEBUG__}
// _Log('close result %d', [GetLastError]);
{$ENDIF}
FSocket := INVALID_HANDLE_VALUE;
Expand Down
22 changes: 22 additions & 0 deletions Utils/Utils.DateTime.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ TDateTimeHelper = record helper for TDateTime
property Millisecond: Word read GetMillisecond write SetMillisecond;

function ToString(const AFormatStr: string = ''): string; inline;
function ToISO8601: string; inline;
function ToMilliseconds: Int64; inline;

function StartOfYear: TDateTime; inline;
Expand Down Expand Up @@ -465,6 +466,27 @@ function TDateTimeHelper.StartOfYear: TDateTime;
Result := StartOfTheYear(Self);
end;

function TDateTimeHelper.ToISO8601: string;
var
LOffset: TDateTime;
LYear, LMonth, LDay, LHour, LMinute, LSecond, LMilliseconds: Word;
begin
DecodeDate(Self, LYear, LMonth, LDay);
DecodeTime(Self, LHour, LMinute, LSecond, LMilliseconds);
Result := Format('%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%d', [LYear, LMonth, LDay, LHour, LMinute, LSecond, LMilliseconds]);
if (Self <> 0) then
LOffset := Self - TTimeZone.Local.ToUniversalTime(Self)
else
LOffset := 0;
DecodeTime(LOffset, LHour, LMinute, LSecond, LMilliseconds);
if LOffset < 0 then
Result := Format('%s-%.2d:%.2d', [Result, LHour, LMinute])
else if LOffset > 0 then
Result := Format('%s+%.2d:%.2d', [Result, LHour, LMinute])
else
Result := Result + 'Z';
end;

function TDateTimeHelper.ToLocalTime: TDateTime;
begin
Result := TTimeZone.Local.ToLocalTime(Self);
Expand Down

0 comments on commit 601f7f9

Please sign in to comment.