P2pCommon.pas
资源名称:P2P.rar [点击查看]
上传用户:zhanghw123
上传日期:2021-11-15
资源大小:312k
文件大小:2k
源码类别:
P2P编程
开发平台:
Delphi
- Unit P2pCommon;
- Interface
- Uses
- Windows, SysUtils, Classes,
- NMUDP;
- Type
- TP2pBinding = Packed Record
- PeerIP: String;
- PeerPort: Integer;
- End;
- TP2pUserConfig = Class
- Private
- FSayHelloInv: Integer; {心跳间隔时间}
- FLastSayHelloTime: Cardinal; {*最后一次心跳的时间}
- FSayHelloCount: Byte; {重发心跳包 无回应就断开的次数}
- FMaxSendCount: Word; {每次最大发送量}
- FServerRSPtime: Cardinal; {服务器返回最后时间}
- Public
- {心跳间隔时间}
- Property Livinginv: Integer Read FSayHelloInv Write FSayHelloInv;
- {上次心跳时间}
- Property LastLiveTime: Cardinal Read FLastSayHelloTime Write FLastSayHelloTime;
- {可以存活的次数}
- Property LifePoint: Byte Read FSayHelloCount Write FSayHelloCount;
- {服务器返回的时间}
- Property ServerRspTime: Cardinal Read FServerRSPtime Write FServerRSPtime;
- Constructor Create; Overload;
- Destructor Destroy; Override;
- End;
- TP2pUser = Class
- Public
- Binding: TP2pBinding;
- Address: String;
- Id: Cardinal; //用户唯一标示
- Call: String;
- Socket: TNMUDP;
- ListOwer: TObject; {客户端所用记录List的句柄}
- MMCfg: TP2pUserConfig; {配置文件}
- Constructor Create(ABinding: TP2pBinding; IsCreateUdp: boolean = True);
- Destructor Destroy; Override;
- End;
- Implementation
- {P2pUserConfig}
- Constructor TP2pUserConfig.Create;
- Begin
- FSayHelloInv := 30 * 1000; {默认30秒心跳一次}
- FLastSayHelloTime := GetTickCount;
- FSayHelloCount := 2; {重试心跳次数}
- FMaxSendCount := 16; {每次最大的发送总量}
- FServerRSPtime := GetTickCount;
- End;
- Destructor TP2pUserConfig.Destroy;
- Begin
- Inherited;
- End;
- {P2pUser}
- Constructor TP2pUser.Create(ABinding: TP2pBinding; IsCreateUdp: boolean);
- Begin
- Binding := ABinding;
- Address := Binding.PeerIP + ':' + IntToStr(Binding.PeerPort);
- MMCfg := TP2pUserConfig.Create;
- If IsCreateUdp Then
- Begin
- Socket := TNMUDP.Create(Nil);
- Socket.RemoteHost := Binding.PeerIP;
- Socket.RemotePort := Binding.PeerPort;
- End;
- End;
- Destructor TP2pUser.Destroy;
- Begin
- If MMCfg <> Nil Then FreeAndNil(MMCfg);
- If Socket <> Nil Then FreeAndNil(Socket);
- Inherited;
- End;
- End.