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

📄 unit1.dfm

📁 delphiUDP安全传输代码示例,欢迎下载.
💻 DFM
📖 第 1 页 / 共 5 页
字号:
      '    LData := GetAnPackedData(LSize);'
      '    LData^.Head.Id := CSafeUdpData;'
      '    LData^.Head.PackedId := LPackedID;'
      '    LData^.Head.Lv := Ilv;'
      '    LData^.Head.Part := I;'
      '    LData^.Head.TotPart := LTotPart;'
      '    LData^.Head.Size := LSize;'
      '    CopyMemory(LData.Data, LP, LSize);'
      '    {'#31227#21160#25351#38024'}'
      '    inc(LP, LSize);'
      '    Gob_Debug.AddLogShower('#39'%s---%d'#39', '
      '[IntToStr(LData^.Head.PackedId) + '#39'_'#39' + '
      'IntToStr(LData^.Head.Part), LSize]);'
      '    {'#21152#20837#21457#36865#38431#21015'}'
      '    SendingList.AddObject(IntToStr(LData^.Head.PackedId) + '#39'_'#39' '
      '+ IntToStr(LData^.Head.Part), TObject(LData));'
      '   // AddToSendList(IntToStr(LData^.Head.PackedId) + '#39'_'#39' + '
      'IntToStr(LData^.Head.Part), TObject(LData));'
      '  end; // for'
      'end;'
      ''
      'procedure TudpCtl.DidSend(IData: PSafeUdpData);'
      'begin'
      '  SendMemory.SetSize(Sizeof(IData^.Head) + '
      'IData^.Head.Size);'
      '  SendMemory.Position := 0;'
      '  SendMemory.WriteBuffer(IData^.Head, Sizeof(IData^.Head));'
      '  SendMemory.WriteBuffer(IData^.Data^, IData^.Head.Size);'
      '  {'#21028#26029#26159#21542#38656#35201#36820#22238'}'
      '  if IData^.Head.PackedId > 0 then begin'
      '    IData^.SendTime := GetTickCount;'
      '    SendedList.AddObject(IntToStr(IData^.Head.PackedId) + '#39'_'#39' '
      '+ IntToStr(IData^.Head.Part), TObject(IData));'
      '  end;'
      '  {'#21457#36865'}'
      '  SendBuffer(SendMemory.Memory^, SendMemory.Size);'
      'end;'
      ''
      'function TudpCtl.MixData(IData: PSafeUdpData; Iidx: integer): '
      'boolean;'
      'var'
      '  Lbuff: TDataMixer;'
      'begin'
      '  Result := False;'
      '  {'#28155#21152#21040#21508#20010#21253#30340#21508#20010#37096#20998'}'
      '  Lbuff := TDataMixer(DataMixList.Objects[Iidx]);'
      '  Inc(Lbuff.CurrPart);'
      '  Lbuff.BeginTime := GetTickCount;'
      '  Lbuff.DataList.AddObject(IntToStr(Idata^.Head.Part), '
      'TObject(IData));'
      '  //Gob_Debug.AddLogShower('#39'Check'#39');'
      '  //Gob_Debug.AddLogShower(Lbuff.CurrPart);'
      '  if Lbuff.CurrPart = Lbuff.TotPart then begin'
      '    UnPackedData(Lbuff);'
      '    {'#22914#26524#32452#21253#25104#21151' '#21017#36820#22238'True'#20801#35768#32487#32493#20256#36882#25968#25454'}'
      '    Result := True;'
      '  end;'
      'end;'
      ''
      'procedure TudpCtl.UnPackedData(IMixer: TDataMixer);'
      'var'
      '  i: Integer;'
      '  LTOT: Cardinal;'
      '  Lbuff: PSafeUdpData;'
      '  Lp: Pchar;'
      '  LFun: TStringListSortCompare;'
      'begin'
      '  with IMixer do begin'
      '    try'
      '      LFun := @OrderShort;'
      '      TStringList(DataList).CustomSort(LFun);'
      '      CurrRealData.Position := 0;'
      '      LTOT := 0;'
      '      Gob_Debug.AddLogShower('#39'Begin Cmb'#39');'
      '      for i := 0 to DataList.Count - 1 do begin // Iterate'
      '        Gob_Debug.AddLogShower(DataList.Strings[i]);'
      '        Lbuff := PSafeUdpData(DataList.Objects[i]);'
      
        '        CurrRealData.WriteBuffer(lbuff^.Data^, lbuff^.Head.Size)' +
        ';'
      '        inc(LTOT, lbuff^.Head.Size);'
      '        ClearOneData(Lbuff);'
      '      end; // for'
      '      DataList.Clear;'
      '    except'
      '      raise Exception.Create('#39#32452#21253#26102#21457#29983#24322#24120#39');'
      '    end;'
      '    CurrDataSize := LTOT;'
      '    CurrRealData.Position := 0;'
      '  end;'
      'end;'
      ''
      'class procedure TudpCtl.ClearOneData(IData: PSafeUdpData);'
      'var'
      '  LP: PSafeUdpData;'
      'begin'
      '  LP := IData;'
      '  FreeMem(LP^.Data);'
      '  Dispose(LP);'
      'end;'
      ''
      'class function TudpCtl.IsUdpCtlData(IData: Pointer; ISize: '
      'Integer): boolean;'
      'begin'
      '  Result := True;'
      '  if (ISize < Sizeof(RSafeUdphead)) or (Pinteger(IData)^ < '
      'CSafeUdpData) then'
      '    Result := False;'
      'end;'
      ''
      'procedure TudpCtl.SetCurrData(Idata: Pointer; ISize: Integer);'
      'begin'
      '  CurrRealData.SetSize(ISize);'
      '  CurrRealData.Position := 0;'
      '  CurrRealData.WriteBuffer(idata^, ISize);'
      '  CurrRealData.Position := 0;'
      'end;'
      ''
      'procedure TudpCtl.SafeSendBuff(var IBuff; ISize: Cardinal; Ilv:'
      '  SheadLv = SDoError);'
      'begin'
      '  {'#22788#29702#21152#24037#25968#25454'}'
      '  PackedData(IBuff, ISize, Ilv);'
      'end;'
      ''
      'procedure TudpCtl.OnData(Sender: TComponent; '
      'NumberBytes: Integer;'
      '  FromIP: string; Port: integer);'
      'begin'
      ' // if (NumberBytes > 0) and (NumberBytes < 10000) then begin'
      '  with TudpCtl(Sender) do begin'
      '    CurrRealData.SetSize(NumberBytes);'
      '    ReadBuffer(CurrRealData.Memory^, NumberBytes);'
      '    if IsUdpCtlData(CurrRealData.Memory, NumberBytes) then '
      'begin'
      '      //  Gob_Debug.AddLogShower(NumberBytes);'
      '      if not CaseData(CurrRealData.Memory, FromIP, Port, '
      'NumberBytes) then'
      '        exit;'
      '    end;'
      '    if assigned(OnDataCase) then'
      '      OnDataCase(TudpCtl(Sender), CurrRealData.Memory, '
      'CurrDataSize, FromIP, Port);'
      '  end; // with'
      '//  end;'
      'end;'
      ''
      ''
      'function TudpCtl.CheckPacked(IData: PSafeUdphead): Integer;'
      'var'
      '  Lidx: Integer;'
      '  Lbuff: TDataMixer;'
      'begin'
      '  Result := -1;'
      
        '  if (IData^.PackedId < 1) or (IData^.Size < 1) or (IData^.Size ' +
        '> '
      '10000) then exit;'
      '  Lidx := DataMixList.IndexOf(IntToStr(Idata^.PackedId));'
      '  if Lidx = -1 then begin {*'#27809#26377#36825#20010#21253'}'
      '    Lbuff := TDataMixer.Create;'
      '    Lbuff.Id := Idata^.PackedId;'
      '    Lbuff.TotPart := Idata^.TotPart;'
      '    Lidx := DataMixList.AddObject(IntToStr(Lbuff.Id), lbuff);'
      '  end;'
      '  Lbuff := TDataMixer(DataMixList.Objects[Lidx]);'
      
        '  if Lbuff.DataList.IndexOf(IntToStr(Idata^.Part)) > -1 then Exi' +
        't;'
      '  Result := Lidx;'
      'end;'
      ''
      '{ TUdpSenderThread }'
      ''
      'constructor TUdpSenderThread.Create(ISStop: boolean; '
      'IOwner: TudpCtl);'
      'begin'
      '  inherited Create(ISStop);'
      '  Owner := IOwner;'
      'end;'
      ''
      'procedure TUdpSenderThread.Execute;'
      'var'
      '  GLData: PSafeUdpData;'
      'begin'
      '  while not Terminated do begin'
      '    try'
      '      with Owner do begin'
      '        {'#26816#26597#21457#36865#20986#21435#30340#21015#34920#20013#30340#29366#24577'}'
      '        CheckState;'
      '        {'#24403#26377#38656#35201#21457#36865#30340#25968#25454#26102#24490#29615'}'
      '        if SendingList.Count > 0 then begin'
      '        {'#21028#26029#21457#36865#27169#24335'}'
      '          case SendKind of //'
      '            SUSKFreeWindows: begin'
      '                {'#22914#26524#24050#21457#36865#21015#34920#20869#25968#25454#23567#20110#28369#21160#31383#21475#25968
      #37327' '#21017#24320
      #22987#21457#36865'}'
      
        '                if SendedList.Count < FreeWindowsCount then begi' +
        'n'
      
        '                  GLData := PSafeUdpData(SendingList.Objects[0])' +
        ';'
      '                  DidSend(GLData);'
      '                  SendingList.Delete(0);'
      '                end'
      '                else'
      '                  Sleep(10);'
      '              end;'
      '            SUSKOnlyOne: begin'
      '                if SendedList.Count = 0 then begin'
      
        '                  GLData := PSafeUdpData(SendingList.Objects[0])' +
        ';'
      '                  DidSend(GLData);'
      '                  SendingList.Delete(0);'
      '                end'
      '                else begin'
      '                  Sleep(10); Break; end;'
      '              end;'
      '          end; // case'
      '        end'
      '        else'
      '          Sleep(10);'
      '      end; // with'
      '    except'
      '      Sleep(10);'
      '    end; // try'
      '  end; // while'
      'end;'
      ''
      '{ TDataMixer }'
      ''
      'constructor TDataMixer.Create;'
      'begin'
      '  DataList := THashedStringList.Create;'
      'end;'
      ''
      'destructor TDataMixer.Destroy;'
      'var'
      '  i: Integer;'
      'begin'
      '  for i := 0 to DataList.Count - 1 do // Iterate'
      '    TudpCtl.ClearOneData(PsafeUdpData(DataList.Objects[i]));'
      '  DataList.Free;'
      '  inherited;'
      'end;'
      ''
      'end.{*******************************************************}'
      '{      '#21333#20803#21517#65306'  UntUdpCtl.pas                          }'
      '{      '#21019#24314#26085#26399#65306'2006-6-9 14:20:12                      }'
      '{      '#21019#24314#32773'    '#39532#25935#38026'                                 }'
      '{      '#21151#33021#65306'    V1.0                                   }'
      '{                UDP'#30340#23433#20840#20256#36755#25511#21046#31867#26242#26102#22522#20110'NMUDP       '
      '}'
      '{                 '#36127#36131'                                  }'
      '{                  '#20998#21253#25511#21046'                             }'
      '{                  '#24212#31572#25511#21046'                             }'
      '{                  '#37325#21457#25511#21046'                             }'
      '{            '#21253#22836#22823#20110'CSafeUdpData'#30340#25968#25454#23601#26159#26412#31867#30340'       '
      '}'
      '{            '#25511#21046#22788#29702#21327#35758'                               }'
      '{*******************************************************}'
      ''
      'unit UntUdpCtl;'
      ''
      'interface'
      ''
      'uses Classes, NMUDP, sysutils;'
      ''
      'const'
      '  CSafeUdpData = 100000; {UDP'#25968#25454#25511#21046#21253#30340#26368#20302#21629#20196
      #21495'}'
      '  CSafeUdpData_Uses = 200000; {'#29992#25143#20351#29992#30340#21629#20196'}'
      '  CSafeUdpData_RecvResp = CSafeUdpData_Uses + 1; //'#22238
      #24212#25910#21040#21629#20196
      ''
      'type'
      '  EUdpCtlReTryNoResp = class(Exception); {'#37325#35797'N'#27425#21518#25253
      #38169'}'
      '  EUdpCtlTimeOut = class(Exception); {'#19968#30452#37325#35797#30452#21040#36229#26102'}'
      '  {'#20002#21253#30340#22788#29702#26041#24335'}'
      '  SHeadLv = (SDoDrop {'#37325#35797'N'#27425#21518#23601#19981#20877#21457#36865'}, '
      'SDoError {'
      #37325#35797'N'#27425#21518#25253#38169'}, SDoTimeOut {'#19968#30452#37325#35797#30452#21040#36229#26102'});'
      '  {'#21253#22836'}'
      '  PSafeUdphead = ^RSafeUdphead;'
      '  RSafeUdphead = packed record'
      '    Id: Integer; {'#21629#20196#21495'}'
      '    Part, TotPart: Byte; {'#21253#30340#31532#20960#37096#20998','#20849#26377#20960#37096#20998',}'
      '    Lv: SheadLv; {'#21253#30340#31561#32423' 0'#12289' 1'#12289' 2}'
      '    PackedId: Cardinal; {'#21253#30340'ID'#21495' '#65292#22914#26524#27492#21495#22823#20110'0'#35828#26126
      #38656#35201
      #22238#24212'}'
      '    Size: Cardinal; {'#22823#23567'}'
      '  end;'
      '  {'#25968#25454#21253'}'
      '  PSafeUdpData = ^RSafeUdpData;'
      '  RSafeUdpData = packed record'
      '    Head: RSafeUdphead; {'#21253#22836'}'
      '    Data: Pointer; {'#21253#20307'}'
      '    SendTime: Cardinal; {'#21457#36865#26102#38388'}'
      '    ReTryCount: Byte; {'#37325#35797#27425#25968'}'
      '  end;'
      '  {'#28151#21512#21253#31867'}'
      '  TDataMixer = class'
      '  public'
      '    Id: Cardinal; {'#21253#30340'ID}'
      '    BeginTime: Cardinal; {'#24320#22987#28151#21512#30340#26102#38388'}'
      '    TotPart, CurrPart: Byte; {'#21253#30340#24635#25968#65292#29616#26377#30340#21253#25968'}'
      '    DataList: TStrings; {'#25968#25454#21015#34920'}'
      '    constructor Create();'
      '    destructor Destroy; override;'
      '  end;'
      ''
      '  SUDPSendKind = (SUSKFreeWindows {'#28369#21160#31383#21475#27169#24335'}, '
      'SUSKOnlyOne {'#27599#27425#21482#21457#36865#19968#20010#21629#20196#30452#21040#36820#22238#25165#32487#32493
      '});'
      '  SUdpCtlState = (SRaiseExpection {'#25243#20986#24322#24120'}, '
      'SUseErrorEvent {'#25191#34892#38169#35823#20107#20214'}); {Udp'#25511#21046#29366#24577'}'
      '  TudpCtl = class;'
      '  TUdpSenderThread = class(TThread) {'

⌨️ 快捷键说明

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