Protocol Buffers

Download Report

Transcript Protocol Buffers

Protocol Buffers
Portable binary serialization, the
Google way
What problem?
For high volume work, no ideal serializer:
• Xml/data-contracts: (relatively) expensive to
process; large due to repeated tags
• Soap: as xml, but more
• BinaryFormatter (/NDCS): proprietary closed
[non-]standard
• Bespoke: lots of work; lots of potential for
error
What is protobuf?
Protocol Buffers defines two things:
• A compact binary serialization format (pb)
• A text-based descriptor language (.proto)
Implementation specific:
• Runtime serialization library / code
• .proto parser / code generator
protobuf-net is one of 3 in-progress efforts for .NET
What is a .proto?
Yet another descriptor language...
message Test1 {
required int32 a = 1;
}
message Test3 {
optional Test1 c = 3;
}
service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}
So why bother?
• Compact:
– no string field names (contrast: XmlSerializer; DataContractSerializer)
– uses variable-length encoding where sensible
– minimal framing overhead
• Portable:
– cross-platform (contrast: BinaryFormatter)
– extensible (roundtrip-safe)
– (protobuf-net works with .NET 2.0; .NET 3.0; CF 2.0; CF 3.0; Silverlight
2.0; Mono)
• Fast:
– efficient to parse
Optimised for common scenarios, but doesn’t support every complex
worst-case scenario.
Look familiar?
Closely related to data/service-contracts
[DataContract]
public class Test1
{
[DataMember(Name="a", Order=1, IsRequired=true)]
public int A {get;set;}
}
[DataContract]
public class Test3
{
[DataMember(Name="c", Order=3, IsRequired=false)]
public Test1 C {get;set;}
}
[ServiceContract]
public interface ISearchService
{
[OperationContract]
SearchResponse Search(SearchRequest request);
}
Demo
Where can we use it?
• Communications
– Remoting
– WCF
– Sockets / http (proto-rpc?)
• Persistance
– File system
– Database BLOB
– SQL/CLR?
Existing data?
• Works with common frameworks:
– WCF
– LINQ-based data-contracts
– XmlType / XmlElement
• Can be used to replace, or in parallel with,
standard serializers:
– ISerializable
– IXmlSerializable
• Custom attributes for full control
– Can also be specified at the class (rather than
member) for use with partial classes