torckapi 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3112c8317a08948c59951be48af26276e8c2577
4
- data.tar.gz: 79b61c263454d66d48be430c6d7f306e7efc86e8
3
+ metadata.gz: f0e78cdf84987163d2164e0eab58654e6c0825c6
4
+ data.tar.gz: 7516aecf37f02107ce58f5c193dfd1a9285f7289
5
5
  SHA512:
6
- metadata.gz: de86e0c8f5c0587eef46b3c1aa75ec6001a02b0698da54a0bb8f72f461a014fe8b4dd3a932981704b1f55030e641c219a4a6ecf911b88afa0a071cc72189d86a
7
- data.tar.gz: 66b12c835cae8252a7bb35794434e18b4505710c1c6fe1b7ee399c9ee988d3ec7f17072dab2321d8f39b8654b60e7fd706d29515d6679aed3a70e77ada54fd11
6
+ metadata.gz: 3c560a87c520bf0f0220fb8608e72cc0072455aeb7be1ed884d8bbb1bb285eb3e9e3f01a3d500e49ee31c99cdaeab6ed241acb6e8e035fbf9dcfe6cea265a0a6
7
+ data.tar.gz: 76931f6d709de866750b102a8097d7fbf2b30dbc098c419d4208cf83a10fd54dc411545cf080b8fcd371446a3653e783abd93ce333671eb180b025337a554e38
@@ -28,7 +28,7 @@ module Torckapi
28
28
  connect
29
29
  response = communicate action, data
30
30
 
31
- RESPONSE_CLASSES[response[0][0..3].unpack('L>')[0]].from_udp(*args, response[0][8..-1])
31
+ RESPONSE_CLASSES[response[:type]].from_udp(*args, response[:data])
32
32
  end
33
33
 
34
34
  def announce_request_data info_hash, peer_id
@@ -42,39 +42,45 @@ module Torckapi
42
42
  def connect
43
43
  return if @connection_id && @communicated_at.to_i >= Time.now.to_i - CONNECTION_TIMEOUT
44
44
 
45
- @connection_id = [0x41727101980].pack('Q>')
45
+ @connection_id = [0x041727101980].pack('Q>')
46
46
  response = communicate Connect
47
- @connection_id = response[0][8..15]
47
+ @connection_id = response[:data]
48
48
  end
49
49
 
50
50
  def communicate action, data=nil
51
51
  @socket ||= UDPSocket.new
52
52
 
53
53
  transaction_id = SecureRandom.random_bytes(4)
54
- packet = [@connection_id, [action].pack('L>'), transaction_id, data].join
55
-
56
54
  tries = 0
57
55
  response = nil
58
56
 
59
57
  begin
58
+ packet = [@connection_id, [action].pack('L>'), transaction_id, data].join
59
+
60
60
  Timeout::timeout(@options[:timeout], CommunicationTimeoutError) do
61
61
  @socket.send(packet, 0, @url.host, @url.port)
62
- response = @socket.recvfrom(65536)
63
- raise TransactionIdMismatchError if transaction_id != response[0][4..7]
64
-
65
- response_type = response[0][0..3].unpack('L>')[0]
66
- raise(MalformedResponseError, response) if !(0...RESPONSE_CLASSES.length).include?(response_type) ||
67
- RESPONSE_MIN_LENGTHS[response_type] > response[0].length
62
+ response = parse_response @socket.recvfrom(65536), transaction_id
68
63
  @communicated_at = Time.now
69
64
  end
70
65
  rescue CommunicationTimeoutError
71
66
  retry if (tries += 1) <= @options[:tries]
72
67
  end
73
68
 
74
- raise CommunicationFailedError if response.nil?
69
+ raise CommunicationFailedError unless response
75
70
 
76
71
  response
77
72
  end
73
+
74
+ def parse_response data, transaction_id
75
+ response, sender_addrinfo = data
76
+
77
+ response_type = response[0..3].unpack('L>')[0]
78
+
79
+ raise TransactionIdMismatchError, response.inspect if transaction_id != response[4..7]
80
+ raise MalformedResponseError, response.inspect if RESPONSE_MIN_LENGTHS[response_type] > response.length
81
+
82
+ {type: response_type, data: response[8..-1]}
83
+ end
78
84
  end
79
85
  end
80
86
  end
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.22"
2
+ VERSION = "0.0.23"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torckapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Krupenik