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

📄 share.~pas

📁 SMGSession,一个短信网关接口代码
💻 ~PAS
📖 第 1 页 / 共 5 页
字号:

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

  lock:=TLock.create;
end;

function TJPWaitObjectQueue.cusGetByID(id: cardinal;
  var index: integer): TWaitUnit;
var
  ind:cardinal;
begin
  result:=nil;
  if not lock.getState then
  begin
    lock.lock;

    try
      result:=getObjByID(id,ind);

      index:=ind;
    finally
      lock.unlock;
    end;
  end;
end;

function TJPWaitObjectQueue.getFreePos: integer;
var
  i:integer;
begin
  result:=-1;

  for i:=low(queue) to high(queue) do
    if queue[i]=nil then
    begin
      result:=i;
      break;
    end;
end;

function TJPWaitObjectQueue.getObjByID(id:cardinal;var index:cardinal): TWaitUnit;
var
  i:integer;
begin
  result:=nil;

  for i:=low(queue) to high(queue) do
  begin
    if queue[i].getID=id then
    begin
      result:=queue[i];
      index:=i;

      break;
    end;
  end;
end;

function TJPWaitObjectQueue.isEmpty: boolean;
begin
  result:=false;

  if count<=0 then result:=true;
end;

function TJPWaitObjectQueue.isFull: boolean;
begin
  result:=false;

  if count>=size then result:=true;
end;

function TJPWaitObjectQueue.isSafeFull: boolean;
begin
 result:=false;

 if count>=size-safeSize then  result:=true;
end;

function TJPWaitObjectQueue.cusPush(obj: TWaitUnit): boolean;
var
  pos:integer;
begin
  result:=false;

  if not lock.getState then
  begin
    lock.lock;

    try
      if isFull then exit;

      pos:=getFreePos;

      if pos<0 then exit;

      queue[pos]:=obj;
      queue[pos].resetTime;

      inc(FCount);

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

procedure TJPWaitObjectQueue.removeByID(id: cardinal);
var
  ind:cardinal;
  obj:TObject;
begin
  if not lock.getState then
  begin
    lock.lock;

    try
      obj:=getObjByID(id,ind);

      if obj<>nil then
      begin
        queue[ind].freeData:=true;
        queue[ind].Free;

        queue[ind]:=nil
      end;

      dec(FCount);
    finally
      lock.unlock;
    end;
  end;
end;

procedure TJPWaitObjectQueue.removeByIndex(index: cardinal);
begin
  if not lock.getState then
  begin
    lock.lock;

    try
      {删除对象}
      if queue[index]<>nil then
      begin
        queue[index].freeData:=true;
        queue[index].Free;

        queue[index]:=nil;
      end;

      dec(FCount);
    finally
      lock.unlock;
    end;
  end;
end;

procedure TJPWaitObjectQueue.removeNoFreeByID(id: cardinal);
var
  obj:TObject;
  ind:cardinal;
begin
  if not lock.getState then
  begin
    lock.lock;

    try
      obj:=getObjByID(id,ind);

      if obj<>nil then
      begin
        queue[ind].freeData:=false;
        queue[ind].Free;

        queue[ind]:=nil
      end;

      dec(FCount);
    finally
      lock.unlock;
    end;
  end;
end;

function TJPWaitObjectQueue.cusSafePush(obj: TWaitUnit): boolean;
var
  pos:integer;
begin
  result:=false;

  if not lock.getState then
  begin
    lock.lock;

    try
      if isSafeFull then exit;

      pos:=getFreePos;

      if pos<0 then exit;

      queue[pos]:=obj;
      queue[pos].resetTime;

      inc(FCount);

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

procedure TJPWaitObjectQueue.timeGo;
var
  i:integer;
  tmpCount:integer;
begin
  tmpCount:=0;

  for i:=low(queue) to high(queue) do
  begin
    if tmpCount>count then break;

    if queue[i]<>nil then
    begin
      queue[i].timeGo;
    end;

    inc(tmpCount);
  end;
end;

function TJPWaitObjectQueue.getByIndex(ind: integer): TWaitUnit;
begin
  result:=queue[ind];
end;

{ TWaitUnit }

constructor TWaitUnit.create(data: TObject);
begin
  self.data:=data;

  resetTime;
end;

destructor TWaitUnit.destroy;
begin
  if isFreeData then
    data.Free;

  inherited;
end;

function TWaitUnit.getData: TObject;
begin
  result:=data;
end;

function TWaitUnit.getTime: cardinal;
begin
  result:=waitTime;
end;

procedure TWaitUnit.resetTime;
begin
  waitTime:=0;
end;

procedure TWaitUnit.timeGo;
begin
  inc(waitTime);
end;

{ TJPAbstactLink }

constructor TJPAbstractLink.create(max: integer);
begin
  FMax:=max;

  link:=nil;
  rear:=nil;

  cnt:=0;

  initializeCriticalSection(sect);
end;

function TJPAbstractLink.getIterator:IIterator;
begin
  result:=TJPIterator.create(self);
end;

function TJPAbstractLink.vNext(pNode:PLinkNode):PLinkNode;
begin
  enterCriticalSection(sect);
  try
    result:=pNode^.next;
  finally
    leaveCriticalSection(sect);
  end;
end;

function TJPAbstractLink.vPrior(pNode:PLinkNode):PLinkNode;
begin
  enterCriticalSection(sect);
  try
    result:=pNode^.prior;
  finally
    leaveCriticalSection(sect);
  end;
end;

function TJPAbstractLink.vPush(pData: pointer): boolean;
var
  p:PLinkNode;
begin
  enterCriticalSection(sect);
  try
    result:=false;
    if cnt>=FMax then exit;

    new(p);
    p^.data:=pData;
    p^.prior:=rear;
    p^.next:=nil;

    if rear<>nil then
    begin
      rear^.next:=p;
      rear:=p;
    end else begin
          link:=p;
          rear:=p;
        end;

    inc(cnt);

    result:=true;
  finally
    leaveCriticalSection(sect);
  end;
end;

function TJPAbstractLink.vRemove(pNode: PLinkNode): boolean;
begin
  enterCriticalSection(sect);

  result:=false;
  try
    if cnt<=0 then exit;

    if (pNode<>nil) then
    begin
      if pNode=link then//删除的是第一个元素
      begin
        link:=pNode^.next;

        if link<>nil then
          link^.prior:=nil
        else rear:=nil;
      end else
          if pNode=rear then
          begin
            rear:=pNode^.prior;

            (pNode^.prior)^.next:=pNode^.next;
          end else
              begin
                (pNode^.prior)^.next:=pNode^.next;
                (pNode^.next)^.prior:=pNode^.prior;
              end;

      if pNode.data<>nil then
        dispose(pNode^.data);

      dispose(pNode);
      dec(cnt);

      result:=true;
    end;
  finally
    leaveCriticalSection(sect);
  end;
end;

{ TSubject }

procedure TSubject.addObserver(obj: TAbstractObserver);
begin
  if obj=nil then exit;

  list.Add(pointer(obj));
end;

constructor TSubject.create;
begin
  list:=TList.Create;
end;

procedure TSubject.delObserver(obj: TAbstractObserver);
begin
  if obj=nil then exit;

  list.Remove(pointer(obj));
end;

destructor TSubject.destroy;
begin
  list.Free;

  inherited;
end;


procedure TSubject.inform(param: pointer);
var
  i:integer;
begin
  for i:=0 to list.count-1 do
    if list[i]<>nil then
    begin
      TAbstractObserver(list[i]).action(param);
    end;
end;

{ TDBIOOper }

constructor TDBIOOper.create(dbTabInf: TDBTabInf);
begin
  self.dbTabInf:=dbTabInf;

  query:=TADOQuery.Create(nil);
  query.Connection:=self.dbTabInf.con;
end;

procedure TDBIOOper.retrieve(bufSize: cardinal; var min, max: cardinal);
var
  tmpCard,tmpCard1:Cardinal;
  strSQL:string;
begin
  try
    {预定式模式:首先,把数据存储值修改。然后,完成本地的数据修改}
    strSQL:=format('SELECT %S FROM %S WHERE %S=''%S''',[dbTabInf.valName,dbTabInf.tabName,
            dbTabInf.propertyName,dbTabInf.propertyVal]); 
    TShare.adoDataSetOpen(dbTabInf.con,query,strSQL);
    tmpCard:=strtocard(query.fieldByName(dbTabInf.valName).asString);

    tmpCard1:=tmpCard+bufSize;

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

    {写库操作}
    strSQL:=format('UPDATE %S SET %S=%D WHERE %S=''%S''',[dbTabInf.tabName,dbTabInf.valName,tmpCard1,
                                                          dbTabInf.propertyName,dbTabInf.propertyVal]);
    query.close;
    query.SQL.Clear;
    query.SQL.Add(strSQL);
    TShare.adoDataSetUpdate(dbTabInf.con,query);

    min:=tmpCard;
    max:=tmpCard1-1;
  finally
    query.Close;
  end;
end;

{ TAbstractXML }
constructor TAbstractXML.create;
var
  opFile:TextFile;
  path:string;
begin
  createLogDir;//创建log目录

  path:=extractFilePath(application.ExeName)+'log\'+formatDateTime('yyyymmdd',now)+'.xml';
  try
    if not fileExists(path) then
    begin
      try
        try
        AssignFile(opFile,path);
        Rewrite(opFile);
        Writeln(opFile,'<?xml version="1.0" encoding="GBK"?>');
        Writeln(opFile,'<log>');
        Writeln(opFile,'</log>');
        except
          on E: Exception do
            codesite.sendmsg(E.Message+#13+#10+'TAbstractXML.create in share of cmserver 1');
        end;
      finally
        CloseFile(opFile);
      end;
    end;
  except
    on E: Exception do codesite.sendmsg(E.Message+#13+#10+'TAbstractXML.create in share of cmserver 2');
  end;
end;

procedure TAbstractXML.createLogDir;
var
  dir: TSearchRec;
  ret: integer;
  path: string;
begin
  try
    path:=extractFilePath(forms.application.ExeName);
    path:=path+'log';
    ret:=sysUtils.findFirst(path,faAnyFile,dir);


    if ret<>NO_ERROR then
    begin
      createDir(path);
    end;
  finally
    sysutils.findClose(dir);
  end;
end;

function TAbstractXML.getPath: string;
begin
  result:=extractFilePath(application.ExeName)+'log\'+formatDateTime('yyyymmdd',now)+'.xml';
end;

{ TJPIterator }
constructor TJPIterator.create(link: TJpAbstractLink);
begin
  self.link:=link;
  top:=link.head;
end;

function TJPIterator.eol: boolean;
begin
  result:=false;

  if top=nil then result:=true;
end;

procedure TJPIterator.first;
begin
  top:=link.head;
end;

function TJPIterator.getData: PLinkNode;
begin
  result:=top;
end;

procedure TJPIterator.next;
begin
  top:=top^.next;
end;

{ TADODBWriter }

procedure TADODBWriter.commit(sqlStrs: string);
var
  i:integer;
begin
  i:=0;

  while i<=2 do
  begin
    try
      with query do
      begin
        close;
        SQL.Clear;
        SQL.Add(sqlStrs);
        execSQL;
      end;

      break;
    except
      on F:exception do
      begin
        try
          con.Close;
          con.Open;
        except
          on E: Exception do codesite.sendmsg(E.Message+#13+#10+'TADODBWriter.commit in share of cmserver 1');
        end;
        codesite.sendmsg(F.Message+#13+#10+'TADODBWriter.commit in share of cmserver 2');
        inc(i);
      end;
    end;
  end;
end;

constructor TADODBWriter.create(con: TADOConnection);
begin
  self.con:=con;

  query:=TADOQuery.Create(nil);
  query.Connection:=self.con;
end;

{ TStaticQueue }

constructor TStaticQueue.create(size: integer);
var
  i:integer;
begin
  self.size:=size;
  setLength(buffer,size);

  for i:=low(buffer) to high(buffer) do
    buffer[i]:=nil;

  cnt:=0;
  top:=0;
  rear:=0;

  initializeCriticalSection(sect);
end;

function TStaticQueue.isEmpty: boolean;
begin
  result:=false;

  if cnt<=0 then
    result:=true;
end;

function TStaticQueue.isFull: Boolean;
begin
  result:=false;

  if cnt>=size then
    result:=true;
end;

function TStaticQueue.pop:pointer;
begin
  enterCriticalSection(sect);
  try
    result:=nil;

⌨️ 快捷键说明

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