Socket

A GSocket is a low-level networking primitive. It is a more or less direct mapping of the BSD socket API in a portable GObject based API. It supports both the UNIX socket implementations and winsock2 on Windows.

GSocket is the platform independent base upon which the higher level network primitives are based. Applications are not typically meant to use it directly, but rather through classes like GSocketClient, GSocketService and GSocketConnection. However there may be cases where direct use of GSocket is useful.

GSocket implements the GInitable interface, so if it is manually constructed by e.g. g_object_new() you must call g_initable_init() and check the results before using the object. This is done automatically in g_socket_new() and g_socket_new_from_fd(), so these functions can return NULL.

Sockets operate in two general modes, blocking or non-blocking. When in blocking mode all operations block until the requested operation is finished or there is an error. In non-blocking mode all calls that would block return immediately with a G_IO_ERROR_WOULD_BLOCK error. To know when a call would successfully run you can call g_socket_condition_check(), or g_socket_condition_wait(). You can also use g_socket_create_source() and attach it to a GMainContext to get callbacks when I/O is possible. Note that all sockets are always set to non blocking mode in the system, and blocking mode is emulated in GSocket.

When working in non-blocking mode 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.

GSockets can be either connection oriented or datagram based. For connection oriented types you must first establish a connection by either connecting to an address or accepting a connection from another address. For connectionless socket types the target/source address is specified or received in each I/O operation.

All socket file descriptors are set to be close-on-exec.

Note that creating a GSocket causes the signal SIGPIPE to be ignored for the remainder of the program. If you are writing a command-line utility that uses GSocket, you may need to take into account the fact that your program will not automatically be killed if it tries to write to stdout after it has been closed.

class Socket : ObjectG , InitableIF {}

Constructors

this
this(GSocket* gSocket)

Sets our main struct and passes it to the parent class

this
this(GSocketFamily family, GSocketType type, GSocketProtocol protocol)

Creates a new GSocket with the defined family, type and protocol. If protocol is 0 (G_SOCKET_PROTOCOL_DEFAULT) the default protocol type for the family and type is used. The protocol is a family and type specific int that specifies what kind of protocol to use. GSocketProtocol lists several common ones. Many families only support one protocol, and use 0 for this, others support several and using 0 means to use the default protocol for the family and type. The protocol id is passed directly to the operating system, so you can use protocols not listed in GSocketProtocol if you know the protocol number used for it. Since 2.22

this
this(int fd)

Creates a new GSocket from a native file descriptor or winsock SOCKET handle. This reads all the settings from the file descriptor so that all properties should work. Note that the file descriptor will be set to non-blocking mode, independent on the blocking mode of the GSocket. Since 2.22

Members

Functions

accept
GSocket* accept(Cancellable cancellable)

Accept incoming connections on a connection-based socket. This removes the first outstanding connection request from the listening socket and creates a GSocket object for it. The socket must be bound to a local address with g_socket_bind() and must be listening for incoming connections (g_socket_listen()). If there are no outstanding connections then the operation will block or return G_IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled. To be notified of an incoming connection, wait for the G_IO_IN condition. Since 2.22

bind
int bind(SocketAddress address, int allowReuse)

When a socket is created it is attached to an address family, but it doesn't have an address in this family. g_socket_bind() assigns the address (sometimes called name) of the socket. It is generally required to bind to a local address before you can receive connections. (See g_socket_listen() and g_socket_accept() ). In certain situations, you may also want to bind a socket that will be used to initiate connections, though this is not normally required. If socket is a TCP socket, then allow_reuse controls the setting of the SO_REUSEADDR socket option; normally it should be TRUE for server sockets (sockets that you will eventually call g_socket_accept() on), and FALSE for client sockets. (Failing to set this flag on a server socket may cause g_socket_bind() to return G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then immediately restarted.) If socket is a UDP socket, then allow_reuse determines whether or not other UDP sockets can be bound to the same address at the same time. In particular, you can have several UDP sockets bound to the same address, and they will all receive all of the multicast and broadcast packets sent to that address. (The behavior of unicast UDP packets to an address with multiple listeners is not defined.) Since 2.22

checkConnectResult
int checkConnectResult()

Checks and resets the pending connect error for the socket. This is used to check for errors when g_socket_connect() is used in non-blocking mode. Since 2.22

close
int close()

Closes the socket, shutting down any active connection. Closing a socket does not wait for all outstanding I/O operations to finish, so the caller should not rely on them to be guaranteed to complete even if the close returns with no error. Once the socket is closed, all other operations will return G_IO_ERROR_CLOSED. Closing a socket multiple times will not return an error. Sockets will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible. Beware that due to the way that TCP works, it is possible for recently-sent data to be lost if either you close a socket while the G_IO_IN condition is set, or else if the remote connection tries to send something to you after you close the socket but before it has finished reading all of the data you sent. There is no easy generic way to avoid this problem; the easiest fix is to design the network protocol such that the client will never send data "out of turn". Another solution is for the server to half-close the connection by calling g_socket_shutdown() with only the shutdown_write flag set, and then wait for the client to notice this and close its side of the connection, after which the server can safely call g_socket_close(). (This is what GTcpConnection does if you call g_tcp_connection_set_graceful_disconnect(). But of course, this only works if the client will close its connection after the server does.) Since 2.22

conditionCheck
GIOCondition conditionCheck(GIOCondition condition)

Checks on the readiness of socket to perform operations. The operations specified in condition are checked for and masked against the currently-satisfied conditions on socket. The result is returned. Note that on Windows, it is possible for an operation to return G_IO_ERROR_WOULD_BLOCK even immediately after g_socket_condition_check() has claimed that the socket is ready for writing. Rather than calling g_socket_condition_check() and then writing to the socket if it succeeds, it is generally better to simply try writing to the socket right away, and try again later if the initial attempt returns G_IO_ERROR_WOULD_BLOCK. It is meaningless to specify G_IO_ERR or G_IO_HUP in condition; these conditions will always be set in the output if they are true. This call never blocks. Since 2.22

conditionTimedWait
int conditionTimedWait(GIOCondition condition, long timeout, Cancellable cancellable)

Waits for up to timeout microseconds for condition to become true on socket. If the condition is met, TRUE is returned. If cancellable is cancelled before the condition is met, or if timeout (or the socket's "timeout") is reached before the condition is met, then FALSE is returned and error, if non-NULL, is set to the appropriate value (G_IO_ERROR_CANCELLED or G_IO_ERROR_TIMED_OUT). If you don't want a timeout, use g_socket_condition_wait(). (Alternatively, you can pass -1 for timeout.) Note that although timeout is in microseconds for consistency with other GLib APIs, this function actually only has millisecond resolution, and the behavior is undefined if timeout is not an exact number of milliseconds. Since 2.32

conditionWait
int conditionWait(GIOCondition condition, Cancellable cancellable)

Waits for condition to become true on socket. When the condition is met, TRUE is returned. If cancellable is cancelled before the condition is met, or if the socket has a timeout set and it is reached before the condition is met, then FALSE is returned and error, if non-NULL, is set to the appropriate value (G_IO_ERROR_CANCELLED or G_IO_ERROR_TIMED_OUT). See also g_socket_condition_timed_wait(). Since 2.22

connect
int connect(SocketAddress address, Cancellable cancellable)

Connect the socket to the specified remote address. For connection oriented socket this generally means we attempt to make a connection to the address. For a connection-less socket it sets the default address for g_socket_send() and discards all incoming datagrams from other sources. Generally connection oriented sockets can only connect once, but connection-less sockets can connect multiple times to change the default address. If the connect call needs to do network I/O it will block, unless non-blocking I/O is enabled. Then G_IO_ERROR_PENDING is returned and the user can be notified of the connection finishing by waiting for the G_IO_OUT condition. The result of the connection must then be checked with g_socket_check_connect_result(). Since 2.22

createSource
Source createSource(GIOCondition condition, Cancellable cancellable)

Creates a GSource that can be attached to a GMainContext to monitor for the availibility of the specified condition on the socket. The callback on the source is of the GSocketSourceFunc type. It is meaningless to specify G_IO_ERR or G_IO_HUP in condition; these conditions will always be reported output if they are true. cancellable if not NULL can 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(). If socket has a timeout set, and it is reached before condition occurs, the source will then trigger anyway, reporting G_IO_IN or G_IO_OUT depending on condition. However, socket will have been marked as having had a timeout, and so the next GSocket I/O method you call will then fail with a G_IO_ERROR_TIMED_OUT. Since 2.22

getAvailableBytes
gssize getAvailableBytes()

Get the amount of data pending in the OS input buffer. If socket is a UDP or SCTP socket, this will return the size of just the next packet, even if additional packets are buffered after that one. Note that on Windows, this function is rather inefficient in the UDP case, and so if you know any plausible upper bound on the size of the incoming packet, it is better to just do a g_socket_receive() with a buffer of that size, rather than calling g_socket_get_available_bytes() first and then doing a receive of exactly the right size. Since 2.32

getBlocking
int getBlocking()

Gets the blocking mode of the socket. For details on blocking I/O, see g_socket_set_blocking(). Since 2.22

getBroadcast
int getBroadcast()

Gets the broadcast setting on socket; if TRUE, it is possible to send packets to broadcast addresses. Since 2.32

getCredentials
Credentials getCredentials()

Returns the credentials of the foreign process connected to this socket, if any (e.g. it is only supported for G_SOCKET_FAMILY_UNIX sockets). If this operation isn't supported on the OS, the method fails with the G_IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented by reading the SO_PEERCRED option on the underlying socket. Other ways to obtain credentials from a foreign peer includes the GUnixCredentialsMessage type and g_unix_connection_send_credentials() / g_unix_connection_receive_credentials() functions. Since 2.26

getFamily
GSocketFamily getFamily()

Gets the socket family of the socket. Since 2.22

getFd
int getFd()

Returns the underlying OS socket object. On unix this is a socket file descriptor, and on Windows this is a Winsock2 SOCKET handle. This may be useful for doing platform specific or otherwise unusual operations on the socket. Since 2.22

getKeepalive
int getKeepalive()

Gets the keepalive mode of the socket. For details on this, see g_socket_set_keepalive(). Since 2.22

getListenBacklog
int getListenBacklog()

Gets the listen backlog setting of the socket. For details on this, see g_socket_set_listen_backlog(). Since 2.22

getLocalAddress
SocketAddress getLocalAddress()

Try to get the local address of a bound socket. This is only useful if the socket has been bound to a local address, either explicitly or implicitly when connecting. Since 2.22

getMulticastLoopback
int getMulticastLoopback()

Gets the multicast loopback setting on socket; if TRUE (the default), outgoing multicast packets will be looped back to multicast listeners on the same host. Since 2.32

getMulticastTtl
uint getMulticastTtl()

Gets the multicast time-to-live setting on socket; see g_socket_set_multicast_ttl() for more details. Since 2.32

getOption
int getOption(int level, int optname, int value)

Gets the value of an integer-valued option on socket, as with getsockopt(). (If you need to fetch a non-integer-valued option, you will need to call getsockopt() directly.) The <gio/gnetworking.h> header pulls in system headers that will define most of the standard/portable socket options. For unusual socket protocols or platform-dependent options, you may need to include additional headers. Note that even for socket options that are a single byte in size, value is still a pointer to a gint variable, not a guchar; g_socket_get_option() will handle the conversion internally. Since 2.36

getProtocol
GSocketProtocol getProtocol()

Gets the socket protocol id the socket was created with. In case the protocol is unknown, -1 is returned. Since 2.22

getRemoteAddress
SocketAddress getRemoteAddress()

Try to get the remove address of a connected socket. This is only useful for connection oriented sockets that have been connected. Since 2.22

getSocketStruct
GSocket* getSocketStruct()
Undocumented in source. Be warned that the author may not have intended to support it.
getSocketType
GSocketType getSocketType()

Gets the socket type of the socket. Since 2.22

getStruct
void* getStruct()

the main Gtk struct as a void*

getTimeout
uint getTimeout()

Gets the timeout setting of the socket. For details on this, see g_socket_set_timeout(). Since 2.26

getTtl
uint getTtl()

Gets the unicast time-to-live setting on socket; see g_socket_set_ttl() for more details. Since 2.32

isClosed
int isClosed()

Checks whether a socket is closed. Since 2.22

isConnected
int isConnected()

Check whether the socket is connected. This is only useful for connection-oriented sockets. Since 2.22

joinMulticastGroup
int joinMulticastGroup(InetAddress group, int sourceSpecific, string iface)

Registers socket to receive multicast messages sent to group. socket must be a G_SOCKET_TYPE_DATAGRAM socket, and must have been bound to an appropriate interface and port with g_socket_bind(). If iface is NULL, the system will automatically pick an interface to bind to based on group. If source_specific is TRUE, source-specific multicast as defined in RFC 4604 is used. Note that on older platforms this may fail with a G_IO_ERROR_NOT_SUPPORTED error. Since 2.32

leaveMulticastGroup
int leaveMulticastGroup(InetAddress group, int sourceSpecific, string iface)

Removes socket from the multicast group defined by group, iface, and source_specific (which must all have the same values they had when you joined the group). socket remains bound to its address and port, and can still receive unicast messages after calling this. Since 2.32

listen
int listen()

Marks the socket as a server socket, i.e. a socket that is used to accept incoming requests using g_socket_accept(). Before calling this the socket must be bound to a local address using g_socket_bind(). To set the maximum amount of outstanding clients, use g_socket_set_listen_backlog(). Since 2.22

receive
gssize receive(string buffer, gsize size, Cancellable cancellable)

Receive data (up to size bytes) from a socket. This is mainly used by connection-oriented sockets; it is identical to g_socket_receive_from() with address set to NULL. For G_SOCKET_TYPE_DATAGRAM and G_SOCKET_TYPE_SEQPACKET sockets, g_socket_receive() will always read either 0 or 1 complete messages from the socket. If the received message is too large to fit in buffer, then the data beyond size bytes will be discarded, without any explicit indication that this has occurred. For G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any number of bytes, up to size. If more than size bytes have been received, the additional data will be returned in future calls to g_socket_receive(). If the socket is in blocking mode the call will block until there is some data to receive, the connection is closed, or there is an error. If there is no data available and the socket is in non-blocking mode, a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the G_IO_IN condition. On error -1 is returned and error is set accordingly. Since 2.22

receiveFrom
gssize receiveFrom(SocketAddress address, char[] buffer, Cancellable cancellable)

Receive data (up to size bytes) from a socket. If address is non-NULL then address will be set equal to the source address of the received packet. address is owned by the caller. See g_socket_receive() for additional information. Since 2.22

receiveMessage
gssize receiveMessage(SocketAddress address, GInputVector[] vectors, SocketControlMessage[] messages, int flags, Cancellable cancellable)

Receive data from a socket. This is the most complicated and fully-featured version of this call. For easier use, see g_socket_receive() and g_socket_receive_from(). If address is non-NULL then address will be set equal to the source address of the received packet. address is owned by the caller. vector must point to an array of GInputVector structs and num_vectors must be the length of this array. These structs describe the buffers that received data will be scattered into. If num_vectors is -1, then vectors is assumed to be terminated by a GInputVector with a NULL buffer pointer. As a special case, if num_vectors is 0 (in which case, vectors may of course be NULL), then a single byte is received and discarded. This is to facilitate the common practice of sending a single '\0' byte for the purposes of transferring ancillary data. messages, if non-NULL, will be set to point to a newly-allocated array of GSocketControlMessage instances or NULL if no such messages was received. These correspond to the control messages received from the kernel, one GSocketControlMessage per message from the kernel. This array is NULL-terminated and must be freed by the caller using g_free() after calling g_object_unref() on each element. If messages is NULL, any control messages received will be discarded. num_messages, if non-NULL, will be set to the number of control messages received. If both messages and num_messages are non-NULL, then num_messages gives the number of GSocketControlMessage instances in messages (ie: not including the NULL terminator). flags is an in/out parameter. The commonly available arguments for this are available in the GSocketMsgFlags enum, 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 (and g_socket_receive_message() may pass system-specific flags out). As with g_socket_receive(), data may be discarded if socket is G_SOCKET_TYPE_DATAGRAM or G_SOCKET_TYPE_SEQPACKET and you do not provide enough buffer space to read a complete message. You can pass G_SOCKET_MSG_PEEK in flags to peek at the current message without removing it from the receive queue, but there is no portable way to find out the length of the message other than by reading it into a sufficiently-large buffer. If the socket is in blocking mode the call will block until there is some data to receive, the connection is closed, or there is an error. If there is no data available and the socket is in non-blocking mode, a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the G_IO_IN condition. On error -1 is returned and error is set accordingly. Since 2.22

receiveWithBlocking
gssize receiveWithBlocking(string buffer, gsize size, int blocking, Cancellable cancellable)

This behaves exactly the same as g_socket_receive(), except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by socket's properties. Since 2.26

send
gssize send(string buffer, gsize size, Cancellable cancellable)

Tries to send size bytes from buffer on the socket. This is mainly used by connection-oriented sockets; it is identical to g_socket_send_to() with address set to NULL. If the socket is in blocking mode the call will block until there is space for the data in the socket queue. If there is no space available and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when space is available, wait for the G_IO_OUT condition. Note though that you may still receive G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a G_IO_OUT condition. (On Windows in particular, this is very common due to the way the underlying APIs work.) On error -1 is returned and error is set accordingly. Since 2.22

sendMessage
gssize sendMessage(SocketAddress address, GOutputVector[] vectors, GSocketControlMessage[] messages, int flags, Cancellable cancellable)

Send data to address on socket. This is the most complicated and fully-featured version of this call. For easier use, see g_socket_send() and g_socket_send_to(). If address is NULL then the message is sent to the default receiver (set by g_socket_connect()). vectors must point to an array of GOutputVector structs and num_vectors must be the length of this array. (If num_vectors is -1, then vectors is assumed to be terminated by a GOutputVector with a NULL buffer pointer.) The GOutputVector structs describe the buffers that the sent data will be gathered from. Using multiple GOutputVectors is more memory-efficient than manually copying data from multiple sources into a single buffer, and more network-efficient than making multiple calls to g_socket_send(). messages, if non-NULL, is taken to point to an array of num_messages GSocketControlMessage instances. These correspond to the control messages to be sent on the socket. If num_messages is -1 then messages is treated as a NULL-terminated array. flags modify how the message is sent. The commonly available arguments for this are available in the GSocketMsgFlags enum, 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. If the socket is in blocking mode the call will block until there is space for the data in the socket queue. If there is no space available and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when space is available, wait for the G_IO_OUT condition. Note though that you may still receive G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a G_IO_OUT condition. (On Windows in particular, this is very common due to the way the underlying APIs work.) On error -1 is returned and error is set accordingly. Since 2.22

sendTo
gssize sendTo(SocketAddress address, string buffer, gsize size, Cancellable cancellable)

Tries to send size bytes from buffer to address. If address is NULL then the message is sent to the default receiver (set by g_socket_connect()). See g_socket_send() for additional information. Since 2.22

sendWithBlocking
gssize sendWithBlocking(string buffer, gsize size, int blocking, Cancellable cancellable)

This behaves exactly the same as g_socket_send(), except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by socket's properties. Since 2.26

setBlocking
void setBlocking(int blocking)

Sets the blocking mode of the socket. In blocking mode all operations block until they succeed or there is an error. In non-blocking mode all functions return results immediately or with a G_IO_ERROR_WOULD_BLOCK error. All sockets are created in blocking mode. However, note that the platform level socket is always non-blocking, and blocking mode is a GSocket level feature. Since 2.22

setBroadcast
void setBroadcast(int broadcast)

Sets whether socket should allow sending to broadcast addresses. This is FALSE by default. Since 2.32

setKeepalive
void setKeepalive(int keepalive)

Sets or unsets the SO_KEEPALIVE flag on the underlying socket. When this flag is set on a socket, the system will attempt to verify that the remote socket endpoint is still present if a sufficiently long period of time passes with no data being exchanged. If the system is unable to verify the presence of the remote endpoint, it will automatically close the connection. This option is only functional on certain kinds of sockets. (Notably, G_SOCKET_PROTOCOL_TCP sockets.) The exact time between pings is system- and protocol-dependent, but will normally be at least two hours. Most commonly, you would set this flag on a server socket if you want to allow clients to remain idle for long periods of time, but also want to ensure that connections are eventually garbage-collected if clients crash or become unreachable. Since 2.22

setListenBacklog
void setListenBacklog(int backlog)

Sets the maximum number of outstanding connections allowed when listening on this socket. If more clients than this are connecting to the socket and the application is not handling them on time then the new connections will be refused. Note that this must be called before g_socket_listen() and has no effect if called after that. Since 2.22

setMulticastLoopback
void setMulticastLoopback(int loopback)

Sets whether outgoing multicast packets will be received by sockets listening on that multicast address on the same host. This is TRUE by default. Since 2.32

setMulticastTtl
void setMulticastTtl(uint ttl)

Sets the time-to-live for outgoing multicast datagrams on socket. By default, this is 1, meaning that multicast packets will not leave the local network. Since 2.32

setOption
int setOption(int level, int optname, int value)

Sets the value of an integer-valued option on socket, as with setsockopt(). (If you need to set a non-integer-valued option, you will need to call setsockopt() directly.) The <gio/gnetworking.h> header pulls in system headers that will define most of the standard/portable socket options. For unusual socket protocols or platform-dependent options, you may need to include additional headers. Since 2.36

setStruct
void setStruct(GObject* obj)
Undocumented in source. Be warned that the author may not have intended to support it.
setTimeout
void setTimeout(uint timeout)

Sets the time in seconds after which I/O operations on socket will time out if they have not yet completed. On a blocking socket, this means that any blocking GSocket operation will time out after timeout seconds of inactivity, returning G_IO_ERROR_TIMED_OUT. On a non-blocking socket, calls to g_socket_condition_wait() will also fail with G_IO_ERROR_TIMED_OUT after the given time. Sources created with g_socket_create_source() will trigger after timeout seconds of inactivity, with the requested condition set, at which point calling g_socket_receive(), g_socket_send(), g_socket_check_connect_result(), etc, will fail with G_IO_ERROR_TIMED_OUT. If timeout is 0 (the default), operations will never time out on their own. Note that if an I/O operation is interrupted by a signal, this may cause the timeout to be reset. Since 2.26

setTtl
void setTtl(uint ttl)

Sets the time-to-live for outgoing unicast packets on socket. By default the platform-specific default value is used. Since 2.32

shutdown
int shutdown(int shutdownRead, int shutdownWrite)

Shut down part of a full-duplex connection. If shutdown_read is TRUE then the receiving side of the connection is shut down, and further reading is disallowed. If shutdown_write is TRUE then the sending side of the connection is shut down, and further writing is disallowed. It is allowed for both shutdown_read and shutdown_write to be TRUE. One example where this is used is graceful disconnect for TCP connections where you close the sending side, then wait for the other side to close the connection, thus ensuring that the other side saw all sent data. Since 2.22

speaksIpv4
int speaksIpv4()

Checks if a socket is capable of speaking IPv4. IPv4 sockets are capable of speaking IPv4. On some operating systems and under some combinations of circumstances IPv6 sockets are also capable of speaking IPv4. See RFC 3493 section 3.7 for more information. No other types of sockets are currently considered as being capable of speaking IPv4. Since 2.22

Mixins

__anonymous
mixin InitableT!(GSocket)
Undocumented in source.

Variables

gSocket
GSocket* gSocket;

the main Gtk struct

Inherited Members

From ObjectG

gObject
GObject* gObject;

the main Gtk struct

getObjectGStruct
GObject* getObjectGStruct()
Undocumented in source. Be warned that the author may not have intended to support it.
getStruct
void* getStruct()

the main Gtk struct as a void*

isGcRoot
bool isGcRoot;
Undocumented in source.
destroyNotify
void destroyNotify(ObjectG obj)
Undocumented in source. Be warned that the author may not have intended to support it.
toggleNotify
void toggleNotify(ObjectG obj, GObject* object, int isLastRef)
Undocumented in source. Be warned that the author may not have intended to support it.
~this
~this()
Undocumented in source.
getDObject
RT getDObject(U obj)

Gets a D Object from the objects table of associations.

setStruct
void setStruct(GObject* obj)
Undocumented in source. Be warned that the author may not have intended to support it.
setProperty
void setProperty(string propertyName, int value)
setProperty
void setProperty(string propertyName, string value)
setProperty
void setProperty(string propertyName, long value)
setProperty
void setProperty(string propertyName, ulong value)
unref
void unref()
Undocumented in source. Be warned that the author may not have intended to support it.
doref
ObjectG doref()
Undocumented in source. Be warned that the author may not have intended to support it.
connectedSignals
int[string] connectedSignals;
Undocumented in source.
onNotifyListeners
void delegate(ParamSpec, ObjectG)[] onNotifyListeners;
Undocumented in source.
addOnNotify
void addOnNotify(void delegate(ParamSpec, ObjectG) dlg, string property, ConnectFlags connectFlags)

The notify signal is emitted on an object when one of its properties has been changed. Note that getting this signal doesn't guarantee that the value of the property has actually changed, it may also be emitted when the setter for the property is called to reinstate the previous value.

callBackNotify
void callBackNotify(GObject* gobjectStruct, GParamSpec* pspec, ObjectG _objectG)
Undocumented in source. Be warned that the author may not have intended to support it.
classInstallProperty
void classInstallProperty(GObjectClass* oclass, uint propertyId, ParamSpec pspec)

Installs a new property. This is usually done in the class initializer. Note that it is possible to redefine a property in a derived class, by installing a property with the same name. This can be useful at times, e.g. to change the range of allowed values or the default value.

classInstallProperties
void classInstallProperties(GObjectClass* oclass, ParamSpec[] pspecs)

Installs new properties from an array of GParamSpecs. This is usually done in the class initializer. The property id of each property is the index of each GParamSpec in the pspecs array. The property id of 0 is treated specially by GObject and it should not be used to store a GParamSpec. This function should be used if you plan to use a static array of GParamSpecs and g_object_notify_by_pspec(). For instance, this Since 2.26

classFindProperty
ParamSpec classFindProperty(GObjectClass* oclass, string propertyName)

Looks up the GParamSpec for a property of a class.

classListProperties
ParamSpec[] classListProperties(GObjectClass* oclass)

Get an array of GParamSpec* for all properties of a class.

classOverrideProperty
void classOverrideProperty(GObjectClass* oclass, uint propertyId, string name)

Registers property_id as referring to a property with the name name in a parent class or in an interface implemented by oclass. This allows this class to override a property implementation in a parent class or to provide the implementation of a property from an interface. Note Internally, overriding is implemented by creating a property of type GParamSpecOverride; generally operations that query the properties of the object class, such as g_object_class_find_property() or g_object_class_list_properties() will return the overridden property. However, in one case, the construct_properties argument of the constructor virtual function, the GParamSpecOverride is passed instead, so that the param_id field of the GParamSpec will be correct. For virtually all uses, this makes no difference. If you need to get the overridden property, you can call g_param_spec_get_redirect_target(). Since 2.4

interfaceInstallProperty
void interfaceInstallProperty(void* iface, ParamSpec pspec)

Add a property to an interface; this is only useful for interfaces that are added to GObject-derived types. Adding a property to an interface forces all objects classes with that interface to have a compatible property. The compatible property could be a newly created GParamSpec, but normally g_object_class_override_property() will be used so that the object class only needs to provide an implementation and inherits the property description, default value, bounds, and so forth from the interface property. This function is meant to be called from the interface's default vtable initialization function (the class_init member of GTypeInfo.) It must not be called after after class_init has been called for any object types implementing this interface. Since 2.4

interfaceFindProperty
ParamSpec interfaceFindProperty(void* iface, string propertyName)

Find the GParamSpec with the given name for an interface. Generally, the interface vtable passed in as g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek(). Since 2.4

interfaceListProperties
ParamSpec[] interfaceListProperties(void* iface)

Lists the properties of an interface.Generally, the interface vtable passed in as g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek(). Since 2.4

doref
void* doref(void* object)

Increases the reference count of object.

unref
void unref(void* object)

Decreases the reference count of object. When its reference count drops to 0, the object is finalized (i.e. its memory is freed).

refSink
void* refSink(void* object)

Increase the reference count of object, and possibly remove the floating reference, if object has a floating reference. In other words, if the object is floating, then this call "assumes ownership" of the floating reference, converting it to a normal reference by clearing the floating flag while leaving the reference count unchanged. If the object is not floating, then this call adds a new normal reference increasing the reference count by one. Since 2.10

clearObject
void clearObject(ObjectG objectPtr)

Clears a reference to a GObject. object_ptr must not be NULL. If the reference is NULL then this function does nothing. Otherwise, the reference count of the object is decreased and the pointer is set to NULL. This function is threadsafe and modifies the pointer atomically, using memory barriers where needed. A macro is also included that allows this function to be used without pointer casts. Since 2.28

isFloating
int isFloating(void* object)

Checks whether object has a floating reference. Since 2.10

forceFloating
void forceFloating()

This function is intended for GObject implementations to re-enforce a floating object reference. Doing this is seldom required: all GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling g_object_ref_sink(). Since 2.10

weakRef
void weakRef(GWeakNotify notify, void* data)

Adds a weak reference callback to an object. Weak references are used for notification when an object is finalized. They are called "weak references" because they allow you to safely hold a pointer to an object without calling g_object_ref() (g_object_ref() adds a strong reference, that is, forces the object to stay alive). Note that the weak references created by this method are not thread-safe: they cannot safely be used in one thread if the object's last g_object_unref() might happen in another thread. Use GWeakRef if thread-safety is required.

weakUnref
void weakUnref(GWeakNotify notify, void* data)

Removes a weak reference callback to an object.

addWeakPointer
void addWeakPointer(void** weakPointerLocation)

Adds a weak reference from weak_pointer to object to indicate that the pointer located at weak_pointer_location is only valid during the lifetime of object. When the object is finalized, weak_pointer will be set to NULL. Note that as with g_object_weak_ref(), the weak references created by this method are not thread-safe: they cannot safely be used in one thread if the object's last g_object_unref() might happen in another thread. Use GWeakRef if thread-safety is required.

removeWeakPointer
void removeWeakPointer(void** weakPointerLocation)

Removes a weak reference from object that was previously added using g_object_add_weak_pointer(). The weak_pointer_location has to match the one used with g_object_add_weak_pointer().

addToggleRef
void addToggleRef(GToggleNotify notify, void* data)

Increases the reference count of the object by one and sets a callback to be called when all other references to the object are dropped, or when this is already the last reference to the object and another reference is established. This functionality is intended for binding object to a proxy object managed by another memory manager. This is done with two paired references: the strong reference added by g_object_add_toggle_ref() and a reverse reference to the proxy object which is either a strong reference or weak reference. The setup is that when there are no other references to object, only a weak reference is held in the reverse direction from object to the proxy object, but when there are other references held to object, a strong reference is held. The notify callback is called when the reference from object to the proxy object should be toggled from strong to weak (is_last_ref true) or weak to strong (is_last_ref false). Since a (normal) reference must be held to the object before calling g_object_add_toggle_ref(), the initial state of the reverse link is always strong. Multiple toggle references may be added to the same gobject, however if there are multiple toggle references to an object, none of them will ever be notified until all but one are removed. For this reason, you should only ever use a toggle reference if there is important state in the proxy object. Since 2.8

removeToggleRef
void removeToggleRef(GToggleNotify notify, void* data)

Removes a reference added with g_object_add_toggle_ref(). The reference count of the object is decreased by one. Since 2.8

notify
void notify(string propertyName)

Emits a "notify" signal for the property property_name on object. When possible, eg. when signaling a property change from within the class that registered the property, you should use g_object_notify_by_pspec() instead.

notifyByPspec
void notifyByPspec(ParamSpec pspec)

Emits a "notify" signal for the property specified by pspec on object. This function omits the property name lookup, hence it is faster than g_object_notify(). One way to avoid using g_object_notify() from within the class that registered the properties, and using g_object_notify_by_pspec() instead, is to store the GParamSpec used with Since 2.26

freezeNotify
void freezeNotify()

Increases the freeze count on object. If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one "notify" signal is emitted for each property modified while the object is frozen. This is necessary for accessors that modify multiple properties to prevent premature notification while the object is still being modified.

thawNotify
void thawNotify()

Reverts the effect of a previous call to g_object_freeze_notify(). The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted. Duplicate notifications for each property are squashed so that at most one "notify" signal is emitted for each property. It is an error to call this function when the freeze count is zero.

getData
void* getData(string key)

Gets a named field from the objects table of associations (see g_object_set_data()).

setData
void setData(string key, void* data)

Each object carries around a table of associations from strings to pointers. This function lets you set an association. If the object already had an association with that name, the old association will be destroyed.

setDataFull
void setDataFull(string key, void* data, GDestroyNotify destroy)

Like g_object_set_data() except it adds notification for when the association is destroyed, either by setting it to a different value or when the object is destroyed. Note that the destroy callback is not called if data is NULL.

stealData
void* stealData(string key)

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.

dupData
void* dupData(string key, GDuplicateFunc dupFunc, void* userData)

This is a variant of g_object_get_data() which returns a 'duplicate' of the value. dup_func defines the meaning of 'duplicate' in this context, it could e.g. take a reference on a ref-counted object. If the key is not set on the object then dup_func will be called with a NULL argument. Note that dup_func is called while user data of object is locked. This function can be useful to avoid races when multiple threads are using object data on the same key on the same object. Since 2.34

replaceData
int replaceData(string key, void* oldval, void* newval, GDestroyNotify destroy, GDestroyNotify* oldDestroy)

Compares the user data for the key key on object with oldval, and if they are the same, replaces oldval with newval. This is like a typical atomic compare-and-exchange operation, for user data on an object. If the previous value was replaced then ownership of the old value (oldval) is passed to the caller, including the registered destroy notify for it (passed out in old_destroy). Its up to the caller to free this as he wishes, which may or may not include using old_destroy as sometimes replacement should not destroy the object in the normal way. Return: TRUE if the existing value for key was replaced by newval, FALSE otherwise. Since 2.34

getQdata
void* getQdata(GQuark quark)

This function gets back user data pointers stored via g_object_set_qdata().

setQdata
void setQdata(GQuark quark, void* data)

This sets an opaque, named pointer on an object. The name is specified through a GQuark (retrived e.g. via g_quark_from_static_string()), and the pointer can be gotten back from the object with g_object_get_qdata() until the object is finalized. Setting a previously set user data pointer, overrides (frees) the old pointer set, using NULL as pointer essentially removes the data stored.

setQdataFull
void setQdataFull(GQuark quark, void* data, GDestroyNotify destroy)

This function works like g_object_set_qdata(), but in addition, a void (*destroy) (gpointer) function may be specified which is called with data as argument when the object is finalized, or the data is being overwritten by a call to g_object_set_qdata() with the same quark.

stealQdata
void* stealQdata(GQuark quark)

This function gets back user data pointers stored via g_object_set_qdata() and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update

dupQdata
void* dupQdata(GQuark quark, GDuplicateFunc dupFunc, void* userData)

This is a variant of g_object_get_qdata() which returns a 'duplicate' of the value. dup_func defines the meaning of 'duplicate' in this context, it could e.g. take a reference on a ref-counted object. If the quark is not set on the object then dup_func will be called with a NULL argument. Note that dup_func is called while user data of object is locked. This function can be useful to avoid races when multiple threads are using object data on the same key on the same object. Since 2.34

replaceQdata
int replaceQdata(GQuark quark, void* oldval, void* newval, GDestroyNotify destroy, GDestroyNotify* oldDestroy)

Compares the user data for the key quark on object with oldval, and if they are the same, replaces oldval with newval. This is like a typical atomic compare-and-exchange operation, for user data on an object. If the previous value was replaced then ownership of the old value (oldval) is passed to the caller, including the registered destroy notify for it (passed out in old_destroy). Its up to the caller to free this as he wishes, which may or may not include using old_destroy as sometimes replacement should not destroy the object in the normal way. Return: TRUE if the existing value for quark was replaced by newval, FALSE otherwise. Since 2.34

setProperty
void setProperty(string propertyName, Value value)

Sets a property on an object.

getProperty
void getProperty(string propertyName, Value value)

Gets a property of an object. value must have been initialized to the expected type of the property (or a type to which the expected type can be transformed) using g_value_init(). In general, a copy is made of the property contents and the caller is responsible for freeing the memory by calling g_value_unset(). Note that g_object_get_property() is really intended for language bindings, g_object_get() is much more convenient for C programming.

setValist
void setValist(string firstPropertyName, void* varArgs)

Sets properties on an object.

getValist
void getValist(string firstPropertyName, void* varArgs)

Gets properties of an object. In general, a copy is made of the property contents and the caller is responsible for freeing the memory in the appropriate manner for the type, for instance by calling g_free() or g_object_unref(). See g_object_get().

watchClosure
void watchClosure(Closure closure)

This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling g_closure_invalidate() on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, g_object_ref() and g_object_unref() are added as marshal guards to the closure, to ensure that an extra reference count is held on object during invocation of the closure. Usually, this function will be called on closures that use this object as closure data.

runDispose
void runDispose()

Releases all references to other objects. This can be used to break reference cycles. This functions should only be called from object system implementations.

From InitableIF

getInitableTStruct
GInitable* getInitableTStruct()
Undocumented in source.
getStruct
void* getStruct()

the main Gtk struct as a void*

init
int init(Cancellable cancellable)

Initializes the object implementing the interface. The object must be initialized before any real use after initial construction, either with this function or g_async_initable_init_async(). Implementations may also support cancellation. If cancellable is not NULL, then initialization can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If cancellable is not NULL and the object doesn't support cancellable initialization the error G_IO_ERROR_NOT_SUPPORTED will be returned. If the object is not initialized, or initialization returns with an error, then all operations on the object except g_object_ref() and g_object_unref() are considered to be invalid, and have undefined behaviour. See the ??? section introduction for more details. Implementations of this method must be idempotent, i.e. multiple calls to this function with the same argument should return the same results. Only the first call initializes the object, further calls return the result of the first call. This is so that it's safe to implement the singleton pattern in the GObject constructor function. Since 2.22

newValist
ObjectG newValist(GType objectType, string firstPropertyName, void* varArgs, Cancellable cancellable)

Helper function for constructing GInitable object. This is similar to g_object_new_valist() but also initializes the object and returns NULL, setting an error on failure. Since 2.22

newv
void* newv(GType objectType, GParameter[] parameters, Cancellable cancellable)

Helper function for constructing GInitable object. This is similar to g_object_newv() but also initializes the object and returns NULL, setting an error on failure. Since 2.22

Meta