diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-11-15 22:24:09 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-11-15 22:24:09 +0000 |
commit | 052cbfd922a9512d1143fc10f5ab4c82b5470bcc (patch) | |
tree | 3d580e434f3efc8e069c5fc9de68c60cf905f75c | |
parent | 862c485d46f3f8e992a9ca9fea40fb33f0b43959 (diff) | |
download | libgats-052cbfd922a9512d1143fc10f5ab4c82b5470bcc.tar.gz libgats-052cbfd922a9512d1143fc10f5ab4c82b5470bcc.tar.bz2 libgats-052cbfd922a9512d1143fc10f5ab4c82b5470bcc.tar.xz libgats-052cbfd922a9512d1143fc10f5ab4c82b5470bcc.zip |
Ok, Packet based stream reading/writing works and is compliant.
Diffstat (limited to '')
-rw-r--r-- | cs-dotnet/src/gatsstream.cs | 79 | ||||
-rw-r--r-- | cs-dotnet/src/tests/packet.cs | 70 |
2 files changed, 149 insertions, 0 deletions
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 @@ | |||
1 | using System.IO; | ||
2 | using System.Net; | ||
3 | using System; | ||
4 | |||
5 | namespace Com.Xagasoft.Gats | ||
6 | { | ||
7 | public class GatsStream | ||
8 | { | ||
9 | private Stream s; | ||
10 | private MemoryStream ReadBuf; | ||
11 | private int size = -1; | ||
12 | private int version; | ||
13 | private BinaryWriter bw = null; | ||
14 | private BinaryReader br = null; | ||
15 | |||
16 | public GatsStream( Stream s ) | ||
17 | { | ||
18 | this.s = s; | ||
19 | this.ReadBuf = new MemoryStream(); | ||
20 | } | ||
21 | |||
22 | public GatsObject ReadObject() | ||
23 | { | ||
24 | if( size == -1 ) | ||
25 | { | ||
26 | for(;;) | ||
27 | { | ||
28 | version = s.ReadByte(); | ||
29 | if( version == -1 ) | ||
30 | return null; | ||
31 | if( version > 0 ) | ||
32 | break; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | if( br == null ) | ||
37 | this.br = new BinaryReader( s ); | ||
38 | |||
39 | switch( version ) | ||
40 | { | ||
41 | case 1: | ||
42 | // Verion 1 of gats | ||
43 | if( size == -1 ) | ||
44 | size = IPAddress.NetworkToHostOrder( br.ReadInt32() )-5; | ||
45 | byte[] buf = new byte[4096]; | ||
46 | while( ReadBuf.Length < size ) | ||
47 | { | ||
48 | int goal = (int)(size-ReadBuf.Length); | ||
49 | if( goal > 4096 ) | ||
50 | goal = 4096; | ||
51 | int amnt = s.Read( buf, 0, goal ); | ||
52 | if( amnt <= 0 ) | ||
53 | return null; | ||
54 | ReadBuf.Write( buf, 0, amnt ); | ||
55 | } | ||
56 | ReadBuf.Seek( 0, SeekOrigin.Begin ); | ||
57 | GatsObject ret = GatsObject.Read( ReadBuf ); | ||
58 | ReadBuf.SetLength( 0 ); | ||
59 | size = version = -1; | ||
60 | return ret; | ||
61 | } | ||
62 | |||
63 | return null; | ||
64 | } | ||
65 | |||
66 | public void WriteObject( GatsObject obj ) | ||
67 | { | ||
68 | MemoryStream ms = new MemoryStream(); | ||
69 | obj.Write( ms ); | ||
70 | |||
71 | if( bw == null ) | ||
72 | this.bw = new BinaryWriter( s ); | ||
73 | bw.Write( (byte)1 ); | ||
74 | bw.Write( IPAddress.HostToNetworkOrder( (int)ms.Length+5 ) ); | ||
75 | bw.Write( ms.GetBuffer(), 0, (int)ms.Length ); | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | |||
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 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using Com.Xagasoft.Gats; | ||
4 | |||
5 | public class Test | ||
6 | { | ||
7 | public static void Write( GatsObject o, Stream s ) | ||
8 | { | ||
9 | GatsStream gs = new GatsStream( s ); | ||
10 | gs.WriteObject( o ); | ||
11 | Console.WriteLine("Wrote: " + o ); | ||
12 | } | ||
13 | |||
14 | public static GatsObject Read( Stream s ) | ||
15 | { | ||
16 | GatsStream gs = new GatsStream( s ); | ||
17 | GatsObject o = gs.ReadObject(); | ||
18 | if( o == null ) | ||
19 | Console.WriteLine("Nothing Read"); | ||
20 | else | ||
21 | { | ||
22 | Console.WriteLine("Read type: " + o.GetType() ); | ||
23 | Console.WriteLine("Read value: " + o ); | ||
24 | } | ||
25 | return o; | ||
26 | } | ||
27 | |||
28 | public static void Main() | ||
29 | { | ||
30 | MemoryStream ms = new MemoryStream(); | ||
31 | GatsDictionary d = new GatsDictionary(); | ||
32 | GatsList l = new GatsList(); | ||
33 | l.Add( new GatsFloat( Math.PI ) ); | ||
34 | l.Add( new GatsInteger( 1337 ) ); | ||
35 | l.Add( new GatsString("Hello") ); | ||
36 | d.Add("list", l ); | ||
37 | d.Add("int", new GatsInteger( 998877 ) ); | ||
38 | d.Add("float", new GatsFloat( 87.332 ) ); | ||
39 | d.Add("string", new GatsString("Yup, a string") ); | ||
40 | d.Add("bool", new GatsBoolean( false ) ); | ||
41 | Write( d, ms ); | ||
42 | ms.Seek( 0, SeekOrigin.Begin ); | ||
43 | Read( ms ); | ||
44 | |||
45 | try | ||
46 | { | ||
47 | FileStream fs = new FileStream("packet.gats", FileMode.Open, | ||
48 | FileAccess.Read ); | ||
49 | Console.WriteLine("===Reading from file==="); | ||
50 | GatsStream gs = new GatsStream( fs ); | ||
51 | GatsObject o = null; | ||
52 | do | ||
53 | { | ||
54 | o = gs.ReadObject(); | ||
55 | if( o == null ) | ||
56 | Console.WriteLine("Nothing Read"); | ||
57 | else | ||
58 | { | ||
59 | Console.WriteLine("Read type: " + o.GetType() ); | ||
60 | Console.WriteLine("Read value: " + o ); | ||
61 | } | ||
62 | } while( o != null ); | ||
63 | } | ||
64 | catch( Exception e ) | ||
65 | { | ||
66 | Console.WriteLine("Can't test files: " + e.Message ); | ||
67 | Console.WriteLine( e ); | ||
68 | } | ||
69 | } | ||
70 | } | ||