aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src/gatsstream.cs
diff options
context:
space:
mode:
Diffstat (limited to 'cs-dotnet/src/gatsstream.cs')
-rw-r--r--cs-dotnet/src/gatsstream.cs79
1 files changed, 79 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 @@
1using System.IO;
2using System.Net;
3using System;
4
5namespace 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