Ignore:
Timestamp:
Dec 18, 2009, 12:21:39 PM (14 years ago)
Author:
george
Message:
  • Upraveno: Nedokončené přepracování správy vláken. Použit systém přidělování ze společné sady o dané velikosti namísto přidělování vláken ke každému požadavku samostatně. Vlákna jsou inicializována při spuštění aplikace, jsou opakovaně používána a uvolněna až při ukončení běhu aplikace.
  • Přidáno: Třída pro práci s opakovaně spustitelnými vlákny.
  • Přidáno: Třída přidělování objektů ze zásoby o dané velikosti.
  • Přidáno: Třída pro zobrazování stránkování seznamů položek.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DirectWeb/UHTTPSessionFile.pas

    r87 r88  
    66
    77uses
    8   Classes, SysUtils, UHTTPServer;
     8  Classes, SysUtils, UHTTPServer, syncobjs;
    99
    10 type 
     10type
    1111
    12   { THTTPSessionFile }
     12  { TFileHTTPSessionStorage }
    1313
    14   THTTPSessionFile = class(THTTPSession)
     14  TFileHTTPSessionStorage = class(THTTPSessionStorage)
     15  private
     16    Lock: TCriticalSection;
     17    procedure GetSessionId(HandlerData: THTTPHandlerData);
     18  public
    1519    Directory: string;
    1620    SessionIdCookieName: string;
    17     SessionId: string;
    18     procedure Load(Request: THTTPRequest); override;
    19     procedure Save(Response: THTTPResponse); override;
     21    Sessions: TStringList;
     22    procedure Load(HandlerData: THTTPHandlerData); override;
     23    procedure Save(HandlerData: THTTPHandlerData); override;
    2024    constructor Create; override;
    2125    destructor Destroy; override;
     
    2630{ THTTPSession }
    2731
    28 procedure THTTPSessionFile.Load(Request: THTTPRequest);
     32procedure TFileHTTPSessionStorage.GetSessionId(HandlerData: THTTPHandlerData);
    2933begin
     34  with HandlerData do
    3035  if Request.Cookies.IndexOfName(SessionIdCookieName) <> -1 then begin
    31     SessionId := Request.Cookies.Values[SessionIdCookieName];
    32   end else begin
    33     SessionId := IntToStr(Random(1000));
    34     // TODO: Check if new id already used
     36      SessionId := Request.Cookies.Values[SessionIdCookieName];
     37    end else begin
     38      SessionId := IntToStr(Random(2000000000));
     39      // TODO: Check if new id already used
     40    end;
     41end;
     42
     43procedure TFileHTTPSessionStorage.Load(HandlerData: THTTPHandlerData);
     44var
     45  SessionFile: string;
     46begin
     47  GetSessionId(HandlerData);
     48  try
     49    Lock.Acquire;
     50    SessionFile := Directory + '/' + HandlerData.SessionId;
     51    if FileExists(SessionFile) then
     52      HandlerData.Session.LoadFromFile(SessionFile);
     53  finally
     54    Lock.Release;
    3555  end;
    36 
    37   if FileExists(Directory + '/' + SessionId) then
    38     Variables.LoadFromFile(Directory + '/' + SessionId);
    3956  inherited;
    4057end;
    4158
    42 procedure THTTPSessionFile.Save(Response: THTTPResponse);
     59procedure TFileHTTPSessionStorage.Save(HandlerData: THTTPHandlerData);
     60var
     61  SessionFile: string;
    4362begin
    44   ForceDirectories(Directory);
    45   if DirectoryExists(Directory) then
    46     Variables.SaveToFile(Directory + '/' + SessionId)
    47     else raise Exception.Create('Can''t create session storage directory.');
     63  try
     64    Lock.Acquire;
     65    SessionFile := Directory + '/' + HandlerData.SessionId;
     66    ForceDirectories(Directory);
     67    if DirectoryExists(Directory) then begin
     68      DeleteFile(SessionFile);
     69      HandlerData.Session.SaveToFile(SessionFile)
     70    end else raise Exception.Create('Can''t create session storage directory.');
    4871
    49   Response.Cookies.Values[SessionIdCookieName] := SessionId;
    50   inherited Save(Response);
     72    HandlerData.Response.Cookies.Values[SessionIdCookieName] := HandlerData.SessionId;
     73  finally
     74    Lock.Release;
     75  end;
     76  inherited;
    5177end;
    5278
    53 constructor THTTPSessionFile.Create;
     79constructor TFileHTTPSessionStorage.Create;
    5480begin
    55   inherited;
     81  inherited Create;
     82  Lock := TCriticalSection.Create;
     83  Sessions := TStringList.Create;
    5684  SessionIdCookieName := 'SessionId';
    5785  Directory := 'Session';
    5886end;
    5987
    60 destructor THTTPSessionFile.Destroy;
     88destructor TFileHTTPSessionStorage.Destroy;
    6189begin
     90  Sessions.Destroy;
     91  Lock.Destroy;
    6292  inherited Destroy;
    6393end;
Note: See TracChangeset for help on using the changeset viewer.