public virtual bool Encode <T>(T obj, out MessageMeta meta, out MemoryStream stream) where T : class { meta = MessageMeta.Empty; stream = null; return(false); }
public bool Send <T>(T msg) where T : class { MemoryStream stream = null; MessageMeta meta = MessageMeta.Empty; if (!_peer.Codec.Encode <T>(msg, out meta, out stream)) { return(false); } return(RawSend(meta.id, stream.ToArray())); }
public override bool Decode <T>(MessageMeta meta, MemoryStream stream, out T obj) { obj = default(T); try { obj = ProtoBuf.Serializer.NonGeneric.Deserialize(meta.type, stream) as T; } catch (Exception) { return(false); } return(true); }
/// <summary> /// 将消息注册 /// </summary> /// <param name="id"></param> /// <param name="t"></param> public static MessageMeta Register(Type t) { if (!GetByType(t).Equals(MessageMeta.Empty)) { throw new Exception("重复的消息ID"); } var mi = new MessageMeta(t); mi.id = StringHash.Hash(t.FullName); mi.type = t; _idmap.Add(mi.id, mi); _typemap.Add(t, mi); _namemap.Add(mi.name, mi); return(mi); }
public override bool Encode <T>(T obj, out MessageMeta meta, out MemoryStream stream) { stream = new MemoryStream(); meta = MessageMetaSet.GetByType(typeof(T)); try { ProtoBuf.Serializer.Serialize(stream, obj); } catch (Exception) { return(false); } return(true); }
public virtual bool Decode <T>(MessageMeta meta, MemoryStream stream, out T obj) where T : class { obj = default(T); return(false); }