#[repr(C)]pub struct C4SocketFactory {
pub framing: C4SocketFraming,
pub context: *mut c_void,
pub open: Option<unsafe extern "C" fn(socket: *mut C4Socket, addr: *const C4Address, options: C4Slice, context: *mut c_void)>,
pub write: Option<unsafe extern "C" fn(socket: *mut C4Socket, allocatedData: C4SliceResult)>,
pub completedReceive: Option<unsafe extern "C" fn(socket: *mut C4Socket, byteCount: usize)>,
pub close: Option<unsafe extern "C" fn(socket: *mut C4Socket)>,
pub requestClose: Option<unsafe extern "C" fn(socket: *mut C4Socket, status: c_int, message: C4String)>,
pub dispose: Option<unsafe extern "C" fn(socket: *mut C4Socket)>,
}Expand description
A group of callbacks that define the implementation of sockets; the client must fill this out and pass it to c4socket_registerFactory() before using any socket-based API. These callbacks will be invoked on arbitrary background threads owned by LiteCore. They should return quickly, and perform the operation asynchronously without blocking.
Fields§
§framing: C4SocketFramingThis should be set to kC4NoFraming if the socket factory acts as a stream of messages,
kC4WebSocketClientFraming or kC4WebSocketServerFraming if it’s a byte stream.
context: *mut c_voidAn arbitrary value that will be passed to the open callback.
open: Option<unsafe extern "C" fn(socket: *mut C4Socket, addr: *const C4Address, options: C4Slice, context: *mut c_void)>Called to open a socket to a destination address. This function should operate asynchronously, returning immediately. When the socket opens, call \ref c4socket_opened, or if it fails to open, call \ref c4socket_closed with an appropriate error.
@param socket A new C4Socket instance to be opened. Its nativeHandle will be NULL;
the implementation of this function will probably store a native socket
reference there.
@param addr The address (URL) to connect to.
@param options A Fleece-encoded dictionary containing additional parameters, such as
kC4SocketOptionWSProtocols, which provides the WebSocket protocol names
to include in the HTTP request header.
@param context The value of the C4SocketFactory’s context field.
write: Option<unsafe extern "C" fn(socket: *mut C4Socket, allocatedData: C4SliceResult)>Called to write to the socket. If framing equals kC4NoFraming, the data is a complete
message, and your socket implementation is responsible for framing it;
otherwise, it’s just raw bytes to write to the stream, including the necessary WebSocket
framing.
After data has been written, you must call \ref c4socket_completedWrite. (You can call this
once after all the data is written, or multiple times with smaller numbers, as long as the
total byte count equals the size of the allocatedData.)
@param socket The socket to write to.
@param allocatedData The data/message to send. As this is a C4SliceResult, your
implementation of this function is responsible for releasing it when done.
completedReceive: Option<unsafe extern "C" fn(socket: *mut C4Socket, byteCount: usize)>Called to inform the socket that LiteCore has finished processing the data from a \ref c4socket_received call.
This can be used for flow control: you should keep track of
the number of bytes you’ve sent to LiteCore, incrementing the number when you call
\ref c4socket_received, and decrementing it when completedReceived is called.
If the number goes above some threshold, you should take measures to stop receiving more
data, e.g. by not reading more bytes from the socket. Otherwise memory usage can balloon as
more and more data arrives and must be buffered in memory.
@param socket The socket that whose incoming data was processed.
@param byteCount The number of bytes of data processed (the size of the data
slice passed to a c4socket_received() call.)
close: Option<unsafe extern "C" fn(socket: *mut C4Socket)>Called to close the socket. This is only called if framing doesn’t equal kC4NoFraming, i.e.
the socket operates at the byte level. Otherwise it may be left NULL.
No more write calls will be made; you should process any remaining incoming bytes
by calling \ref c4socket_received, then call \ref c4socket_closed when the socket closes.
\warning You MUST call \ref c4socket_closed, or the replicator will wait forever!
@param socket The socket to close.
requestClose: Option<unsafe extern "C" fn(socket: *mut C4Socket, status: c_int, message: C4String)>Called to close the socket. This is only called if framing equals to kC4NoFraming, i.e.
the socket operates at the message level. Otherwise it may be left NULL.
Your implementation should:
- send a message that tells the remote peer that it’s closing the connection;
- wait for acknowledgement;
- while waiting, handle any further incoming messages by calling \ref c4socket_received;
- after 5 seconds of waiting, give up and go to step 5;
- upon acknowledgement or timeout, close its connection and call \ref c4socket_closed.
This call can also occur before the socket has opened, if the replicator decides to time out. In this situation – i.e. if you have not yet called \ref c4socket_opened – you should tear down your connection and call \ref c4socket_closed.
\warning You MUST call \ref c4socket_closed, or the replicator will wait forever!
@param socket The C4Socket to close.
@param status The WebSocket status code to send in the CLOSE message.
@param message The text to send in the CLOSE message.
dispose: Option<unsafe extern "C" fn(socket: *mut C4Socket)>Called to tell the client that a C4Socket object is being disposed/freed after it’s
closed. The implementation of this function can then dispose any state associated with
the nativeHandle.
Set this to NULL if you don’t need the call.
@param socket The socket being disposed.
Trait Implementations§
Source§impl Clone for C4SocketFactory
impl Clone for C4SocketFactory
Source§fn clone(&self) -> C4SocketFactory
fn clone(&self) -> C4SocketFactory
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more