protected bool ApplySendedPermission(SecurityTCP.Client client, string msgName) { //请求发送 var sendPermit = false; using (var signal = new ManualResetEventSlim(false)) { EventHandler <Tuple <SecurityTCP.Client, string> > allowback = (object sender, Tuple <SecurityTCP.Client, string> pair) => { if (client == pair.Item1 && pair.Item2 == msgName) { signal.Set(); } }; this.AllowEvent += allowback; sendPermit = false; this.SendMsg(client, Commands[0], msgName); //等待允许 if (signal.Wait(1500)) { //获得准许 sendPermit = true; } //this.PushError(this, UserList[client], $"No permission to sending from {user.Name} !"); this.AllowEvent -= allowback; } return(sendPermit); }
protected void RemoveUser(SecurityTCP.Client client) { UserList[client].Client = null; MsgList.Remove(client); BigMsgList.Remove(client); UserList.Remove(client); }
public void AcceptUser(object sender, SecurityTCP.Client client) { this.AddUser(client, new UIUser("null", "未知用户") { Client = client }); }
private void PostOffineEvent(object obj, SecurityTCP.Client c) { Task.Run(() => { this.Disconnect(); OfflineEvent?.Invoke(); }); }
private void SendMsg(SecurityTCP.Client client, Command cmd, string text) { cmd.Data.CopyTo(_sendBuff, 0); var data = Encoding.Unicode.GetBytes(text); data.CopyTo(_sendBuff, cmd.Data.Length); this.TCP.Send(client, _sendBuff, cmd.Data.Length + data.Length); }
private void CommandOfEntity(byte[] data, int size, SecurityTCP.Client client) { var queue = MsgList[client]; while (queue.Count > 0) { var msgClass = queue.Dequeue(); var realMsg = Message.SetMessageEntity(msgClass.MsgName, data, CommandSize, size - CommandSize); if (realMsg != null) { realMsg.User = UserList[client]; this.PushMessge(realMsg); break; } } }
private void CommandOfApply(byte[] data, int size, SecurityTCP.Client client) { if (size - CommandSize > 128) { return; } string msgName = Encoding.Unicode.GetString(data, CommandSize, size - CommandSize); Message msgClass = Message.CreateInstance(msgName); if (msgClass != null) { msgClass.User = UserList[client]; MsgList[client].Enqueue(msgClass); //允许发送 this.SendMsg(client, Commands[1], msgClass.MsgName); } }
protected void AddUser(SecurityTCP.Client client, UIUser user) { UserList.Add(client, user); BigMsgList.Add(client, new Queue <byte[]>()); MsgList.Add(client, new Queue <Message>()); }
public void MissUser(object sender, SecurityTCP.Client client) { this.RemoveUser(client); }
private void CommandOfFlush(byte[] data, int size, SecurityTCP.Client client) { }
private void CommandOfAllow(byte[] data, int size, SecurityTCP.Client client) { string msgStr = Encoding.Unicode.GetString(data, CommandSize, size - CommandSize); AllowEvent?.Invoke(this, new Tuple <SecurityTCP.Client, string>(client, msgStr)); }