mirror of
https://github.com/warppipe/swift-torrent.git
synced 2026-05-28 15:27:20 +00:00
6c8c581517
Full BEP-3 peer wire protocol, BEP-5 DHT, BEP-15 UDP trackers, magnet link support, bencode serialization, rarest-first piece selection, and async session management using SwiftNIO and swift-crypto. Includes 64 passing unit tests. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
45 lines
1.4 KiB
Swift
45 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
public struct BencodeEncoder: Sendable {
|
|
public init() {}
|
|
|
|
public func encode(_ value: BencodeValue) -> Data {
|
|
var data = Data()
|
|
encodeValue(value, into: &data)
|
|
return data
|
|
}
|
|
|
|
private func encodeValue(_ value: BencodeValue, into data: inout Data) {
|
|
switch value {
|
|
case .integer(let v):
|
|
data.append(UInt8(ascii: "i"))
|
|
data.append(contentsOf: String(v).utf8)
|
|
data.append(UInt8(ascii: "e"))
|
|
|
|
case .string(let v):
|
|
data.append(contentsOf: String(v.count).utf8)
|
|
data.append(UInt8(ascii: ":"))
|
|
data.append(v)
|
|
|
|
case .list(let items):
|
|
data.append(UInt8(ascii: "l"))
|
|
for item in items {
|
|
encodeValue(item, into: &data)
|
|
}
|
|
data.append(UInt8(ascii: "e"))
|
|
|
|
case .dictionary(let pairs):
|
|
data.append(UInt8(ascii: "d"))
|
|
// Keys must be sorted lexicographically
|
|
let sorted = pairs.sorted { $0.key.lexicographicallyPrecedes($1.key) }
|
|
for pair in sorted {
|
|
data.append(contentsOf: String(pair.key.count).utf8)
|
|
data.append(UInt8(ascii: ":"))
|
|
data.append(pair.key)
|
|
encodeValue(pair.value, into: &data)
|
|
}
|
|
data.append(UInt8(ascii: "e"))
|
|
}
|
|
}
|
|
}
|