comparison Pml/RW/PmlBinaryRW.cs @ 1:28dc7d535036

Small improvements, introduced PacketStream
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 07 Jan 2013 16:43:28 +0100
parents 3ab940a0c7a0
children
comparison
equal deleted inserted replaced
0:3ab940a0c7a0 1:28dc7d535036
32 get { return pWriter; } 32 get { return pWriter; }
33 } 33 }
34 } 34 }
35 35
36 public class PmlBinaryWriter : IPmlWriter { 36 public class PmlBinaryWriter : IPmlWriter {
37 //private BinaryWriter pWriter;
38 private Stream pStream; 37 private Stream pStream;
39 private Encoding pEncoding; 38 private Encoding pEncoding;
40 39
41 /*public PmlBinaryWriter(BinaryWriter Writer) {
42 pWriter = Writer;
43 }*/
44 public PmlBinaryWriter(Stream Stream) { 40 public PmlBinaryWriter(Stream Stream) {
45 //pWriter = new BinaryWriter(Stream);
46 pStream = Stream; 41 pStream = Stream;
47 pEncoding = Encoding.UTF8; 42 pEncoding = Encoding.UTF8;
48 } 43 }
49 public PmlBinaryWriter(Stream Stream, Encoding Encoding) { 44 public PmlBinaryWriter(Stream Stream, Encoding Encoding) {
50 //pWriter = new BinaryWriter(Stream, Encoding);
51 pStream = Stream; 45 pStream = Stream;
52 pEncoding = Encoding; 46 pEncoding = Encoding;
53 } 47 }
54
55 /*public BinaryWriter BaseWriter {
56 get { return pWriter; }
57 set { pWriter = value; }
58 }*/
59 48
60 public void WriteMessage(PmlElement Message) { 49 public void WriteMessage(PmlElement Message) {
61 MemoryStream stream = new MemoryStream(); 50 MemoryStream stream = new MemoryStream();
62 BinaryWriter writer = new BinaryWriter(stream, pEncoding); 51 BinaryWriter writer = new BinaryWriter(stream, pEncoding);
63 WriteMessageTo(Message, writer); 52 WriteMessageTo(Message, writer);
65 stream.WriteTo(pStream); 54 stream.WriteTo(pStream);
66 pStream.Flush(); 55 pStream.Flush();
67 } 56 }
68 } 57 }
69 58
59 public static Byte[] EncodeMessage(PmlElement message) {
60 MemoryStream stream = new MemoryStream();
61 BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
62 WriteMessageTo(message, writer);
63 return stream.ToArray();
64 }
65
70 public static void WriteMessageTo(PmlElement Message, BinaryWriter Writer) { 66 public static void WriteMessageTo(PmlElement Message, BinaryWriter Writer) {
71 lock (Writer) { 67 lock (Writer) {
72 Writer.Write((byte)255); 68 Writer.Write((byte)255);
73 WriteElementTo(Message, Writer); 69 WriteElementTo(Message, Writer);
74 Writer.Write((byte)255); 70 Writer.Write((byte)255);
75 Writer.Flush(); 71 Writer.Flush();
76 } 72 }
77 } 73 }
78 74
79 private static void WriteElementTo(PmlElement Element, BinaryWriter Writer) { 75 private static void WriteElementTo(PmlElement Element, BinaryWriter Writer) {
80 //byte[] Buffer = null;
81 if (Element == null) { 76 if (Element == null) {
82 //Writer.Write((byte)PmlElementType.Null);
83 Writer.Write((byte)0); 77 Writer.Write((byte)0);
84 return; 78 return;
85 } 79 }
86 //Writer.Write((byte)Element.Type);
87 switch (Element.Type) { 80 switch (Element.Type) {
88 case PmlType.Null: 81 case PmlType.Null:
89 Writer.Write((byte)0); 82 Writer.Write((byte)0);
90 break; 83 break;
91 case PmlType.Dictionary: 84 case PmlType.Dictionary:
117 } break; 110 } break;
118 case PmlType.String: 111 case PmlType.String:
119 Writer.Write((byte)11); 112 Writer.Write((byte)11);
120 string Str = Element.ToString(); 113 string Str = Element.ToString();
121 if (Str == null) { 114 if (Str == null) {
122 Writer.Write(""); 115 Writer.Write(String.Empty);
123 } else { 116 } else {
124 Writer.Write(Str); 117 Writer.Write(Str);
125 } 118 }
126 119
127 break; 120 break;
162 set { pReader = value; } 155 set { pReader = value; }
163 } 156 }
164 157
165 public PmlElement ReadMessage() { 158 public PmlElement ReadMessage() {
166 return ReadMessageFrom(pReader); 159 return ReadMessageFrom(pReader);
160 }
161
162 public static PmlElement DecodeMessage(Byte[] message) {
163 using (MemoryStream ms = new MemoryStream(message)) {
164 using (BinaryReader reader = new BinaryReader(ms, Encoding.UTF8)) {
165 return ReadMessageFrom(reader);
166 }
167 }
167 } 168 }
168 169
169 public static PmlElement ReadMessageFrom(BinaryReader Reader) { 170 public static PmlElement ReadMessageFrom(BinaryReader Reader) {
170 PmlElement Element = null; 171 PmlElement Element = null;
171 lock (Reader) { 172 lock (Reader) {
211 case 20: { 212 case 20: {
212 byte B = Reader.ReadByte(); 213 byte B = Reader.ReadByte();
213 if (B == 0) return new PmlInteger(Reader.ReadUInt64()); 214 if (B == 0) return new PmlInteger(Reader.ReadUInt64());
214 else if (B == 1) return new PmlInteger(Reader.ReadInt64()); 215 else if (B == 1) return new PmlInteger(Reader.ReadInt64());
215 else return null; 216 else return null;
216
217 } 217 }
218 default: 218 default:
219 throw new Exception("Unknown PML type code " + EType.ToString()); 219 throw new Exception("Unknown PML type code " + EType.ToString());
220 } 220 }
221 } 221 }