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

📄 unit1.dfm

📁 delphiUDP安全传输代码示例,欢迎下载.
💻 DFM
📖 第 1 页 / 共 5 页
字号:
      '    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) {'#25968#25454#21457#36865#22788#29702#32447#31243
      '}'
      '  public'
      '    Owner: TudpCtl;'
      '    procedure Execute; override;'
      '    constructor Create(ISStop: boolean; IOwner: TudpCtl);'
      '  end;'
      '  TSafeUDPRecData = procedure(ISender: TudpCtl; IData: '
      'Pointer; ISize: Cardinal;'
      '    IFromIP: string; IPort: integer) of object;'
      '  TSafeUDPError = procedure(Sender: TObject; IData: '
      'PSafeUdpData; IErrorKind: SHeadLv) of object;'
      '  TudpCtl = class(TNMUDP)'
      '  private'
      '    FSendKind: SUDPSendKind;'
      '    FErrorKind: SUdpCtlState;'
      '    FFreeWindowsCount: Byte;'
      '    FPackedId: Cardinal;'
      '    procedure ClearOneData(iidx: Integer; IList: Tstrings); '
      'overload; {'#37322#25918#19968#20010#21253'}'
      '    procedure ClearList(IList: TStrings); {'#28165#31354#21015#34920'}'
      '    function GetAnPackedId: Cardinal; {'#33719#21462#19968#20010#25968#25454#21253'ID}'
      '    function GetAnPackedData(IDataSize: integer): '
      'PSafeUdpData; {'#33719#21462#19968#20010#21253'}'
      '    procedure CheckState; {'#29366#24577#26816#26597'}'
      '    procedure OnData(Sender: TComponent; NumberBytes: '
      'Integer; FromIP: string; Port: integer); {'#22788#29702#25509#25910#25968#25454'}'
      '  protected'
      '    SenderThread: TUdpSenderThread; {'#21457#36865#22788#29702#32447#31243'}'
      '    SendedList, SendingList, DataMixList: TStrings; {'#24050#21457#36865#30340
      #25968
      #25454#21015#34920#65292#38431#21015#20013#31561#24453#21457#36865#30340#25968#25454#21015#34920'}'
      '    SendMemory: TMemoryStream; {'#27491#22312#21457#36865#32531#20914#65292#21644#21097
      #20313#21457
      #36865#32531#20914'}'
      
        '//--------------------------------------------------------------' +
        '----------------'
      '// '#33258#21160#35302#21457#30340#20107#20214' 2006-6-5 '#39532#25935#38026
      
        '//--------------------------------------------------------------' +
        '----------------'
      '    procedure OnReSendEvent(Sender: TObject); {'#21040#26102#37325#21457
      #20107
      #20214'}'
      '    procedure OnTimeOutEvent(Sender: TObject); {'#25509#25910#36229#26102
      #20107
      #20214'}'
      
        '//--------------------------------------------------------------' +
        '----------------'
      '// '#21487#20379#23376#31867#25913#20889#30340#26041#27861' 2006-6-9 '#39532#25935#38026
      
        '//--------------------------------------------------------------' +
        '----------------'
      '    procedure PackedData(var IBuff; ISize: Cardinal; Ilv: '
      'SheadLv); {'#21253#35065#25968#25454'}'
      '    procedure UnPackedData(IMixer: TDataMixer); {'#21453#21253#35065#25968
      #25454
      '}'
      '    procedure AddToSendList(IId: string; IData: Pointer);'
      '    procedure DidSend(IData: PSafeUdpData); {'#23454#38469#30340#21457#36865
      #36807
      #31243'}'
      '    function CheckPacked(IData: PSafeUdphead): Integer;'
      '    function MixData(IData: PSafeUdpData; Iidx: integer): '
      'boolean;'
      '  public'
      '    MaxReSendCount: Byte; {'#26368#22823#37325#35797#27425#25968' '#40664#35748#20026'3}'
      '    PeerSize: Word; {'#20998#21253#26102#27599#20010#21253#30340#22823#23567' '#40664#35748#20026'1024}'
      '    WaiteTimeOut, ReSendTime: Cardinal; {'#36229#26102#26102#38388' '#40664#35748
      #20026'3000,'#20002#21253#37325#21457#26102#38388'500}'
      '    CurrRealData {'#24403#21069#30340#23454#38469#24050#32463#22788#29702#23436#27605#30340#21253'}: '
      'TMemoryStream;'
      '    CurrDataSize: Cardinal; {'#24403#21069#25968#25454#30340#22823#23567'}'
      '    OnDataCase: TSafeUDPRecData; {'#29992#25143#33258#24049#30340#22788#29702#20107
      #20214'}'
      '    OnDataError: TSafeUDPError; {'#25968#25454#38169#35823#26102#35302#21457#30340#20107#20214
      '}'
      
        '//--------------------------------------------------------------' +
        '----------------'
      '// '#23646#24615' 2006-6-9 '#39532#25935#38026
      
        '//--------------------------------------------------------------' +
        '----------------'
      '   {'#38169#35823#22788#29702#26041#24335' '#40664#35748#20026#20351#29992#20107#20214'}'
      '    property ErrorKind: SUdpCtlState read FErrorKind write '

⌨️ 快捷键说明

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