Ok, i have yet another problem, that is, having the bytes, i cant do anything with them, as it gives type errors.
Any ideas how to make this work? I dont specificly need a bytearray, but i want to be able todo the logic as follows.
Any ideas how to make this work? I dont specificly need a bytearray, but i want to be able todo the logic as follows.
SuperStrict Graphics 640, 480 Local client:OClient = OClient.Create("127.0.0.1", 6121) While Not KeyHit(KEY_ESCAPE) client.onRecieve() Wend Rem Function ToString(bytes:Byte[]) Local str:String Local char:String Local i:Int = 0 While char = Chr(bytes[i..i+1]) str = str + char i = i + 1 Wend Return str End Function EndRem Type OClient Field host:String Field port:Int Field socket:TSocket Field stream:TSocketstream Field buffer:TBytes Function Create:OClient(host:String, port:Int) Local client:OClient = New OClient client.host = host client.port = port client.socket = CreateTCPSocket() ConnectSocket(client.socket, HostIp(host), port) If client Then client.stream = CreateSocketStream(client.socket) Return client Else Return Null End If End Function Method onPacketReceive(packetType:Byte, packetData:Byte[]) Print "Received packet type: " + packetType + " with data: " + packetData[..SizeOf(packetData)] End Method Method onRecieve() If SocketReadAvail(Self.socket) > 0 Self.buffer.Append(Self._ReadBytes()) Local size:Int = SizeOf(Self.buffer.bytes) If size > 3 Then packetSize = Self.buffer.ReadShort(1) If size - 3 >= packetSize Then packetType = Self.buffer.ReadByte(0) packetData = Self.buffer.ReadBytes(3, packetSize) Self.buffer.Pop(size) Self.onPacketReceive(packetType, packetData) End If End If End If End Method Method _ReadBytes() Local bytes:Byte[] While SocketReadAvail(Self.socket) > 0 bytes = bytes + ReadByte(Self.stream) Wend Return bytes End Method End Type Type TBytes Field bytes:Byte[] Method Write(bytes:Byte) Self.bytes = bytes End Method Method Append(bytes:Byte) Self.bytes = Self.bytes + bytes End Method Method Pop(length:Int) Self.bytes = Self.bytes[length..(SizeOf(Self.bytes)-length)] End Method Method Read() Return Self.bytes End Method Method ReadBytes:Byte(offset:Int, length:Int) Return Self.bytes[offset..(offset+length)] End Method Method ReadByte:Byte(offset:Int) Return Self.bytes[offset..(offset+1)] End Method Method ReadShort:Short(offset:Int) Return Self.bytes[offset..(offset+2)] End Method Method ReadInt:Int(offset:Int) Return Self.bytes[offset..(offset+4)] End Method Method ReadLong:Long(offset:Int) Return Self.bytes[offset..(offset+4)] End Method Method ReadFloat:Float(offset:Int) Return Self.bytes[offset..(offset+4)] End Method Method ReadDouble:Double(offset:Int) Return Self.bytes[offset..(offset+8)] End Method End Type