RTC Forums
March 29, 2024, 12:44:08 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Mobile App  (Read 3837 times)
Chris Fieldhouse
RTC Expired
*
Posts: 10


« on: March 21, 2018, 03:51:13 PM »

I've been using Delphi's System.Net.HttpClient for an Android app that I'm writing.  I used this library as it has inbuilt support for ssl but I'm starting to see issues with the SSL handshake failing on some Android devices, so I've decided to swap to RTC and SecureBlackBox (boy I wish you would support Devart or similar which is a lot chaper).

Anyway, although I've used your server components a lot, I've not really used the client stuff and have a few questions

Can I have one TRtcHTTPClient in the app with multiple TRtcDataRequests?
What about if I am using TRtcDataRequest inside of a thread (so there may be multiple TRtcDataRequests all using TRtcHTTPClient at once)?
Does your components make use of System.Net.HttpClient or are they totally separate?
If I don't use threads, am I correct in thinking that TRtcDataRequest uses its own thread and then (if AutoSyncEvent is set) calls the data received events in the main thread, and if AutoSyncEvent is not set will call the events in the context of its own thread?


Many thanks

Chris
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: March 21, 2018, 04:43:28 PM »

1. Can I have one TRtcHTTPClient in the app with multiple TRtcDataRequests?

Yes, but keep in mind that one TRtcHttpClient component represents one TCP/IP connection, so that all requests posted to the same TRtcHttpClient component will be serialized and sent out one-at-a-time.

2. What about if I am using TRtcDataRequest inside of a thread (so there may be multiple TRtcDataRequests all using TRtcHTTPClient at once)?

If you need to use a single TRtcDataRequest component from multiple threads, set its "HyperThreading" property to TRUE.

3. Does your components make use of System.Net.HttpClient or are they totally separate?

RTC SDK is uses POSIX sockets on non-Windows platforms (not using System.Net.HttpClient).

4. If I don't use threads, am I correct in thinking that TRtcDataRequest uses its own thread and then (if AutoSyncEvent is set) calls the data received events in the main thread, and if AutoSyncEvent is not set will call the events in the context of its own thread?

TRtcHttpClient component has a MultiThreaded property, which you can set to TRUE if you want the component to use a banground thread for data processing and communication. Components like TRtcDataRequest and TRtcClientModule have the AutoSyncEvents property, which you can set to TRUE if you want all events triggered on that component to be synchronized with the Main Thread atomatically when the TRtcHttpClient component is running with MultiThreaded=TRUE, which then ensures that all the code you write on the component will be executed from the Main Thread, even though the rest of the code will be handled in a background thread.
Logged
Chris Fieldhouse
RTC Expired
*
Posts: 10


« Reply #2 on: March 22, 2018, 11:52:06 AM »

Hi Danijel

Thanks for the reply.

I've been playing and I think I've got most things sorted, but I wondered how the below scenario would work.

All my classes (that access HTTP data) run in their own threads and I will dedicate one TRtcHTTPClient to each thread (from a pool).  The TRtcHTTPClient will not be accessed by any other thread. I've set Multithreaded = true and AutoConnect = true.

Each thread will also have a dedicated TRtcDataRequest, this will not be accessed by any other threads.  I've set HyperThreading = false and AutoSyncEvents = false.

In my thread code, I call TRtcDataRequest.Post and then my thread waits on a TEvent to be signaled.

In the TRtcDataRequest.OnDataReceived event, I signal the TEvent when I get Response.Done.  Because the data being downloaded may be large I call ReadEx in the TRtcDataRequest.OnDataReceived event and store it in my thread.

This seems to work great so I moved on to think about how to handle lost connections.

I have connected the TRtcDataRequest.OnConnectionLost event, in it I have:

  
Code:
 FHasErrorCode := -1;
  FErrorMessage := 'Connection lost';
  Sender.Disconnect;
  FWaitEvent.SetEvent;
  


My thread was waiting on the event so now continues, sees the FHasErrorCode then exits.  

However, I think this will interfere with the TRtcDataRequest.AutoRepost setting.

I'd like TRtcDataRequest to try a seccond time if the connection fails, so should I, instead of signalling my TEvent in the OnConnectionLost event, do something like the following

Code:

  Inc(FConnectionLostCount);    //Keep track of how many tries we've done
  FStream.Size := 0;                  //Delete any data already downloaded ready for a retry
  if FConnectionLostCount = 2 then
  begin
    Sender.Disconnect;
    FHasErrorCode := 1;
   FWaitEvent.SetEvent;
  end;



I hope the above makes sense?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: March 22, 2018, 12:03:41 PM »

TRtcDataRequest component has the "AutoRepost" property, which you can use to define how many times you want the component to post each request in case the first attempt fails, before the "OnResponseAbort" event is triggered (or "OnResponseReject" - if you manually cancel the request).

If you set the "AutoConnect" and all "ReconnectOn" properties on the TRtcHttpClient component to TRUE, the component will try to re-establish a connection when necessary. And by using the "AutoRepost" property on the TRtcDataRequest component and implementing the "OnBeginRequest", "OnDataReceived", "OnDataSent", "OnResponseAbot" and "OnResponseReject" events, you can control the flow of communication without using semaphores and/or blocking a thread - be it the main thread or any one of your own threads.

PS. If you do create your own threads, avoid using the "WaitForCompletion" and "WaitForCompletionEx" methods (available on TRtcHttpClient and TRtcDataRequest componets), because they were designed primarily for use from the Main Thread.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.025 seconds with 17 queries.