annotate ProtocolBuffers.cs @ 28:3bfb3ee64362

Make XmlSocket useable stand-alone
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 17 Apr 2013 16:11:09 +0200
parents 5d9a7186c9f7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 using System.IO;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 using System.Text;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 namespace UCIS.ProtocolBuffers {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 public interface IPBReader {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7 void Reset();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 Boolean NextField();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 int FieldNumber { get; }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 int WireType { get; }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 Int64 GetVarint();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 Int64 GetFixed64();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 Byte[] GetBytes();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 String GetString();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 IPBReader GetMessageReader();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 Int32 GetFixed32();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 public interface IPBWriter {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19 void WriteVarint(int fieldNumber, Int64 value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 void WriteFixed64(int fieldNumber, Int64 value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 void WriteBytes(int fieldNumber, Byte[] bytes);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22 void WriteBytes(int fieldNumber, Byte[] bytes, int offset, int count);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 void WriteFixed32(int fieldNumber, Int32 value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 public interface IPBMessage {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 void Decode(IPBReader r);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
27 void Encode(IPBWriter w);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
28 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 public class ArrayPBReader : IPBReader {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 Byte[] buffer;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31 int offset, length, index;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 int currentField;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33 Boolean hasCurrentField;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 int currentFieldLength;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
35
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
36 public ArrayPBReader(Byte[] buffer) : this(buffer, 0, buffer.Length) { }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
37 public ArrayPBReader(Byte[] buffer, int offset, int length) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 this.buffer = buffer;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 this.offset = offset;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40 this.length = length;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 Reset();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 public void Reset() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
45 index = 0;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
46 hasCurrentField = false;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
49 public bool NextField() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50 if (hasCurrentField) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 index += currentFieldLength;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52 hasCurrentField = false;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
54 if (index >= length) return false;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 index += ReadVarIntTo(out currentField);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 int dummy;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57 switch (WireType) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 case 0: currentFieldLength = ReadVarIntTo(out dummy); ; break; //Varint
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 case 1: currentFieldLength = 8; break; //64bit
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60 case 2: index += ReadVarIntTo(out currentFieldLength); break; //Bytes
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 case 5: currentFieldLength = 4; break; //32bit
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62 default: throw new InvalidDataException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
64 if (index + currentFieldLength > length) throw new InvalidDataException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
65 hasCurrentField = true;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 return true;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
67 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
68
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
69 private int ReadVarIntTo(out int v) {
17
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
70 long vl;
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
71 int l = ReadVarIntTo(out vl);
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
72 v = (int)vl;
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
73 return l;
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
74 }
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
75 private int ReadVarIntTo(out long v) {
11
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
76 v = 0;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77 int h = 0;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
78 int b = 0x80;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 int l = 0;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 while ((b & 0x80) != 0) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 b = buffer[offset + index + l];
17
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
82 v |= ((long)b & 0x7FL) << h;
11
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
83 h += 7;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
84 l++;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
85 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
86 return l;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
88
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
89 public int FieldNumber { get { return (int)((UInt32)currentField >> 3); } }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 public int WireType { get { return currentField & 7; } }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 public long GetVarint() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 if (!hasCurrentField || WireType != 0) throw new InvalidOperationException();
17
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
94 long v;
11
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 ReadVarIntTo(out v);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 return v;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99 public long GetFixed64() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 if (!hasCurrentField || WireType != 1) throw new InvalidOperationException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 int i = offset + index;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 return ((long)buffer[i + 0] << 0) | ((long)buffer[i + 1] << 8) | ((long)buffer[i + 2] << 16) | ((long)buffer[i + 3] << 24) |
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
103 ((long)buffer[i + 4] << 32) | ((long)buffer[i + 5] << 40) | ((long)buffer[i + 6] << 48) | ((long)buffer[i + 7] << 56);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
106 public byte[] GetBytes() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
107 if (!hasCurrentField || WireType != 2) throw new InvalidOperationException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 Byte[] bytes = new Byte[currentFieldLength];
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
109 Buffer.BlockCopy(buffer, offset + index, bytes, 0, currentFieldLength);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
110 return bytes;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
111 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
112
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
113 public string GetString() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114 if (!hasCurrentField || WireType != 2) throw new InvalidOperationException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
115 return Encoding.UTF8.GetString(buffer, offset + index, currentFieldLength);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
118 public IPBReader GetMessageReader() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 if (!hasCurrentField || WireType != 2) throw new InvalidOperationException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 return new ArrayPBReader(buffer, offset + index, currentFieldLength);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
121 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
122
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
123 public int GetFixed32() {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 if (!hasCurrentField || WireType != 5) throw new InvalidOperationException();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
125 int i = offset + index;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 return ((int)buffer[i + 0] << 0) | ((int)buffer[i + 1] << 8) | ((int)buffer[i + 2] << 16) | ((int)buffer[i + 3] << 24);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
127 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
128 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
129 public class StreamPBWriter : IPBWriter {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 Stream stream;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
131
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
132 public StreamPBWriter(Stream stream) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
133 this.stream = stream;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
134 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
135
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
136 public static Byte[] Encode(IPBMessage message) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
137 using (MemoryStream ms = new MemoryStream()) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
138 message.Encode(new StreamPBWriter(ms));
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
139 return ms.ToArray();
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
140 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
141 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
143 public static int EncodeToStreamBuffered(Stream stream, IPBMessage message) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
144 using (MemoryStream ms = new MemoryStream()) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
145 message.Encode(new StreamPBWriter(ms));
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
146 ms.WriteTo(stream);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
147 return (int)ms.Length;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
148 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
149 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
150
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
151 private void WriteVarint(long value) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
152 while ((value & ~0x7fL) != 0) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
153 stream.WriteByte((Byte)(0x80 | value));
17
5d9a7186c9f7 ProtocolBuffers: fix handling of 64 bit varint
Ivo Smits <Ivo@UCIS.nl>
parents: 11
diff changeset
154 value = (long)((ulong)value >> 7);
11
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
155 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
156 stream.WriteByte((Byte)value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
157 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
158 private void WriteTag(int fieldNumber, int wireType) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
159 WriteVarint((fieldNumber << 3) | wireType);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
160 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
161
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
162 public void WriteVarint(int fieldNumber, long value) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
163 WriteTag(fieldNumber, 0);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
164 WriteVarint(value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
165 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
166
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
167 public void WriteFixed64(int fieldNumber, long value) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
168 WriteTag(fieldNumber, 1);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
169 for (int i = 0; i < 8; i++) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
170 stream.WriteByte((Byte)value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
171 value >>= 8;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
172 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
173 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
174
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
175 public void WriteBytes(int fieldNumber, byte[] bytes) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
176 WriteBytes(fieldNumber, bytes, 0, bytes.Length);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
177 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
178
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
179 public void WriteBytes(int fieldNumber, byte[] bytes, int offset, int count) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
180 WriteTag(fieldNumber, 2);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
181 WriteVarint(count);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
182 stream.Write(bytes, offset, count);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
183 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
184
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
185 public void WriteFixed32(int fieldNumber, int value) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
186 WriteTag(fieldNumber, 5);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
187 for (int i = 0; i < 4; i++) {
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
188 stream.WriteByte((Byte)value);
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
189 value >>= 8;
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
190 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
191 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
192 }
2e0ff842aa4a Added Protocol Buffers encoding/decoding code
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
193 }