Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DigitalPlatform/chord
Browse files Browse the repository at this point in the history
  • Loading branch information
renyh committed Jun 21, 2022
2 parents 1389be7 + 570542a commit 7a1f598
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 12 deletions.
7 changes: 4 additions & 3 deletions ChordInstaller/ChordInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<SuiteName>Chord</SuiteName>
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>publish.htm</WebPage>
<ApplicationRevision>79</ApplicationRevision>
<ApplicationRevision>80</ApplicationRevision>
<ApplicationVersion>1.2.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -61,10 +61,10 @@
<SignManifests>true</SignManifests>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>3EC46D6B99520A1DCDAD027B726C38528625A83D</ManifestCertificateThumbprint>
<ManifestCertificateThumbprint>57CE53D10A26B55C487F4A07A2D6B3BCCB573D75</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>数字平台(北京)软件有限责任公司.pfx</ManifestKeyFile>
<ManifestKeyFile>数字平台(北京)软件有限责任公司_2022.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<TargetZone>LocalIntranet</TargetZone>
Expand Down Expand Up @@ -146,6 +146,7 @@
</Content>
<None Include="代码签名证书-2020.pfx" />
<None Include="数字平台(北京)软件有限责任公司.pfx" />
<None Include="数字平台(北京)软件有限责任公司_2022.pfx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DigitalPlatform.Drawing\DigitalPlatform.Drawing.csproj">
Expand Down
118 changes: 118 additions & 0 deletions dp2Capo/LoginCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DigitalPlatform.SIP.Server;

namespace dp2Capo
{
/// <summary>
/// 登录信息缓存机制。避免高密度的登录操作过度耗费 CPU 资源
/// </summary>
public class LoginCache
{
Hashtable _table = new Hashtable();

// 储存信息
public void Set(SipChannel channel,
string userName,
string password,
string style)
{
lock (_table.SyncRoot)
{
// 限制 hashtable 最大元素数
if (_table.Count > 10000)
_table.Clear();

_table[channel.GetHashCode()] = new LoginCacheItem
{
ChannelCode = channel.GetHashCode(),
UserName = userName,
Password = password,
Style = style,
};
}
}

// 清除
public void Clear(SipChannel channel)
{
lock (_table.SyncRoot)
{
_table.Remove(channel.GetHashCode());
}
}

// 是否包含?
public bool Contains(SipChannel channel,
string userName,
string password,
string style)
{
var code = channel.GetHashCode();
lock (_table.SyncRoot)
{
var item = _table[code] as LoginCacheItem;
if (item == null)
return false;
if (item.UserName != userName
|| item.Password != password
|| item.Style != style)
{
_table.Remove(code);
return false;
}

item.Touch();
return true;
}
}

// 清除衰老的事项
public void CleanOld(TimeSpan length)
{
DateTime now = DateTime.Now;
lock (_table.SyncRoot)
{
List<int> delete_keys = new List<int>();
foreach(int key in _table.Keys)
{
var item = _table[key] as LoginCacheItem;
if (now - item.CreatedTime > length)
delete_keys.Add(key);
}

foreach(int key in delete_keys)
{
_table.Remove(key);
}
}
}
}

class LoginCacheItem
{
public int ChannelCode { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Style { get; set; }

public DateTime CreatedTime { get; set; }
public DateTime LastTime { get; set; }

public LoginCacheItem()
{
CreatedTime = DateTime.Now;
LastTime = CreatedTime;
}

public void Touch()
{
LastTime = DateTime.Now;
}
}
}
7 changes: 4 additions & 3 deletions dp2Capo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.44.*")]
[assembly: AssemblyFileVersion("1.44.0.0")]
[assembly: AssemblyVersion("1.45.*")]
[assembly: AssemblyFileVersion("1.45.0.0")]

// 1.1 (2016/6/26) 首次使用了版本号
// 1.2 (2016/9/14) 管理线程中会不断重试连接 dp2mserver,并将此情况写入日志
Expand Down Expand Up @@ -82,4 +82,5 @@
// 1.42 (2022/3/29) SIP Server 账户配置增加了 isManager 参数,用于定义是否管理员身份。只有管理员才被允许使用 listChannel 功能
// 1.43 (2022/3/31) SIP Server 修正了计算每个账户通道数过程中的 bug。
// 登录时,把对 dp2library 的 Login() 调用用记录锁保护起来,令针对同一个用户名(和实例名)的登录从并发变为顺次调用,避免突然在 dp2libraryChannelPool 中分配很多 dp2library 通道(可能导致通道配额耗尽)
// 1.44 (2022/5/24) ScStatus() API 改用 capo 账户。Login() API 增加检查危险权限功能
// 1.44 (2022/5/24) ScStatus() API 改用 capo 账户。Login() API 增加检查危险权限功能
// 1.45 (2022/6/20) 增加了 LoginCache 机制,避免高密度的 Login() 请求引起 dp2library CPU 耗用居高不下
2 changes: 2 additions & 0 deletions dp2Capo/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ public static void BackgroundWork()

// 顺便清理一下 hangup 状态缓存
SipProcessor.ClearHangupStatusTable();
// 清理闲置的 LoginCache
SipProcessor.ClearLoginCache();
}

// 阻塞,直到全部任务完成。避免 BeginConnect() 函数被重叠调用
Expand Down
47 changes: 43 additions & 4 deletions dp2Capo/SipProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ static string Login(SipChannel sip_channel,
return response.ToText();
}

static LoginCache _loginCache = new LoginCache();

// 清除 LoginCache 中衰老的事项
public static void ClearLoginCache()
{
_loginCache.CleanOld(TimeSpan.FromMinutes(5));
}

static int DoLogin(
SipChannel sip_channel,
Instance instance,
Expand Down Expand Up @@ -637,10 +645,29 @@ static int DoLogin(
if (skipLogin)
lRet = 1;
else
lRet = library_channel.Login(strPureUserName,
{
if (_loginCache.Contains(sip_channel,
strPureUserName + "@" + instance.MessageConnection.GetHashCode().ToString(),
strPassword,
style, // $"type=worker,client=dp2SIPServer|0.01,location={currentLocation},clientip=" + strClientIP,
out strError);
style) == true)
lRet = 1;
else
{
lRet = library_channel.Login(strPureUserName,
strPassword,
style, // $"type=worker,client=dp2SIPServer|0.01,location={currentLocation},clientip=" + strClientIP,
out strError);

if (lRet == 1)
_loginCache.Set(sip_channel,
strPureUserName + "@" + instance.MessageConnection.GetHashCode().ToString(),
strPassword,
style);
else
_loginCache.Clear(sip_channel);
}
}

if (skipLogin == false
&& (lRet == -1 || lRet == 0))
{
Expand Down Expand Up @@ -704,6 +731,7 @@ static int DoLogin(
_userNameLocks.UnlockForWrite(lock_string);
}
ERROR1:
_loginCache.Clear(sip_channel);
return -1;
}

Expand Down Expand Up @@ -3268,12 +3296,15 @@ static string PatronInfo(SipChannel sip_channel, string message)
XmlNodeList overdues = dom.DocumentElement.SelectNodes("overdues/overdue");
if (overdues != null && overdues.Count > 0)
{
// List<VariableLengthField> fineItems = new List<VariableLengthField>();

List<string> prices = new List<string>();

string strWords = "押金,租金";
string strWords2 = "超期,丢失";
foreach (XmlNode node in overdues)
foreach (XmlElement node in overdues)
{
string id = node.GetAttribute("id");
string strReason = DomUtil.GetAttr(node, "reason");
string strPart = "";
if (strReason.Length > 2)
Expand All @@ -3290,6 +3321,8 @@ static string PatronInfo(SipChannel sip_channel, string message)
// 计算金额
string price = DomUtil.GetAttr(node, "price");
prices.Add(price);

// fineItems.Add(new VariableLengthField(SIPConst.F_AV_FineItems, false, $"{id}:{price}"));
}

// 累计欠款金额
Expand All @@ -3305,6 +3338,12 @@ static string PatronInfo(SipChannel sip_channel, string message)
}
response.BV_feeAmount_o = "-" + currItem.Value.ToString(); //设为负值
response.BH_CurrencyType_3 = currItem.Prefix;

/*
// 2022/5/26
if (fineItems.Count > 0)
response.AV_FineItems_o = fineItems;
*/
}

string strBarcode = DomUtil.GetElementText(dom.DocumentElement, "barcode");
Expand Down
1 change: 1 addition & 0 deletions dp2Capo/dp2Capo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
</Compile>
<Compile Include="Instance.cs" />
<Compile Include="LifeThread.cs" />
<Compile Include="LoginCache.cs" />
<Compile Include="NotifyThread.cs" />
<Compile Include="Program.cs">
<SubType>Component</SubType>
Expand Down
5 changes: 3 additions & 2 deletions dp2SIPClient/dp2SIPClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>3EC46D6B99520A1DCDAD027B726C38528625A83D</ManifestCertificateThumbprint>
<ManifestCertificateThumbprint>57CE53D10A26B55C487F4A07A2D6B3BCCB573D75</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>数字平台(北京)软件有限责任公司.pfx</ManifestKeyFile>
<ManifestKeyFile>数字平台(北京)软件有限责任公司_2022.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
Expand Down Expand Up @@ -174,6 +174,7 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<None Include="数字平台(北京)软件有限责任公司.pfx" />
<None Include="数字平台(北京)软件有限责任公司_2022.pfx" />
</ItemGroup>
<ItemGroup>
<Content Include="delete.txt" />
Expand Down

0 comments on commit 7a1f598

Please sign in to comment.