-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.rb
284 lines (253 loc) · 7.1 KB
/
connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require 'monitor'
class ConnectionManager
attr_accessor :on_connect, :on_close, :on_unhandled_data, :socket
def initialize(socket, accept: true)
@socket = socket
@connections = {}
@accept_queue = Thread::Queue.new
@accept_new_connection = accept
Thread.new { run_recv }
Thread.new { run_tick }
end
def accept_new_connection? = @accept_new_connection
def run_recv
loop do
data, addr = @socket.recvfrom 65536
unless data[0, 3] == Connection::SIGNATURE
on_unhandled_data&.call data, addr
next
end
ip = addr[3]
port = addr[1]
key = [ip, port]
type_id = data[3].ord
msg = data[4..]
existing_conn = @connections[key]
if existing_conn&.marked_for_accept
existing_conn.marked_for_accept = false
@accept_queue << existing_conn
end
case Connection::TYPES[type_id]
when :req
find_or_accept_connection(ip, port)&.send_ack
when :ack
rid, ridx, sid, scnt = msg.unpack 'NNNN'
existing_conn&.handle_ack rid, ridx, sid, scnt
when :data
connection_id, idx = msg.unpack 'NN'
existing_conn&.handle_data connection_id, idx, msg[8..]
when :resend
connection_id, *idxs = msg.unpack 'N*'
existing_conn&.handle_resend connection_id, idxs
when :close
existing_conn&.handle_close
@connections.delete key
end
end
end
def find_or_accept_connection(ip, port)
key = [ip, port]
conn = @connections[key]
if conn.nil? && accept_new_connection?
conn = @connections[key] = Connection.new self, ip, port
@accept_queue << conn
end
conn
end
attr_accessor :emulate_packet_loss, :emulate_packet_delay
def send_raw(data, ip, port)
if emulate_packet_loss && rand < emulate_packet_loss
p :loss
return
end
if emulate_packet_delay
Thread.new do
sleep rand(emulate_packet_delay)
@socket.send data, 0, ip, port
end
else
@socket.send data, 0, ip, port
end
end
def accept
@accept_queue.deq
end
def connect(ip, port, mark_for_accept: false)
conn = @connections[[ip, port]]
unless conn
conn = @connections[[ip, port]] = Connection.new self, ip, port
conn.marked_for_accept = mark_for_accept
conn.send_req
end
conn
end
def run_tick
loop do
@connections.each_value { _1.tick }
sleep 0.1
end
end
def remove(ip, port)
@connections.delete [ip, port]
end
end
class Connection
TYPES = %i[req ack data resend close]
SIGNATURE = [0x35, 0x26, 0x41].pack 'C*'
attr_accessor :marked_for_accept
attr_reader :ip, :port
def initialize(manager, ip, port)
@manager = manager
@send_connection_id = rand 0xffffffff
@ip = ip
@port = port
@event_queue = Thread::Queue.new
@status = :connecting
@initialized_at = @last_recv_ack = Time.now
@last_send_ack = nil
@last_send_ack_idx = 0
@terminated_recv_connection_id = nil
@monitor = Monitor.new
@cond = @monitor.new_cond
reset_send_connection
reset_recv_connection 0
end
def reset_send_connection
@send_buffer = []
@send_idx = 0
end
def reset_recv_connection(recv_id)
@terminated_recv_connection_id = @recv_connection_id
@recv_connection_id = recv_id
@recv_idx = 0
@recv_buffer = []
@unreceiveds = {}
end
def close
return if closed?
handle_close
socket_send :close
end
def handle_close
return if closed?
@status = :closed
@manager.on_close&.call self
trigger :close
@manager.remove @ip, @port
end
def trigger(type, message = nil)
@event_queue << [type, message]
end
def read
@event_queue.deq
end
MAX_BODY_SIZE = 1024
MAX_PACKET_BUFFER = 128
def send_data(message)
@monitor.synchronize do
@cond.wait_while { @send_buffer.size >= MAX_PACKET_BUFFER }
end
message.chars.each_slice(MAX_BODY_SIZE).map(&:join).map do |msg|
idx = @send_idx + @send_buffer.size
@send_buffer << msg
socket_send :data, @send_connection_id, idx, msg
end
end
def closed? = @status == :closed
def connecting? = @status == :connecting
def connected? = @status == :connected
def send_close
return if closed?
socket_send :close
end
def send_req
@last_send_req = Time.now
socket_send :req
end
def tick
current = Time.now
if connecting?
if current - @initialized_at > 30
handle_close
elsif @last_send_req.nil? || current - @last_send_req > 1
send_req
end
elsif connected?
return handle_close if current - @last_recv_ack > 30
send_ack if @last_send_ack.nil? || current - @last_send_ack > 5
request_resend
end
end
def send_ack
@last_send_ack = Time.now
@last_send_ack_idx = @recv_idx
socket_send :ack, @recv_connection_id, @recv_idx, @send_connection_id, @send_idx + @send_buffer.size
end
def handle_ack(send_id, reached_idx, recv_id, remote_sent_count)
if connecting?
trigger :open
@status = :connected
@manager.on_connect&.call self
end
return if send_id != @send_connection_id && reached_idx != 0
return if recv_id == @terminated_recv_connection_id
reset_recv_connection recv_id if recv_id != @recv_connection_id
if reached_idx >= @send_idx
@send_buffer.shift reached_idx - @send_idx
@send_idx = reached_idx
elsif reached_idx == 0
reset_send_connection
trigger :reset
end
i = remote_sent_count - 1 - @recv_idx
@recv_buffer[i] ||= nil if i >= 0
@last_recv_ack = Time.now
request_resend
@monitor.synchronize do
@cond.signal if @send_buffer.size < MAX_PACKET_BUFFER
end
end
def handle_data(recv_id, idx, data)
reset_recv_connection recv_id if recv_id != @recv_connection_id && recv_id != @terminated_recv_connection_id
@recv_buffer[idx - @recv_idx] = data if idx >= @recv_idx
@unreceiveds.delete idx
while @recv_buffer.first
@recv_idx += 1
trigger :data, @recv_buffer.shift
end
send_ack if @recv_idx > @last_send_ack_idx + 16
request_resend
end
def handle_resend(send_id, idxs)
return if send_id != @send_connection_id
idxs.each do |idx|
next if idx < @send_idx
msg = @send_buffer[idx - @send_idx]
socket_send :data, @send_connection_id, idx, msg
end
end
def request_resend
current = Time.now
@recv_buffer.each.with_index @recv_idx do |msg, id|
next if msg
@unreceiveds[id] ||= current
end
timeout = 0.2
max_resend_req = (MAX_BODY_SIZE - 5) / 4
missing_ids = []
@unreceiveds.each_key do |id|
if current - @unreceiveds[id] > timeout
missing_ids << id
break if missing_ids.size >= max_resend_req
end
end
return if missing_ids.empty?
missing_ids.each { @unreceiveds[_1] = current + timeout }
socket_send :resend, @recv_connection_id, missing_ids
end
def socket_send(type, *data)
sdata = data.flatten.map { _1.is_a?(Numeric) ? [_1].pack('N') : _1 }.join
type_id = TYPES.index type
@manager.send_raw SIGNATURE + type_id.chr + sdata, @ip, @port
end
end