Class OcsfmlNetwork.socket_selector

class socket_selector : object .. end
Multiplexer that allows to read from multiple sockets.

Socket selectors provide a way to wait until some data is available on a set of sockets, instead of just one.

This is convenient when you have multiple sockets that may possibly receive data, but you don't know which one will be ready first. In particular, it avoids to use a thread for each socket; with selectors, a single thread can handle all the sockets.

All types of sockets can be used in a selector:

A selector doesn't store its own copies of the sockets (socket classes are not copyable anyway), it simply keeps a reference to the original sockets that you pass to the add function. Therefore, you can't use the selector as a socket container, you must store them oustide and make sure that they are alive as long as they are used in the selector.

Using a selector is simple:

Usage example:
    (* Create a socket to listen to new connections *)
    let listener = new tcp_listener in
    listener#listen 55001 ;

    (* Create a list to store the future clients *)
    let clients = ref [] in

    (* Create a selector *)
    let selector = new socket_selector (SocketSelector.default ()) in

    (* Add the listener to the selector *)
    selector#add listener 

    (* Endless loop that waits for new connections *)
    let rec connection_loop () =
      (* Make the selector wait for data on any socket *)
      if selector#wait
      then begin
        (* Test the listener *)
        if selector#is_ready listener
          (* The listener is ready: there is a pending connection *)
          let client = new tcp_socket in
          if (listener#accept client) = Done
          then begin
            (* Add the new client to the clients list *)
            clients := client :: !clients ;
            (* Add the new client to the selector so that we will
               be notified when he sends something *)

            selector#add client
      else begin
        (* The listener socket is not ready, test all other sockets 
           (the clients) *)

        List.iter (function client ->
                      if selector#is_ready client
                        (* The client has sent some data, we can receive it *)
                        let packet = new packet in
                        if (client#receive packet) = Done
                        then ...) 
    in connection_loop ()

method add : 'a. (#socket as 'a) -> unit
Add a new socket to the selector.

This function keeps a weak reference to the socket, so you have to make sure that the socket is not destroyed while it is stored in the selector.

method clear : unit
Remove all the sockets stored in the selector.

This function doesn't destroy any instance, it simply removes all the references that the selector has to external sockets.

method destroy : unit
method is_ready : 'b. (#socket as 'b) -> bool
Test a socket to know if it is ready to receive data.

This function must be used after a call to wait, to know which sockets are ready to receive data. If a socket is ready, a call to receive will never block because we know that there is data available to read. Note that if this function returns true for a TcpListener, this means that it is ready to accept a new connection.
Returns True if the socket is ready to read, false otherwise

method remove : 'c. (#socket as 'c) -> unit
Remove a socket from the selector.

This function doesn't destroy the socket, it simply removes the reference that the selector has to it.