So... ich habe gerade eben das Modul für VB fertiggestellt und die PHP-Datei oben editiert.
Um auf die Funktionen zuzugreifen, braucht man ein Winsock-Steuerelement in irgendeinem Formular. Beim Start des Formulars muss dann LobbySetWinsock() mit diesem Winsock-Steuerelement als Parameter aufgerufen werden.
Es ist auch ziemlich wichtig, die Ereignisse "Connect" und "DataArrival" an meinen Code weiterzuleiten. Siehe dafür auch den Beispielcode.
Um die Serverliste zu bekommen, definiert man ein String-Array mit variabler Größe und füllt dieses dann mit dem Rückgabewert der LobbyGetServers()-Funktion, z.B. so:
Dim z() As String
z = LobbyGetServers()
Bevor man jetzt mit dem Array z weitermacht, sollte man zuerst den ersten Eintrag des Arrays überprüfen. Dieser Eintrag sollte nämlich "OK" sein.
Darauf folgt dann eine Liste der Server, wobei jeder Eintrag in diesem Format gespeichert ist: "ServerIP,Spielname" (Keine Leerstelle nach dem Komma!)
Der letze Eintrag der Liste sollte ein "END" sein.
Um seine IP dem Lobbyserver bekannt zu machen, muss man die Funktion LobbyNewServer() benutzen. Rückgabewert ist bei Erfolg des Aufrufes 1, ansonsten 0.
Damit der Lobbyserver auch den Spielnamen kennt, benutzt man die Funktion LobbyUpdateServer(). Als Parameter übergibt man hier den Spielnamen. Ebenfalls ist der Rückgabewert bei Erfolg 1, sonst 0.
Um sich schließlich wieder vom Lobbyserver zu verabschieden, benutzt man die LobbyRemoveServer()-Funktion. Auch hier ist der Rückgabewert bei Erfolg 1, in anderen Fällen 0.
Ich hoffe, es ist alles klar soweit. Hier kommt der Code für ein Visual Basic-Modul:
Code: Alles auswählen
Option Explicit
' VB client for the generic PHP-based lobby server
' by The.Modificator
' This is free code. Use it however you want, but please give credits where credits have to be given.
Private Declare Function GetTickCount Lib "kernel32" () As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Change values below
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const HTTP_Host As String = "localhost"
Const HTTP_Path As String = "dudoslobby.php"
Const HTTP_Port As Long = 80
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Change values above
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private http As Winsock
Private DataArrived As String
Private Connected As Boolean
Private LobbyInUse As Boolean
Public Sub LobbyWinsockConnect()
Connected = True
End Sub
Public Sub LobbyWinsockDataArrival(ByVal bytesTotal As Long)
http.GetData DataArrived
End Sub
Private Function WaitForData()
Dim Start As Long
Dim I&
Start = GetTickCount()
Do
For I = 1 To 100: DoEvents: Next
If DataArrived <> "" Then WaitForData = 1: Exit Function
Loop While Start > GetTickCount() - 30000
WaitForData = 0
End Function
Private Function WaitForConnection()
Dim Start As Long
Dim I&
Start = GetTickCount()
Do
For I = 1 To 100: DoEvents: Next
If Connected = True Then WaitForConnection = 1: Exit Function
Loop While Start > (GetTickCount() - 30000)
WaitForConnection = 0
End Function
Private Function HexA(inp As Byte) As String
If (Len(Hex(inp)) = 1) Then HexA = "0" & Hex(inp) Else HexA = Hex(inp)
End Function
Private Function EncodeForWeb(inp As String) As String
Dim I&
For I = 1 To Len(inp)
If ((Mid(inp, I, 1) = vbCr) Or (Mid(inp, I, 1) = vbLf)) Then GoTo NextOne
EncodeForWeb = EncodeForWeb & "%" & Hex(Asc(Mid(inp, I, 1)))
NextOne:
Next
End Function
Public Function LobbyGetServers() As String()
If LobbyInUse Then GoTo ErrorOut
LobbyInUse = True
Dim ret As Long
http.Close
http.Connect HTTP_Host, HTTP_Port
Connected = False
ret = WaitForConnection()
If ret = 0 Then GoTo ErrorOut
DataArrived = ""
http.SendData "GET /" & HTTP_Path & "?command=getservers HTTP/1.0" & vbCrLf & _
"Host: " & HTTP_Host & vbCrLf & _
"User-Agent: DUDOS/" & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Connection: close" & vbCrLf & _
vbCrLf
ret = WaitForData()
If ret = 1 Then
Dim DataPos As Long
DataPos = InStr(1, DataArrived, vbCrLf & vbCrLf)
If DataPos = 0 Then GoTo ErrorOut
Dim Lines() As String
Lines = Split(Mid(DataArrived, DataPos + 4), vbLf)
If (UBound(Lines) > 1) Then
For ret = 1 To UBound(Lines) - 1
Lines(ret) = Replace(Lines(ret), "&br;", vbNewLine)
Lines(ret) = Replace(Lines(ret), "&", "&")
Next
End If
LobbyGetServers = Lines
Else
GoTo ErrorOut
End If
http.Close
LobbyInUse = False
Exit Function
ErrorOut:
Dim LGetServers(1) As String
LGetServers(1) = "ERR"
LobbyGetServers = LGetServers
http.Close
LobbyInUse = False
End Function
Public Function LobbyNewServer() As Long
If LobbyInUse Then GoTo ErrorOut
LobbyInUse = True
Dim ret As Long
http.Close
http.Connect HTTP_Host, HTTP_Port
Connected = False
ret = WaitForConnection()
If ret = 0 Then GoTo ErrorOut
DataArrived = ""
http.SendData "GET /" & HTTP_Path & "?command=newserver HTTP/1.0" & vbCrLf & _
"Host: " & HTTP_Host & vbCrLf & _
"User-Agent: DUDOS/" & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Connection: close" & vbCrLf & _
vbCrLf
ret = WaitForData()
If ret = 1 Then
Dim DataPos As Long
DataPos = InStr(1, DataArrived, vbCrLf & vbCrLf)
If DataPos = 0 Then GoTo ErrorOut
If (Mid(DataArrived, DataPos + 4, 2)) <> "OK" Then LobbyNewServer = 0 Else LobbyNewServer = 1
Else
GoTo ErrorOut
End If
http.Close
LobbyInUse = False
Exit Function
ErrorOut:
LobbyNewServer = 0
http.Close
LobbyInUse = False
End Function
Public Function LobbyUpdateServer(GameName As String) As Long
If LobbyInUse Then GoTo ErrorOut
LobbyInUse = True
Dim ret As Long
http.Close
http.Connect HTTP_Host, HTTP_Port
Connected = False
ret = WaitForConnection()
If ret = 0 Then GoTo ErrorOut
DataArrived = ""
http.SendData "GET /" & HTTP_Path & "?command=refresh&gamename=" & EncodeForWeb(GameName) & " HTTP/1.0" & vbCrLf & _
"Host: " & HTTP_Host & vbCrLf & _
"User-Agent: DUDOS/" & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Connection: close" & vbCrLf & _
vbCrLf
ret = WaitForData()
If ret = 1 Then
Dim DataPos As Long
DataPos = InStr(1, DataArrived, vbCrLf & vbCrLf)
If DataPos = 0 Then GoTo ErrorOut
If (Mid(DataArrived, DataPos + 4, 2)) <> "OK" Then LobbyUpdateServer = 0 Else LobbyUpdateServer = 1
Else
GoTo ErrorOut
End If
http.Close
LobbyInUse = False
Exit Function
ErrorOut:
LobbyUpdateServer = 0
http.Close
LobbyInUse = False
End Function
Public Function LobbyRemoveServer() As Long
If LobbyInUse Then GoTo ErrorOut
LobbyInUse = True
Dim ret As Long
http.Close
http.Connect HTTP_Host, HTTP_Port
Connected = False
ret = WaitForConnection()
If ret = 0 Then GoTo ErrorOut
DataArrived = ""
http.SendData "GET /" & HTTP_Path & "?command=removeserver HTTP/1.0" & vbCrLf & _
"Host: " & HTTP_Host & vbCrLf & _
"User-Agent: DUDOS/" & App.Major & "." & App.Minor & "." & App.Revision & vbCrLf & _
"Connection: close" & vbCrLf & _
vbCrLf
ret = WaitForData()
If ret = 1 Then
Dim DataPos As Long
DataPos = InStr(1, DataArrived, vbCrLf & vbCrLf)
If DataPos = 0 Then GoTo ErrorOut
If (Mid(DataArrived, DataPos + 4, 2)) <> "OK" Then LobbyRemoveServer = 0 Else LobbyRemoveServer = 1
Else
GoTo ErrorOut
End If
http.Close
LobbyInUse = False
Exit Function
ErrorOut:
LobbyRemoveServer = 0
http.Close
LobbyInUse = False
End Function
Public Sub LobbySetWinsock(wsk As Winsock)
Set http = wsk
End Sub
Und hier noch ein Beispielprogrämmchen:
Code: Alles auswählen
Private Sub cmdGetServers_Click()
Dim z() As String
z = LobbyGetServers()
If z(1) <> "OK" Then
'Fehler... Abbruch
MsgBox "Error"
Exit Sub
End If
lstServers.Clear
Dim I&
For I = 1 To UBound(z) - 1
lstServers.AddItem z(I)
Next
'... hier können die Server z.B. zu einem List-Steuerelement hinzugefügt werden
End Sub
Private Sub cmdAddMe_Click()
Dim z As Long
z = LobbyNewServer()
If z = 0 Then
'Fehler... Abbruch
MsgBox "Error"
Exit Sub
End If
z = LobbyUpdateServer(txtGameName.Text)
If z = 0 Then
'Fehler...
MsgBox "Error"
End If
End Sub
Private Sub cmdUpdateName_Click()
Dim z As Long
z = LobbyUpdateServer(txtGameName.Text)
If z = 0 Then
'Fehler...
MsgBox "Error"
End If
End Sub
Private Sub cmdRemoveMe_Click()
Dim z As Long
z = LobbyRemoveServer()
If z = 0 Then
'Fehler...
MsgBox "Error"
End If
End Sub
Private Sub Form_Load()
LobbySetWinsock Winsock1
End Sub
Private Sub Winsock1_Connect()
LobbyWinsockConnect
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
LobbyWinsockDataArrival bytesTotal
End Sub
Gruß
Modi
The cake is a lie.