Sign in to follow this  
Followers 0
Luigi

pseudo-code: error tolerant in client-server communication

1 post in this topic

Hi, some expert in RPC (remote procedure call) or client-server communication can help me?
 
I'm writing a script that sends some information to a server in PHP.
Sometimes the content being sent is a bit extensive.
 
I want him to be tolerant of errors.
The probable errors I can see are these:
  1. __ServerReady (): checks if the server is running.
  • Local connection problems 
  • address unreachable host 
  • there is no internet connection 
  1. __SendData (): sends the set of information, but only understands return header 200 with valid response, any response diferente from 200 is considered error 
  2. __ErrorHandling: analyzes the data set received __SendData and verifies that the answer has the required items to be considered valid. 
  • for example, the server is running, the package was sent, but the PHP server has no connection with the database, so it was impossible to server process the request from the client, it is necessary to repeat the whole process 
 
If the script came to __HerrorHandling without error, then the function delivers the processed information. 
 
First did the design, and then wrote the text, and now I see that maybe not the __SendData functions () necessary and __ErrorHangling () pass for a Sleep ()
 
What help do I need:
  • I saw the main mistakes? 
  • Is there anything more to add?

post-53968-0-70926000-1414168459_thumb.p

 

Func __ServerReady()
    Local $bTry = True
    Local $iSocket = TCPConnect($iSERVER_IP, $iSERVER_PORT)
    If @error Or ($iSocket == 0) Or ($iSocket == -1) Then $bTry = False
    TCPCloseSocket($iSocket)
    Return $bTry
EndFunc   ;==>__ServerReady
Func __SendData($sPage = "", $data = "")
    Local $oStatusCode, $oReceived
    Local $sAddress = $SERVER_URL & "/" & $sPage
    Local $oHttp = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If @error Then Return SetError(1, 0, 0)
    $oHttp.Open("POST", $sAddress, False)
    $oHttp.setRequestHeader("Connection", "Keep-Alive")
    $oHttp.setRequestHeader("Referer", $sAddress)
    $oHttp.Send(StringToBinary($data, 4))
    $oHttp.WaitForResponse()
    Local $oAllHeaders = $oHttp.GetAllResponseHeaders()
    $oReceived = $oHttp.ResponseText
    $oStatusCode = $oHttp.Status
    $oHttp = 0
    If $oStatusCode == 200 Then
        Return $oReceived
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>Send_Http

__ErrorHandling()

Func __ErrorHandling($input)
    ; try decode $input in $oReply
    Local $oReply = Jsmn_Decode($input) ; https://github.com/ez2sugul/stopwatch_client/blob/master/JSMN.au3
    If @error Then Return SetError(1, 0, 0)
    ; if $oReply was decoded, then now it is a "Scripting.Dictionary"
    If Not (ObjName($oReply, 2) == "Scripting.Dictionary") Then SetError(2, 0, 0)
    ; if exist a key "error", there was an error
    If $oReply.Exists("error") Then
        Return SetError(1, 0, $oReply.Item("error")
    ElseIf $oReply.Exists("return") Then
        ; if not exist key "error", and exist key "return", there is no error
        Return SetError(1, 0, $oReply.Item("return")
    Else
        Return SetError(1, 0, "unknow error")
    EndIf
EndFunc   ;==>__ErrorHandling

 

 


m(o.O)m

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0