⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 share.~pas

📁 SMGSession,一个短信网关接口代码
💻 ~PAS
📖 第 1 页 / 共 5 页
字号:
  finally
    closeFile(f);
  end;
end;

{ TSPTimer }

constructor TSPTimer.Create(AOwner: TComponent);
begin
  inherited;
  FEnabled:=true;
  FInterval:=1000;

  FOnTimer:=nil;
  FWindowHandle := Classes.AllocateHWnd(WndProc);
end;

destructor TSPTimer.Destroy;
begin
  FEnabled := False;
  UpdateTimer;

  Classes.DeallocateHWnd(FWindowHandle);

  inherited;
end;

procedure TSPTimer.setDoTimer(doTimer: TTimerProc);
begin
  FDoTimer:=doTimer;

  UpdateTimer;
end;

procedure TSPTimer.SetEnabled(Value: Boolean);
begin
  if Value <> FEnabled then
  begin
    FEnabled := Value;

    UpdateTimer;
  end;
end;

procedure TSPTimer.SetInterval(Value: Cardinal);
begin
  if Value <> FInterval then
  begin
    FInterval := Value;
    UpdateTimer;
  end;
end;

procedure TSPTimer.setOnTimer(onTimer: TTimerEvent);
begin
  FOnTimer:=onTimer;

  UpdateTimer;
end;

procedure TSPTimer.Timer;
begin
  if Assigned(OnTimer) then
    OnTimer(nil);

  if Assigned(DoTimer) then
    DoTimer;
end;

procedure TSPTimer.UpdateTimer;
begin
  KillTimer(FWindowHandle, 1);

  if (FInterval <> 0) and FEnabled and  (Assigned(DoTimer) or Assigned(OnTimer))  then
  begin
    if SetTimer(FWindowHandle, 1, FInterval, nil) = 0 then
      raise EOutOfResources.Create('时钟类在开启的时候出错!');
  end;
end;

procedure TSPTimer.WndProc(var Msg: TMessage);
begin
  with Msg do
    if Msg = WM_TIMER then
      try
        Timer;
      except
        Application.HandleException(Self);
      end
    else
      Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;

{ TMutexer }

constructor TMutexer.create;
begin
  seed:=true;
end;

function TMutexer.getSeed: boolean;
begin
  result:=seed;
end;

procedure TMutexer.Lock;
begin
  if seed then
    seed:=false;
end;

procedure TMutexer.Unlock;
begin
  seed:=true;
end;

{ TCardList }

constructor TCardList.create;
begin
  top:=-1;
end;

function TCardList.FindElement(element: Cardinal):boolean;
var
  i:integer;
begin
  result:=true;
  i:=0;
  while (i<size) and (element<>list[i]) do
    inc(i);

  if i>=size then result:=false;
end;

procedure TCardList.first;
begin
  top:=-1;
end;

function TCardList.getElement: Cardinal;
begin
  result:=list[top];
end;

function TCardList.getElemets: string;
var
  i:integer;
begin
  for i:=low(list) to high(list) do
    if result<>'' then
      result:=result+';'+inttostr(list[i])
    else result:=inttostr(list[i]);
end;

procedure TCardList.last;
begin
  top:=size-1;
end;

procedure TCardList.LoadData(lst: array of Cardinal);
var
  i:integer;
  high1,high2:integer;
begin
  size:=high(lst)+1;

  high1:=high(list);
  high2:=high(lst);

  i:=0;
  while (i<=high1) and (i<=high2) do
  begin
    list[i]:=lst[i];
    inc(i);
  end;
end;

function TCardList.next:integer;
begin
  inc(top);

  result:=top;
  if top>=size then begin
    result:=EOF;
    top:=-1;
  end;
end;

procedure TCardList.setSize(val: integer);
begin
  if val<=0 then exit;

  if val<>FSize then
  begin
    FSize:=val;
    SetLength(List,FSize);
  end;
end;

{ TIterator }

procedure TIterator.addNode(obj: TObject);
begin
  setLength(link,high(link)+1);
  link[high(link)]:=obj;
end;

constructor TIterator.create;
begin
  top:=-1;
end;

procedure TIterator.first;
begin
  top:=-1;
end;

function TIterator.getNode: TObject;
begin
  result:=link[top];
end;

procedure TIterator.next;
begin
  inc(top);
  if top>=high(link)+1 then
    top:=EOF;
end;

{ TFilter }

constructor TFilter.create(behavior: IBehavior;data:array of TObject);
var
  i:integer;
begin
  Action:=behavior;

  for i:=low(data) to high(data) do
    if Action.Filter(data[i]) then
      reslt.addNode(data[i]);
end;

function TFilter.getIterator: TIterator;
begin
  result:=reslt;
end;

{ TSeqNumGenerator }

constructor TSeqNumGenerator.create(bufSize: cardinal;ioOper:IIOOperator);
begin
  self.bufSize:=bufSize;
  self.ioOper:=ioOper;

  buf.min:=1;
  buf.max:=bufSize;
  buf.seqNum:=bufSize;

  initializeCriticalSection(sect);
end;

procedure TSeqNumGenerator.fillBuf;
begin
  ioOper.retrieve(BufSize,buf.min,buf.max);
  buf.seqNum:=buf.min;
end;

function TSeqNumGenerator.getCurSeqNum: cardinal;
begin
  result:=buf.seqNum;
end;

function TSeqNumGenerator.getSeqNum: cardinal;
begin
  enterCriticalSection(sect);

  try
    inc(buf.seqNum);

    if buf.seqNum>buf.max then
       fillBuf;

    result:=buf.seqNum;
  finally
    leaveCriticalSection(sect);
  end;
end;

{ TIniIOOper }

constructor TIniIOOper.create(iniFileInf: TIniFileInf);
begin
  self.iniFileInf:=iniFileInf;
end;

procedure TIniIOOper.retrieve(bufSize:cardinal;var min, max: cardinal);
var
  iniFile:TIniFile;
  tmpCard,tmpCard1:Cardinal;
begin
  iniFile:=TIniFile.Create(iniFileInf.filePath);

  try
    {预定式模式:首先,把数据存储值修改。然后,完成本地的数据修改}
    tmpCard:=strtocard(iniFile.ReadString(iniFileInf.section,iniFileInf.key,'1'));
    tmpCard1:=tmpCard+bufSize;

    {如果超出了Cardinal的最大范围,从1从新开始}
    if tmpCard1>=high(cardinal)-BufSize  then
    begin
      tmpCard:=1;
      tmpCard1:=tmpCard+bufSize;
    end;
    iniFile.WriteString(iniFileInf.section,iniFileInf.key,inttostr(tmpCard1));

    min:=tmpCard;
    max:=tmpCard1-1;
  finally
    iniFile.Free;
  end;
end;

{ TDBWriteBuffer }

procedure TDBWriteBuffer.commit;
begin
  enterCriticalSection(sect);

  try
    if sqlCounter<=0 then  exit;

    DBWriter.commit(sqlPop);
  finally
    leaveCriticalSection(sect);
  end;
end;

constructor TDBWriteBuffer.create(waitTime,size: integer;DBWriter:IDBWriter);
begin
  sqlStrs:=TStringList.Create;

  self.DBWriter:=DBWriter;
  self.waitTime:=waitTime;
  self.size:=size;

  sqlCounter:=0;
  timeCounter:=0;

  initializeCriticalSection(sect);

  timer:=TTimer.create(nil);
  timer.Interval:=1000;
  timer.OnTimer:=OnTimer;
  timer.Enabled:=true;
end;

destructor TDBWriteBuffer.destroy;
begin
  sqlStrs.Free;

  inherited;
end;

procedure TDBWriteBuffer.OnTimer(sender: TObject);
begin
  timeGo;
end;

function TDBWriteBuffer.sqlPop:string;
begin
  enterCriticalSection(sect);
  try
    result:='';

    if trim(sqlStrs.Text)<>'' then
    begin
      result:=sqlStrs.Text;
      sqlStrs.Clear;

      sqlCounter:=0;
      timeCounter:=0;
    end;
  finally
    leaveCriticalSection(sect);
  end;
end;

procedure TDBWriteBuffer.sqlPush(sqlStr:string);
var
  tmpStr:string;
begin
  enterCriticalSection(sect);

  try
    tmpStr:=trim(sqlStr);
    if tmpStr='' then exit;

    if copy(tmpStr,length(tmpStr),1)<>';' then
      tmpStr:=tmpStr+';';

    sqlStrs.Add(tmpStr);

    inc(sqlCounter);

    if (sqlCounter>=size) and (trim(sqlStrs.Text)<>'') then
    begin
      DBWriter.commit(sqlPop);
    end;
  finally
    leaveCriticalSection(sect);
  end;
end;

procedure TDBWriteBuffer.timeGo;
begin
  entercriticalSection(sect);
  try
    if sqlCounter<=0 then exit;

    inc(timeCounter);

    if (timeCounter>=waitTime) then
    begin
      DBWriter.commit(sqlPop);
    end;
  finally
    leaveCriticalSection(sect);
  end;
end;

function TDBWriteBuffer.toStrinig: string;
begin
  result:=sqlStrs.Text;
end;

{ TJPObjectQueue }
constructor TJPObjectQueue.create(size: integer;safeSize:integer);
var
  i:integer;
begin
  self.FSize:=size;

  setLength(queue,size);
  for i:=0 to size-1 do
    queue[i]:=nil;

  self.FSafeSize:=safeSize;

  FCount:=0;
  rear:=0;
  top:=0;

  lock:=TLock.create;
end;

function TJPObjectQueue.isEmpty: boolean;
begin
  result:=false;
  if count<=0 then result:=true;
end;

function TJPObjectQueue.isFull: boolean;
begin
  result:=false;
  if count>=size then result:=true;
end;

function TJPObjectQueue.isSafeFull: boolean;
begin
  result:=false;
  if count>=size-safeSize then  result:=true;
end;

function TJPObjectQueue.pPop: TObject;
begin
  result:=nil;

  if not lock.getState then
  begin
    lock.lock;
    try
      if isEmpty then exit;

      result:=queue[top];
    finally
      lock.unlock;
    end;
  end;
end;

function TJPObjectQueue.pPush(obj: TObject): boolean;
begin
  result:=false;

  if not lock.getState then
  begin
    lock.lock;

    try
      if IsFull then  exit;

      queue[rear]:=obj;

      inc(rear);
      inc(FCount);

      if rear>=size then Rear:=0;

      Result:=true;
    finally
      lock.unlock;
    end;
  end;
end;

procedure TJPObjectQueue.remove;
begin
  if not lock.getState then
  begin
    lock.lock;

    try
      if isEmpty then exit;

      {删除对象}
      if queue[top]<>nil then
      begin
        queue[top].Free;
        queue[top]:=nil;
      end;

      inc(top);
      dec(FCount);
      if top>=Size then top:=0;
    finally
      lock.unlock;
    end;
  end;
end;

function TJPObjectQueue.pSafePush(obj: TObject): boolean;
begin
  result:=false;

  if not lock.getState then
  begin
    lock.lock;

    try
      if isSafeFull then  exit;

      queue[rear]:=obj;

      inc(rear);
      inc(FCount);

      if rear>=size then Rear:=0;

      Result:=true;
    finally
      lock.unlock;
    end;
  end;
end;

procedure TJPObjectQueue.pRemoveNoFree;
begin
  if not lock.getState then
  begin
    lock.lock;

    try
      if isEmpty then exit;

      if queue[top]<>nil then
      begin
        queue[top]:=nil;
      end;

      inc(top);
      dec(FCount);

      if top>=Size then top:=0;
    finally
      lock.unlock;
    end;
  end;
end;

{ TLock }

constructor TLock.create;
begin
  aLock:=false;
end;

function TLock.getState: boolean;
begin
  result:=aLock;
end;

procedure TLock.lock;
begin
  aLock:=true;
end;

procedure TLock.unlock;
begin
  aLock:=false;
end;

{ TJPCounter }

constructor TJPCounter.create(maxTime: integer);
begin
  self.maxTime:=maxTime;
  waitTime:=0;
end;

procedure TJPCounter.resetCounter;
begin
  waitTime:=0;
end;

function TJPCounter.timeGo:boolean;
begin
  result:=false;
  inc(waitTime);

  if waitTime>=maxTime then
  begin
    result:=true;
    resetCounter;

    onCounter;
  end;
end;

{ TJPWaitObjectQueue }

constructor TJPWaitObjectQueue.create(size, safeSize,maxTime: integer);
var
  i:integer;
begin
  FSize:=size;
  FSafeSize:=safeSize;
  FMaxTime:=maxTime;

⌨️ 快捷键说明

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