| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . S O C K E T S . P O L L -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This package provides an interface to wait for one of a set of sockets to |
| 33 |
-- become ready to perform I/O. |
| 34 |
|
| 35 |
with System.OS_Constants; |
| 36 |
|
| 37 |
package GNAT.Sockets.Poll is |
| 38 |
|
| 39 |
type Event_Type is (Input, Output, Error, Hang_Up, Invalid_Request); |
| 40 |
-- I/O events we can expect on socket. |
| 41 |
-- Input - socket ready to read; |
| 42 |
-- Output - socket available for write; |
| 43 |
-- Error - socket is in error state; |
| 44 |
-- Hang_Up - peer closed; |
| 45 |
-- Invalid_Request - invalid socket; |
| 46 |
|
| 47 |
type Event_Set is array (Event_Type) of Boolean; |
| 48 |
-- The type to get results on events waiting |
| 49 |
|
| 50 |
subtype Wait_Event_Type is Event_Type range Input .. Output; |
| 51 |
type Wait_Event_Set is array (Wait_Event_Type) of Boolean; |
| 52 |
-- The type to set events to wait. Note that Error event would be waited |
| 53 |
-- anyway. |
| 54 |
|
| 55 |
------------------------------- |
| 56 |
-- Predefined set of events -- |
| 57 |
------------------------------- |
| 58 |
|
| 59 |
Input_Event : constant Wait_Event_Set; |
| 60 |
-- Wait for input availability only |
| 61 |
|
| 62 |
Output_Event : constant Wait_Event_Set; |
| 63 |
-- Wait for output availability only |
| 64 |
|
| 65 |
Both_Events : constant Wait_Event_Set; |
| 66 |
-- Wait for Input and Output availability |
| 67 |
|
| 68 |
Error_Event : constant Wait_Event_Set; |
| 69 |
-- Wait only for error state on socket |
| 70 |
|
| 71 |
type Set (Size : Positive) is private; |
| 72 |
-- Set of sockets with I/O event set to wait on |
| 73 |
|
| 74 |
function Create (Size : Positive) return Set; |
| 75 |
-- Create empty socket set with defined size |
| 76 |
|
| 77 |
function To_Set |
| 78 |
(Socket : Socket_Type; |
| 79 |
Events : Wait_Event_Set; |
| 80 |
Size : Positive := 1) return Set; |
| 81 |
-- Create socket set and put the Socket there at the first place. |
| 82 |
-- Events parameter is defining what state of the socket we are going to |
| 83 |
-- wait. |
| 84 |
|
| 85 |
procedure Append |
| 86 |
(Self : in out Set; |
| 87 |
Socket : Socket_Type; |
| 88 |
Events : Wait_Event_Set); |
| 89 |
-- Add Socket and its I/O waiting state at the end of Self |
| 90 |
|
| 91 |
procedure Insert |
| 92 |
(Self : in out Set; |
| 93 |
Socket : Socket_Type; |
| 94 |
Events : Wait_Event_Set; |
| 95 |
Index : Positive; |
| 96 |
Keep_Order : Boolean := False); |
| 97 |
-- Insert Socket and its I/O waiting state at the Index position. |
| 98 |
-- If Keep_Order is True then all next elements moved to the next index up. |
| 99 |
-- Otherwise the old element from Index moved to the end of the Self set. |
| 100 |
|
| 101 |
procedure Remove |
| 102 |
(Self : in out Set; Index : Positive; Keep_Order : Boolean := False); |
| 103 |
-- Remove socket from Index. If Keep_Order is True then move all next |
| 104 |
-- elements after removed one to previous index. If Keep_Order is False |
| 105 |
-- then move the last element on place of the removed one. |
| 106 |
|
| 107 |
procedure Set_Event |
| 108 |
(Self : in out Set; |
| 109 |
Index : Positive; |
| 110 |
Event : Wait_Event_Type; |
| 111 |
Value : Boolean); |
| 112 |
-- Set I/O waiting event to Value for the socket at Index position |
| 113 |
|
| 114 |
procedure Set_Events |
| 115 |
(Self : in out Set; |
| 116 |
Index : Positive; |
| 117 |
Events : Wait_Event_Set); |
| 118 |
-- Set I/O waiting events for the socket at Index position |
| 119 |
|
| 120 |
function Get_Events |
| 121 |
(Self : Set; Index : Positive) return Wait_Event_Set; |
| 122 |
-- Get I/O waiting events for the socket at Index position |
| 123 |
|
| 124 |
function Length (Self : Set) return Natural; |
| 125 |
-- Get the number of sockets currently in the Self set |
| 126 |
|
| 127 |
function Full (Self : Set) return Boolean; |
| 128 |
-- Return True if there is no more space in the Self set for new sockets |
| 129 |
|
| 130 |
procedure Wait (Self : in out Set; Timeout : Duration; Count : out Natural); |
| 131 |
-- Wait no longer than Timeout on the socket set for the I/O events. |
| 132 |
-- Count output parameter is the number of elements in the Self set are |
| 133 |
-- detected for I/O events. Zero Count mean timeout on wait. |
| 134 |
-- The iteration over activated elements in set could be done with routine |
| 135 |
-- Next. The kind of I/O events on element could be cheched with State or |
| 136 |
-- Status routines. |
| 137 |
|
| 138 |
procedure Next (Self : Set; Index : in out Natural); |
| 139 |
-- Iterate over set looking for the next index with active I/O event state. |
| 140 |
-- Put 0 initially into Index. Each iteration increments Index and then |
| 141 |
-- checks for state. End of iterations can be detected by 0 in the Index. |
| 142 |
|
| 143 |
procedure Copy (Source : Set; Target : out Set); |
| 144 |
-- Copy sockets and its I/O waiting events from Source set into Target |
| 145 |
|
| 146 |
function Resize (Self : Set; Size : Positive) return Set; |
| 147 |
-- Returns the copy of Source with modified Size |
| 148 |
|
| 149 |
function Growth (Self : Set) return Set; |
| 150 |
-- Returns the copy of Source with increased Size |
| 151 |
|
| 152 |
function Socket (Self : Set; Index : Positive) return Socket_Type; |
| 153 |
-- Returns the Socket from Index position |
| 154 |
|
| 155 |
function Status (Self : Set; Index : Positive) return Event_Set; |
| 156 |
-- Returns I/O events detected in previous Wait call at Index position |
| 157 |
|
| 158 |
procedure State |
| 159 |
(Self : Set; |
| 160 |
Index : Positive; |
| 161 |
Socket : out Socket_Type; |
| 162 |
Status : out Event_Set); |
| 163 |
-- Returns Socket and its I/O events detected in previous Wait call at |
| 164 |
-- Index position. |
| 165 |
|
| 166 |
function C_Status |
| 167 |
(Self : Set; Index : Positive) return Interfaces.C.unsigned; |
| 168 |
-- Return word with I/O events detected flags in previous Wait call at |
| 169 |
-- Index position. Possible flags are defined in System.OS_Constants names |
| 170 |
-- starting with POLL prefix. |
| 171 |
|
| 172 |
private |
| 173 |
|
| 174 |
Input_Event : constant Wait_Event_Set := [Input => True, Output => False]; |
| 175 |
Output_Event : constant Wait_Event_Set := [Input => False, Output => True]; |
| 176 |
Both_Events : constant Wait_Event_Set := [others => True]; |
| 177 |
Error_Event : constant Wait_Event_Set := [others => False]; |
| 178 |
|
| 179 |
package SOC renames System.OS_Constants; |
| 180 |
|
| 181 |
type nfds_t is mod 2 ** SOC.SIZEOF_nfds_t; |
| 182 |
for nfds_t'Size use SOC.SIZEOF_nfds_t; |
| 183 |
|
| 184 |
FD_Type_Bound : constant := 2 ** (SOC.SIZEOF_fd_type - 1); |
| 185 |
|
| 186 |
type FD_Type is range -FD_Type_Bound .. FD_Type_Bound - 1; |
| 187 |
for FD_Type'Size use SOC.SIZEOF_fd_type; |
| 188 |
|
| 189 |
type Events_Type is mod 2 ** SOC.SIZEOF_pollfd_events; |
| 190 |
for Events_Type'Size use SOC.SIZEOF_pollfd_events; |
| 191 |
|
| 192 |
type Pollfd is record |
| 193 |
Socket : FD_Type; |
| 194 |
Events : Events_Type := 0; |
| 195 |
REvents : Events_Type := 0; |
| 196 |
end record with Convention => C; |
| 197 |
|
| 198 |
type Poll_Set is array (Positive range <>) of Pollfd with Convention => C; |
| 199 |
|
| 200 |
type Set (Size : Positive) is record |
| 201 |
Length : Natural := 0; |
| 202 |
Max_FD : FD_Type := 0; |
| 203 |
Max_OK : Boolean; |
| 204 |
-- Is the Max_FD actual. It can became inactual after remove socket with |
| 205 |
-- Max_FD from set and became actual again after add socket with FD more |
| 206 |
-- than Max_FD. |
| 207 |
Fds : Poll_Set (1 .. Size); |
| 208 |
end record; |
| 209 |
|
| 210 |
function Length (Self : Set) return Natural |
| 211 |
is (Self.Length); |
| 212 |
|
| 213 |
function Full (Self : Set) return Boolean |
| 214 |
is (Self.Size = Self.Length); |
| 215 |
|
| 216 |
end GNAT.Sockets.Poll; |