/// <summary> /// 设置会话的心跳包 /// </summary> /// <param name="socket">客户端</param> /// <param name="dueTime">延迟的时间量(以毫秒为单位)</param> /// <param name="period">时间间隔(以毫秒为单位)</param> /// <returns></returns> private bool TrySetKeepAlive(Socket socket, int dueTime, int period) { var inOptionValue = new byte[12]; var outOptionValue = new byte[12]; ByteConverter.ToBytes(1, Endians.Little).CopyTo(inOptionValue, 0); ByteConverter.ToBytes(dueTime, Endians.Little).CopyTo(inOptionValue, 4); ByteConverter.ToBytes(period, Endians.Little).CopyTo(inOptionValue, 8); try { socket.IOControl(IOControlCode.KeepAliveValues, inOptionValue, outOptionValue); return(true); } catch (NotSupportedException) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, inOptionValue); return(true); } catch (NotImplementedException) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, inOptionValue); return(true); } catch (Exception) { return(false); } }
/// <summary> /// 获取一次Tcp端口快照信息 /// </summary> /// <param name="ipVersion">ip版本</param> /// <returns></returns> public unsafe static PortOwnerPid[] Snapshot(AddressFamily ipVersion) { var size = 0; var TCP_TABLE_OWNER_PID_ALL = 5; TcpSnapshot.GetExtendedTcpTable(null, &size, false, ipVersion, TCP_TABLE_OWNER_PID_ALL, 0); var pTable = stackalloc byte[size]; var state = TcpSnapshot.GetExtendedTcpTable(pTable, &size, false, ipVersion, TCP_TABLE_OWNER_PID_ALL, 0); if (state != 0) { return(new PortOwnerPid[0]); } var hashSet = new HashSet <PortOwnerPid>(); var rowLength = *(int *)(pTable); var pRow = pTable + Marshal.SizeOf(rowLength); for (int i = 0; i < rowLength; i++) { var row = *(MIB_TCPROW_OWNER_PID *)pRow; var port = ByteConverter.ToBytes(row.LocalPort, Endians.Little); var portOwner = new PortOwnerPid { Port = ByteConverter.ToUInt16(port, 0, Endians.Big), OwerPid = (int)row.OwningPid }; hashSet.Add(portOwner); pRow = pRow + Marshal.SizeOf(typeof(MIB_TCPROW_OWNER_PID)); } return(hashSet.OrderBy(item => item.Port).ToArray()); }
/// <summary> /// 设置客户端的心跳包 /// </summary> /// <param name="socket">客户端</param> private void SetKeepAlive(Socket socket) { var inOptionValue = new byte[12]; var outOptionValue = new byte[12]; ByteConverter.ToBytes(1, Endians.Little).CopyTo(inOptionValue, 0); ByteConverter.ToBytes(5 * 1000, Endians.Little).CopyTo(inOptionValue, 4); ByteConverter.ToBytes(5 * 1000, Endians.Little).CopyTo(inOptionValue, 8); try { socket.IOControl(IOControlCode.KeepAliveValues, inOptionValue, outOptionValue); } catch (NotSupportedException) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, inOptionValue); } catch (NotImplementedException) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, inOptionValue); } catch (Exception) { } }
/// <summary> /// 将32位整数转换为byte数组再添加 /// </summary> /// <param name="value">整数</param> public void Add(int value) { var bytes = ByteConverter.ToBytes(value, this.Endian); this.Add(bytes); }
/// <summary> /// 将32位整数转换为byte数组再添加 /// </summary> /// <param name="value">整数</param> /// <param name="littleEndian">是否低位在前</param> public void Add(int value, bool littleEndian) { var bytes = ByteConverter.ToBytes(value, littleEndian); this.Add(bytes); }
/// <summary> /// 将16位整数转换为byte数组再添加 /// </summary> /// <param name="value">整数</param> /// <param name="endian">高低位</param> public void Add(ushort value, Endians endian) { var bytes = ByteConverter.ToBytes(value, endian); this.Add(bytes); }