#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
Public connected As Boolean
Private client As Socket
Public server As ServerSocket
Private astream As AsyncStreams
Private const PORT As Int = 51042
'-------------------------------------------
Private logs As StringBuilder
Private logcat As LogCat
Private const emailAddress As String = "alfatau@infol.it"
End Sub
Sub Service_Create
server.Initialize(PORT, "server")
server.Listen
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
CloseExistingConnection
client = NewSocket
astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
UpdateState(True)
End If
server.Listen
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Public Sub ConnectToServer(Host As String)
Log("Trying to connect to: " & Host)
CloseExistingConnection
Dim client As Socket
client.Initialize("client")
client.Connect(Host, PORT, 10000)
End Sub
Public Sub Disconnect
CloseExistingConnection
End Sub
Sub Client_Connected (Successful As Boolean)
If Successful Then
astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
UpdateState (True)
'''ToastMessageShow("CONNESSIONE ATTIVA!",True)
Return True
Else
ToastMessageShow("ASSENZA DI CONNESSIONE!",True)
Return False
End If
End Sub
Sub CloseExistingConnection
If astream.IsInitialized Then astream.Close
If client.IsInitialized Then client.Close
UpdateState (False)
End Sub
Sub UpdateState (NewState As Boolean)
connected = NewState
CallSub(Main, "SetState")
End Sub
Sub AStream_Error
UpdateState(False)
End Sub
Sub AStream_Terminated
UpdateState(False)
End Sub
Sub AStream_NewData (Buffer() As Byte)
CallSub2(Main, "RX_Dati", Buffer)
End Sub
Public Sub SendData (data() As Byte)
If connected Then
astream.Write(data)
Else
ToastMessageShow("LA TRASMISSIONE DEL DATO NON E' AVVENUTA!",False)
End If
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error1 (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
'wait for 500ms to allow the logs to be updated.
Dim jo As JavaObject
Dim l As Long = 500
jo.InitializeStatic("java.lang.Thread").RunMethod("sleep", Array(l))
logcat.LogCatStop
logs.Append(StackTrace).Append(CRLF).Append(Error)
Dim email As Email
email.To.Add(emailAddress)
email.Subject = "Pippo - crashed"
email.Body = logs
StartActivity(email.GetIntent)
ToastMessageShow("APP CRASHED" & CRLF & " send email to developer",False)
'''Return True
End Sub