On 04/05/2011 01:39 AM, Kevin Fishburne wrote:
Alright, I've transformed all the networking code to the suggested
solution, and it's so close to working I can smell it. While there is
more code now, I like it better as it's easier to read (important!).

Some code:

    Dim id As Byte
    Dim type As Byte
    Dim data As String

    id = Read #udp As Byte
    type = Read #udp As Byte
    If Eof(udp) = False Then data = Read #udp, Lof(udp) - 2

Using "Lof(udp) - 2" as the length seems to remove bytes from the end
rather than the beginning. I get an unwanted byte at the beginning as well.

Maybe this is a late April Fools joke about endianness? Other than that,
the whole app is ready to put pixels on the screen again

After checking my code for bugs (none found, knock on wood) and doing some experimentation, it looks like sending certain data types using the new method works fine, but other data has an extra byte added in front of or in-between variables. Any info about what's really happening is appreciated.

Again what I'm trying to do is send an ID (Byte), type (Byte), and a sequence of arbitrary variables via UDP. When they're received, the ID and type are read normally as Bytes into variables and the rest of the data is read into a string and stored in an array for later processing. The arbitrary variables stored as a string (everything after ID and type) are later interpreted via the *@() functions and assigned to variables. To do this I just created a different UDP_Write procedure for each transaction type so the packets/transactions are properly assembled. I've attached some code excerpts if anyone desires greater specificity.

In summary of the attachments, the failure basically goes like this:

' Send username and password.
  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 50 As Byte
Write #udp, credentials As String ' credentials = "myusername,mypassword"
  udp.Send

' Read the username and password.
  id = Read #udp As Byte
  type = Read #udp As Byte
If Eof(udp) = False Then data = Read #udp, Lof(udp) ' Username and password separated elsewhere, not a problem.

data should be "myusername,mypassword" but it actually is "^Uanon@codex,mypassword". "\x15" prepends the string, strangely. I think this errant byte also precedes any number of datatypes that are send separately and later read as a single string. What is this byte, and how may I get rid of it?

--
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sa...@eightvirtues.com
phone: (770) 853-6271

02:15:44.093 - Player ^Uanon@codex with password mypassword added at position 1.

"Player" should be "anon@codex". The password is correct. "^U" is the mystery 
byte that shouldn't be there. In some transactions the mystery byte isn't 
there. I'm guessing it precedes certain datatypes written to the UDP packet.

  ' Authenticate to server.
  Console.Say("Authenticating to server.")
  Network.UDP_Write_50(Network.Server_IP, Network.Server_Port, tcid, username & 
"," & password)
Public Sub UDP_Read()

  ' UDP data received, add to appropriate transaction queue.

  ' General declarations.
  Dim timestamp As String ' Time transaction was received.
  Dim ip As String        ' Sender's IP address.
  Dim port As Integer     ' Sender's UDP port.
  Dim id As Byte          ' Transaction ID.
  Dim type As Byte        ' Transaction type.
  Dim data As String      ' Transaction data.
  Dim p As Short          ' Number of the player sending a transaction to the 
server.

  ' Parse UDP data and assign to variables.
  timestamp = Str$(Timer)
  ip = udp.SourceHost
  port = udp.SourcePort
  id = Read #udp As Byte
  type = Read #udp As Byte
  If Eof(udp) = False Then data = Read #udp, Lof(udp)

  ' Determine client/server mode.
  If mode = "server" Then
    ' Handle authentation requests.
    If type = 50 Then
      ' Authenticate the player.
      Server.Player_Authenticate(ip, port, data)
      Return
    Endif
    ' Ignore packet if player isn't authenticated.
    p = Server.Player_Find_IP(ip)
    If p = 0 Then Return
    ' Add variables to server's client transaction queue.
    Server.tc[p, id, 0] = "new"
    Server.tc[p, id, 1] = ip
    Server.tc[p, id, 2] = port
    Server.tc[p, id, 3] = id
    Server.tc[p, id, 4] = timestamp
    Server.tc[p, id, 5] = type
    Server.tc[p, id, 6] = data
    ' Display debug info.
    Console.Say("Received from player " & p & " at " & ip & ", Port: " & port & 
", ID: " & Str$(id) & ", Type: " & type)
  Endif

  ' Determine client/server mode.
  If mode = "client" Then
    ' Parse UDP data and add to client's server transaction queue.
    Client.ts[id, 0] = "new"  ' Status.
    Client.ts[id, 1] = id     ' ID.
    Client.ts[id, 2] = type   ' Type.
    Client.ts[id, 3] = data   ' Data
    ' Display debug info.
    Console.Say("Received from server at " & ip & ", Port: " & port & ", ID: " 
& id & ", Type: " & type)
  Endif

End
Public Sub UDP_Write_50(ip As String, port As Integer, id As Byte, credentials 
As String)

  ' Send "authenticate to server" transaction to specified IP address and port 
over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 50 As Byte
    Write #udp, credentials As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 50)

End

Public Sub UDP_Write_51(ip As String, port As Integer, id As Byte)

  ' Send "disconnect from server" transaction to specified IP address and port 
over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 51 As Byte
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 51)

End

Public Sub UDP_Write_52(ip As String, port As Integer, id As Byte)

  ' Send "authentication successful" transaction to specified IP address and 
port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 52 As Byte
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 52)

End

Public Sub UDP_Write_70(ip As String, port As Integer, id As Byte, worldx As 
Single, worldy As Single)

  ' Send "player update, world coordinates" transaction to specified IP address 
and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 70 As Byte
    Write #udp, worldx As Single
    Write #udp, worldy As Single
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 70)

End

Public Sub UDP_Write_71(ip As String, port As Integer, id As Byte, orientation 
As Single)

  ' Send "player update, orientation" transaction to specified IP address and 
port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 71 As Byte
    Write #udp, orientation As Single
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 71)

End

Public Sub UDP_Write_73(ip As String, port As Integer, id As Byte, skin As Byte)

  ' Send "player update, skin" transaction to specified IP address and port 
over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 73 As Byte
    Write #udp, skin As Byte
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 73)

End

Public Sub UDP_Write_100(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, north cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 100 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 100)

End

Public Sub UDP_Write_101(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, south cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 101 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 101)

End

Public Sub UDP_Write_102(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, east cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 102 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 102)

End

Public Sub UDP_Write_103(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, west cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 103 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 103)

End

Public Sub UDP_Write_104(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, northeast cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 104 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 104)

End

Public Sub UDP_Write_105(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, northwest cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 105 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 105)

End

Public Sub UDP_Write_106(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, southeast cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 106 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 106)

End

Public Sub UDP_Write_107(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, southwest cells" transaction to specified IP 
address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 107 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 107)

End

Public Sub UDP_Write_108(ip As String, port As Integer, id As Byte, data As 
String)

  ' Send "landscape data update, all cells" transaction to specified IP address 
and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 108 As Byte
    Write #udp, data As String
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 108)

End

Public Sub UDP_Write_120(ip As String, port As Integer, id As Byte, player As 
Short, worldx As Single, worldy As Single)

  ' Send "players data update, world coordinates, one player" transaction to 
specified IP address and port over UDP.

  ' Define target's IP address and port.
  udp.TargetHost = ip
  udp.TargetPort = port

  ' Send the transaction.
  udp.Begin
    Write #udp, id As Byte
    Write #udp, 120 As Byte
    Write #udp, player As Short
    Write #udp, worldx As Single
    Write #udp, worldy As Single
  udp.Send

  ' Increment the transaction ID and print debug info.
  UDP_Write_Finish(ip, port, id, 120)

End

Public Sub UDP_Write_Finish(ip As String, port As Integer, id As Byte, type As 
Byte)

  ' Perform post-write tasks.
  ' Called after a transaction has been sent.

  ' Determine client/server mode.
  If mode = "server" Then
    ' Increment the server transaction ID for the client.
    Server.Increment_ID(Server.Player_Find_IP(ip))
    ' Display debug info.
    Console.Say("Sent to client " & ip & ", Port: " & port & ", ID: " & id & ", 
Type: " & type)
  Else
    ' Increment the client transaction ID.
    Client.Increment_ID
    ' Display debug info.
    Console.Say("Sent to server " & ip & ", Port: " & port & ", ID: " & id & ", 
Type: " & type)
  Endif

End

------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to