1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| public class Udp_Server : IDisposable { public Udp_Server(int ListeningPort) { _listeningPort = ListeningPort; _remotePoint = new IPEndPoint(IPAddress.Any, 0); _serverThread = new Thread(Run); }
~Udp_Server() { Dispose(); }
private int _listeningPort;
private UdpClient _server;
private readonly Thread _serverThread;
private IPEndPoint _remotePoint;
private static ConcurrentDictionary<string, HoleUserInfo> _loginUser; public static ConcurrentDictionary<string, HoleUserInfo> LoginUser { get { if (_loginUser == null) { _loginUser = new ConcurrentDictionary<string, HoleUserInfo>(); } return _loginUser; } set => _loginUser = value; }
public void Start() { try { System.Timers.Timer overtime = new System.Timers.Timer(); overtime.Interval = 60000 * 5; overtime.Elapsed += new System.Timers.ElapsedEventHandler(timer_Overtime); overtime.Start(); _server = new UdpClient(_listeningPort); _serverThread.Start(); NLogHelper.SaveInfo($"服务启动,监听端口:{_listeningPort}"); } catch (Exception ex) { NLogHelper.SaveError(ex.ToString()); } }
private void timer_Overtime(object sender, System.Timers.ElapsedEventArgs e) { List<string> removeKeys = LoginUser.Where(o => DateTime.Now - o.Value.LastLoginTime >= new TimeSpan(0, 5, 0)).Select(o => o.Key).ToList(); if (removeKeys != null && removeKeys.Count >= 1) { foreach (var item in removeKeys) { LoginUser.TryRemove(item, out _); } } }
private void Run() { byte[] msgBuffer; while (true) { try { msgBuffer = _server.Receive(ref _remotePoint); var msgObject = ObjectSerializerHelper.JsonDeserialize(msgBuffer); if (msgObject == null) { continue; } if (msgObject.ModelName == typeof(MessageLogin).Name) { var lginMsg = JsonConvert.DeserializeObject<MessageLogin>(msgObject.ModelData); if (LoginUser.ContainsKey(lginMsg.MacAddress)) { var vLoginUser = LoginUser.Where(o => o.Key.Equals(lginMsg.MacAddress)).First(); vLoginUser.Value.UserName = lginMsg.UserName; vLoginUser.Value.NetPoint = new IPEndPoint(_remotePoint.Address, _remotePoint.Port); vLoginUser.Value.LastLoginTime = DateTime.Now; } else { HoleUserInfo holeUserInfo = new HoleUserInfo { MacAddress = lginMsg.MacAddress, UserName = lginMsg.UserName, NetPoint = new IPEndPoint(_remotePoint.Address, _remotePoint.Port), LastLoginTime = DateTime.Now }; LoginUser.TryAdd(lginMsg.MacAddress, holeUserInfo); } } else if (msgObject.ModelName == typeof(MessageHolePunchingRequest).Name) { var holeMsg = JsonConvert.DeserializeObject<MessageHolePunchingRequest>(msgObject.ModelData); var vSelectUsers = LoginUser.Where(o => o.Key.Equals(holeMsg.TargetMacAddress))?.ToList(); if (vSelectUsers != null && vSelectUsers.Count >= 1) { var vSelectUser = vSelectUsers.First(); byte[] buffer = ObjectSerializerHelper.JsonSerialize(new MessageHolePunching() { MacAddress = holeMsg.RequestorMacAddress, RequestorNetIP = _remotePoint.Address.ToString(), RequestorNetPort = _remotePoint.Port, IsFirst = holeMsg.IsAtoB }); _server.Send(buffer, buffer.Length, vSelectUser.Value.NetPoint); } } } catch (Exception ex) { NLogHelper.SaveError(ex.ToString()); } } }
public void Dispose() { try { if (_server != null) { _serverThread.Abort(); _server.Close(); NLogHelper.SaveInfo($"服务停止!"); } } catch (Exception ex) { NLogHelper.SaveError(ex.ToString()); } } }
|