1 Star 0 Fork 8

zilin_zhang/fnsync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AlivePhones.cs 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
holmium 提交于 2021-02-23 20:30 . I don't remember what I have modified.
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace FnSync
{
class AlivePhones : IEnumerable<PhoneClient>
{
public static readonly AlivePhones Singleton = new AlivePhones();
private readonly ConcurrentDictionary<string, PhoneClient> map = new ConcurrentDictionary<string, PhoneClient>();
public PhoneClient this[string id]
{
get
{
return TryGet(id);
}
}
public long AliveCount
{
get
{
int Count = 0;
foreach(PhoneClient client in this)
{
if (client.IsAlive)
++Count;
}
return Count;
}
}
public int Count => map.Count;
private AlivePhones()
{
}
public bool Contains(string id)
{
return map.ContainsKey(id);
}
public PhoneClient AddOrUpdate(string id, PhoneClient phone)
{
PhoneClient old = null;
map.AddOrUpdate(id, phone, (k, v) => { old = v; return phone; });
return old;
}
public PhoneClient TryGet(string id)
{
if (id == null)
return null;
PhoneClient phone;
if (map.TryGetValue(id, out phone))
{
return phone;
}
else
{
return null;
}
}
public void Remove(string id)
{
map.TryRemove(id, out PhoneClient _);
}
public void DisconnectAll()
{
foreach (var kv in map)
{
kv.Value.Dispose();
}
}
public void PushMsg(JObject msg, String msgType)
{
foreach (var kv in map)
{
if (kv.Value.IsAlive)
{
kv.Value.SendMsg(msg, msgType);
}
}
}
public IEnumerator<PhoneClient> GetEnumerator()
{
return map.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zilin__zhang/fnsync.git
git@gitee.com:zilin__zhang/fnsync.git
zilin__zhang
fnsync
fnsync
master

搜索帮助