From 49eccea44ffb1b37bbfbd5ce01eb7abf21106944 Mon Sep 17 00:00:00 2001 From: Jesper Louis Andersen Date: Sat, 2 Aug 2008 14:31:04 +0200 Subject: [PATCH] Implement encoding/decoding of fast extension messages. --- .../src/etorrent_peer_communication.erl | 89 ++++++++++++++++------ 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/lib/etorrent-1.0/src/etorrent_peer_communication.erl b/lib/etorrent-1.0/src/etorrent_peer_communication.erl index 0a718af..945e244 100644 --- a/lib/etorrent-1.0/src/etorrent_peer_communication.erl +++ b/lib/etorrent-1.0/src/etorrent_peer_communication.erl @@ -32,6 +32,13 @@ -define(CANCEL, 8:8). -define(PORT, 9:8). +%% FAST EXTENSION Packet types +-define(SUGGEST, 13:8). +-define(HAVE_ALL, 14:8). +-define(HAVE_NONE, 15:8). +-define(REJECT_REQUEST, 16:8). +-define(ALLOWED_FAST, 17:8). + %%==================================================================== %% API %%==================================================================== @@ -42,30 +49,42 @@ %%-------------------------------------------------------------------- recv_message(Rate, Message) -> MSize = size(Message), - Decoded = case Message of - <<>> -> - keep_alive; - <> -> - choke; - <> -> - unchoke; - <> -> - interested; - <> -> - not_interested; - <> -> - {have, PieceNum}; - <> -> - {bitfield, BitField}; - <> -> - {request, Index, Begin, Len}; - <> -> - {piece, Index, Begin, Data}; - <> -> - {cancel, Index, Begin, Len}; - <> -> - {port, Port} - end, + Decoded = + case Message of + <<>> -> + keep_alive; + <> -> + choke; + <> -> + unchoke; + <> -> + interested; + <> -> + not_interested; + <> -> + {have, PieceNum}; + <> -> + {bitfield, BitField}; + <> -> + {request, Index, Begin, Len}; + <> -> + {piece, Index, Begin, Data}; + <> -> + {cancel, Index, Begin, Len}; + <> -> + {port, Port}; + %% FAST EXTENSION MESSAGES + <> -> + {suggest, Index}; + <> -> + have_all; + <> -> + have_none; + <> -> + {reject_request, Index, Offset, Len}; + <> -> + {allowed_fast, decode_allowed_fast(FastSet)} + end, {Decoded, etorrent_rate:update(Rate, MSize), MSize}. %%-------------------------------------------------------------------- @@ -97,7 +116,19 @@ send_message(Rate, Socket, Message) -> {cancel, Index, Begin, Len} -> <>; {port, PortNum} -> - <> + <>; + %% FAST EXTENSION + {suggest, Index} -> + <>; + have_all -> + <>; + have_none -> + <>; + {reject_request, Index, Offset, Len} -> + <>; + {allowed_fast, FastSet} -> + BinFastSet = encode_fastset(FastSet), + <> end, Sz = size(Datagram), Res = gen_tcp:send(Socket, <>), @@ -280,3 +311,11 @@ decode_bytes(SoFar, [B | Rest]) -> [decode_byte(B, SoFar) | decode_bytes(SoFar + 8, Rest)]. +decode_allowed_fast(<<>>) -> []; +decode_allowed_fast(<>) -> + [Index | decode_allowed_fast(Rest)]. + +encode_fastset([]) -> <<>>; +encode_fastset([Idx | Rest]) -> + R = encode_fastset(Rest), + <>. -- 2.11.4.GIT