public List <UtopiaMessage> getNewMessages() { List <UtopiaMessage> inmessageQueue = new List <UtopiaMessage>(); inbuffer.clear(); // copy from the stream to the byte-buffer // TODO: [] check for stream close! while (networkStream.DataAvailable && inbuffer.capacity() > inbuffer.position()) { inbuffer.put((byte)networkStream.ReadByte()); } inbuffer.flip(); if (VERBOSITY >= 1) { Console.WriteLine(inbuffer.remaining() + " to read in channel"); } while ((inbuffer.remaining() > 0)) { RawMessage rawmessage = null; try { rawmessage = RawMessage.deserialize(inbuffer); } catch (ClientException ex) { Console.WriteLine("Something wrong deserializing client message... skipped"); Console.WriteLine(ex.getMessage()); break; } try { UtopiaMessage msg = rawmessage.decodePayload(); inmessageQueue.Add(msg); } catch (ClientException ex) { Console.WriteLine("Something wrong decoding client message... skipped"); Console.WriteLine(ex.getMessage()); } } if ((VERBOSITY >= 1)) { Console.WriteLine(("New message queue size: " + inmessageQueue.Count)); } sendHeartbeatIfTimeout(); return(inmessageQueue); }