/// <summary> /// 关闭所有连接 /// </summary> /// <returns></returns> public bool CloseAll() { _rwLock.AcquireReaderLock(-1); List <long> list; try { list = new List <long>(_socketsCache.Keys); } finally { _rwLock.ReleaseReaderLock(); } if (list == null || list.Count <= 0) { return(true); } foreach (long socketId in list) { SocketTCPHandler handler = GetHandlerById(socketId); if (handler == null) { continue; } try { handler.Disconnect(); } catch { } } return(true); }
/// <summary> /// 关闭指定连接 /// </summary> /// <param name="connectID">连接号</param> /// <returns></returns> public bool CloseConnect(long connectID) { bool result = false; SocketTCPHandler tcpHandler = null; _rwLock.AcquireReaderLock(-1); try { if (_socketsCache.ContainsKey(connectID)) { tcpHandler = _socketsCache[connectID]; } } finally { _rwLock.ReleaseReaderLock();//获取集合中的对象后,立即释放锁,不然会在后面的CLOSE事件里引起死锁 } try { if (tcpHandler != null) { tcpHandler.Disconnect(); result = true; } } catch { } return(result); }
/// <summary> /// 发送数据 /// </summary> /// <param name="connectID">连接号</param> /// <param name="data">数据包</param> /// <param name="dataLength">长度</param> /// <returns></returns> public bool Send(long connectID, byte[] data, long dataLength) { bool result = false; SocketTCPHandler handler = GetHandlerById(connectID); if (handler != null) { try { handler.Send(data); result = true; } catch { }; } return(result); }
/// <summary> /// 心跳检查 /// </summary> /// <param name="state"></param> private void AutoCheckHeartbeat(object state) { _rwLock.AcquireReaderLock(-1); List <long> list; try { list = new List <long>(_socketsCache.Keys); } finally { _rwLock.ReleaseReaderLock(); } if (list == null || list.Count <= 0) { return; } foreach (long socketId in list) { SocketTCPHandler handler = GetHandlerById(socketId); if (handler == null) { continue; } try { var result = handler.CheckHeartbeat(); if (!result) { //如果发送心跳包时失败,直接调用对象的断开连接接口。 handler.Disconnect(); } } catch { } } }
/// <summary> /// 根据SOCKET ID获取处理器 /// </summary> /// <param name="socketId"></param> /// <returns></returns> private SocketTCPHandler GetHandlerById(long socketId) { SocketTCPHandler result = null; _rwLock.AcquireReaderLock(-1); try { if (_socketsCache.ContainsKey(socketId)) { result = _socketsCache[socketId]; } } catch { } finally { _rwLock.ReleaseReaderLock(); } return(result); }
/// <summary> /// 新的TCP连接处理 /// </summary> /// <param name="socketId"></param> /// <param name="newSocket"></param> void _socketListener_OnNewSocketAccept(long socketId, Socket newSocket) { SocketTCPHandler socketHandler = new SocketTCPHandler(socketId, newSocket); bool result = socketHandler.ReceiveAuth(); //建立连接后,第一次数据的连接效验 todo if (result == false) //如果效验不成功,则直接关闭连接 { socketHandler.Disconnect(); return; } _rwLock.AcquireWriterLock(-1); try { if (!_socketsCache.ContainsKey(socketId)) //添加到缓存 { //避免由于外面申请锁的过程中(可能时间较长或者其它原因),在此期间socket连接异常断开,此种情况则不把socket放入_socketsCache缓存 //(应该很出现这种情况) if (socketHandler.IsConnected) { _socketsCache.Add(socketId, socketHandler); } else { //socket断开后,断开连接处理 socketHandler.Disconnect(); socketHandler = null; return; } } else { //socketId id 重复(理论上不可能出现) //todo write error log } } catch { //如果在新增对象到缓存出错,则直接返回; 20171214 return; } finally { _rwLock.ReleaseWriterLock(); } socketHandler.OnConnectClose += socketHandler_OnConnectClose; //new NetEventHandler(socketHandler_OnConnectClose);//注册事件 socketHandler.OnDataArrive += socketHandler_OnDataArrive; // new DataArriveEventHandler(socketHandler_OnDataArrive); if (this.OnAccept != null) { try { //抛出新连接事件 this.OnAccept(this, new NetEventArgs(socketId, socketHandler.IP, socketHandler.MAC, socketHandler.Port, "新连接已经建立!")); } catch { //todo write log, } } }