I’m running into a strange issue trying to use ByteArrays to send an object and unsigned int over a Socket. I’m trying to handle a case where I may receive data in multiple packets.
Here is my socket data handler:
protected function handleSocketData(e:ProgressEvent):void{
var s:Socket = e.target as Socket;
trace("pre if " + len + " " + s.bytesAvailable);
if (len == 0 && s.bytesAvailable >= 4) {
len = s.readUnsignedInt();
}
trace(len + " " + s.bytesAvailable);
if (len > 0 && s.bytesAvailable >= len){
var ba:ByteArray = new ByteArray();
//s.readBytes(ba, 0, len);
s.readBytes(ba, 0, s.bytesAvailable); //should be len
//trace("handle socket before inflate " + ba.length);
ba.inflate();
//trace("handle socket after inflate " + ba.length);
var o:Object = ba.readObject();
Overlord.updatePlayers(o);
trace(o.player);
len = 0;
}
}
The problem lies with s.readBytes(ba, 0, len);
I’m using len (defined at the class scope) to handle a case where I may get multiple packets. Now this seems to work the first time I get data over the socket.
pre if 0 44 - trace I'm getting before the first if statement
38 40 - trace I'm getting after
frank - Everything comes in fine, working!
Now if I send data over the socket again:
**first send:**
pre if 0 44
38 40
frank - working, yay!
**things start to fail every send after:**
pre if 0 46
218759168 42
pre if 218759168 84
218759168 84
pre if 218759168 127
218759168 127
Now it seems like readBytes is leaving data in the buffer.
The strange part is when I swap s.readBytes(ba, 0, len); for s.readBytes(ba, 0, s.bytesAvailable); everything seems to work:
With s.readBytes(ba, 0, s.bytesAvailable);
**first send**
pre if 0 44
38 40
frank
**everything seems to work after, no old data in the buffer?**
pre if 0 44
38 40
frank
pre if 0 43
37 39
frank
The issue with using bytesAvailable is that now I can’t handle multiple packets.
pre if 2214 2218
2214 2218
RangeError: Error #2006: The supplied index is out of bounds.
at flash.utils::ByteArray/readObject()
I’m not quite sure what I’m missing here, it seems like the buffer is only getting cleared when I use socket.bytesAvailable. Anyone have any input?
edit
I’m using this method for sending data over the socket. By writing the length of the ByteArray and reading it back in the data handler I’m trying to handle getting the data object across multiple packets.
public function sendData(socketData:*):void{
if(_connected){
//trace("sending");
var ba:ByteArray = new ByteArray();
ba.writeObject(socketData);
ba.position = 0;
//trace("byteArray len before send and defalte: " + ba.length)
ba.deflate();
//trace("byteArray len before send and after defalte: " + ba.length)
socket.writeUnsignedInt(ba.length);
socket.writeBytes(ba, 0, ba.length);
socket.flush();
} else {
throw new Error("Socket not open");
}
}
You should really pull the data out of the socket into a byte array and then do your manipulations on the bytearray and not the socket. When you call any of the read***() methods on the socket or a bytearray, you move the .position property by the value of your read method. This is also the reason why you’re getting the supplied index error on readObject. Before calling readObject, set the .position property of BA to 0 first.
My comment on your question and the details in this answer I believe are at the very least a fundamental issue with your code. You should also post the code of the sending socket so that we can figure this out easier.