Interface DatagramBased
- All Superinterfaces:
Proxy
- All Known Subinterfaces:
DtlsClientConnection,DtlsConnection,DtlsServerConnection
- All Known Implementing Classes:
DatagramBased.DatagramBased$Impl,DtlsClientConnection.DtlsClientConnection$Impl,DtlsConnection.DtlsConnection$Impl,DtlsServerConnection.DtlsServerConnection$Impl,Socket
A GDatagramBased is a networking interface for representing datagram-based
communications. It is a more or less direct mapping of the core parts of the
BSD socket API in a portable GObject interface. It is implemented by
Socket, which wraps the UNIX socket API on UNIX and winsock2 on Windows.
GDatagramBased is entirely platform independent, and is intended to be used
alongside higher-level networking APIs such as IOStream.
It uses vectored scatter/gather I/O by default, allowing for many messages
to be sent or received in a single call. Where possible, implementations of
the interface should take advantage of vectored I/O to minimise processing
or system calls. For example, GSocket uses recvmmsg() and sendmmsg()
where possible. Callers should take advantage of scatter/gather I/O (the use of
multiple buffers per message) to avoid unnecessary copying of data to
assemble or disassemble a message.
Each GDatagramBased operation has a timeout parameter which may be negative
for blocking behaviour, zero for non-blocking behaviour, or positive for
timeout behaviour. A blocking operation blocks until finished or there is an
error. A non-blocking operation will return immediately with a
G_IO_ERROR_WOULD_BLOCK error if it cannot make progress. A timeout operation
will block until the operation is complete or the timeout expires; if the
timeout expires it will return what progress it made, or
G_IO_ERROR_TIMED_OUT if no progress was made. To know when a call would
successfully run you can call conditionCheck(java.util.Set<org.gnome.glib.IOCondition>) or
conditionWait(java.util.Set<org.gnome.glib.IOCondition>, long, org.gnome.gio.Cancellable). You can also use
createSource(java.util.Set<org.gnome.glib.IOCondition>, org.gnome.gio.Cancellable) and attach it to a GLib.MainContext
to get callbacks when I/O is possible.
When running a non-blocking operation applications should always be able to
handle getting a G_IO_ERROR_WOULD_BLOCK error even when some other function
said that I/O was possible. This can easily happen in case of a race
condition in the application, but it can also happen for other reasons. For
instance, on Windows a socket is always seen as writable until a write
returns G_IO_ERROR_WOULD_BLOCK.
As with GSocket, GDatagramBaseds can be either connection oriented (for
example, SCTP) or connectionless (for example, UDP). GDatagramBaseds must be
datagram-based, not stream-based. The interface does not cover connection
establishment — use methods on the underlying type to establish a connection
before sending and receiving data through the GDatagramBased API. For
connectionless socket types the target/source address is specified or
received in each I/O operation.
Like most other APIs in GLib, GDatagramBased is not inherently thread safe.
To use a GDatagramBased concurrently from multiple threads, you must
implement your own locking.
- Since:
- 2.48
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classThe DatagramBased$Impl type represents a native instance of the DatagramBased interface.static classProvides an interface for socket-like objects which have datagram semantics, following the Berkeley sockets API. -
Method Summary
Modifier and TypeMethodDescriptiondefault Set<IOCondition> conditionCheck(Set<IOCondition> condition) Checks on the readiness of this DatagramBased to perform operations.default Set<IOCondition> conditionCheck(IOCondition... condition) Checks on the readiness of this DatagramBased to perform operations.default booleanconditionWait(Set<IOCondition> condition, long timeout, @Nullable Cancellable cancellable) Waits for up totimeoutmicroseconds for condition to become true ondatagramBased.If the condition is met,trueis returned.default booleanconditionWait(IOCondition condition, long timeout, @Nullable Cancellable cancellable) Waits for up totimeoutmicroseconds for condition to become true ondatagramBased.If the condition is met,trueis returned.default SourcecreateSource(Set<IOCondition> condition, @Nullable Cancellable cancellable) Creates aGSourcethat can be attached to aGMainContextto monitor for the availability of the specifiedconditionon theGDatagramBased.default SourcecreateSource(IOCondition condition, @Nullable Cancellable cancellable) Creates aGSourcethat can be attached to aGMainContextto monitor for the availability of the specifiedconditionon theGDatagramBased.static @Nullable TypegetType()Get the GType of the DatagramBased classdefault intreceiveMessages(@Nullable InputMessage @Nullable [] messages, int flags, long timeout, @Nullable Cancellable cancellable) Receive one or more data messages from this DatagramBased in one go.default intsendMessages(@Nullable OutputMessage @Nullable [] messages, int flags, long timeout, @Nullable Cancellable cancellable) Send one or more data messages from this DatagramBased in one go.
-
Method Details
-
getType
-
conditionCheck
Checks on the readiness of this DatagramBased to perform operations. The operations specified inconditionare checked for and masked against the currently-satisfied conditions ondatagramBased.The result is returned.IOCondition.INwill be set in the return value if data is available to read with g_datagram_based_receive_messages(), or if the connection is closed remotely (EOS); and if the datagram_based has not been closed locally using some implementation-specific method (such as g_socket_close() or g_socket_shutdown() withshutdownReadset, if it’s aGSocket).If the connection is shut down or closed (by calling g_socket_close() or g_socket_shutdown() with
shutdownReadset, if it’s aGSocket, for example), all calls to this function will returnIOErrorEnum.CLOSED.IOCondition.OUTwill be set if it is expected that at least one byte can be sent using g_datagram_based_send_messages() without blocking. It will not be set if the datagram_based has been closed locally.IOCondition.HUPwill be set if the connection has been closed locally.IOCondition.ERRwill be set if there was an asynchronous error in transmitting data previously enqueued using g_datagram_based_send_messages().Note that on Windows, it is possible for an operation to return
IOErrorEnum.WOULD_BLOCKeven immediately after g_datagram_based_condition_check() has claimed that theGDatagramBasedis ready for writing. Rather than calling g_datagram_based_condition_check() and then writing to theGDatagramBasedif it succeeds, it is generally better to simply try writing right away, and try again later if the initial attempt returnsIOErrorEnum.WOULD_BLOCK.It is meaningless to specify
IOCondition.ERRorIOCondition.HUPincondition;these conditions will always be set in the output if they are true. Apart from these flags, the output is guaranteed to be masked bycondition.This call never blocks.
- Parameters:
condition- aGIOConditionmask to check- Returns:
- the
GIOConditionmask of the current state - Since:
- 2.48
-
conditionCheck
Checks on the readiness of this DatagramBased to perform operations. The operations specified inconditionare checked for and masked against the currently-satisfied conditions ondatagramBased.The result is returned.IOCondition.INwill be set in the return value if data is available to read with g_datagram_based_receive_messages(), or if the connection is closed remotely (EOS); and if the datagram_based has not been closed locally using some implementation-specific method (such as g_socket_close() or g_socket_shutdown() withshutdownReadset, if it’s aGSocket).If the connection is shut down or closed (by calling g_socket_close() or g_socket_shutdown() with
shutdownReadset, if it’s aGSocket, for example), all calls to this function will returnIOErrorEnum.CLOSED.IOCondition.OUTwill be set if it is expected that at least one byte can be sent using g_datagram_based_send_messages() without blocking. It will not be set if the datagram_based has been closed locally.IOCondition.HUPwill be set if the connection has been closed locally.IOCondition.ERRwill be set if there was an asynchronous error in transmitting data previously enqueued using g_datagram_based_send_messages().Note that on Windows, it is possible for an operation to return
IOErrorEnum.WOULD_BLOCKeven immediately after g_datagram_based_condition_check() has claimed that theGDatagramBasedis ready for writing. Rather than calling g_datagram_based_condition_check() and then writing to theGDatagramBasedif it succeeds, it is generally better to simply try writing right away, and try again later if the initial attempt returnsIOErrorEnum.WOULD_BLOCK.It is meaningless to specify
IOCondition.ERRorIOCondition.HUPincondition;these conditions will always be set in the output if they are true. Apart from these flags, the output is guaranteed to be masked bycondition.This call never blocks.
- Parameters:
condition- aGIOConditionmask to check- Returns:
- the
GIOConditionmask of the current state - Since:
- 2.48
-
conditionWait
default boolean conditionWait(Set<IOCondition> condition, long timeout, @Nullable Cancellable cancellable) throws GErrorException Waits for up totimeoutmicroseconds for condition to become true ondatagramBased.If the condition is met,trueis returned.If
cancellableis cancelled before the condition is met, or iftimeoutis reached before the condition is met, thenfalseis returned anderroris set appropriately (IOErrorEnum.CANCELLEDorIOErrorEnum.TIMED_OUT).- Parameters:
condition- aGIOConditionmask to wait fortimeout- the maximum time (in microseconds) to wait, 0 to not block, or -1 to block indefinitelycancellable- aGCancellable- Returns:
trueif the condition was met,falseotherwise- Throws:
GErrorException- seeGError- Since:
- 2.48
-
conditionWait
default boolean conditionWait(IOCondition condition, long timeout, @Nullable Cancellable cancellable) throws GErrorException Waits for up totimeoutmicroseconds for condition to become true ondatagramBased.If the condition is met,trueis returned.If
cancellableis cancelled before the condition is met, or iftimeoutis reached before the condition is met, thenfalseis returned anderroris set appropriately (IOErrorEnum.CANCELLEDorIOErrorEnum.TIMED_OUT).- Parameters:
condition- aGIOConditionmask to wait fortimeout- the maximum time (in microseconds) to wait, 0 to not block, or -1 to block indefinitelycancellable- aGCancellable- Returns:
trueif the condition was met,falseotherwise- Throws:
GErrorException- seeGError- Since:
- 2.48
-
createSource
Creates aGSourcethat can be attached to aGMainContextto monitor for the availability of the specifiedconditionon theGDatagramBased. TheGSourcekeeps a reference to thedatagramBased.The callback on the source is of the
GDatagramBasedSourceFunctype.It is meaningless to specify
IOCondition.ERRorIOCondition.HUPincondition;these conditions will always be reported in the callback if they are true.If non-
null,cancellablecan be used to cancel the source, which will cause the source to trigger, reporting the current condition (which is likely 0 unless cancellation happened at the same time as a condition change). You can check for this in the callback using g_cancellable_is_cancelled().- Parameters:
condition- aGIOConditionmask to monitorcancellable- aGCancellable- Returns:
- a newly allocated
GSource - Since:
- 2.48
-
createSource
Creates aGSourcethat can be attached to aGMainContextto monitor for the availability of the specifiedconditionon theGDatagramBased. TheGSourcekeeps a reference to thedatagramBased.The callback on the source is of the
GDatagramBasedSourceFunctype.It is meaningless to specify
IOCondition.ERRorIOCondition.HUPincondition;these conditions will always be reported in the callback if they are true.If non-
null,cancellablecan be used to cancel the source, which will cause the source to trigger, reporting the current condition (which is likely 0 unless cancellation happened at the same time as a condition change). You can check for this in the callback using g_cancellable_is_cancelled().- Parameters:
condition- aGIOConditionmask to monitorcancellable- aGCancellable- Returns:
- a newly allocated
GSource - Since:
- 2.48
-
receiveMessages
default int receiveMessages(@Nullable InputMessage @Nullable [] messages, int flags, long timeout, @Nullable Cancellable cancellable) throws GErrorException Receive one or more data messages from this DatagramBased in one go.messagesmust point to an array ofGInputMessagestructs andnumMessagesmust be the length of this array. EachGInputMessagecontains a pointer to an array ofGInputVectorstructs describing the buffers that the data received in each message will be written to.flagsmodify how all messages are received. The commonly available arguments for this are available in theGSocketMsgFlagsenum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too. These flags affect the overall receive operation. Flags affecting individual messages are returned inGInputMessage.flags.The other members of
GInputMessageare treated as described in its documentation.If
timeoutis negative the call will block untilnumMessageshave been received, the connection is closed remotely (EOS),cancellableis cancelled, or an error occurs.If
timeoutis 0 the call will return up tonumMessageswithout blocking, orIOErrorEnum.WOULD_BLOCKif no messages are queued in the operating system to be received.If
timeoutis positive the call will block on the same conditions as iftimeoutwere negative. If the timeout is reached before any messages are received,IOErrorEnum.TIMED_OUTis returned, otherwise it will return the number of messages received before timing out. (Note: This is effectively the behaviour ofMSG_WAITFORONEwith recvmmsg().)To be notified when messages are available, wait for the
IOCondition.INcondition. Note though that you may still receiveIOErrorEnum.WOULD_BLOCKfrom g_datagram_based_receive_messages() even if you were previously notified of aIOCondition.INcondition.If the remote peer closes the connection, any messages queued in the underlying receive buffer will be returned, and subsequent calls to g_datagram_based_receive_messages() will return 0 (with no error set).
If the connection is shut down or closed (by calling g_socket_close() or g_socket_shutdown() with
shutdownReadset, if it’s aGSocket, for example), all calls to this function will returnIOErrorEnum.CLOSED.On error -1 is returned and
erroris set accordingly. An error will only be returned if zero messages could be received; otherwise the number of messages successfully received before the error will be returned. Ifcancellableis cancelled,IOErrorEnum.CANCELLEDis returned as with any other error.- Parameters:
messages- an array ofGInputMessagestructsflags- an int containingGSocketMsgFlagsflags for the overall operationtimeout- the maximum time (in microseconds) to wait, 0 to not block, or -1 to block indefinitelycancellable- aGCancellable- Returns:
- number of messages received, or -1 on error. Note that the number
of messages received may be smaller than
numMessagesiftimeoutis zero or positive, if the peer closed the connection, or ifnumMessageswas larger thanUIO_MAXIOV(1024), in which case the caller may re-try to receive the remaining messages. - Throws:
GErrorException- seeGError- Since:
- 2.48
-
sendMessages
default int sendMessages(@Nullable OutputMessage @Nullable [] messages, int flags, long timeout, @Nullable Cancellable cancellable) throws GErrorException Send one or more data messages from this DatagramBased in one go.messagesmust point to an array ofGOutputMessagestructs andnumMessagesmust be the length of this array. EachGOutputMessagecontains an address to send the data to, and a pointer to an array ofGOutputVectorstructs to describe the buffers that the data to be sent for each message will be gathered from.flagsmodify how the message is sent. The commonly available arguments for this are available in theGSocketMsgFlagsenum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too.The other members of
GOutputMessageare treated as described in its documentation.If
timeoutis negative the call will block untilnumMessageshave been sent,cancellableis cancelled, or an error occurs.If
timeoutis 0 the call will send up tonumMessageswithout blocking, or will returnIOErrorEnum.WOULD_BLOCKif there is no space to send messages.If
timeoutis positive the call will block on the same conditions as iftimeoutwere negative. If the timeout is reached before any messages are sent,IOErrorEnum.TIMED_OUTis returned, otherwise it will return the number of messages sent before timing out.To be notified when messages can be sent, wait for the
IOCondition.OUTcondition. Note though that you may still receiveIOErrorEnum.WOULD_BLOCKfrom g_datagram_based_send_messages() even if you were previously notified of aIOCondition.OUTcondition. (On Windows in particular, this is very common due to the way the underlying APIs work.)If the connection is shut down or closed (by calling g_socket_close() or g_socket_shutdown() with
shutdownWriteset, if it’s aGSocket, for example), all calls to this function will returnIOErrorEnum.CLOSED.On error -1 is returned and
erroris set accordingly. An error will only be returned if zero messages could be sent; otherwise the number of messages successfully sent before the error will be returned. Ifcancellableis cancelled,IOErrorEnum.CANCELLEDis returned as with any other error.- Parameters:
messages- an array ofGOutputMessagestructsflags- an int containingGSocketMsgFlagsflagstimeout- the maximum time (in microseconds) to wait, 0 to not block, or -1 to block indefinitelycancellable- aGCancellable- Returns:
- number of messages sent, or -1 on error. Note that the number of
messages sent may be smaller than
numMessagesiftimeoutis zero or positive, or ifnumMessageswas larger thanUIO_MAXIOV(1024), in which case the caller may re-try to send the remaining messages. - Throws:
GErrorException- seeGError- Since:
- 2.48
-