public static MessageMeta RegisterMessage <T>(EventDispatcher ed, Action <T, Session> callback) where T : class { var meta = MessageMetaSet.GetByType <T>(); if (meta.Equals(MessageMeta.Empty)) { throw new Exception("Register message failed: " + typeof(T).FullName); } ed.Add(meta.id, (obj) => { var ev = (SessionEvent)obj; if (ev.Stream != null) { T msg; if (ev.Ses.Peer.Codec.Decode <T>(meta, ev.Stream.ToStream(), out msg)) { callback(msg, ev.Ses); } } else { callback(null, ev.Ses); } }); return(meta); }
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); }