@@ -1291,36 +1291,49 @@ handle_msg(#ssh_msg_global_request{name = _Type,
12911291 end ;
12921292
12931293handle_msg (# ssh_msg_request_failure {},
1294- # connection {requests = [{_ , From } | Rest ]} = Connection , _ , _SSH ) ->
1295- {[{channel_request_reply , From , {failure , <<>>}}],
1296- Connection # connection {requests = Rest }};
1297-
1298- handle_msg (# ssh_msg_request_failure {},
1299- # connection {requests = [{_ , From ,_ } | Rest ]} = Connection , _ , _SSH ) ->
1300- {[{channel_request_reply , From , {failure , <<>>}}],
1301- Connection # connection {requests = Rest }};
1294+ # connection {requests = Requests } = Connection , _ , _SSH ) ->
1295+ handle_global_response ({failure , <<>>}, Requests , Connection );
13021296
13031297handle_msg (# ssh_msg_request_success {data = Data },
1304- # connection {requests = [{_ , From } | Rest ]} = Connection , _ , _SSH ) ->
1305- {[{channel_request_reply , From , {success , Data }}],
1306- Connection # connection {requests = Rest }};
1298+ # connection {requests = Requests } = Connection , _ , _SSH ) ->
1299+ handle_global_response ({success , Data }, Requests , Connection ).
13071300
1308- handle_msg (# ssh_msg_request_success {data = Data },
1309- # connection {requests = [{_ , From , Fun } | Rest ]} = Connection0 , _ , _SSH ) ->
1310- Connection = Fun ({success ,Data }, Connection0 ),
1311- {[{channel_request_reply , From , {success , Data }}],
1312- Connection # connection {requests = Rest }};
1313-
1314- % % alive responses
1315- handle_msg (# ssh_msg_request_success {},
1316- # connection {requests = []} = Connection , _ , _SSH ) ->
1317- {[], Connection };
13181301
1319- handle_msg (# ssh_msg_request_failure {},
1320- # connection {requests = []} = Connection , _ , _SSH ) ->
1321- {[], Connection }.
1302+
1303+ % %%----------------------------------------------------------------
1304+ % %% Handle a global request response (success or failure).
1305+ % %% Finds the first global request entry and delivers the reply.
1306+ % %%
1307+ handle_global_response (Reply , Requests , Connection0 ) ->
1308+ case take_global_request (Requests ) of
1309+ {{_ , From }, Rest } ->
1310+ {[{channel_request_reply , From , Reply }],
1311+ Connection0 # connection {requests = Rest }};
1312+ {{_ , From , Fun }, Rest } ->
1313+ Connection = Fun (Reply , Connection0 ),
1314+ {[{channel_request_reply , From , Reply }],
1315+ Connection # connection {requests = Rest }};
1316+ false ->
1317+ {[], Connection0 }
1318+ end .
13221319
13231320
1321+ % %%----------------------------------------------------------------
1322+ % %% Find and remove the first global request entry (keyed by reference)
1323+ % %% from the requests list, skipping channel entries (keyed by integer).
1324+ % %% Returns {Entry, RemainingList} or false.
1325+ % %%
1326+ take_global_request ([]) ->
1327+ false ;
1328+ take_global_request ([{Ref , _ } = E | Rest ]) when is_reference (Ref ) ->
1329+ {E , Rest };
1330+ take_global_request ([{Ref , _ , _ } = E | Rest ]) when is_reference (Ref ) ->
1331+ {E , Rest };
1332+ take_global_request ([H | T ]) ->
1333+ case take_global_request (T ) of
1334+ {E , Rest } -> {E , [H | Rest ]};
1335+ false -> false
1336+ end .
13241337
13251338% %%----------------------------------------------------------------
13261339% %% Returns pending responses to be delivered to the peer when a
0 commit comments