Skip to content

Commit 89ec778

Browse files
committed
WIP: ClientProtocolReceiver
1 parent 6347b97 commit 89ec778

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

lib/net/imap.rb

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,8 +3484,8 @@ def tcp_socket(host, port)
34843484
end
34853485

34863486
def receive_responses
3487-
connection_closed = false
3488-
until connection_closed
3487+
@connection_closed = false
3488+
until @connection_closed
34893489
synchronize do
34903490
@exception = nil
34913491
end
@@ -3505,44 +3505,7 @@ def receive_responses
35053505
end
35063506
break
35073507
end
3508-
begin
3509-
synchronize do
3510-
case resp
3511-
when TaggedResponse
3512-
@tagged_responses[resp.tag] = resp
3513-
@tagged_response_arrival.broadcast
3514-
case resp.tag
3515-
when @logout_command_tag
3516-
state_logout!
3517-
return
3518-
when @continued_command_tag
3519-
@continuation_request_exception =
3520-
RESPONSE_ERRORS[resp.name].new(resp)
3521-
@continuation_request_arrival.signal
3522-
end
3523-
when UntaggedResponse
3524-
record_untagged_response(resp)
3525-
if resp.name == "BYE" && @logout_command_tag.nil?
3526-
state_logout!
3527-
@sock.close
3528-
@exception = ByeResponseError.new(resp)
3529-
connection_closed = true
3530-
end
3531-
when ContinuationRequest
3532-
@continuation_request_arrival.signal
3533-
end
3534-
state_unselected! if resp in {data: {code: {name: "CLOSED"}}}
3535-
@response_handlers.each do |handler|
3536-
handler.call(resp)
3537-
end
3538-
end
3539-
rescue Exception => e
3540-
@exception = e
3541-
synchronize do
3542-
@tagged_response_arrival.broadcast
3543-
@continuation_request_arrival.broadcast
3544-
end
3545-
end
3508+
handle_response(resp)
35463509
end
35473510
synchronize do
35483511
@receiver_thread_terminating = true
@@ -3594,6 +3557,61 @@ def get_response
35943557
#############################
35953558
# built-in response handlers
35963559

3560+
def handle_response(resp)
3561+
synchronize do
3562+
case resp
3563+
when TaggedResponse
3564+
handle_tagged_response(resp)
3565+
when UntaggedResponse
3566+
handle_untagged_response(resp)
3567+
when ContinuationRequest
3568+
@continuation_request_arrival.signal
3569+
end
3570+
run_response_handlers(resp)
3571+
end
3572+
rescue Exception => e
3573+
@exception = e
3574+
synchronize do
3575+
@tagged_response_arrival.broadcast
3576+
@continuation_request_arrival.broadcast
3577+
end
3578+
end
3579+
3580+
def handle_tagged_response(resp)
3581+
synchronize do
3582+
@tagged_responses[resp.tag] = resp
3583+
@tagged_response_arrival.broadcast
3584+
case resp.tag
3585+
when @logout_command_tag
3586+
state_logout!
3587+
return
3588+
when @continued_command_tag
3589+
@continuation_request_exception =
3590+
RESPONSE_ERRORS[resp.name].new(resp)
3591+
@continuation_request_arrival.signal
3592+
end
3593+
end
3594+
end
3595+
3596+
def handle_untagged_response(resp)
3597+
synchronize do
3598+
record_untagged_response(resp)
3599+
if resp.name == "BYE" && @logout_command_tag.nil?
3600+
state_logout!
3601+
@sock.close
3602+
@exception = ByeResponseError.new(resp)
3603+
@connection_closed = true
3604+
end
3605+
state_unselected! if resp in {data: {code: {name: "CLOSED"}}}
3606+
end
3607+
end
3608+
3609+
def run_response_handlers(resp)
3610+
@response_handlers.each do |handler|
3611+
handler.call(resp)
3612+
end
3613+
end
3614+
35973615
# store name => [..., data]
35983616
def record_untagged_response(resp)
35993617
@responses[resp.name] << resp.data
@@ -3635,6 +3653,7 @@ def send_command(cmd, *args, &block)
36353653
validate_data(i)
36363654
end
36373655
tag = generate_tag
3656+
36383657
put_string(tag + " " + cmd)
36393658
args.each do |i|
36403659
put_string(" ")

0 commit comments

Comments
 (0)