/// <summary> /// 监测数据 /// </summary> private void DoWork() { while (!isStop) { if (socketChannel.IsClose) { //说明监听被关闭; this.Close(); } SocketArgEvent item = null; if (!channels.TryTake(out item, 1000)) { continue; } TCPSocketChannel channel = item.chanel as TCPSocketChannel; if (channel != null) { IChannelHandler rwhandler = null; IChannelHandler handler = null; if (!handers.TryGetValue(HanderType.Decoder, out handler)) { //如果没有解析类,就直接获取读取管道 handers.TryGetValue(HanderType.ReadWrite, out rwhandler); } if (channel.Socket.Available > 0) { //读取数据 while (channel.Socket.Connected && channel.Socket.Available > 0) { int num = channel.Socket.Available; List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num); for (int i = 0; i < buffers.Count; i++) { int r = channel.Socket.Receive(buffers[i].buffer); buffers[i].Reset(0, r); } if (channel.AddBuffer(buffers, num) > 0) { if (handler != null) { foreach (byte[] result in channel.RecBuffer) { handler.ChannelRead(item, result); } } else if (rwhandler != null) { foreach (byte[] result in channel.RecBuffer) { rwhandler.ChannelRead(item, result); } } } } } if (handler != null) { //当前一次读取已经完成 handler.ChannelReadComplete(item); } if (channel.Socket.Connected) { //还是连接状态就放回等待监测数据 channels.Add(item); } else { if (handler != null) { // handler.ChannelInactive(item); handler.CloseAsync(item); } Interlocked.Decrement(ref m_numConnectedSockets); } } } }
/// <summary> /// 监测数据 /// </summary> private void DoWork() { SocketArgEvent item = new SocketArgEvent(); IPEndPoint iPEndPoint = socketChannel.Socket.LocalEndPoint as IPEndPoint; item.localIP = iPEndPoint.Address.ToString(); item.localPort = iPEndPoint.Port; localIP = item.localIP; localPort = item.localPort; item.chanel = socketChannel; if (socketChannel.Socket.RemoteEndPoint != null) { iPEndPoint = socketChannel.Socket.RemoteEndPoint as IPEndPoint; item.remoteIP = iPEndPoint.Address.ToString(); item.remotePort = iPEndPoint.Port; } while (!isStop) { if (socketChannel != null) { if (socketChannel.IsClose) { //已经关闭socket,最后接收一次数据 Close(); } IChannelHandler rwhandler = null; IChannelHandler handler = null; if (!handers.TryGetValue(HanderType.Decoder, out handler)) { //如果没有解析类,就直接获取读取管道 handers.TryGetValue(HanderType.ReadWrite, out rwhandler); } if (socketChannel.Socket.Available > 0) { //读取数据 if (isTCP) { TCPSocketChannel channel = socketChannel as TCPSocketChannel; while (channel.Socket.Connected && channel.Socket.Available > 0) { int num = channel.Socket.Available; List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num); for (int i = 0; i < buffers.Count; i++) { int r = channel.Socket.Receive(buffers[i].buffer); buffers[i].Reset(0, r); } if (channel.AddBuffer(buffers, num) > 0) { if (handler != null) { foreach (byte[] result in channel.RecBuffer) { handler.ChannelRead(item, result); } } else if (rwhandler != null) { foreach (byte[] result in channel.RecBuffer) { rwhandler.ChannelRead(item, result); } } } } } } else { UDPSocketChannel channel = socketChannel as UDPSocketChannel; EndPoint endPoint = null; while (!channel.IsClose && channel.Socket.Available > 0) { int num = channel.Socket.Available; List <ByteBuffer> buffers = BufferManager.GetInstance().GetBufferSize(num); for (int i = 0; i < buffers.Count; i++) { int r = channel.Socket.ReceiveFrom(buffers[i].buffer, ref endPoint); buffers[i].Reset(0, r); } if (channel.AddBuffer(buffers, num) > 0) { SocketArgEvent udpItem = new SocketArgEvent(); // udpItem.localIP = localIP; udpItem.localPort = localPort; if (endPoint != null) { iPEndPoint = endPoint as IPEndPoint; item.remoteIP = iPEndPoint.Address.ToString(); item.remotePort = iPEndPoint.Port; } if (handler != null) { foreach (byte[] result in channel.RecBuffer) { handler.ChannelRead(udpItem, result); } } else if (rwhandler != null) { foreach (byte[] result in channel.RecBuffer) { rwhandler.ChannelRead(udpItem, result); } } handler.ChannelReadComplete(udpItem); } } } if (isTCP) { if (handler != null) { //当前一次读取已经完成 handler.ChannelReadComplete(item); } if (item.chanel.Socket.Connected) { //还是连接状态就放回等待监测数据 Thread.Sleep(100); } else { if (handler != null) { // handler.ChannelInactive(item); handler.CloseAsync(item); } } } else { UDPSocketChannel channel = item.chanel as UDPSocketChannel; if (!channel.IsClose) { //还是连接状态就放回等待监测数据 Thread.Sleep(100); } else { if (handler != null) { // handler.ChannelInactive(item); handler.CloseAsync(item); } } } } } }