public UnpackerState CreateMessageData(ref IMessageData message, Packet packet) { message = null; if (!packet.IsReady) { return(UnpackerState.NotReady); } if (packet.Size < sizeof(MessageID)) { return(UnpackerState.SizeOut); } using (MemoryStream stream = new MemoryStream(packet.GetData())) { if (!stream.CanRead) { throw new SystemException("haha"); } using (BinaryReader reader = new BinaryReader(stream)) { MessageID type = reader.ReadByte(); IUnpackerMessage unpacker = _register.Find(type); if (unpacker == null) { return(UnpackerState.NotFoundType); } return(unpacker.CreateMessageData(ref message, reader, packet.Size)); } } }
private Dictionary <MessageID, IUnpackerMessage> GetDictionary() { var assemblyType = typeof(Assembly); var packers = new Dictionary <MessageID, IUnpackerMessage>(); foreach (var type in assemblyType.Assembly.GetTypes()) { if (!type.IsClass) { continue; } if (type.IsAbstract) { continue; } if (typeof(IUnpackerMessage).IsAssignableFrom(type)) { IUnpackerMessage p = Activator.CreateInstance(type) as IUnpackerMessage; packers.Add(p.Type, p); } } return(packers); }