text
stringlengths
0
234
HS : constant Handshake_Type := +Data (1 .. Handshake_Image'Length);
begin
if HS.Length = Header'Length
and then HS.Head = Header
and then HS.Info_Hash = Self.Meta.Info_Hash
then
Self.Got_Handshake := True;
Self.Unparsed.Clear;
Self.Unparsed.Append
(Data (Handshake_Image'Length + 1 .. Data'Last));
return True;
else
return False;
end if;
end Get_Handshake;
----------------
-- Get_Length --
----------------
function Get_Length
(Data : Ada.Streams.Stream_Element_Array)
return Ada.Streams.Stream_Element_Offset
is
subtype X is Ada.Streams.Stream_Element_Offset;
begin
return ((X (Data (Data'First)) * 256
+ X (Data (Data'First + 1))) * 256
+ X (Data (Data'First + 2))) * 256
+ X (Data (Data'First + 3));
end Get_Length;
----------------
-- On_Message --
----------------
procedure On_Message (Data : Ada.Streams.Stream_Element_Array) is
function Get_Int
(From : Ada.Streams.Stream_Element_Count := 0) return Natural
is (Get_Int (Data, From));
Index : Piece_Index;
begin
case Data (Data'First) is
when 0 => -- choke
pragma Debug
(Torrent.Logs.Enabled,
Torrent.Logs.Print
(GNAT.Sockets.Image (Self.Peer) & " choke"));
Self.We_Choked := True;
Self.Unreserve_Intervals;
when 1 => -- unchoke
pragma Debug
(Torrent.Logs.Enabled,
Torrent.Logs.Print
(GNAT.Sockets.Image (Self.Peer) & " unchoke"));
Self.We_Choked := False;
Send_Initial_Requests;
when 2 => -- interested
pragma Debug
(Torrent.Logs.Enabled,
Torrent.Logs.Print
(GNAT.Sockets.Image (Self.Peer) & " interested"));
Self.He_Intrested := True;
when 3 => -- not interested
pragma Debug
(Torrent.Logs.Enabled,
Torrent.Logs.Print
(GNAT.Sockets.Image (Self.Peer) & " not interested"));
Self.He_Intrested := False;
when 4 => -- have
declare
Index : constant Piece_Index :=
Piece_Index (Get_Int (1) + 1);
begin
if Index in Self.Piece_Map'Range then
pragma Debug
(Torrent.Logs.Enabled,
Torrent.Logs.Print
(GNAT.Sockets.Image (Self.Peer)
& " have" & (Index'Img)));
Self.Piece_Map (Index) := True;
Check_Intrested;
end if;
Function Definition: procedure Send_Initial_Requests is
Function Body: Length : Piece_Interval_Count;
begin
if Self.Current_Piece.Intervals.Is_Empty then
Self.Listener.Reserve_Intervals
(Map => Self.Piece_Map,
Value => Self.Current_Piece);