Struct gio::Socket [−][src]
pub struct Socket(_);
Expand description
A Socket
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.
Socket
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 SocketClient
,
SocketService
and SocketConnection
. However there may be cases where
direct use of Socket
is useful.
Socket
implements the GInitable
interface, so if it is manually constructed
by e.g. glib::Object::new()
you must call g_initable_init()
and check the
results before using the object. This is done automatically in
new()
and from_fd()
, so these functions can return
None
.
Sockets operate in two general modes, blocking or non-blocking. When
in blocking mode all operations (which don’t take an explicit blocking
parameter) 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 IOErrorEnum::WouldBlock
error.
To know when a call would successfully run you can call SocketExt::condition_check()
,
or SocketExt::condition_wait()
. You can also use g_socket_create_source()
and
attach it to a glib::MainContext
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 IOErrorEnum::WouldBlock
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 IOErrorEnum::WouldBlock
.
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 Socket
causes the signal SIGPIPE
to be
ignored for the remainder of the program. If you are writing a
command-line utility that uses Socket
, 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.
Like most other APIs in GLib, Socket
is not inherently thread safe. To use
a Socket
concurrently from multiple threads, you must implement your own
locking.
Implements
Implementations
Creates a new Socket
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 Socket
.
On success, the returned Socket
takes ownership of fd
. On failure, the
caller must close fd
themselves.
Since GLib 2.46, it is no longer a fatal error to call this on a non-socket
descriptor. Instead, a GError will be set with code IOErrorEnum::Failed
fd
a native socket file descriptor.
Returns
a Socket
or None
on error.
Free the returned object with g_object_unref()
.
pub fn new(
family: SocketFamily,
type_: SocketType,
protocol: SocketProtocol
) -> Result<Socket, Error>
pub fn new(
family: SocketFamily,
type_: SocketType,
protocol: SocketProtocol
) -> Result<Socket, Error>
Creates a new Socket
with the defined family, type and protocol.
If protocol
is 0 (SocketProtocol::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. SocketProtocol
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 SocketProtocol
if you
know the protocol number used for it.
family
the socket family to use, e.g. SocketFamily::Ipv4
.
type_
the socket type to use.
protocol
the id of the protocol to use, or 0 for default.
Returns
a Socket
or None
on error.
Free the returned object with g_object_unref()
.
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
impl RefUnwindSafe for Socket
impl UnwindSafe for Socket
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
V: ToValue,
N: Into<&'a str>,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
N: Into<&'a str>,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value> + 'static,
N: Into<&'a str>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
F: Fn(&[Value]) -> Option<Value>,
N: Into<&'a str>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Emit signal with details by signal id.
Emit signal by it’s name.
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Same as emit
but takes Value
for the arguments.
Same as emit_by_name
but takes Value
for the arguments.
Returns a SendValue
clone of self
.
impl<'a, T, C> FromValueOptional<'a> for T where
C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError>,
T: FromValue<'a, Checker = C>,