Пример #1
0
        public static ClientMessage Parse(Stream stream)
        {
            if (stream == null)
            {
                return(null);
            }

            ClientMessage message;

            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                var messageTypeValue = reader.ReadByte();
                if (!Enum.IsDefined(typeof(ClientMessageType), messageTypeValue))
                {
                    throw new Exception($"Unknown client message type: {messageTypeValue}.");
                }

                var messageType = (ClientMessageType)messageTypeValue;
                switch (messageType)
                {
                case ClientMessageType.Greeting:
                    message = new Greeting();
                    break;

                default:
                    throw new Exception($"No responsible class defined for client message type {messageType}.");
                }
                message.Deserialize(reader);
            }
            return(message);
        }