Skip to content

Commit e7939bb

Browse files
abradymeta-codesync[bot]
authored andcommitted
Add Thrift C# runtime library
Summary: Add C# runtime library for Thrift binary and compact protocol serialization Introduces a production-ready C# Thrift runtime library supporting both binary and compact protocols, enabling Unity and other C# applications to serialize/deserialize Thrift data. Key components: - Protocol readers/writers: ThriftBinaryReader/Writer and ThriftCompactReader/Writer - Protocol-agnostic interfaces: IThriftProtocolReader/Writer for polymorphic usage - Core types: IThriftSerializable interface, ThriftWireType enum, ThriftProtocolException - Helper utilities: ThriftProtocolHelper with Skip(), ReadValue<T>(), WriteValue<T>() for shared logic Implementation details: - Binary protocol: Big-endian encoding for all multi-byte values - Compact protocol: Varint encoding, zigzag for signed integers, delta field IDs, inline booleans, compressed collection headers - Security: Configurable string size limits, nesting depth protection (64 max), strict UTF-8 validation - Testing: Comprehensive protocol-agnostic round-trip tests plus format-specific wire encoding tests Mirrored via dirsync between xplat/thrift/lib/csharp/ and fbcode/thrift/lib/csharp/ to support both build environments. Reviewed By: vitaut Differential Revision: D94160930 fbshipit-source-id: 21289b4bad3aaecec2ce5a53526b8d3dbb7bd47f
1 parent 478289c commit e7939bb

7 files changed

Lines changed: 832 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Collections.Generic;
18+
19+
namespace FBThrift
20+
{
21+
/// <summary>
22+
/// Protocol-agnostic interface for reading Thrift data.
23+
/// Implemented by ThriftBinaryReader and ThriftCompactReader.
24+
/// </summary>
25+
public interface IThriftProtocolReader
26+
{
27+
/// <summary>
28+
/// Gets or sets the maximum allowed size for string and binary reads.
29+
/// When set to 0 (default), no configurable limit is enforced, but reads
30+
/// are still validated against remaining stream bytes.
31+
/// </summary>
32+
int StringSizeLimit { get; set; }
33+
34+
(ThriftWireType fieldType, short fieldId) ReadFieldBegin();
35+
bool ReadBool();
36+
sbyte ReadByte();
37+
short ReadI16();
38+
int ReadI32();
39+
long ReadI64();
40+
float ReadFloat();
41+
double ReadDouble();
42+
string ReadString();
43+
byte[] ReadBinary();
44+
(ThriftWireType elemType, int size) ReadListBegin();
45+
(ThriftWireType elemType, int size) ReadSetBegin();
46+
(ThriftWireType keyType, ThriftWireType valType, int size) ReadMapBegin();
47+
T ReadStruct<T>() where T : IThriftSerializable, new();
48+
HashSet<T> ReadSet<T>();
49+
List<T> ReadList<T>();
50+
Dictionary<K, V> ReadMap<K, V>();
51+
void Skip(ThriftWireType fieldType, short? fieldId = null);
52+
T ReadValue<T>();
53+
}
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
20+
namespace FBThrift
21+
{
22+
/// <summary>
23+
/// Protocol-agnostic interface for writing Thrift data.
24+
/// Implemented by ThriftBinaryWriter and ThriftCompactWriter.
25+
/// </summary>
26+
public interface IThriftProtocolWriter
27+
{
28+
void WriteFieldBegin(ThriftWireType fieldType, short fieldId);
29+
void WriteFieldStop();
30+
void WriteBool(bool value);
31+
void WriteByte(sbyte value);
32+
void WriteI16(short value);
33+
void WriteI32(int value);
34+
void WriteI64(long value);
35+
void WriteFloat(float value);
36+
void WriteDouble(double value);
37+
void WriteString(string value);
38+
void WriteBinary(byte[] value);
39+
void WriteListBegin(ThriftWireType elemType, int size);
40+
void WriteSetBegin(ThriftWireType elemType, int size);
41+
void WriteMapBegin(ThriftWireType keyType, ThriftWireType valType, int size);
42+
void WriteStruct(IThriftSerializable value);
43+
void WriteSet<T>(IEnumerable<T> value);
44+
void WriteList<T>(IEnumerable<T> value);
45+
void WriteMap<K, V>(IDictionary<K, V> value);
46+
void WriteValue<T>(T value);
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
namespace FBThrift
18+
{
19+
/// <summary>
20+
/// Interface for types that can be serialized and deserialized
21+
/// using any Thrift protocol (Binary, Compact, etc.).
22+
/// </summary>
23+
public interface IThriftSerializable
24+
{
25+
/// <summary>
26+
/// Writes this object to the specified Thrift protocol writer.
27+
/// </summary>
28+
/// <param name="writer">The protocol writer to write to.</param>
29+
void Write(IThriftProtocolWriter writer);
30+
31+
/// <summary>
32+
/// Reads this object's fields from the specified Thrift protocol reader.
33+
/// </summary>
34+
/// <param name="reader">The protocol reader to read from.</param>
35+
void Read(IThriftProtocolReader reader);
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System;
18+
19+
namespace FBThrift
20+
{
21+
/// <summary>
22+
/// Exception thrown when a Thrift protocol error occurs during serialization or deserialization.
23+
/// </summary>
24+
public class ThriftProtocolException : Exception
25+
{
26+
public ThriftProtocolException(string message)
27+
: base(message)
28+
{
29+
}
30+
31+
public ThriftProtocolException(string message, Exception innerException)
32+
: base(message, innerException)
33+
{
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)