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?
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