gi-gio-2.0.35: Gio bindings

Copyright | Will Thompson and Iñaki García Etxebarria
License | LGPL-2.1
Maintainer | Iñaki García Etxebarria
Safe Haskell | Safe-Inferred
Language | Haskell2010

GI.Gio.Objects.SocketListener

Contents

- Exported types
- Methods
  - accept
  - acceptAsync
  - acceptFinish
  - acceptSocket
  - acceptSocketAsync
  - acceptSocketFinish
  - addAddress
  - addAnyInetPort
  - addInetPort
  - addSocket
  - close
  - new
  - setBacklog
- Properties
  - listenBacklog
- Signals
  - event

Description

A `GSocketListener` is an object that keeps track of a set of server sockets and helps you accept sockets from any of the socket, either sync or async.

Add addresses and ports to listen on using `socketListenerAddAddress` and `socketListenerAddInetPort`. These will be listened on until `socketListenerClose` is called. Dropping your final reference to the `GSocketListener` will not cause `socketListenerClose` to be called implicitly, as some references to the `GSocketListener` may be held internally.

If you want to implement a network server, also look at `SocketService` and `ThreadedSocketService` which are subclasses of `GSocketListener` that make this even easier.

Since: 2.22

Synopsis

- newtype SocketListener = SocketListener (ManagedPtr SocketListener)
- class (GObject o, IsDescendantOf SocketListener o) => IsSocketListener o
- toSocketListener :: (MonadIO m, IsSocketListener o) => o -> m SocketListener
- socketListenerAccept :: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) => a -> Maybe b -> m (SocketConnection, Maybe Object)
- socketListenerAcceptAsync :: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) => a -> Maybe b -> Maybe AsyncReadyCallback -> m ()
- socketListenerAcceptFinish :: (HasCallStack, MonadIO m, IsSocketListener a, IsAsyncResult b) => a -> b -> m (SocketConnection, Maybe Object)
- socketListenerAcceptSocket :: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) => a -> Maybe b -> m (Socket, Maybe Object)
- socketListenerAcceptSocketAsync :: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) => a -> Maybe b -> Maybe AsyncReadyCallback -> m ()
- socketListenerAcceptSocketFinish :: (HasCallStack, MonadIO m, IsSocketListener a, IsAsyncResult b) => a -> b -> m (Socket, Maybe Object)
- socketListenerAddAddress :: (HasCallStack, MonadIO m, IsSocketListener a, IsSocketAddress b, IsObject c) => a -> b -> SocketType -> SocketProtocol -> Maybe c -> m SocketAddress
- socketListenerAddAnyInetPort :: (HasCallStack, MonadIO m, IsSocketListener a, IsObject b) => a -> Maybe b -> m Word16
- socketListenerAddInetPort :: (HasCallStack, MonadIO m, IsSocketListener a, IsObject b) => a -> Word16 -> Maybe b -> m ()
- socketListenerAddSocket :: (HasCallStack, MonadIO m, IsSocketListener a, IsSocket b, IsObject c) => a -> b -> Maybe c -> m ()
- socketListenerClose :: (HasCallStack, MonadIO m, IsSocketListener a) => a -> m ()
- socketListenerNew :: (HasCallStack, MonadIO m) => m SocketListener
- socketListenerSetBacklog :: (HasCallStack, MonadIO m, IsSocketListener a) => a -> Int32 -> m ()
- constructSocketListenerListenBacklog :: (IsSocketListener o, MonadIO m) => Int32 -> m (GValueConstruct o)
- getSocketListenerListenBacklog :: (MonadIO m, IsSocketListener o) => o -> m Int32
- setSocketListenerListenBacklog :: (MonadIO m, IsSocketListener o) => o -> Int32 -> m ()
- type SocketListenerEventCallback = SocketListenerEvent -> Socket -> IO ()
- afterSocketListenerEvent :: (IsSocketListener a, MonadIO m) => a -> ((?self :: a) => SocketListenerEventCallback) -> m SignalHandlerId
- onSocketListenerEvent :: (IsSocketListener a, MonadIO m) => a -> ((?self :: a) => SocketListenerEventCallback) -> m SignalHandlerId

# Exported types

newtype SocketListener Source #

Memory-managed wrapper type.

Constructors

SocketListener (ManagedPtr SocketListener) |

#### Instances

Instances details

Eq SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener Methods (==) :: SocketListener -> SocketListener -> Bool Source # (/=) :: SocketListener -> SocketListener -> Bool Source #
GObject SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener
ManagedPtrNewtype SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener Methods toManagedPtr :: SocketListener -> ManagedPtr SocketListener Source #
TypedObject SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener Methods glibType :: IO GType Source #
HasParentTypes SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener
IsGValue (Maybe SocketListener) Source # | Convert `SocketListener` to and from `GValue`. See `toGValue` and `fromGValue`.
Instance details Defined in GI.Gio.Objects.SocketListener Methods gvalueGType_ :: IO GType Source # gvalueSet_ :: Ptr GValue -> Maybe SocketListener -> IO () Source # gvalueGet_ :: Ptr GValue -> IO (Maybe SocketListener) Source #
type ParentTypes SocketListener Source # |
Instance details Defined in GI.Gio.Objects.SocketListener type ParentTypes SocketListener = '[Object]

class (GObject o, IsDescendantOf SocketListener o) => IsSocketListener o Source #

Type class for types which can be safely cast to `SocketListener`, for instance with `toSocketListener`.

#### Instances

Instances details

(GObject o, IsDescendantOf SocketListener o) => IsSocketListener o Source # |
Instance details Defined in GI.Gio.Objects.SocketListener

toSocketListener :: (MonadIO m, IsSocketListener o) => o -> m SocketListener Source #

Cast to `SocketListener`, for types for which this is known to be safe. For general casts, use `castTo`.

# Methods

### Click to display all available methods, including inherited ones

Expand

#### Methods

accept, acceptAsync, acceptFinish, acceptSocket, acceptSocketAsync, acceptSocketFinish, addAddress, addAnyInetPort, addInetPort, addSocket, bindProperty, bindPropertyFull, close, forceFloating, freezeNotify, getv, isFloating, notify, notifyByPspec, ref, refSink, runDispose, stealData, stealQdata, thawNotify, unref, watchClosure.

#### Getters

getData, getProperty, getQdata.

#### Setters

setBacklog, setData, setDataFull, setProperty.

## accept

socketListenerAccept Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) |
=> a | `listener`: a `SocketListener`
-> Maybe b | `cancellable`: optional `Cancellable` object, `Nothing` to ignore.
-> m (SocketConnection, Maybe Object) | Returns: a `SocketConnection` on success, `Nothing` on error. (Can throw `GError`)

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a `SocketConnection` for the socket that was accepted.

If `sourceObject` is not `Nothing` it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If `cancellable` is not `Nothing`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error `IOErrorEnumCancelled` will be returned.

Since: 2.22

## acceptAsync

socketListenerAcceptAsync Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) |
=> a | `listener`: a `SocketListener`
-> Maybe b | `cancellable`: a `Cancellable`, or `Nothing`
-> Maybe AsyncReadyCallback | `callback`: a `AsyncReadyCallback`
-> m () |

This is the asynchronous version of `socketListenerAccept`.

When the operation is finished `callback` will be called. You can then call `socketListenerAcceptFinish` to get the result of the operation.

Since: 2.22

## acceptFinish

socketListenerAcceptFinish Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsAsyncResult b) |
=> a | `listener`: a `SocketListener`
-> b | `result`: a `AsyncResult`.
-> m (SocketConnection, Maybe Object) | Returns: a `SocketConnection` on success, `Nothing` on error. (Can throw `GError`)

Finishes an async accept operation. See `socketListenerAcceptAsync`

Since: 2.22

## acceptSocket

socketListenerAcceptSocket Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) |
=> a | `listener`: a `SocketListener`
-> Maybe b | `cancellable`: optional `Cancellable` object, `Nothing` to ignore.
-> m (Socket, Maybe Object) | Returns: a `Socket` on success, `Nothing` on error. (Can throw `GError`)

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the `Socket` that was accepted.

If you want to accept the high-level `SocketConnection`, not a `Socket`, which is often the case, then you should use `socketListenerAccept` instead.

If `sourceObject` is not `Nothing` it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If `cancellable` is not `Nothing`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error `IOErrorEnumCancelled` will be returned.

Since: 2.22

## acceptSocketAsync

socketListenerAcceptSocketAsync Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsCancellable b) |
=> a | `listener`: a `SocketListener`
-> Maybe b | `cancellable`: a `Cancellable`, or `Nothing`
-> Maybe AsyncReadyCallback | `callback`: a `AsyncReadyCallback`
-> m () |

This is the asynchronous version of `socketListenerAcceptSocket`.

When the operation is finished `callback` will be called. You can then call `socketListenerAcceptSocketFinish` to get the result of the operation.

Since: 2.22

## acceptSocketFinish

socketListenerAcceptSocketFinish Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsAsyncResult b) |
=> a | `listener`: a `SocketListener`
-> b | `result`: a `AsyncResult`.
-> m (Socket, Maybe Object) | Returns: a `Socket` on success, `Nothing` on error. (Can throw `GError`)

Finishes an async accept operation. See `socketListenerAcceptSocketAsync`

Since: 2.22

## addAddress

socketListenerAddAddress Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsSocketAddress b, IsObject c) |
=> a | `listener`: a `SocketListener`
-> b | `address`: a `SocketAddress`
-> SocketType | `type`: a `SocketType`
-> SocketProtocol | `protocol`: a `SocketProtocol`
-> Maybe c | `sourceObject`: Optional `Object` identifying this source
-> m SocketAddress | (Can throw `GError`)

Creates a socket of type `type` and protocol `protocol`, binds it to `address` and adds it to the set of sockets we're accepting sockets from.

Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see `socketListenerAddInetPort`.

`sourceObject` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

If successful and `effectiveAddress` is non-`Nothing` then it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: "any port"). This address, if requested, belongs to the caller and must be freed.

Call `socketListenerClose` to stop listening on `address`; this will not be done automatically when you drop your final reference to `listener`, as references may be held internally.

Since: 2.22

## addAnyInetPort

socketListenerAddAnyInetPort Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsObject b) |
=> a | `listener`: a `SocketListener`
-> Maybe b | `sourceObject`: Optional `Object` identifying this source
-> m Word16 | Returns: the port number, or 0 in case of failure. (Can throw `GError`)

Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).

This is useful if you need to have a socket for incoming connections but don't care about the specific port number.

`sourceObject` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

Since: 2.24

## addInetPort

socketListenerAddInetPort Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsObject b) |
=> a | `listener`: a `SocketListener`
-> Word16 | `port`: an IP port number (non-zero)
-> Maybe b | `sourceObject`: Optional `Object` identifying this source
-> m () | (Can throw `GError`)

Helper function for `socketListenerAddAddress` that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.

`sourceObject` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

Call `socketListenerClose` to stop listening on `port`; this will not be done automatically when you drop your final reference to `listener`, as references may be held internally.

Since: 2.22

## addSocket

socketListenerAddSocket Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a, IsSocket b, IsObject c) |
=> a | `listener`: a `SocketListener`
-> b | `socket`: a listening `Socket`
-> Maybe c | `sourceObject`: Optional `Object` identifying this source
-> m () | (Can throw `GError`)

Adds `socket` to the set of sockets that we try to accept new clients from. The socket must be bound to a local address and listened to.

`sourceObject` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

The `socket` will not be automatically closed when the `listener` is finalized unless the listener held the final reference to the socket. Before GLib 2.42, the `socket` was automatically closed on finalization of the `listener`, even if references to it were held elsewhere.

Since: 2.22

## close

socketListenerClose Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a) |
=> a | `listener`: a `SocketListener`
-> m () |

Closes all the sockets in the listener.

Since: 2.22

## new

socketListenerNew Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> m SocketListener | Returns: a new `SocketListener`.

Creates a new `SocketListener` with no sockets to listen for. New listeners can be added with e.g. `socketListenerAddAddress` or `socketListenerAddInetPort`.

Since: 2.22

## setBacklog

socketListenerSetBacklog Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocketListener a) |
=> a | `listener`: a `SocketListener`
-> Int32 | `listenBacklog`: an integer
-> m () |

Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the `SocketListener` (for example, by calling `socketListenerAddInetPort`) to be effective.

See `socketSetListenBacklog` for details

Since: 2.22

# Properties

## listenBacklog

The number of outstanding connections in the listen queue.

Since: 2.22

constructSocketListenerListenBacklog :: (IsSocketListener o, MonadIO m) => Int32 -> m (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`listen-backlog`” property. This is rarely needed directly, but it is used by `new`.

getSocketListenerListenBacklog :: (MonadIO m, IsSocketListener o) => o -> m Int32 Source #

Get the value of the “`listen-backlog`” property. When overloading is enabled, this is equivalent to

    get socketListener #listenBacklog

setSocketListenerListenBacklog :: (MonadIO m, IsSocketListener o) => o -> Int32 -> m () Source #

Set the value of the “`listen-backlog`” property. When overloading is enabled, this is equivalent to

    set socketListener [ #listenBacklog := value ]

# Signals

## event

type SocketListenerEventCallback Source #

Arguments

= SocketListenerEvent | `event`: the event that is occurring
-> Socket | `socket`: the `Socket` the event is occurring on
-> IO () |

Emitted when `listener`'s activity on `socket` changes state. Note that when `listener` is used to listen on both IPv4 and IPv6, a separate set of signals will be emitted for each, and the order they happen in is undefined.

Since: 2.46

afterSocketListenerEvent :: (IsSocketListener a, MonadIO m) => a -> ((?self :: a) => SocketListenerEventCallback) -> m SignalHandlerId Source #

Connect a signal handler for the event signal, to be run after the default handler. When overloading is enabled, this is equivalent to

    after socketListener #event callback

By default the object invoking the signal is not passed to the callback. If you need to access it, you can use the implit `?self` parameter. Note that this requires activating the `ImplicitParams` GHC extension.

onSocketListenerEvent :: (IsSocketListener a, MonadIO m) => a -> ((?self :: a) => SocketListenerEventCallback) -> m SignalHandlerId Source #

Connect a signal handler for the event signal, to be run before the default handler. When overloading is enabled, this is equivalent to

    on socketListener #event callback