Use timer to deal with missing response from socket server

knutf

Active Member
Licensed User
Longtime User
Hi all

I plan to make an socket client app with a foreground service that connects to a Socket server. The service should be able to send strings to request values from the server. I plan to use message strings that ends with CRLF, so I'll use the AsyncSteamsText module

The app must have a mechanism that take action if the server does not return the values. This could be a Timer object that is enabled when the request is sent to the server.

In the NewText event of the AsyncSteamsText-object I stop the timer like this: TimerTimeoutCom.Enabled = False

My questions are
-Is it possible that the timer fires the tick event while the AsyncSteamsText-object is handling input from the socket stream, or is the tick-event queued until the inputstream-code is finished?

-Will setting the timer property to false stop a queued tick event? Or do I have to set a boolean variabel to true in the NewText event and check this variabel in the tick-event?

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private Socket1 As Socket
   Private TimerTimeoutCom As Timer
   Private RemainingTries As Short
   Private Stream As AsyncSteamsText
   Dim Notification1 As Notification
End Sub

Sub Service_Create
    Notification1.Initialize
    Notification1.Sound = False
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = False
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Destroy
End Sub

Sub ConnecSoccet(lHost As String, lPort As Int)
   Socket1.Initialize("Socket1_Connected")
   Socket1.Connect(lHost,lPort,10000)
End Sub

Sub Socket1_Connected (Successful As Boolean)
   If Successful Then
       Notification1.SetInfo("MyApp","Connected",  Main)
       Service.StartForeground(1, Notification1) 
      If Stream.IsInitialized Then Stream.Close
      Stream.Initialize(Me,"Stream",Socket1.InputStream,Socket1.OutputStream )
   Else
      Service.StopForeground(1)
   End If
End Sub

Sub RequestMessageFromServer
   Stream.Write("  A string that ask server to send a message   ")
   TimerTimeoutCom.Initialize("MessageFromServerDidNotArrive",20000) '20seconds
   TimerTimeoutCom.Enabled = True
End Sub

Sub Stream_NewText(Input As String)
   TimerTimeoutCom.Enabled = False
   
   'Code to handel the answer from server
   
End Sub

Sub MessageFromServerDidNotArrive
   TimerTimeoutCom.Enabled = False
   If RemainingTries > 0 Then
      RemainingTries = RemainingTries - 1
      RequestMessageFromServer
   Else
      ToastMessageShow("Could not get a message from server",True)
   End If
End Sub

Sub Stream_Terminated
   TimerTimeoutCom.Enabled = False
    Service.StopForeground(1)
   Socket1.Close
End Sub
 

mc73

Well-Known Member
Licensed User
Longtime User
If I understand correctly, you can avoid timers, by simply setting the timeOut parameter upon connection, then handling the success flag of the connection, and if something goes really wrong afterwards, you'll get an aStreams_error.

By the way, this line
B4X:
 Socket1.Initialize("Socket1_Connected")
should be
B4X:
 Socket1.Initialize("Socket1")
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
The aStreams_error is raised when something goes wrong in the connection between the socket server and client.

But I did not tell that the socket server is a box that links a RS-232 device to an TCP-IP network. So actualy it is the RS-232 device that recieve requests and return values through the soccket server. If somethings go wrong between the RS-232 device and the soccet server box I don't think the astream_error will be raised.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
-Is it possible that the timer fires the tick event while the AsyncSteamsText-object is handling input from the socket stream, or is the tick-event queued until the inputstream-code is finished?

-Will setting the timer property to false stop a queued tick event? Or do I have to set a boolean variabel to true in the NewText event and check this variabel in the tick-event?
1. No. The tick event will only fire after your current code finishes executing.

2. Yes (for the first question). There will be no Tick events after the the timer is disabled.
 
Upvote 0
Top