本文整理汇总了C#中SilentOrbit.ProtocolBuffers.ProtoMessage类的典型用法代码示例。如果您正苦于以下问题:C# ProtoMessage类的具体用法?C# ProtoMessage怎么用?C# ProtoMessage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ProtoMessage类属于SilentOrbit.ProtocolBuffers命名空间,在下文中一共展示了ProtoMessage类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: ParseEnum
static void ParseEnum(TokenReader tr, ProtoMessage parent, string package)
{
ProtoEnum me = new ProtoEnum(parent, package);
LocalParser.ParseComments(me, lastComment, tr);
me.ProtoName = tr.ReadNext();
parent.Enums.Add(me.ProtoName, me); //must be after .ProtoName is read
if (tr.ReadNext() != "{")
throw new ProtoFormatException("Expected: {", tr);
while (true)
{
string name = tr.ReadNext();
if (ParseComment(name))
continue;
if (name == "}")
return;
//Ignore options
if (name == "option")
{
ParseOption(tr, null);
lastComment.Clear();
continue;
}
ParseEnumValue(tr, me, name);
}
}
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:33,代码来源:ProtoParser.cs
示例2: GenerateClassSerializer
public void GenerateClassSerializer(ProtoMessage m)
{
if (options.NoGenerateImported && m.IsImported)
{
Console.Error.WriteLine("Skipping imported " + m.FullProtoName);
return;
}
if (m.OptionExternal || m.OptionType == "interface")
{
//Don't make partial class of external classes or interfaces
//Make separate static class for them
cw.Bracket(m.OptionAccess + " static class " + m.SerializerType);
}
else
{
if (options.SerializableAttributes)
cw.Attribute("System.Serializable");
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.SerializerType);
}
GenerateReader(m);
GenerateWriter(m);
foreach (ProtoMessage sub in m.Messages.Values)
{
cw.WriteLine();
GenerateClassSerializer(sub);
}
cw.EndBracket();
cw.WriteLine();
return;
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:33,代码来源:MessageSerializer.cs
示例3: GenerateProperties
/// <summary>
/// Generates the properties.
/// </summary>
/// <param name='template'>
/// if true it will generate only properties that are not included by default, because of the [generate=false] option.
/// </param>
static void GenerateProperties(ProtoMessage m, CodeWriter cw)
{
foreach (Field f in m.Fields.Values)
{
if (f.OptionExternal)
cw.WriteLine("//" + GenerateProperty(f) + " // Implemented by user elsewhere");
else
{
if (f.Comments != null)
cw.Summary(f.Comments);
cw.WriteLine(GenerateProperty(f));
cw.WriteLine();
}
}
//Wire format field ID
#if DEBUG
cw.Comment("ProtocolBuffers wire field id");
foreach (Field f in m.Fields.Values)
{
cw.WriteLine("public const int " + f.CsName + "FieldID = " + f.ID + ";");
}
#endif
}
开发者ID:4eJI0Be4ur,项目名称:ProtoBuf,代码行数:31,代码来源:MessageCode.cs
示例4: GenerateClassSerializer
public static void GenerateClassSerializer(ProtoMessage m, CodeWriter cw)
{
if (m.OptionExternal || m.OptionType == "interface")
{
//Don't make partial class of external classes or interfaces
//Make separate static class for them
cw.Bracket(m.OptionAccess + " static class " + m.SerializerType);
}
else
{
cw.Attribute("System.Serializable()");
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.SerializerType);
}
GenerateReader(m, cw);
GenerateWriter(m, cw);
foreach (ProtoMessage sub in m.Messages.Values)
{
cw.WriteLine();
GenerateClassSerializer(sub, cw);
}
cw.EndBracket();
cw.WriteLine();
return;
}
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:26,代码来源:MessageSerializer.cs
示例5: GenerateEnums
static void GenerateEnums(ProtoMessage m, CodeWriter cw)
{
foreach (ProtoEnum me in m.Enums.Values)
{
GenerateEnum(me, cw);
}
}
开发者ID:4eJI0Be4ur,项目名称:ProtoBuf,代码行数:7,代码来源:MessageCode.cs
示例6: PrepareMessage
void PrepareMessage(ProtoMessage m)
{
//Name of message and enums
m.CsType = GetCamelCase(m.ProtoName);
foreach (ProtoEnum e in m.Enums.Values)
{
e.CsType = GetCamelCase(e.ProtoName);
}
foreach (ProtoMessage sub in m.Messages.Values)
PrepareMessage(sub);
//Prepare fields
foreach (Field f in m.Fields.Values)
{
PrepareProtoType(m, f);
DetectNameClash(m, f);
if (f.OptionDefault != null)
{
if (f.ProtoType is ProtoBuiltin && ((ProtoBuiltin)f.ProtoType).ProtoName == "bytes")
throw new NotImplementedException();
if (f.ProtoType is ProtoMessage)
throw new ProtoFormatException("Message can't have a default", f.Source);
}
}
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:29,代码来源:ProtoPrepare.cs
示例7: GenerateClass
public void GenerateClass(ProtoMessage m)
{
if (options.NoGenerateImported && m.IsImported)
{
Console.Error.WriteLine("Skipping imported " + m.FullProtoName);
return;
}
//Do not generate class code for external classes
if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsType + " {}");
return;
}
//Default class
cw.Summary(m.Comments);
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.CsType);
if (options.GenerateDefaultConstructors)
GenerateCtorForDefaults(m);
GenerateEnums(m);
GenerateProperties(m);
//if(options.GenerateToString...
// ...
if (m.OptionPreserveUnknown)
{
cw.Summary("Values for unknown fields.");
cw.WriteLine("public List<global::SilentOrbit.ProtocolBuffers.KeyValue> PreservedFields;");
cw.WriteLine();
}
if (m.OptionTriggers)
{
cw.Comment("protected virtual void BeforeSerialize() {}");
cw.Comment("protected virtual void AfterDeserialize() {}");
cw.WriteLine();
}
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub);
cw.WriteLine();
}
cw.EndBracket();
return;
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:52,代码来源:MessageCode.cs
示例8: ProtoType
/// <summary>
/// Used by types within a namespace
/// </summary>
public ProtoType(ProtoMessage parent, string package)
: this()
{
if (this is ProtoCollection == false)
{
if (parent == null)
throw new ArgumentNullException("parent");
if (package == null)
throw new ArgumentNullException("package");
}
this.Parent = parent;
this.Package = package;
}
开发者ID:4eJI0Be4ur,项目名称:ProtoBuf,代码行数:16,代码来源:ProtoType.cs
示例9: ParseMessageFlags
static void ParseMessageFlags(ProtoMessage message, string flag)
{
switch (flag)
{
case "triggers":
message.OptionTriggers = true;
break;
case "preserveunknown":
message.OptionPreserveUnknown = true;
break;
case "external":
message.OptionExternal = true;
break;
default:
throw new NotImplementedException("Unknown option: " + flag);
}
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:17,代码来源:LocalParser.cs
示例10: GenerateClass
public static void GenerateClass(ProtoMessage m, CodeWriter cw, Options options)
{
//Do not generate class code for external classes
if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsType + " {}");
return;
}
//Default class
cw.Summary(m.Comments);
cw.Bracket(m.OptionAccess + " partial " + m.OptionType + " " + m.CsType);
GenerateEnums(m, cw);
GenerateProperties(m, cw);
//if(options.GenerateToString...
// ...
if (m.OptionPreserveUnknown)
{
cw.Summary("Values for unknown fields.");
cw.WriteLine("public List<global::SilentOrbit.ProtocolBuffers.KeyValue> PreservedFields;");
cw.WriteLine();
}
if (m.OptionTriggers)
{
cw.Comment("protected virtual void BeforeSerialize() {}");
cw.Comment("protected virtual void AfterDeserialize() {}");
cw.WriteLine();
}
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub, cw, options);
cw.WriteLine();
}
cw.EndBracket();
return;
}
开发者ID:4eJI0Be4ur,项目名称:ProtoBuf,代码行数:43,代码来源:MessageCode.cs
示例11: ParseMessageOption
static void ParseMessageOption(ProtoMessage message, string key, string value)
{
//Parse value
switch (key)
{
case "namespace":
message.OptionNamespace = value;
break;
case "access":
message.OptionAccess = value;
break;
case "type":
message.OptionType = value;
break;
case "buffer":
message.BufferSize = int.Parse(value);
break;
default:
throw new NotImplementedException("Unknown option: " + key);
}
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:21,代码来源:LocalParser.cs
示例12: DetectNameClash
/// <summary>
/// Detect field which have the same name as a submessage in the same message.
/// </summary>
/// <param name="m">Parent message</param>
/// <param name="f">Field to check</param>
void DetectNameClash(ProtoMessage m, Field f)
{
bool nameclash = false;
if (m.CsType == f.CsName)
nameclash = true;
foreach (var tm in m.Messages.Values)
if (tm.CsType == f.CsName)
nameclash = true;
foreach (var te in m.Enums.Values)
if (te.CsType == f.CsName)
nameclash = true;
foreach (var tf in m.Fields.Values)
{
if (tf == f)
continue;
if (tf.CsName == f.CsName)
nameclash = true;
}
if (nameclash == false)
return;
//Name clash
if (options.FixNameclash)
{
if (ConvertToCamelCase)
f.CsName += "Field";
else
f.CsName += "_field";
Console.Error.WriteLine("Warning: renamed field: " + m.FullCsType + "." + f.CsName);
//Make sure our change did not result in another name collission
DetectNameClash(m, f);
}
else
throw new ProtoFormatException("The field: " + m.FullCsType + "." + f.CsName +
" has the same name as a sibling class/enum type which is not allowed in C#. " +
"Use --fix-nameclash to automatically rename the field.", f.Source);
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:45,代码来源:ProtoPrepare.cs
示例13: GenerateClass
public static void GenerateClass(ProtoMessage m, CodeWriter cw)
{
//Do not generate class code for external classes
/*if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsNamespace + "_" + m.CsType + " {}");
return;
}*/
//Default class
cw.Summary(m.Comments);
cw.Bracket(m.OptionAccess + " " + m.OptionType + " " + m.CsNamespace + "_" + m.CsType);
GenerateEnums(m, cw);
GenerateProperties(m, cw);
if (m.OptionPreserveUnknown)
{
cw.Summary("Values for unknown fields.");
cw.WriteLine("public List<KeyValue> PreservedFields;");
cw.WriteLine();
}
if (m.OptionTriggers)
{
cw.Comment("protected virtual void BeforeSerialize() {}");
cw.Comment("protected virtual void AfterDeserialize() {}");
cw.WriteLine();
}
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub, cw);
cw.WriteLine();
}
cw.EndBracket();
return;
}
开发者ID:huodianyan,项目名称:CitoProtobuf,代码行数:40,代码来源:MessageCode.cs
示例14: SearchSubMessages
static ProtoType SearchSubMessages(ProtoMessage msg, string fullPath)
{
foreach (ProtoMessage sub in msg.Messages.Values)
{
if (fullPath == sub.FullProtoName)
return sub;
if (fullPath.StartsWith(sub.FullProtoName + "."))
{
ProtoType pt = SearchSubMessages(sub, fullPath);
if (pt != null)
return pt;
}
}
foreach (ProtoEnum subEnum in msg.Enums.Values)
{
if (fullPath == subEnum.FullProtoName)
return subEnum;
}
return null;
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:23,代码来源:Search.cs
示例15: GetProtoType
/// <summary>
/// Search for message in hierarchy
/// </summary>
public static ProtoType GetProtoType(ProtoMessage msg, string path)
{
//Search for message or enum
ProtoType pt;
//Search from one level up until a match is found
while (msg is ProtoCollection == false)
{
//Search sub messages
pt = SearchSubMessages(msg, msg.Package + "." + msg.ProtoName + "." + path);
if (pt != null)
return pt;
//Search siblings
pt = SearchSubMessages(msg.Parent, msg.Package + "." + path);
if (pt != null)
return pt;
msg = msg.Parent;
}
//Finally search for global namespace
return SearchSubMessages(msg, path);
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:27,代码来源:Search.cs
示例16: GenerateCtorForDefaults
void GenerateCtorForDefaults(ProtoMessage m)
{
// Collect all fields with default values.
var fieldsWithDefaults = new List<Field>();
foreach (Field field in m.Fields.Values)
{
if (field.OptionDefault != null)
{
fieldsWithDefaults.Add(field);
}
}
if (fieldsWithDefaults.Count > 0)
{
cw.Bracket("public " + m.CsType + "()");
foreach (var field in fieldsWithDefaults)
{
string formattedValue = field.FormatDefaultForTypeAssignment();
string line = string.Format("{0} = {1};", field.CsName, formattedValue);
cw.WriteLine(line);
}
cw.EndBracket();
}
}
开发者ID:StepWoodProductions,项目名称:ProtoBuf,代码行数:24,代码来源:MessageCode.cs
示例17: ParseMessage
static void ParseMessage(TokenReader tr, ProtoMessage parent, string package)
{
var msg = new ProtoMessage(parent, package);
LocalParser.ParseComments(msg, lastComment, tr);
msg.ProtoName = tr.ReadNext();
tr.ReadNextOrThrow("{");
try
{
while (ParseField(tr, msg))
continue;
}
catch (Exception e)
{
throw new ProtoFormatException(e.Message, e, tr);
}
parent.Messages.Add(msg.ProtoName, msg);
}
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:20,代码来源:ProtoParser.cs
示例18: ParseField
static bool ParseField(TokenReader tr, ProtoMessage m)
{
string rule = tr.ReadNext();
while (true)
{
if (ParseComment(rule) == false)
break;
rule = tr.ReadNext();
}
Field f = new Field(tr);
//Rule
switch (rule)
{
case ";":
lastComment.Clear();
return true;
case "}":
lastComment.Clear();
return false;
case "required":
f.Rule = FieldRule.Required;
break;
case "optional":
f.Rule = FieldRule.Optional;
break;
case "repeated":
f.Rule = FieldRule.Repeated;
break;
case "option":
//Save options
ParseOption(tr, m);
return true;
case "message":
ParseMessage(tr, m, m.Package + "." + m.ProtoName);
return true;
case "enum":
ParseEnum(tr, m, m.Package + "." + m.ProtoName);
return true;
case "extensions":
ParseExtensions(tr, m);
return true;
default:
throw new ProtoFormatException("unknown rule: " + rule, tr);
}
//Field comments
LocalParser.ParseComments(f, lastComment, tr);
//Type
f.ProtoTypeName = tr.ReadNext();
//Name
f.ProtoName = tr.ReadNext();
//ID
tr.ReadNextOrThrow("=");
f.ID = int.Parse(tr.ReadNext());
if (19000 <= f.ID && f.ID <= 19999)
throw new ProtoFormatException("Can't use reserved field ID 19000-19999", tr);
if (f.ID > (1 << 29) - 1)
throw new ProtoFormatException("Maximum field id is 2^29 - 1", tr);
//Add Field to message
m.Fields.Add(f.ID, f);
//Determine if extra options
string extra = tr.ReadNext();
if (extra == ";")
return true;
//Field options
if (extra != "[")
throw new ProtoFormatException("Expected: [ got " + extra, tr);
ParseFieldOptions(tr, f);
return true;
}
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:79,代码来源:ProtoParser.cs
示例19: ParseExtensions
static void ParseExtensions(TokenReader tr, ProtoMessage m)
{
//extensions 100 to max;
tr.ReadNext(); //100
tr.ReadNextOrThrow("to");
tr.ReadNext(); //number or max
tr.ReadNextOrThrow(";");
}
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:8,代码来源:ProtoParser.cs
示例20: GenerateReader
static void GenerateReader(ProtoMessage m, CodeWriter cw)
{
#region Helper Deserialize Methods
string refstr = (m.OptionType == "struct") ? "ref " : "";
if (m.OptionType != "interface")
{
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " Deserialize(Stream stream)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("Deserialize(stream, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " DeserializeLengthDelimited(Stream stream)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("DeserializeLengthDelimited(stream, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " DeserializeLength(Stream stream, int length)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("DeserializeLength(stream, length, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
cw.Summary("Helper: put the buffer into a MemoryStream and create a new instance to deserializing into");
cw.Bracket(m.OptionAccess + " static " + m.CsType + " Deserialize(byte[] buffer)");
cw.WriteLine(m.CsType + " instance = new " + m.CsType + "();");
cw.WriteLine("using (var ms = new MemoryStream(buffer))");
cw.WriteIndent("Deserialize(ms, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
}
cw.Summary("Helper: put the buffer into a MemoryStream before deserializing");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " Deserialize(byte[] buffer, " + refstr + m.FullCsType + " instance)");
cw.WriteLine("using (var ms = new MemoryStream(buffer))");
cw.WriteIndent("Deserialize(ms, " + refstr + "instance);");
cw.WriteLine("return instance;");
cw.EndBracketSpace();
#endregion
string[] methods = new string[]
{
"Deserialize", //Default old one
"DeserializeLengthDelimited", //Start by reading length prefix and stay within that limit
"DeserializeLength", //Read at most length bytes given by argument
};
//Main Deserialize
foreach (string method in methods)
{
if (method == "Deserialize")
{
cw.Summary("Takes the remaining content of the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, " + refstr + m.FullCsType + " instance)");
}
else if (method == "DeserializeLengthDelimited")
{
cw.Summary("Read the VarInt length prefix and the given number of bytes from the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, " + refstr + m.FullCsType + " instance)");
}
else if (method == "DeserializeLength")
{
cw.Summary("Read the given number of bytes from the stream and deserialze it into the instance.");
cw.Bracket(m.OptionAccess + " static " + m.FullCsType + " " + method + "(Stream stream, int length, " + refstr + m.FullCsType + " instance)");
}
else
throw new NotImplementedException();
if (m.IsUsingBinaryWriter)
cw.WriteLine("BinaryReader br = new BinaryReader(stream);");
//Prepare List<> and default values
foreach (Field f in m.Fields.Values)
{
if (f.Rule == FieldRule.Repeated)
{
//Initialize lists of the custom DateTime or TimeSpan type.
string csType = f.ProtoType.FullCsType;
if (f.OptionCodeType != null)
csType = f.OptionCodeType;
cw.WriteLine("if (instance." + f.CsName + " == null)");
cw.WriteIndent("instance." + f.CsName + " = new List<" + csType + ">();");
}
else if (f.OptionDefault != null)
{
if (f.ProtoType is ProtoEnum)
cw.WriteLine("instance." + f.CsName + " = " + f.ProtoType.FullCsType + "." + f.OptionDefault + ";");
else
cw.WriteLine("instance." + f.CsName + " = " + f.OptionDefault + ";");
}
else if (f.Rule == FieldRule.Optional)
{
if (f.ProtoType is ProtoEnum)
{
ProtoEnum pe = f.ProtoType as ProtoEnum;
//.........这里部分代码省略.........
开发者ID:huodianyan,项目名称:ProtoBuf,代码行数:101,代码来源:MessageSerializer.cs
注:本文中的SilentOrbit.ProtocolBuffers.ProtoMessage类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论