From 052cbfd922a9512d1143fc10f5ab4c82b5470bcc Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 15 Nov 2012 22:24:09 +0000 Subject: Ok, Packet based stream reading/writing works and is compliant. --- cs-dotnet/src/gatsstream.cs | 79 +++++++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/tests/packet.cs | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 cs-dotnet/src/gatsstream.cs create mode 100644 cs-dotnet/src/tests/packet.cs diff --git a/cs-dotnet/src/gatsstream.cs b/cs-dotnet/src/gatsstream.cs new file mode 100644 index 0000000..0e85379 --- /dev/null +++ b/cs-dotnet/src/gatsstream.cs @@ -0,0 +1,79 @@ +using System.IO; +using System.Net; +using System; + +namespace Com.Xagasoft.Gats +{ + public class GatsStream + { + private Stream s; + private MemoryStream ReadBuf; + private int size = -1; + private int version; + private BinaryWriter bw = null; + private BinaryReader br = null; + + public GatsStream( Stream s ) + { + this.s = s; + this.ReadBuf = new MemoryStream(); + } + + public GatsObject ReadObject() + { + if( size == -1 ) + { + for(;;) + { + version = s.ReadByte(); + if( version == -1 ) + return null; + if( version > 0 ) + break; + } + } + + if( br == null ) + this.br = new BinaryReader( s ); + + switch( version ) + { + case 1: + // Verion 1 of gats + if( size == -1 ) + size = IPAddress.NetworkToHostOrder( br.ReadInt32() )-5; + byte[] buf = new byte[4096]; + while( ReadBuf.Length < size ) + { + int goal = (int)(size-ReadBuf.Length); + if( goal > 4096 ) + goal = 4096; + int amnt = s.Read( buf, 0, goal ); + if( amnt <= 0 ) + return null; + ReadBuf.Write( buf, 0, amnt ); + } + ReadBuf.Seek( 0, SeekOrigin.Begin ); + GatsObject ret = GatsObject.Read( ReadBuf ); + ReadBuf.SetLength( 0 ); + size = version = -1; + return ret; + } + + return null; + } + + public void WriteObject( GatsObject obj ) + { + MemoryStream ms = new MemoryStream(); + obj.Write( ms ); + + if( bw == null ) + this.bw = new BinaryWriter( s ); + bw.Write( (byte)1 ); + bw.Write( IPAddress.HostToNetworkOrder( (int)ms.Length+5 ) ); + bw.Write( ms.GetBuffer(), 0, (int)ms.Length ); + } + } +} + diff --git a/cs-dotnet/src/tests/packet.cs b/cs-dotnet/src/tests/packet.cs new file mode 100644 index 0000000..a572505 --- /dev/null +++ b/cs-dotnet/src/tests/packet.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; +using Com.Xagasoft.Gats; + +public class Test +{ + public static void Write( GatsObject o, Stream s ) + { + GatsStream gs = new GatsStream( s ); + gs.WriteObject( o ); + Console.WriteLine("Wrote: " + o ); + } + + public static GatsObject Read( Stream s ) + { + GatsStream gs = new GatsStream( s ); + GatsObject o = gs.ReadObject(); + if( o == null ) + Console.WriteLine("Nothing Read"); + else + { + Console.WriteLine("Read type: " + o.GetType() ); + Console.WriteLine("Read value: " + o ); + } + return o; + } + + public static void Main() + { + MemoryStream ms = new MemoryStream(); + GatsDictionary d = new GatsDictionary(); + GatsList l = new GatsList(); + l.Add( new GatsFloat( Math.PI ) ); + l.Add( new GatsInteger( 1337 ) ); + l.Add( new GatsString("Hello") ); + d.Add("list", l ); + d.Add("int", new GatsInteger( 998877 ) ); + d.Add("float", new GatsFloat( 87.332 ) ); + d.Add("string", new GatsString("Yup, a string") ); + d.Add("bool", new GatsBoolean( false ) ); + Write( d, ms ); + ms.Seek( 0, SeekOrigin.Begin ); + Read( ms ); + + try + { + FileStream fs = new FileStream("packet.gats", FileMode.Open, + FileAccess.Read ); + Console.WriteLine("===Reading from file==="); + GatsStream gs = new GatsStream( fs ); + GatsObject o = null; + do + { + o = gs.ReadObject(); + if( o == null ) + Console.WriteLine("Nothing Read"); + else + { + Console.WriteLine("Read type: " + o.GetType() ); + Console.WriteLine("Read value: " + o ); + } + } while( o != null ); + } + catch( Exception e ) + { + Console.WriteLine("Can't test files: " + e.Message ); + Console.WriteLine( e ); + } + } +} -- cgit v1.2.3