public void CloseConnect(BeeClient beeClient, string reason) { try { if (beeClient == null) { Console.WriteLine("clientBee NULL"); return; } var ip = beeClient.Socket.RemoteEndPoint; beeClient.Close(); _clientBeePool.EnQueue(beeClient); // TODO 此处单单使用这个并不科学,没有给客户端回应,应该设置一个tmp缓存服务层 _semaphore.Release(); Console.WriteLine($"{ip} -断开连接!当前蜜蜂池:{_clientBeePool.Count}/{_maxClientBee}"); // 通知应用层 _application.OnDisconnect(beeClient, reason); } catch (Exception e) { Console.WriteLine(e); } }
private void InitClientBeePool(int capacity) { _maxClientBee = capacity; _clientBeePool = new BeeClientPool(capacity); for (int i = 0; i < capacity; i++) { var clientBee = new BeeClient(); clientBee.DecodeOver = ReceiveData; // 注册收到消息回调 clientBee.SendOver = SendPacket; // 注册发送完包回调 clientBee.Disconnected = CloseConnect; // 注册发送完包回调 _clientBeePool.EnQueue(clientBee); } }
/// <summary> /// 服务蜜蜂收到一条消息 /// </summary> private void ReceiveData(BeeClient beeClient, BeePacket packet) { // undone 给应用层 _application.OnReceive(beeClient, packet); }
// 回调 private void SendPacket(BeeClient clientBee, string status) { }
public void EnQueue(BeeClient clientBee) { // 它的容量会自动扩增,不需要你去担心,也就是说Queue的容量基本是无限的。 clientBeeQueue.Enqueue(clientBee); --_workingBee; }