@@ -21,7 +21,7 @@ defmodule Thrift.Binary.Framed.Client do
2121 alias Thrift.TApplicationException
2222 alias Thrift.Transport.SSL
2323
24- @ immutable_tcp_opts [ active: false , packet: 4 , mode: :binary ]
24+ @ immutable_tcp_opts [ active: true , packet: 4 , mode: :binary ]
2525
2626 @ type error :: { :error , atom } | { :error , { :exception , struct } }
2727 @ type success :: { :ok , binary }
@@ -168,10 +168,13 @@ defmodule Thrift.Binary.Framed.Client do
168168 end
169169
170170 @ impl Connection
171- def disconnect ( info , % { sock: { transport , sock } } ) do
171+ def disconnect ( info , % { sock: { transport , sock } } = s ) do
172172 :ok = transport . close ( sock )
173173
174174 case info do
175+ { :reconnect , _ } = reconnect ->
176+ { :connect , info , % { s | sock: nil } }
177+
175178 { :close , from } ->
176179 Connection . reply ( from , :ok )
177180 { :stop , :normal , nil }
@@ -268,13 +271,13 @@ defmodule Thrift.Binary.Framed.Client do
268271 timeout = Keyword . get ( tcp_opts , :timeout , default_timeout )
269272
270273 with :ok <- transport . send ( sock , [ message | serialized_args ] ) ,
271- { :ok , message } <- transport . recv ( sock , 0 , timeout ) do
274+ { :ok , message } <- receive_message ( sock , timeout ) do
272275 reply = deserialize_message_reply ( message , rpc_name , seq_id )
273276 { :reply , reply , s }
274277 else
275278 { :error , :closed } = error ->
276279 if reconnect do
277- { :connect , { :reconnect , :call , msg , from } , % { s | sock: nil } }
280+ { :disconnect , { :reconnect , { :call , msg , from } } , s }
278281 else
279282 { :disconnect , error , error , s }
280283 end
@@ -310,7 +313,7 @@ defmodule Thrift.Binary.Framed.Client do
310313
311314 { :error , :closed } = error ->
312315 if reconnect do
313- { :connect , { :reconnect , :cast , msg } , % { s | sock: nil } }
316+ { :disconnect , { :reconnect , { :cast , msg } } , s }
314317 else
315318 { :disconnect , error , s }
316319 end
@@ -320,10 +323,32 @@ defmodule Thrift.Binary.Framed.Client do
320323 end
321324 end
322325
326+ @ impl Connection
327+ def handle_info ( { :tcp_closed , sock } , % { reconnect: true , sock: { _transport , sock } } = s ) do
328+ { :disconnect , { :reconnect , nil } , s }
329+ end
330+
331+ def handle_info ( _ , s ) do
332+ { :noreply , s }
333+ end
334+
323335 def deserialize_message_reply ( message , rpc_name , seq_id ) do
324336 handle_message ( Binary . deserialize ( :message_begin , message ) , seq_id , rpc_name )
325337 end
326338
339+ defp receive_message ( sock , timeout ) do
340+ receive do
341+ { :tcp , ^ sock , data } -> { :ok , data }
342+ { :tcp_closed , ^ sock } -> { :error , :closed }
343+ { :tcp_error , ^ sock , error } -> { :error , error }
344+ { :ssl , ^ sock , data } -> { :ok , data }
345+ { :ssl_closed , ^ sock } -> { :error , :closed }
346+ { :ssl_error , ^ sock , error } -> { :error , error }
347+ after
348+ timeout -> { :error , :timeout }
349+ end
350+ end
351+
327352 defp handle_message ( { :ok , { :reply , seq_id , rpc_name , serialized_response } } , seq_id , rpc_name ) do
328353 { :ok , serialized_response }
329354 end
@@ -402,7 +427,7 @@ defmodule Thrift.Binary.Framed.Client do
402427 end
403428 end
404429
405- defp maybe_resend_data ( { :ok , s } , { :reconnect , :call , msg , from } ) do
430+ defp maybe_resend_data ( { :ok , s } , { :reconnect , { :call , msg , from } } ) do
406431 case handle_call ( msg , from , s ) do
407432 { :reply , reply , s } ->
408433 GenServer . reply ( from , reply )
@@ -417,7 +442,7 @@ defmodule Thrift.Binary.Framed.Client do
417442 end
418443 end
419444
420- defp maybe_resend_data ( { :ok , s } , { :reconnect , :cast , msg } ) do
445+ defp maybe_resend_data ( { :ok , s } , { :reconnect , { :cast , msg } } ) do
421446 case handle_cast ( msg , s ) do
422447 { :noreply , s } ->
423448 { :ok , s }
0 commit comments