1
\$\begingroup\$

I have a review request about this part of the code:

let private startReading (client:TcpClient) (bus:IBus) = 
 //TODO check if that can be changed to rec parameter
 let frameNumber = ref 0
 let rec StartReading (client:TcpClient) =
 async{
 match client with
 | null -> async { return 0 } |> Async.StartAsTask |> ignore
 | _ ->
 match (client.IsConnectionEstablished()) with
 | true ->
 incr frameNumber
 DeployService.DeployCabninetService
 let! messange = MessagingHandler.ReadMessage client
 match(messange) with
 | Some data -> 
 let frameNumber = uint32 frameNumber.Value
 logger.Info(sprintf "Received message with Frame Number: %i" frameNumber) 
 | None -> 
 _logger.Info(sprintf "Client disconnected : %O" client.Client.RemoteEndPoint)
 return! DisconnectClient client None 
 return! StartReading client
 | false ->
 _logger.Info(sprintf "Client disconnected : %O" client.Client.RemoteEndPoint)
 return! DisconnectClient client None
 }
 StartReading client |> Async.StartAsTask

It is simple reading message procedure from tcp client in f#, but I feel this can be written more gracefully, especially todo part with frame number incrementation using ref in f# feels wrong.

Any suggestions?

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Jul 2, 2019 at 8:00
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Why do you make this call Async.StartAsTask instead of just return the Async<T> to let the client handle that as needed?


let rec StartReading (client:TcpClient) =

I would call it reader and then give it the signature of:

let rec reader frameNo (client:TcpClient) =

in order to get rid of reffor frameNumber.

You can then initially call it:

reader 0 client |> Async.StartAsTask

and recursively:

return! reader (frameNo + 1) client
answered Jul 2, 2019 at 9:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.