Conversation
|
感谢 PR。 只保留大概下面的属性,其他全部用json 字符串,基本上json 字符串中都是各个 outbound 的特有属性。 迁移的时候给数据版本+1 即可,这个过程可能需要半年到一年的时间,然后可以删除迁移代码。 |
|
原来的 ProfileItem 和 ProfileGroupItem 需要保留很长时间,方便迁移。 |
|
https://github.com/2dust/v2rayNG/blob/master/V2rayNG/app/src/main/java/com/v2ray/ang/dto/ProfileItem.kt |
因为全部迁移到 json 我个人感觉没什么好处,反而增加了序列化反序列化的性能损耗 这个是为了给个别协议添加他们独属字段,放到 ProfileItem sql table 里未免有点过于浪费空间了
这个确实, |
|
确实不应该全部迁移,不过这不是序列化的性能问题,这个不值一提。 主要是开发麻烦了些。 大概下面的留下 其中这些也可以考虑放 json ,毕竟是会随着 network 变化 |
|
还有一个考虑,大概以 Vless 协议能用的为准 |
|
和现在的差别也就是 network 传输层? 也就是说要重构传输层那边,重构的工作量肯定不小,从 GUI 到分享链接生成都得改。 不如未来在加一个 NetworkTranExtraItem,又不是不能放两个 json,ProtocolExtraItem 膨胀了也不好
|
|
NetworkTranExtraItem ,这个可行,毕竟Network这里太多属性了。 |
|
把前几次你 PR 的先发一个版本后,再合并这个 pr。 |
|
行,那我这里就继续重构协议 Extra 部分了 看了下 sql 里面可以只保留 password / id; |
|
感觉差不多了,协议部分已经拆出来了 https://github.com/2dust/v2rayN/pull/8659/changes#v2rayN/ServiceLib/Models/ProtocolExtraItem.cs 然后 ProfileItem 这些被弃用了 [Obsolete("Use ProtocolExtraItem.Ports instead.")]
public string Ports { get; set; }
[Obsolete("Use ProtocolExtraItem.AlterId instead.")]
public int AlterId { get; set; }
[Obsolete("Use ProtocolExtraItem.Flow instead.")]
public string Flow { get; set; }
[Obsolete("Use ProfileItem.Password instead.")]
public string Id { get; set; }
[Obsolete("Use ProtocolExtraItem.xxx instead.")]
public string Security { get; set; } |
| ProtoExtra = JsonUtils.Serialize(extraItem, false); | ||
| } | ||
|
|
||
| public ProtocolExtraItem GetProtocolExtra() |
There was a problem hiding this comment.
这里在内部维护一个缓存 _protocolExtraCache ,这样不用每次都转换了。并且在 保存的时候,直接在存储到数据库之前 调用一次就可以了。不要在每个地方都频繁的转换
public void SetProtocolExtra( )
{
ProtoExtra = JsonUtils.Serialize(_protocolExtraCache, false);
}
private ProtocolExtraItem? _protocolExtraCache;
public ProtocolExtraItem GetProtocolExtra()
{
return _protocolExtraCache ??= JsonUtils.Deserialize<ProtocolExtraItem>(ProtoExtra) ?? new ProtocolExtraItem();
}
There was a problem hiding this comment.
public void SetProtocolExtra(ProtocolExtraItem extraItem)
这个函数还保留,在一些地方还用的到
There was a problem hiding this comment.
当时写到一半的时候就考虑过做成属性+缓存的形式
后面是怕未来哪个地方可能直接对 raw jsondata str 读写,所以就没做
There was a problem hiding this comment.
好像也行,jsondata 只读暴露出去,里面只存 ProtocolExtraItem 私有变量
我写写试试
There was a problem hiding this comment.
这个函数再这样改造下,这样保存调用 SetProtocolExtra()的时候行为也统一了
public void SetProtocolExtra(ProtocolExtraItem extraItem)
{
_protocolExtraCache = extraItem;
ProtoExtra = JsonUtils.Serialize(extraItem, false);
}
There was a problem hiding this comment.
这样的话,感觉还可以再优化
[Ignore]
public ProtocolExtraItem ProtocolExtraItem
{
get => ...
set => ...
}
然后 ProtocolExtraItem 用 record class,可以直接
item.ProtocolExtraItem = item.ProtocolExtraItem with { Flow = "xtls" }
就不用担心忘写回了
There was a problem hiding this comment.
[Ignore]
public ProtocolExtraItem ProtocolExtraItem
{
get => ...
set => ...
}
这个方案 get get 挺方便的;但是 get set 里面如果继续每次都 转换json 不合适
|
这样,用时初始化,初始为 null public string ProtoExtra
{
get => _protocolExtraJsonStr;
set
{
_protocolExtraJsonStr = value;
if (_protocolExtraCache is not null)
{
_protocolExtraCache = JsonUtils.Deserialize<ProtocolExtraItem>(_protocolExtraJsonStr) ?? new ProtocolExtraItem();
}
}
}
[Ignore]
public ProtocolExtraItem ProtocolExtraItem
{
get => _protocolExtraCache ??= JsonUtils.Deserialize<ProtocolExtraItem>(_protocolExtraJsonStr) ?? new ProtocolExtraItem();
set
{
_protocolExtraCache = value;
_protocolExtraJsonStr = JsonUtils.Serialize(_protocolExtraCache);
}
} |
只要 ProtoExtra set 里面有 json 处理,从数据库中查询出来会执行 N 次 ,即使不用到也会处理这样很浪费 |
|
所以还是保持你第一版的, public string ProtoExtra , |
|
你先不动,要不我把我的想法在你的这个基础上改下,先只改部分代码。 |
|
还是避不开 string 改了,cache 没改这个问题吧 |
|
如果不考虑这个的话直接这样就行 public string ProtoExtra { get; set;}
[Ignore]
public ProtocolExtraItem ProtocolExtraItem
{
get => _protocolExtraCache ??= JsonUtils.Deserialize<ProtocolExtraItem>(_protocolExtraJsonStr) ?? new ProtocolExtraItem();
set
{
_protocolExtraCache = value;
ProtoExtra = JsonUtils.Serialize(_protocolExtraCache);
}
} |
这个可能行,但是对你原来的改动就有点大了。 |
实测这个不行。 |
|
4ef6b2d 用 record 代替了 class var protocolExtra = item.GetProtocolExtra();
protocolExtra.VlessEncryption = GetQueryValue(query, "encryption", Global.None);
protocolExtra.Flow = GetQueryValue(query, "flow");
item.SetProtocolExtra(protocolExtra);简化成了 item.SetProtocolExtra(item.GetProtocolExtra() with
{
VlessEncryption = GetQueryValue(query, "encryption", Global.None),
Flow = GetQueryValue(query, "flow")
}); |
个人更喜欢用原来的写法,代码虽然多了,但是更清晰明了。 |
|
因为前一种开发时老是忘写 SetProtocolExtra 写回 不过现在应该也不用写回了
|
从这个代码分析,只要 ProtocolExtraItem 是从 GetProtocolExtra() 获取的,那外面使用的 Item 就是 _protocolExtraCache 的引用, 如果外面是 new ProtocolExtraItem() 出来的,就需要自己额外处理下; |
|
第一种也可以优化
|
|
准备合并了,还有要改的吗? |
|
Username 和 Password 放一块吧,虽然目前只有 http 和 socks 用到,你觉得如何? |
|
不用吧 |
|
应该没问题了 |
|
等下,改个 HopInterval |
|
好了 |
确实 Username 放json 合理,但是看到太多这样的代码,感觉有点过度设计了。
|
|
行 |
|
好了 |
Related to #7887
Fixes #8249