Skip to main content

C4SocketFactory

Struct C4SocketFactory 

Source
#[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: C4SocketFraming

This 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_void

An 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:

  1. send a message that tells the remote peer that it’s closing the connection;
  2. wait for acknowledgement;
  3. while waiting, handle any further incoming messages by calling \ref c4socket_received;
  4. after 5 seconds of waiting, give up and go to step 5;
  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

Source§

fn clone(&self) -> C4SocketFactory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for C4SocketFactory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for C4SocketFactory

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.