public int Marshal(byte[] b, int n) { Gobuf.WriteUvarint((ulong)this.IntArray.Count, b, ref n); for (var i1 = 0; i1 < this.IntArray.Count; i1++) { Gobuf.WriteVarint(this.IntArray[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.UintArray.Count, b, ref n); for (var i1 = 0; i1 < this.UintArray.Count; i1++) { Gobuf.WriteUvarint(this.UintArray[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Int8Array.Count, b, ref n); for (var i1 = 0; i1 < this.Int8Array.Count; i1++) { b[n++] = (byte)this.Int8Array[i1]; } Gobuf.WriteBytes(this.Uint8Array, b, ref n); Gobuf.WriteUvarint((ulong)this.Int16Array.Count, b, ref n); for (var i1 = 0; i1 < this.Int16Array.Count; i1++) { Gobuf.WriteUint16((ushort)this.Int16Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint16Array.Count, b, ref n); for (var i1 = 0; i1 < this.Uint16Array.Count; i1++) { Gobuf.WriteUint16((ushort)this.Uint16Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Int32Array.Count, b, ref n); for (var i1 = 0; i1 < this.Int32Array.Count; i1++) { Gobuf.WriteUint32((uint)this.Int32Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint32Array.Count, b, ref n); for (var i1 = 0; i1 < this.Uint32Array.Count; i1++) { Gobuf.WriteUint32((uint)this.Uint32Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Int64Array.Count, b, ref n); for (var i1 = 0; i1 < this.Int64Array.Count; i1++) { Gobuf.WriteUint64((ulong)this.Int64Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint64Array.Count, b, ref n); for (var i1 = 0; i1 < this.Uint64Array.Count; i1++) { Gobuf.WriteUint64((ulong)this.Uint64Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Float32Array.Count, b, ref n); for (var i1 = 0; i1 < this.Float32Array.Count; i1++) { Gobuf.WriteFloat32(this.Float32Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.Float64Array.Count, b, ref n); for (var i1 = 0; i1 < this.Float64Array.Count; i1++) { Gobuf.WriteFloat64(this.Float64Array[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.StringArray.Count, b, ref n); for (var i1 = 0; i1 < this.StringArray.Count; i1++) { Gobuf.WriteString(this.StringArray[i1], b, ref n); } Gobuf.WriteUvarint((ulong)this.BoolArray.Count, b, ref n); for (var i1 = 0; i1 < this.BoolArray.Count; i1++) { b[n++] = this.BoolArray[i1] ? (byte)1 : (byte)0; } return(n); }
public int Marshal(byte[] b, int n) { Gobuf.WriteUvarint((ulong)this.IntMap.Count, b, ref n); foreach (var item1 in this.IntMap) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteVarint(item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.UintMap.Count, b, ref n); foreach (var item1 in this.UintMap) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUvarint(item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Int8Map.Count, b, ref n); foreach (var item1 in this.Int8Map) { Gobuf.WriteVarint(item1.Key, b, ref n); b[n++] = (byte)item1.Value; } Gobuf.WriteUvarint((ulong)this.Uint8Map.Count, b, ref n); foreach (var item1 in this.Uint8Map) { Gobuf.WriteVarint(item1.Key, b, ref n); b[n++] = (byte)item1.Value; } Gobuf.WriteUvarint((ulong)this.Int16Map.Count, b, ref n); foreach (var item1 in this.Int16Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint16((ushort)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint16Map.Count, b, ref n); foreach (var item1 in this.Uint16Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint16((ushort)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Int32Map.Count, b, ref n); foreach (var item1 in this.Int32Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint32((uint)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint32Map.Count, b, ref n); foreach (var item1 in this.Uint32Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint32((uint)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Int64Map.Count, b, ref n); foreach (var item1 in this.Int64Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint64((ulong)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Uint64Map.Count, b, ref n); foreach (var item1 in this.Uint64Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteUint64((ulong)item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Float32Map.Count, b, ref n); foreach (var item1 in this.Float32Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteFloat32(item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.Float64Map.Count, b, ref n); foreach (var item1 in this.Float64Map) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteFloat64(item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.StringMap.Count, b, ref n); foreach (var item1 in this.StringMap) { Gobuf.WriteVarint(item1.Key, b, ref n); Gobuf.WriteString(item1.Value, b, ref n); } Gobuf.WriteUvarint((ulong)this.BoolMap.Count, b, ref n); foreach (var item1 in this.BoolMap) { Gobuf.WriteVarint(item1.Key, b, ref n); b[n++] = item1.Value ? (byte)1 : (byte)0; } return(n); }
public void TestCommunication() { var scalar = new Scalar(); scalar.Byte = System.Byte.MaxValue; scalar.Int = System.Int64.MaxValue; scalar.Uint = System.UInt64.MaxValue; scalar.Int8 = System.SByte.MaxValue; scalar.Uint8 = System.Byte.MaxValue; scalar.Int16 = System.Int16.MaxValue; scalar.Uint16 = System.UInt16.MaxValue; scalar.Int32 = System.Int32.MaxValue; scalar.Uint32 = System.UInt32.MaxValue; scalar.Int64 = System.Int64.MaxValue; scalar.Uint64 = System.UInt64.MaxValue; scalar.Float32 = System.Single.MaxValue; scalar.Float64 = System.Double.MaxValue; scalar.String = "test string content"; scalar.Bytes = new byte[] { 1, 2, 3, 4 }; scalar.Bool = true; var msg1 = new Message(); msg1.Scalar = scalar; msg1.ScalarPtr = scalar; msg1.ScalarArray.Add(scalar); msg1.ScalarMap.Add(1, scalar); var body = new byte[msg1.Size()]; var size = msg1.Marshal(body, 0); Assert.Equal(size, body.Length); var head = new byte[4]; int offset = 0; Gobuf.WriteUint32((uint)size, head, ref offset); var conn = new TcpClient(); var task = conn.ConnectAsync("127.0.0.1", 12345); task.Wait(); Assert.True(conn.Connected); var stream = conn.GetStream(); stream.Write(head, 0, 4); stream.Write(body, 0, body.Length); offset = 0; while (offset != 4) { offset += stream.Read(head, offset, 4 - offset); } offset = 0; size = (int)Gobuf.ReadUint32(head, ref offset); Assert.Equal(size, body.Length); offset = 0; while (offset != body.Length) { offset += stream.Read(body, offset, body.Length - offset); } var msg2 = new Message(); size = msg2.Unmarshal(body, 0); Assert.Equal(size, body.Length); check(msg1.Scalar, msg2.Scalar); check(msg1.ScalarPtr, msg2.ScalarPtr); check(msg1.ScalarArray[0], msg2.ScalarArray[0]); check(msg1.ScalarMap[1], msg2.ScalarMap[1]); }