Ich hab ein UDP system geschrieben aber wenn sich jetzt ein Client abmeldet (Case 2) kommt zwar die nachricht bei nem anderen Clienten an aber der Befehl delete p wird nich ausgeführt und somit wird auch das rcet von nem anderem Spieler noch weiter gerendert...
Das Prinzip: Client meldet sich beim Server an. Der Positionsaustausch läuft über peer to peer. Anmelde und Abmelde Nachrichten laufen über den Server.
Server:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Graphics 600,400,32,2
Type pdata Field name$ Field ip Field port End Type
Global port = 1337, stream = CreateUDPStream(port)
If Not stream Then End
Print \"Server gestartet, IP: \" + ip$()
Repeat While RecvUDPMsg(stream) If ReadAvail(stream) Then msgid = ReadByte(stream) Select msgid Case 0 np.pdata = New pdata np\name$ = ReadLine(stream) np\ip = UDPMsgIP(stream) np\port = UDPMsgPort(stream) Print np\name$ + \" ist dem Spiel beigetreten.\" WriteByte stream, 1 SendUDPMsg stream, np\ip, np\port For p.pdata = Each pdata If Not p = np Then WriteByte stream, 0 WriteLine stream, np\name$ WriteInt stream, np\ip WriteInt stream, np\port SendUDPMsg stream, p\ip, p\port EndIf Next For p.pdata = Each pdata If (Not (p\ip = np\ip And p\port = np\port)) Then WriteByte stream, 0 WriteLine stream, p\name$ WriteInt stream, p\ip WriteInt stream, p\port SendUDPMsg stream, np\ip, np\port EndIf Next Case 1 getip = UDPMsgIP(stream) getport = UDPMsgPort(stream) g_name$ = ReadLine(stream) For p.pdata = Each pdata If p\ip = getip And p\port = getport Then For pp.pdata = Each pdata WriteByte stream, 2 WriteInt stream, getip WriteInt stream, getport SendUDPMsg stream, pp\ip, pp\port Next EndIf Delete p Next
Print g_name$ + \" hat das Spiel verlassen\" WriteByte stream, 1 SendUDPMsg stream, getip, getport End Select EndIf Wend Flip Forever
Function ip$(inte=0) Local ips, ip, ipstr$ ips=CountHostIPs(\"\") If Not ips Then Return 0 ip=HostIP(ips) If inte=1 Then Return ip ipstr$=DottedIP(ip) Return ipstr$ End Function
---
Client:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Type pdata Field x,y Field name$ Field ip Field port End Type
Global port = 1337, ip$ = \"127.0.0.1\", stream = CreateUDPStream(), myname$ = Input(\"Name? \"), x = 0, y = 0
If Not stream Then End
WriteByte stream, 0 WriteLine stream, myname$ SendUDPMsg stream, ipint(ip$), port
Print \"Versuche zu verbinden\"
timer = MilliSecs()
Repeat If MilliSecs()-timer > 10000 Then CloseUDPStream(stream) Print \"Fehler\" Delay 5000 End EndIf If RecvUDPMsg(stream) Then If ReadAvail(stream) Then If ReadByte(stream) = 1 Then Print \"Connected\" Exit EndIf EndIf EndIf Forever
SetBuffer BackBuffer()
Repeat
Cls If RecvUDPMsg(stream) Then If ReadAvail(stream) Then msgid = ReadByte(stream) Select msgid Case 0 g_name$ = ReadLine(stream) g_ip = ReadInt(stream) g_port = ReadInt(stream) Print g_name$ + \" ist dem Spiel begetreten\" pp.pdata = New pdata pp\name$ = g_name$ pp\ip = g_ip pp\port = g_port For p.pdata = Each pdata WriteByte stream, 1 WriteShort stream, p\x WriteShort stream, p\y SendUDPMsg stream, pp\ip, pp\port Next Case 1 getip = UDPMsgIP(stream) getport = UDPMsgPort(stream) xpos = ReadShort(stream) ypos = ReadShort(stream) For p.pdata = Each pdata If p\ip = getip And p\port = getport Then p\x = xpos p\y = ypos EndIf Next Case 2 getip = ReadInt(stream) getport = ReadInt(stream) For p.pdata = Each pdata If p\ip = getip And p\port = getport Then Delete p Exit EndIf Next End Select EndIf EndIf If KeyHit(1) Then WriteByte stream, 1 WriteLine stream, myname$ SendUDPMsg stream, ipint(ip$), port CloseUDPStream(stream) End EndIf If KeyDown(200) Then y = y - 1 If KeyDown(208) Then y = y + 1 If KeyDown(203) Then x = x - 1 If KeyDown(205) Then x = x + 1 For p.pdata = Each pdata WriteByte stream, 1 WriteShort stream, x WriteShort stream, y SendUDPMsg stream, p\ip, p\port Next Color 255,255,255 For p.pdata = Each pdata Rect p\x-1,p\y-1,3,3 Next Rect x-1,y-1,3,3 Rect(0,0,69,63,0)
tempx = x tempx = tempx*100/600 tempx = 67*tempx/100 tempy = y tempy = tempy*100/400 tempy = 61*tempy/100 Color(0,255,0)
If tempx >= 0 And tempy >= 0 Then Rect(tempx+0,tempy+0,1,1) EndIf Color(255,0,0)
For p.pdata= Each pdata tempx = p\x tempx = tempx*100/600 tempx = 67*tempx/100 tempy = p\y tempy = tempy*100/400 tempy = 61*tempy/100 If tempx >= 0 And tempy >= 0 Then Rect(tempx+0,tempy+0,1,1) EndIf Next Color(255,255,255) Flip Forever
Function ipint(inputIP$) Local break1,break2,break3,add1,add2,add3,add4,ipreturn break1 = Instr(inputIP$,\".\") break2 = Instr(inputIP$,\".\",break1+1) break3 = Instr(inputIP$,\".\",break2+1) add1 = Mid(inputIP$,1,break1-1) add2 = Mid(inputIP$,break1+1,break2-1) add3 = Mid(inputIP$,break2+1,break3-1) add4 = Mid(inputIP$,break3+1) ipreturn=(add1 Shl 24) + (add2 Shl 16) + (add3 Shl 8) + add4 Return ipreturn End Function
Bitte helft mir mal weiter.
|