rtcsrvmodule.pas

来自「Delphi快速开发Web Server」· PAS 代码 · 共 1,960 行 · 第 1/4 页

PAS
1,960 行
字号
  CS:TRtcCritSec;

procedure AddObj(o:TObject);
  begin
  CS.Enter;
  try
    List.insert(longword(o),1);
  finally
    CS.Leave;
    end;
  end;

procedure DelObj(o:TObject);
  begin
  CS.Enter;
  try
    List.remove(longword(o));
  finally
    CS.Leave;
    end;
  end;

procedure SetObj(o:TObject;num:integer);
  begin
  CS.Enter;
  try
    if List.search(longword(o))>0 then
      List.change(longword(o),num);
  finally
    CS.Leave;
    end;
  end;

procedure KillObjs;
  var
    ob:TObject;
    i:cardinal;
  begin
  CS.Enter;
  try
    if assigned(List) then
      begin
      while List.Count>0 do
        begin
        ob:=TObject(List.search_min(i));
        if (ob<>nil) and (i=1) then
          ob.Free;
        end;
      List.Free;
      List:=nil;
      end;
  finally
    CS.Leave;
    end;
  end;

function CheckObj(o:TObject):integer;
  begin
  CS.Enter;
  try
    Result:=List.search(longword(o));
  finally
    CS.Leave;
    end;
  end;

function HaveObj(o:TObject):boolean;
  begin
  CS.Enter;
  try
    Result:=List.search(longword(o))>0;
  finally
    CS.Leave;
    end;
  end;

constructor TRtcDelayedCall.Create;
  begin
  inherited Create;
  WaitEvent:=nil;
  Timer:=nil;
  Called:=False;
  output:=nil;

  AddObj(Self);
  end;

destructor TRtcDelayedCall.Destroy;
  begin
  CS.Enter;
  try
    if not HaveObj(self) then Exit;
    DelObj(self);
    if assigned(WaitEvent) then
      WaitEvent.SetEvent;
    if assigned(Timer) then
      TRtcTimer.Stop(Timer);
  finally
    CS.Leave;
    end;

  if assigned(output) then
    begin
    output.Free;
    output:=nil;
    end;

  Param.Free;
  SessionID:='';
  Conn:=nil;
  inherited;
  end;

procedure TRtcDelayedCall.Kill;
  begin
  if not HaveObj(self) then Exit;
  Free;
  end;

procedure TRtcDelayedCall.Cancel;
  begin
  if not HaveObj(self) then Exit;
  Free;
  end;

procedure TRtcDelayedCall.Post(Multi_Threaded:boolean);
  var
    wait:boolean;
  begin
  CS.Enter;
  try
    if not HaveObj(self) then Exit;

    wait:=not Called;
    if wait then
      begin
      Timer:=TRtcTimer.Create(Multi_Threaded);
      {$IFDEF FPC}
      TRtcTimer.Enable(Timer,msDelay,@Execute,True);
      {$ELSE}
      TRtcTimer.Enable(Timer,msDelay,Execute,True);
      {$ENDIF}
      end;
  finally
    CS.Leave;
    end;
  if not wait then
    Execute;
  end;

procedure TRtcDelayedCall.Execute;
  var
    ob:TRtcDelayedCallJob;
    wait:boolean;
  begin
  wait:=False;

  CS.Enter;
  try
    if not HaveObj(self) then Exit;

    // Stop Timer if timer active
    if assigned(Timer) then
      begin
      TRtcTimer.Stop(Timer);
      Timer:=nil;
      end
    // Start Waiting Event if no timer
    else
      begin
      wait:=not Called;
      if wait then
        WaitEvent:=TRtcEvent.Create(True,False);
      end;
  finally
    CS.Leave;
    end;

  if wait then
    begin
    WaitEvent.WaitFor(msDelay);
    CS.Enter;
    try
      if not HaveObj(self) then Exit;

      WaitEvent.Free;
      WaitEvent:=nil;
    finally
      CS.Leave;
      end;
    end;

  // We don't want to have a situation where Execute would be called twice (for example, when Timer triggers and another thread uses "Call")
  CS.Enter;
  try
    if not HaveObj(self) or (CheckObj(self)=2) then Exit;

    SetObj(self,2);
  finally
    CS.Leave;
    end;

  if assigned(Conn) then
    begin
    ob:=TRtcDelayedCallJob.Create(Conn);
    if not Conn.PostJob(ob) then
      ob.Kill;
    end;
  end;

procedure TRtcDelayedCall.WakeUp;
  var
    needtocall:boolean;
  begin
  needtocall:=False;

  CS.Enter;
  try
    if not HaveObj(self) or (CheckObj(self)=2) then Exit;
    Called:=True;

    if assigned(Timer) then
      begin
      // Timer active. Need to stop the timer and call Execute
      needtocall:=True;
      TRtcTimer.Stop(Timer);
      Timer:=nil;
      end
    else if assigned(WaitEvent) then
      // Wait Event active. Only need to Set the Event flag
      WaitEvent.SetEvent;
  finally
    CS.Leave;
    end;

  if needtocall then
    Execute;
  end;

{ TRtDelayedCallJob }

constructor TRtcDelayedCallJob.Create(Con: TRtcConnection);
  begin
  inherited Create;
  Conn:=Con;
  end;

procedure TRtcDelayedCallJob.Kill;
  begin
  Free;
  end;

procedure TRtcDelayedCallJob.Run(Thr:TRtcThread);
  var
    call,mycall:TRtcDelayedCall;
    MyResult:TRtcValue;
    code,temp:string;
    released:boolean;
  begin
  released:=False;
  call:=nil;
  try
    if assigned(Conn) then with TRtcDataServer(Conn) do
      begin
      call:=TRtcDelayedCall(Info.Obj[RTC_SERVERMODULE_DELAYED_CALL]);
      if not assigned(call) then Exit;
      Info.Obj[RTC_SERVERMODULE_DELAYED_CALL]:=nil;

      if (call.SessionID<>'') then
        if not assigned(Session) or (Session.ID<>call.SessionID) then
          begin
          Disconnect;
          Exit;
          end;

      EnterEvent;
      try
        try
          MyResult:=TRtcValue.Create;
          try
            call.CallEvent(call.Conn, call.Param, myResult);

            if assigned(call.output) then
              myResult.to_Code(call.output)
            else
              begin
              {$IFDEF COMPRESS}
              if (call.FMT=fmt_RTC) and (call.Compress<>cNone) then
                begin
                code:=myResult.toCode;
                if length(code)<RTC_MIN_COMPRESS_SIZE then
                  begin
                  CryptWrite(call.crypt, code);
                  Write(code);
                  end
                else
                  begin
                  { we have stored uncompressed data in "output",
                    now we need to compress it all and send it out. }

                  case call.Compress of
                    cFast: temp:=ZCompress_Str(code,zcFastest);
                    cMax: temp:=ZCompress_Str(code,zcMax);
                    else temp:=ZCompress_Str(code,zcDefault);
                    end;

                  if length(temp)>=length(code)-1 then
                    begin
                    temp:='';
                    CryptWrite(call.crypt,code);
                    Write(code);
                    code:='';
                    end
                  else
                    begin
                    code:='';
                    CryptWrite(call.crypt,temp);
                    Write(temp);

                    temp:=#0;
                    CryptWrite(call.crypt,temp);
                    Write(temp);
                    temp:='';
                    end;
                  end;
                end
              else
              {$ENDIF}
                begin
                if call.FMT=fmt_XMLRPC then
                  Write(MyResult.toXMLrpcResponse)
                else
                  begin
                  if assigned(call.crypt) and assigned(call.crypt.Write) then
                    begin
                    temp:=MyResult.toCode;
                    CryptWrite(call.crypt,temp);
                    Write(temp);
                    end
                  else
                    Write(MyResult.toCode);
                  end;
                end;
              end;
          finally
            MyResult.Free;
            end;
        except
          on E:EDelayedCall do
            begin
            // Create and Post the DelayedCall
            mycall:=E.call;
            Info.Obj[RTC_SERVERMODULE_DELAYED_CALL]:=mycall;

            mycall.Conn:=Conn;
            mycall.output:=call.output;
            mycall.FMT:=call.FMT;
            {$IFDEF COMPRESS}
            mycall.Compress:=call.Compress;
            {$ENDIF}
            mycall.crypt:=call.crypt;
            mycall.SessionID:=call.SessionID;
            mycall.AutoSessionsLive:=call.AutoSessionsLive;

            call.crypt:=nil;
            call.output:=nil;

            call.Free;
            call:=nil;

            if Conn.isExtension then
              begin
              self.Free;
              released:=True;
              mycall.Execute;
              end
            else
              mycall.Post(MultiThreaded);

            Exit; // not sending the reponse from here!
            end;
          on E:Exception do
            with TRtcValue.Create do
              try
                asException:=E.Message;
                if assigned(call.output) then
                  to_Code(call.output)
                else
                  begin
                  if call.FMT=fmt_XMLRPC then
                    Write(toXMLrpcResponse)
                  else
                    begin
                    if assigned(call.crypt) and assigned(call.crypt.Write) then
                      begin
                      temp:=asCode;
                      CryptWrite(call.crypt,temp);
                      Write(temp);
                      end
                    else
                      Write(asCode);
                    end;
                  end;
              finally
                Free;
              end;
          end;

        {$IFDEF COMPRESS}
        if assigned(call.output) then
          begin
          try
            code:=call.output.Get;
          finally
            call.output.Free;
            call.output:=nil;
            end;

          if length(code)<RTC_MIN_COMPRESS_SIZE then
            begin
            CryptWrite(call.crypt, code);
            Write(code);
            end
          else
            begin
            { we have stored uncompressed data in "output",
              now we need to compress it all and send it out. }

            case call.Compress of
              cFast: temp:=ZCompress_Str(code,zcFastest);
              cMax: temp:=ZCompress_Str(code,zcMax);
              else temp:=ZCompress_Str(code,zcDefault);
              end;

            if length(temp)>=length(code)-1 then
              begin
              temp:='';
              CryptWrite(call.crypt,code);
              Write(code);
              code:='';
              end
            else
              begin
              code:='';
              CryptWrite(call.crypt,temp);
              Write(temp);

              temp:=#0;
              CryptWrite(call.crypt,temp);
              Write(temp);
              temp:='';
              end;
            end;
          end;
        {$ENDIF}
        
        if assigned(call.crypt) and assigned(call.crypt.Write) then
          begin
          // Add control key to the end of our response
          temp:=call.crypt.ControlKey;
          CryptWrite(call.crypt, temp);
          Write(temp);
          temp:='';
          end;

        Flush;

      finally
        LeaveEvent;
        end;
      end;
  finally
    if assigned(call) then
      call.Free;
    if not released then Free;
    end;
  end;

initialization
randomize;
CS:=TRtcCritSec.Create;
List:=tBinList.Create(128);

finalization
KillObjs;

Garbage(List);
Garbage(CS);
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?