public void testReader() { G2Network.Instance.SelfAddress = System.Net.IPAddress.Parse("127.0.0.1"); G2Packet lni = Settings.SmartLNIPacket(); G2Packet na = new G2PacketNA(new NodeAddress(System.Net.IPAddress.Parse("127.0.0.1"), 6345)); for (int i = 0; i < 300; i++) { lni.AddChild(na); } lni.FinalizePacket(); G2Log.Write(lni.ToString()); ByteBuffer b = lni.ToBuffer(); G2PacketReader reader = new G2PacketReader(new NodePeer(System.Net.IPAddress.Parse("127.0.0.1"), 6346, 0, false)); int nb = 3; byte[][] bytes = new byte[nb][]; int start = 0; int div = b.Length / nb; for (int i = 0; i < nb; i++) { if (i == nb - 1) { div += b.Length % nb; } bytes [i] = new byte[div]; Array.Copy(b.Bytes, start, bytes [i], 0, div); start += div; } for (int i = 0; i < nb; i++) { bool enough = reader.Read(bytes [i], bytes[i].Length); if (enough) { G2Log.Write("PacketReader Test Success"); break; } } G2Packet lni2 = Settings.SmartLNIPacket(); ByteBuffer b2 = lni2.ToBuffer(); bool succ = reader.Read(b2.Bytes, b2.DataOffset); if (succ) { G2Log.Write("PacketReader Test Unit Packet Success"); } else { G2Log.Write("PacketReader Test Unit Packet FAILED"); } b.Append(b2); succ = reader.Read(b.Bytes, b.DataOffset); if (succ) { G2Log.Write("PacketReader Test Sequential Packet Success"); } else { G2Log.Write("PacketReader TEst Sequential Packet FAILED"); } }
private int ReadPacket(MemoryStream stream, out G2Packet pack) { Header h = null; try { h = Header.ReadHeader(stream); } catch (Exception e) { throw e; } int packLength = (int)(h.PayloadLength + h.HeaderLength); // if stream is not enough big to contain all the packet we need to read more if (packLength > stream.Length) { throw new NotEnoughDataException("Not enough data in buffer"); } pack = G2PacketType.getPacketByHeader(h); // Set the remote host into packet for further analysis pack.RemotePeer = this.peer; // we have a unknown packet so we just read till the end of the packet if (pack.type == G2PacketType.DEFAULT) { // anyway, read & store the bytes return((int)(h.HeaderLength + pack.ReadPayload(stream, h.PayloadLength))); } // we have read header, now we calculate how much byte we need to read more (children + payload) int byteToRead = h.PayloadLength; int byteRead = 0; bool endOfChildStream = false; if (h.compound) { while (true) { G2Packet childPacket; try { int bRead = ReadPacket(stream, out childPacket); //Debug.Assert(bRead == childPacket.getTotalPacketLength(), //"ReadPacket:ChildPacket " + childPacket.type + " bRead = " + bRead + " vs " + childPacket.getTotalPacketLength()); } catch (BigEndianPacketException e) { throw e; } catch (EndOfStreamException e) { byteRead += 1; break; } pack.AddChild(childPacket); byteRead += childPacket.getTotalPacketLength(); // root packet does NOT have a payload if (byteRead == byteToRead) { endOfChildStream = true; break; } } } // have to count the remaining bytes, because length in header includes child packets. if (!endOfChildStream && (byteRead < byteToRead)) { byteRead += pack.ReadPayload(stream, byteToRead - byteRead); } // return total read byte number return((int)(byteRead + h.HeaderLength)); }