libUrl tsNet and socketTimeoutInterval

Sannyasin Brahmanathaswami brahma at hindu.org
Fri Dec 8 22:07:48 EST 2017


Ralph -- I sent this to you off list by mistake

copying here FWIW if anyone is lurking. This all looks great and covers "download files" use cases

The remaining puzzles are one off url calls from/for mobile controls :

1)  remote path to audio.mp3; for player
2) url web calls in browser widget to
     a) web pages
     b) youtube (video streaming)

and how these play with TSNet, bandwidth dropping below set limits, or dropped connections.

As yet mysterious….




" <rdimola at evergreeninfo.net> wrote:

    BR,
    
    I async also works in iOS just fine and dandy. In My app I am getting the background images for cards while the user first logs in. Async allows the images to be downloaded in the background. This allows me to change images without releasing a new app without hanging the app up when I used blocking libURL. The implementation was so easy it was frightening. I also have 6 of these queued up and running at the same time!
    
    First is a utility(NetworkType) to check the type of networking and a tsNet initialization handler (EISinitTSnet):
    
    --------------------------------------------------------
    function GetNetworkType
       local tLibUrlDriver
       
       try
          put the behavior of stack"revLibUrl"into tLibUrlDriver
       end try
       if tLibUrlDriver is empty then
          return "Sockets"
       else
          return "tsNet"
       end if
       
    end GetNetworkType
    
    command NetworkType
       if GetNetworkType() = "Sockets" then
          answer"tsNet is disabled"
       else
          answer "tsNet in use"&cr&"Version==>"& tsNetVersion()
       end if
    end NetworkType
    
    command EISinitTSnet
       local tResult
       
       try
          tsNetInit
          put the result into tResult
          if tResult is not empty then
             if tResult = "tsneterr: Already initialised" then
             else
                answer tResult
             end if
          else
             tsNetSetTimeouts 60, 5000, 5000, 5000, 5, 10000
          end if
       catch someerror
          if IsInternalUser(true) then answer TranslateError(someerror)
       end try
    end EISinitTSnet
    
    ----------------------------------------------------------------------------------------------------
    
    The Get_Path function below is a function returns paths that are transparent between IDE and mobile. (Richard G. I got sucked into the returning the trailing slash before I realized it was not the best way to do it). I hacked out this code so there could be an error but the basic idea is here.
    
    I call this first handler(StartBackgroundImageDownload) 12 time quickly and all the downloads work. The app is fully functional while the downloads are in progress. I use the filename as the pID. This makes things easy when the download completes. I have not tested timeouts yet. That's the next step, handling the in/out of network access/slow network speed. Standby....
    This is the guts of the asysnc file downloading:
    ---------------------------------------------------------------------------------------------------
    command StartBackgroundImageDownload pImageName
       local pHeaders, tStatus
       
       put tsNetGetFile(pImageName, Get_Path("Updates")&pImageName,  \
             "https://xxx.com/someFolder/"& \
             urlencode(pImageName), pHeaders, "DownLoadBackgroundImageComplete") into  tStatus-- start download
       if tStatus is not empty then
             answer tStatus
             end if
       
    end StartBackgroundImageDownload
    
    on DownLoadBackgroundImageComplete pID, pResult, pBytes, pCurlCode
       
       local tData, tHeaders
       
       --answer "File==>"&pID&cr&"Bytes==>"&pBytes&cr&"Result==>"&pResult&cr&"CurlCode==>"&pCurlCode
       
       
       if pCurlCode is not 0 then
          
             answer tsNetRetrError(pID)
       else
          if pResult = 404 then
             answer "File'"&&pID&&"not found==>"&pBytes&&"Result==>"&pResult
             delete file Get_Path("Updates")&pID -- delete file with 404 error in it
          else
             --answer "File==>"&pID&cr&"Bytes==>"&pBytes&cr&"Result==>"&pResult&cr&"CurlCode==>"&pCurlCode
             if pBytes > 2000 then -- GOOD DOWNLOAD
                rename file (Get_Path("Updates")&pID) to (Get_Path("BackgroundImages")&pID) -- file download complete
                
                   --answer "File'"&&pID&&"has been downloaded. Result==>"&pResult
               
             else
                   answer "File'"&&pID&&"has been downloaded but is too small==>"&pBytes&&"Result==>"&pResult -- this was before I had the 404 check
                   delete file Get_Path("Updates")&pID
             end if
          end if
       end if
       
       tsNetCloseConn pID
       
       
    end DownLoadBackgroundImageComplete
    
    ---------------------------------------------------------------------
    
    Ralph DiMola
    IT Director
    Evergreen Information Services
    rdimola at evergreeninfo.net
    
    
    -----Original Message-----
    From: use-livecode [mailto:use-livecode-bounces at lists.runrev.com] On Behalf Of Sannyasin Brahmanathaswami via use-livecode
    Sent: Friday, December 08, 2017 2:21 PM
    To: How to use LiveCode
    Cc: Sannyasin Brahmanathaswami
    Subject: Re: libUrl tsNet and socketTimeoutInterval
    
    Awesome Ralph:
    
    Looks like we need to crowd fund Charles for documentation (smile)
    
    You are about 3 days ahead of me… can you post some code snippets  of your methods?
    
     
    
    On 12/7/17, 1:54 PM, "use-livecode on behalf of Ralph DiMola via use-livecode" <use-livecode-bounces at lists.runrev.com on behalf of use-livecode at lists.runrev.com> wrote:
    
        Ok, I just got async tsNet working in IDE and Android(Testing iOS later). I works great! Hat tip to all the contributors. One question and one Observation:
        1) How do you query the current tsNet timeout settings? I have quite a few places where I save the socketTimeoutInterval, set it to a new value, do something and then set it back to the saved timeout. I don't see a way to query this in the dictionary.
        2) I seems the you don't need to do a "tsNetInit" in the IDE. In the IDE one gets "tsneterr: Already initialized". On Android this error does not occur.
        
        Ralph DiMola
        IT Director
        Evergreen Information Services
        rdimola at evergreeninfo.net
        
        
    
    _______________________________________________
    use-livecode mailing list
    use-livecode at lists.runrev.com
    Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
    http://lists.runrev.com/mailman/listinfo/use-livecode
    
    



More information about the Use-livecode mailing list