Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.readBytes not moving pointer
    text
    copied!<p>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.</p> <p>Here is my socket data handler:</p> <pre><code>protected function handleSocketData(e:ProgressEvent):void{ var s:Socket = e.target as Socket; trace("pre if " + len + " " + s.bytesAvailable); if (len == 0 &amp;&amp; s.bytesAvailable &gt;= 4) { len = s.readUnsignedInt(); } trace(len + " " + s.bytesAvailable); if (len &gt; 0 &amp;&amp; s.bytesAvailable &gt;= 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; } } </code></pre> <p>The problem lies with <code>s.readBytes(ba, 0, len);</code></p> <p>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 <strong>first</strong> time I get data over the socket.</p> <pre><code>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! </code></pre> <p>Now if I send data over the socket again:</p> <pre><code>**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 </code></pre> <p>Now it seems like readBytes is leaving data in the buffer.</p> <p>The strange part is when I swap <code>s.readBytes(ba, 0, len);</code> for <code>s.readBytes(ba, 0, s.bytesAvailable);</code> everything seems to work:</p> <p><strong>With s.readBytes(ba, 0, s.bytesAvailable);</strong></p> <pre><code>**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 </code></pre> <p>The issue with using <code>bytesAvailable</code> is that now I can't handle multiple packets.</p> <pre><code>pre if 2214 2218 2214 2218 RangeError: Error #2006: The supplied index is out of bounds. at flash.utils::ByteArray/readObject() </code></pre> <p>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?</p> <p><strong>edit</strong></p> <p>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.</p> <pre><code> 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"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload