It is currently Mon Apr 06, 2020 9:56 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Crash with SendNetworkData
PostPosted: Sun Oct 27, 2019 11:29 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2343
Location: Germany
The network functions are as described ThreadSafe.

If you outsource the sending of data to another thread and the connection to the partner fails, SendNetworkData crashed.
With Windows and Linux, SendNetworkData returns a result of less than zero and can be aborted.

Server
Code:
;-TOP

CompilerIf #PB_Compiler_Thread = 0
  CompilerError "Use Compiler Option ThreadSafe!"
CompilerEndIf

InitNetwork()

Enumeration Windows
  #Main
EndEnumeration

Global ExitApplication
Global MutexServer = CreateMutex()

Procedure thServer(id)
  Protected ServerID, Event, ConnectionID, cnt
  Protected *buffer = AllocateMemory($FFFF)
 
  ServerID = CreateNetworkServer(#PB_Any, 6037)
  If ServerID = 0
    Debug "Error: Create NetworkServer!"
    ProcedureReturn 0
  Else
    Debug "Create NetworkServer"
  EndIf
 
  Repeat
    LockMutex(MutexServer)
    Event = NetworkServerEvent(ServerID)
    ConnectionID = EventClient()
    UnlockMutex(MutexServer)
    Select Event
      Case #PB_NetworkEvent_Connect
        Debug "Client Conneceted ID " + ConnectionID
      Case #PB_NetworkEvent_Data
        cnt = ReceiveNetworkData(ConnectionID, *buffer, $FFFF)
        Debug "Receive Data ID " + ConnectionID + " / Len " + cnt
      Case #PB_NetworkEvent_Disconnect
        Debug "Client Disconneceted ID " + ConnectionID
      Default
        Delay(10)
    EndSelect
   
  Until ExitApplication
 
  Debug "Close NetworkServer"
  CloseNetworkServer(ServerID)
  Debug "Exit Thread"
 
EndProcedure

Procedure Main()
  Protected thServer
 
  If OpenWindow(#Main, 80, 80, 200, 60, "Server" , #PB_Window_SystemMenu)
   
    thServer = CreateThread(@thServer(), 0)
   
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          ExitApplication = #True
          If WaitThread(thServer, 1000) = 0
            KillThread(thServer)
          EndIf
          Break
      EndSelect
    ForEver
   
  EndIf
 
EndProcedure : Main()


Client
Code:
;-TOP

CompilerIf #PB_Compiler_Thread = 0
  CompilerError "Use Compiler Option ThreadSafe!"
CompilerEndIf

InitNetwork()

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
 
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Global ExitApplication, ExitSendData
Global ConnectionID

Procedure thClient(id)
  Protected Event, cnt
  Protected *buffer = AllocateMemory($FFFF)
 
  ConnectionID = OpenNetworkConnection("127.0.0.1", 6037)
  If ConnectionID = 0
    Debug "Error: Create NetworkClient!"
    ProcedureReturn 0
  Else
    Debug "Create NetworkClient"
  EndIf
 
  Repeat
    Event = NetworkClientEvent(ConnectionID)
    Select Event
      Case #PB_NetworkEvent_Data
        cnt = ReceiveNetworkData(ConnectionID, *buffer, $FFFF)
        Debug "Receive Data ID " + ConnectionID + " / Len " + cnt
      Case #PB_NetworkEvent_Disconnect
        Debug "Server Disconneceted"
        ExitSendData = #True
        Break
      Default
        Delay(100)
    EndSelect
   
  Until ExitApplication
 
  Debug "Close NetworkClient"
  CloseNetworkConnection(ConnectionID)
  ConnectionID = 0
  Debug "Exit Thread Client"
 
EndProcedure

Procedure thSendData(ConnectionID)
  Protected cnt
  Repeat
    cnt = SendNetworkData(ConnectionID, @"Hello World!", 12)
    Debug "Send " + cnt
    If cnt < 0
      Debug "Send Error"
      Break
    EndIf
    Delay(40)
  Until ExitApplication ; Or ExitSendData
 
  Debug "Exit Thread SendData"
 
EndProcedure

Procedure Main()
  Protected thClient, thSend
 
  If OpenWindow(#Main, 80, 180, 200, 60, "Client" , #PB_Window_SystemMenu)
    ButtonGadget(0, 10, 10, 180, 30, "Start Send")
   
    thClient = CreateThread(@thClient(), 0)
   
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          ExitApplication = #True
          If thClient
            If WaitThread(thClient, 1000) = 0
              KillThread(thClient)
            EndIf
          EndIf
          If thSend
            If WaitThread(thSend, 1000) = 0
              KillThread(thSend)
            EndIf
          EndIf
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
            Case 0
              If ConnectionID And Not IsThread(thSend)
                thSend = CreateThread(@thSendData(), ConnectionID)
              EndIf
          EndSelect
      EndSelect
    ForEver
   
  EndIf
 
EndProcedure : Main()


Current solution:
Check the client port before sending the data.
Code:
; ***************************************************************************************
; Bugfix MacOS SendNetworkData over Threads. mk-soft, 27.10.2019, Version 1.01

Procedure FixSendNetworkData(ClientID, MemoryBufer, Length)
  If GetClientPort(ClientID)
    ProcedureReturn SendNetworkData(ClientID, MemoryBufer, Length)
  Else
    ProcedureReturn -1
  EndIf
EndProcedure

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  Macro SendNetworkData(ClientID, MemoryBufer, Length)
    FixSendNetworkData(ClientID, MemoryBufer, Length)
  EndMacro
CompilerEndIf

; ***************************************************************************************

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Crash with SendNetworkData
PostPosted: Sun Oct 27, 2019 2:54 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1037
Wiso sind deine Codes immer so klein geschrieben? Bei mir hier ist das nicht lesbar.

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Crash with SendNetworkData
PostPosted: Sun Oct 27, 2019 4:02 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2343
Location: Germany
In diesen fall nur, damit man nicht so viel scrollen muss...

In this case only so that you don't have to scroll so much...

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye