iproto 0.3.14 → 0.3.15

Sign up to get free protection for your applications and to get access to all the features.
data/iproto.gemspec CHANGED
@@ -4,8 +4,8 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.5'
5
5
 
6
6
  s.name = 'iproto'
7
- s.version = '0.3.14'
8
- s.date = '2014-01-15'
7
+ s.version = '0.3.15'
8
+ s.date = '2014-10-27'
9
9
  s.rubyforge_project = 'iproto'
10
10
 
11
11
  s.summary = "Mail.Ru simple network protocol"
@@ -1,9 +1,12 @@
1
1
  require 'bin_utils'
2
+ require 'iproto/core-ext'
2
3
  module IProto
3
4
  module ConnectionAPI
4
5
  DEFAULT_RECONNECT = 0.1
5
6
  HEADER_SIZE = 12
6
7
 
8
+ EMPTY_STR = ''.b.freeze
9
+
7
10
  def next_request_id
8
11
  @next_request_id = ((@next_request_id ||= 0) + 1) & 0x7fffffff
9
12
  end
@@ -17,5 +20,8 @@ module IProto
17
20
  data = ::BinUtils.append_int32_le!(nil, request_type, body.bytesize, request_id)
18
21
  ::BinUtils.append_string!(data, body)
19
22
  end
23
+
24
+ PING = 0xff00
25
+ PING_ID = 0xffffffff
20
26
  end
21
27
  end
@@ -2,3 +2,11 @@ require 'fiber'
2
2
  class Fiber
3
3
  alias call resume
4
4
  end
5
+
6
+ class String
7
+ unless method_defined?(:b)
8
+ def b
9
+ force_encoding(::Encoding::BINARY)
10
+ end
11
+ end
12
+ end
data/lib/iproto/em.rb CHANGED
@@ -10,14 +10,14 @@ module IProto
10
10
  end
11
11
 
12
12
  def receive_data(data)
13
- @buffer ||= ''
13
+ @buffer ||= ''.b
14
14
  offset = 0
15
- while (chunk = data[offset, _needed_size - @buffer.size]).size > 0
15
+ while (chunk = data.byteslice(offset, _needed_size - @buffer.bytesize)).size > 0
16
16
  @buffer << chunk
17
17
  offset += chunk.size
18
18
  if @buffer.size == _needed_size
19
19
  chunk = @buffer
20
- @buffer = ''
20
+ @buffer = ''.b
21
21
  receive_chunk chunk
22
22
  end
23
23
  end
@@ -26,6 +26,10 @@ module IProto
26
26
  def receive_chunk(chunk)
27
27
  # for override
28
28
  end
29
+
30
+ def buffer_reset
31
+ @buffer = ''.b
32
+ end
29
33
  end
30
34
 
31
35
  include IProto::ConnectionAPI
@@ -39,14 +43,36 @@ module IProto
39
43
  @reconnect_timeout = Numeric === reconnect ? reconnect : DEFAULT_RECONNECT
40
44
  @should_reconnect = !!reconnect
41
45
  @reconnect_timer = nil
46
+ @ping_timer = nil
42
47
  @connected = :init_waiting
43
48
  @waiting_requests = {}
44
49
  @waiting_for_connect = []
45
- init_protocol
46
50
  @shutdown_hook = false
51
+ @inactivity_timeout = 0
52
+ init_protocol
47
53
  shutdown_hook
48
54
  end
49
55
 
56
+ def _start_pinger
57
+ if @connected == true && (cit = comm_inactivity_timeout) != 0 && @ping_timer == nil
58
+ @ping_timer = EM.add_periodic_timer([1, cit / 4.0].min, method(:_ping))
59
+ end
60
+ end
61
+
62
+ def _stop_pinger
63
+ if @ping_timer
64
+ @ping_timer.cancel
65
+ @ping_timer = nil
66
+ end
67
+ end
68
+
69
+ def comm_inactivity_timeout=(t)
70
+ _stop_pinger
71
+ @inactivity_timeout = t
72
+ super
73
+ _start_pinger
74
+ end
75
+
50
76
  def connected?
51
77
  @connected == true
52
78
  end
@@ -72,6 +98,8 @@ module IProto
72
98
  def connection_completed
73
99
  @reconnect_timer = nil
74
100
  @connected = true
101
+ init_protocol
102
+ self.comm_inactivity_timeout= @inactivity_timeout
75
103
  _perform_waiting_for_connect(true)
76
104
  end
77
105
 
@@ -79,6 +107,11 @@ module IProto
79
107
  def init_protocol
80
108
  @_needed_size = HEADER_SIZE
81
109
  @_state = :receive_header
110
+ buffer_reset
111
+ end
112
+
113
+ def _ping
114
+ send_data pack_request(PING, PING_ID, EMPTY_STR)
82
115
  end
83
116
 
84
117
  def receive_chunk(chunk)
@@ -93,6 +126,11 @@ module IProto
93
126
  chunk = ''
94
127
  end
95
128
  end
129
+ if @request_id == PING_ID
130
+ @_needed_size = HEADER_SIZE
131
+ @_state = :receive_header
132
+ return
133
+ end
96
134
  request = @waiting_requests.delete @request_id
97
135
  raise IProto::UnexpectedResponse.new("For request id #{@request_id}") unless request
98
136
  @_needed_size = HEADER_SIZE
@@ -187,6 +225,7 @@ module IProto
187
225
  end
188
226
 
189
227
  def unbind
228
+ _stop_pinger
190
229
  prev_connected = @connected
191
230
  @connected = false
192
231
  discard_requests
data/lib/iproto.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module IProto
2
- VERSION = '0.3.14'
2
+ VERSION = '0.3.15'
3
3
  class IProtoError < StandardError; end
4
4
  class ConnectionError < IProtoError; end
5
5
  class CouldNotConnect < ConnectionError; end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: iproto
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.14
5
+ version: 0.3.15
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Rudenko
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-15 00:00:00.000000000 Z
12
+ date: 2014-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime