Note that there are some explanatory texts on larger screens.

plurals
  1. POnew Akka I/O (Java) TCP data processing using ByteIterator need a java example if possible
    text
    copied!<p>I'm trying to implement a Tcp server using the new Akka I/O, unfortunately the documentation is not yet finished, and i'm having some issues implementing it in java :(. I managed to create a client and server and to send a message from the client to the server, but how do you read the recieved bytes using the ByteIterator? am i having the wrong appreach? maybe that's not how you process the data.</p> <p>something weird happens when i send alot of messages very fast, they are all queued to the bi, and the data is never reset event after I make a it.getInt()</p> <pre><code> if (msg instanceof Tcp.Received) { final Tcp.Received recv = (Tcp.Received) msg; final ByteString data = recv.data(); ByteIterator bi = data.iterator(); while(bi.hasNext()) { .... } } else if (msg instanceof Tcp.CommandFailed) { final Tcp.CommandFailed failed = (Tcp.CommandFailed) msg; final Tcp.Command command = failed.cmd(); // react to failed connect, bind, write, etc. } else if (msg instanceof Tcp.ConnectionClosed) { final Tcp.ConnectionClosed closed = (Tcp.ConnectionClosed) msg; if (closed.isAborted()) { // handle close reasons like this } </code></pre> <p>Solution: Oh! now I understand my mistake, I cached the byteStringBuilder on the client, and forgot to clear it -_- how stupid !</p> <p>for those who will want an example of implementation for Java here is how I did it:</p> <p>if you guys have any optimizations im taking em !:)</p> <pre><code> int packetSize = 0; if (msg instanceof Tcp.Received) { final Tcp.Received recv = (Tcp.Received) msg; final ByteString data = recv.data(); ByteIterator bi = data.iterator(); while (bi.hasNext()) { packetSize = bi.getInt(ByteOrder.LITTLE_ENDIAN); Message m = Message.fromByteIterator(bi); getContext().parent().tell(m, null); } } </code></pre> <p>Message.fromByteIterator(bi) will just intialize a new Message Object by taking Ints, Floats, Bytes Arrays ... from the BI</p>
 

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