Changeset 3 for trunk/UNetwork.pas
- Timestamp:
- Sep 16, 2016, 10:49:27 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UNetwork.pas
r2 r3 19 19 class operator Equal(A, B: TNetworkAddress): Boolean; 20 20 function GetString: string; 21 procedure Clear; 21 22 end; 22 23 … … 44 45 public 45 46 Node: TNetworkNode; 46 Address: Integer;47 Address: TNetworkAddress; 47 48 Network: Integer; 48 49 procedure Connect(Remote: TNetworkInterface); … … 50 51 procedure SendPacket(Packet: TNetworkPacket); 51 52 constructor Create; 53 destructor Destroy; override; 52 54 property Remote: TNetworkInterface read FRemote 53 55 write SetRemote; … … 112 114 function NetworkAddress(AParts: array of Integer): TNetworkAddress; 113 115 116 114 117 implementation 115 118 … … 121 124 for I := 0 to High(Result.Parts) do 122 125 Result.Parts[I] := AParts[I]; 123 Result.Context := 0;124 126 end; 125 127 … … 142 144 Result := ''; 143 145 for I := 0 to Length(Parts) - 1 do begin 144 if I = Context then Result := Result + ';' 145 else if I > 0 then Result := Result + '.'; 146 if I > 0 then Result := Result + '.'; 146 147 Result := Result + IntToStr(Parts[I]); 147 148 end; 149 end; 150 151 procedure TNetworkAddress.Clear; 152 begin 153 SetLength(Parts, 0); 148 154 end; 149 155 … … 155 161 I: Integer; 156 162 begin 157 if Packet.Destination = NetworkAddress([Sender.Address])then163 if Packet.Destination = Sender.Address then 158 164 DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString) 159 165 else begin 160 166 if Length(Packet.Destination.Parts) > Packet.Destination.Context then 161 167 for I := 0 to Interfaces.Count - 1 do 162 with TNetworkInterface(Interfaces[I])do begin168 with Interfaces[I] do begin 163 169 if Network = Packet.Destination.Parts[Packet.Destination.Context + 1] then begin 164 170 //Inc … … 177 183 begin 178 184 for I := 0 to Interfaces.Count - 1 do 179 if TNetworkInterface(Interfaces[I])<> Sender then180 TNetworkInterface(Interfaces[I]).SendPacket(Packet);185 if Interfaces[I] <> Sender then 186 Interfaces[I].SendPacket(Packet); 181 187 end; 182 188 … … 263 269 R = 30; 264 270 begin 265 Canvas.Pen.Color := cl Black;271 Canvas.Pen.Color := clRed; 266 272 Canvas.Pen.Style := psSolid; 267 273 … … 277 283 for J := 0 to Interfaces.Count - 1 do 278 284 if Assigned(Interfaces[J]) then begin 279 if Assigned( TNetworkInterface(Interfaces[J]).Remote) then begin280 RemoteInterface := TNetworkInterface(Interfaces[J]).Remote;285 if Assigned(Interfaces[J].Remote) then begin 286 RemoteInterface := Interfaces[J].Remote; 281 287 RemoteNode := RemoteInterface.Node; 282 288 RemoteInterfaceIndex := RemoteNode.Interfaces.IndexOf(RemoteInterface); 283 289 Canvas.Line(Position.X + J * R - R * Interfaces.Count div 2 + R div 2, Position.Y, 284 RemoteInterface.Node.Position.X + RemoteInterfaceIndex * R - R * RemoteNode.Interfaces.Count div 2 + R div 2, RemoteNode.Position.Y); 290 RemoteInterface.Node.Position.X + RemoteInterfaceIndex * R - R * 291 RemoteNode.Interfaces.Count div 2 + R div 2, RemoteNode.Position.Y); 285 292 end; 286 293 end; … … 290 297 for J := 0 to Interfaces.Count - 1 do 291 298 if Assigned(Interfaces[J]) then begin 292 if TNetworkInterface(Interfaces[J]).Address > 0 then293 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(Int ToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,294 Position.Y - Canvas.TextHeight(Int ToStr(TNetworkInterface(Interfaces[J]).Address)) div 2,295 Int ToStr(TNetworkInterface(Interfaces[J]).Address));296 if TNetworkInterface(Interfaces[J]).Network > 0 then297 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(IntToStr( TNetworkInterface(Interfaces[J]).Network)) div 2,298 Position.Y - R - Canvas.TextHeight(IntToStr( TNetworkInterface(Interfaces[J]).Network)) div 2,299 IntToStr( TNetworkInterface(Interfaces[J]).Network));299 //if Interfaces[J].Address > 0 then 300 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(Interfaces[J].Address.GetString) div 2, 301 Position.Y - Canvas.TextHeight(Interfaces[J].Address.GetString) div 2, 302 Interfaces[J].Address.GetString); 303 //if Interfaces[J].Network > 0 then 304 Canvas.TextOut(Position.X + J * R - R * Interfaces.Count div 2 + R div 2 - Canvas.TextWidth(IntToStr(Interfaces[J].Network)) div 2, 305 Position.Y - R - Canvas.TextHeight(IntToStr(Interfaces[J].Network)) div 2, 306 IntToStr(Interfaces[J].Network)); 300 307 end; 301 308 end; … … 312 319 Packet: TNetworkPacket); 313 320 begin 314 if Packet.Destination = NetworkAddress([Sender.Address])then321 if Packet.Destination = Sender.Address then 315 322 DoLog(Self, 'Received ' + Packet.Data + ' from ' + Packet.Source.GetString); 316 323 end; … … 328 335 UseInterface := TNetworkInterface(Interfaces[0]); 329 336 NewPacket := TNetworkPacket.Create; 330 NewPacket.Source := NetworkAddress(UseInterface.Address);337 NewPacket.Source := UseInterface.Address; 331 338 NewPacket.Destination := Address; 332 339 NewPacket.Data := 'ping'; … … 340 347 Result := TNetworkInterface.Create; 341 348 Result.Node := Self; 342 Result.Address := 0;349 Result.Address.Clear; 343 350 Result.OnReceivePacket := ReceivePacket; 344 351 Interfaces.Add(Result); … … 401 408 begin 402 409 FRemote := nil; 403 Address := 0; 410 Address.Clear; 411 end; 412 413 destructor TNetworkInterface.Destroy; 414 begin 415 inherited Destroy; 404 416 end; 405 417
Note:
See TracChangeset
for help on using the changeset viewer.