Empfangsroutine in einen Thread auslagern....

Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Neue Antwort erstellen

 

funkheld

Betreff: Empfangsroutine in einen Thread auslagern....

BeitragFr, Aug 12, 2016 12:04
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo,guten Tag.
Wie kann man diese Empfangsroutine bitte in eine Thread auslagern :
Code: [AUSKLAPPEN]

 Local rec:Int

   While udpsocket.ReadAvail()
      rec = recvfrom_(udpsocket._socket, udpbuffer, UDP_BUFFER_SIZE, 0, remip, remport)
      If rec > 0 Then
         Local bptbuff:Byte Ptr = Byte Ptr(udpbuffer)
         If rec = 8 Then
            ox = udpbuffer[1]
        cls
         drawtext("vom server > "+ox,10,120)     
         EndIf
      EndIf
   Wend



Dies ist das Hauptprogramm:
Code: [AUSKLAPPEN]

SuperStrict

Import brl.timer
Import brl.basic
Import brl.retro

Global SERVERADRESS:String    = "127.0.0.1"
Const SERVERPORT:Int   = 40404

Const UDP_BUFFER_SIZE:Int   = 8
Const ROLE_UNDET:Int   = 0
Const ROLE_CLIENT:Int   = 1
Const ROLE_SERVER:Int   = 2
Global role:Int = ROLE_UNDET

Global udpsocket:TSocket = CreateUDPSocket()
Global udpbuffer:Int Ptr = Int Ptr(MemAlloc(UDP_BUFFER_SIZE))
Global remip:Int, remport:Int

Global timer:TTimer = CreateTimer(50)
global pos:Int ,ox:int

If udpsocket.Bind(0) Then
   role = ROLE_CLIENT
   AppTitle = "UDP CLIENT  Button"
   remip = HostIp(SERVERADRESS)
   remport = SERVERPORT
EndIf

Graphics 400,400, 0, 60

While Not AppTerminate()
  Local rec:Int

   While udpsocket.ReadAvail()
      rec = recvfrom_(udpsocket._socket, udpbuffer, UDP_BUFFER_SIZE, 0, remip, remport)
      If rec > 0 Then
         Local bptbuff:Byte Ptr = Byte Ptr(udpbuffer)
         If rec = 8 Then
            ox = udpbuffer[1]
        cls
         drawtext("vom server > "+ox,10,120)     
         EndIf
      EndIf
   Wend

   If Button(10, 10, 50,20, "1", 0) Then
    If remip <> 0 Then
        udpbuffer[0] = pos
        sendto_(udpsocket._socket, udpbuffer, 8, 0, remip, remport)
     EndIf
  end if
   If Button(10, 35, 50,20, "2", 1) Then pos=pos+1
   If Button(10, 60, 50,20, "3", 2) Then pos=pos+5
   If Button(10,85,50,20, "4", 3)   Then pos=pos+10
   If Button(GraphicsWidth()-100, GraphicsHeight()-45, 90, 35, "Beenden", 4) Then End
   
   Flip 0
   WaitTimer(timer)
Wend

Function Button:Int(x:Short, y:Short, w:Byte, h:Byte, name:String, id:Byte)
   Global mouse_state:Int = -1

   DrawLine x,     y,   x+w-1, y
   DrawLine x,     y,   x,     y+h
   DrawLine x+w-1, y,   x+w-1, y+h
   DrawLine x,     y+h, x+w-1, y+h

   If MouseDown(1) And MouseX() >= x And MouseY() >= y And MouseX() =< x+w And MouseY() =< y+h Then
      DrawText name, x + w/2 - TextWidth(name)/2 + 2, y + h/2 - TextHeight(name)/2 + 2
      mouse_state = id
   Else
      DrawText name, x + w/2 - TextWidth(name)/2, y + h/2 - TextHeight(name)/2
      If mouse_state = id Then
         mouse_state = -1
          FlushMouse()
         Return True
      End If
   End If
End Function


Danke.
Gruss

Mathias-Kwiatkowski

BeitragFr, Aug 12, 2016 13:23
Antworten mit Zitat
Benutzer-Profile anzeigen
eigentlich ganz easy

1. compiler auf MT umstellen je nach ide musst du schauen wo du es machst
2. mingw installieren
3. alle neu compilieren (module ect)

so nun ein kl. bsp zum thread

Code: [AUSKLAPPEN]

Local NeuesProgStarten:TThread = CreateThread(prog2, "")

Repeat

   'Hauptprogramm
   
Forever

Function Prog2:TThread (data:Object)
   Repeat
      '2. Programm
      Delay 1
   Forever
End Function


und schon laufen beide "programme" bzw substanzen von einander getrennt
Skype: Anarchie1984
http://projektworks.de/maxbase/
Icq - Erneuert am 21.08.2017
Yahoo - Erneuert am 21.08.2017
 

funkheld

BeitragFr, Aug 12, 2016 14:15
Antworten mit Zitat
Benutzer-Profile anzeigen
Ja danke, du hast mir sehr geholfen.
Es funktioniert fehlerfrei., der Server im Thread und der Client im Thread.

Ich tue mich schwer, weil die 4 Beispielprogramme in BlitzMax so mit Mutex usw beschrieben wurden und so umständlich. Ich konnte meine Lösung da nicht heraus finden.

Was bedeutet dieses : ...(data:Object)
Es läuft bei mir, ohne das ich dem Thread etwas übergebe.

Danke.
Gruss

FireballFlame

BeitragFr, Aug 12, 2016 16:07
Antworten mit Zitat
Benutzer-Profile anzeigen
Nichts besonderes, es ist einfach ein Parameter, mit dem du dem Thread bei der Erstellung ein Objekt übergeben kannst. Musst du aber nicht.
PC: Intel Core i7 @ 4x2.93GHz | 6 GB RAM | Nvidia GeForce GT 440 | Desktop 2x1280x1024px | Windows 7 Professional 64bit
Laptop: Intel Core i7 @ 4x2.00GHz | 8 GB RAM | Nvidia GeForce GT 540M | Desktop 1366x768px | Windows 7 Home Premium 64bit

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group