Skip to content
This repository
tag: v1280
Fetching contributors…

Cannot retrieve contributors at this time

file 402 lines (333 sloc) 12.774 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

exception Syscall (* temporary, could probably be removed for new scheduler *)

(**
Module to manage asynchronous operations.

@author Henri Binsztok
@author Laurent Le Brun
@author Frederic Ye
@author Cedric Soulas
@author Hugo Venturini
*)

(**
This module provides functions to register
- asynchronous operations : [listen], [read] and [write] over sockets and file descriptors.
- asynchronous calculations : [sleep], [timer] and [yield].

The scheduler is asynchronous and non-preemptive {e i.e.} operations and calculation are registers
for sometime later in the future and they cannot interrupt one another.
The general mechanism is to register operations ([read], [write], [listen]) and calculation
([sleep], [timer], [yield]) and to call for their execution ([wait]).
*)

(** {6 Types} *)

type t
(** The type of schedulers *)

type connection_info = { (* Temporarily exported for debug *)
  addr : NetAddr.t; (* the connection type *)
  conn_id : int (* the unique id of the connection *)
}
(** The type of connections *)

type async_key
(** The type of a key associated to an asynchronous job. See [listen] and [sleep]. *)

(** {6 Exceptions} *)

exception Timeout
  (** Raised when a timeout expires. *)

exception Connection_closed
  (** Raised when trying to connect through a closed connection. *)

exception Busy_direction
  (** Raised when another read event is already waiting for data on that connection *)

exception StopTimer
  (** Raised to stop the [timer] *)

exception Empty
  (** Raised when there is nothing queued in the scheduler *)

exception Unbound_key
  (** Raised when an key of type [async_key] is unbound *)

(** {6 Control of the scheduler} *)

val make : ?is_server:bool -> unit -> t
(** @return a fresh [Scheduler.t]
@param is_server (optional argument) Setting this parameter to true adds a server startup message. Default: [false]
*)

val default : t
(** A default scheduler *)

val wait : t -> block:bool -> bool
(** [wait sched block] Performs the following action in that order:
- Executes functions corresponding to operations which reached a deadline.
- Execute the next pending operation.

@param block Boolean stating whether the waiting blocks or not
{e i.e.} if [block] then it waits until any incoming operation,
otherwise it returns right after the end of execution.

@return [true] if there still are any pending events or any opened connections, [false] otherwise.
*)

val is_empty : t -> bool
(** @return [true] if and only if there is nothing queued in the scheduler, {e e.g.} no connections
open and no events or timeouts pending. This method is needed since [wait]
processes one event and may return [false] whether something was done or
not. *)

(** {6 Manipulation of connections} *)

val make_connection : t -> ?register:bool -> NetAddr.t -> connection_info
(** @return a fresh connection.
@param register States whether the new connection should be registered in the scheduler provided or not. Default: [true]
*)

val remove_connection :
  t -> connection_info -> unit
(**
Removes the given connection.
*)

val check_connection :
  t -> connection_info -> bool
(**
@return [true] if the given connection is registered in the given scheduler, [false] otherwise.
*)

val get_connection_addr: connection_info -> NetAddr.t
(**
@return the address of the given connection.
*)

val get_connection_inet_addr : connection_info -> Unix.inet_addr

val get_connection_fd : connection_info -> Unix.file_descr

val nb_of_connection : t -> int
(**
@return the number of registered connections in the given scheduler
*)

val get_connection_secured_from_normal : connection_info -> Ssl.socket -> connection_info
(**
Creates a secured connection from a given connection and a given socket.
*)

(** {6 Asynchronous operation over [connection_info]} *)

(**
An operation is register in a given scheduler and is set over a connection. It must
provide a continuation, among other parameters specific to each operation e.g. a string to write.

A {b continuation} is a function which returns [unit] and which will be executed when
the corresponding operation is triggered.

An continuation to handle errors can be provide. If an error happen, this error continuation will be used *instead* of the normal continuation.

Beside [listen], any operation can be registered with a timeout and/or an error continuation.
This allows you a fine-grained handling of continuations over time. But {b with great power comes
great responsabilities}. The policy is the following: if you don't provide any error continuation,
we'll handle the connection you provided at registration, fine. But if you decide to provide an error
continuation, you will have to explicitly remove the connection (see [remove_connection] above).
*)

val listen :
  t ->
  connection_info ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (unit -> unit)
  -> async_key
(**
Listen for events onto the given connection.
@return the associated key that can be used with [abort].
*)

val listen_once :
  t ->
  connection_info ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (unit -> unit)
  -> unit
(**
Listen for one event coming on a given connection.
*)

val connect :
  t ->
  connection_info ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (unit -> unit)
  -> unit
(**
Connect on a given connection.
*)

val read_more :
  t ->
  connection_info ->
  ?read_max:int ->
  ?block_size:int ->
  ?timeout:Time.t ->
  FBuffer.t ->
  ?size_max:int ->
  ?err_cont:(exn -> unit) ->
  (int * FBuffer.t -> unit) -> unit
(** Reads more data over a [connection_info], appending those new data in the providing buffer
@raise Busy_direction exception is raised when another callback is already waiting
for datas on that [connection_info] *)

val read_content :
  t ->
  connection_info ->
  ?read_max:int ->
  ?block_size:int ->
  ?timeout:Time.t ->
  Rcontent.content ->
  ?size_max:int ->
  ?err_cont:(exn -> unit) ->
  (int * Rcontent.content -> unit) -> unit

val read_more2 :
  t ->
  connection_info ->
  ?read_max:int ->
  ?timeout:Time.t ->
  Buffer.t ->
  ?size_max:int ->
  ?err_cont:(exn -> unit) ->
  (int * Buffer.t -> unit) -> unit

val read_more4 :
  t ->
  connection_info ->
  ?read_max:int ->
  ?timeout:Time.t ->
  Buf.t ->
  ?size_max:int ->
  ?err_cont:(exn -> unit) ->
  (int * Buf.t -> unit) -> unit

val read :
  t ->
  connection_info ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (int * string -> unit) -> unit
(** Reads over a [connection_info], a maximum of 4096 characters.
@raise Busy_direction exception is raised when another callback is already waiting
for datas on that [connection_info] *)

val read_from :
  t ->
  connection_info ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (int * Unix.sockaddr * string -> unit) -> unit
(** Reads over a [connection_info], a maximum of 4096 characters. The difference with
[read] is that the callback is additionally informed of the address of the sender
of the message. This is useful for un-connected UDP communication.
@raise Busy_direction exception is raised when another callback is already waiting
for data on that [connection_info] *)

val read_until :
  t ->
  connection_info ->
  (int * string -> bool) ->
  ?block_size:int ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (int * string -> unit) -> unit
(** [read_until sched conn read_cond cont] reads over a [connection_info],
until read_cond returns true from the provided couple (number of characters, data) read.
It can be used, for example, to read a minimum number of characters or for your callback
to be called only if the data ends with certain characters.
@raise Busy_direction exception is raised when another callback is already waiting
for datas on that [connection_info] *)

val read_min :
  t ->
  connection_info ->
  int ->
  ?block_size:int ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (int * string -> unit) -> unit
(** [read_min sched conn read_min cont] reads over a [connection_info],
a minimum of [read_min] characters.
@raise Busy_direction exception is raised when another callback is already waiting
for datas on that [connection_info] *)

val read_lines :
  t ->
  connection_info ->
  ?block_size:int ->
  ?timeout:Time.t ->
  ?err_cont:(exn -> unit) ->
  (int * string -> unit) -> unit
(** [read_lines sched conn cont] reads over a [connection_info],
and call you callback only if last characters are "\r\n",
it means only when the data is a line or a set of lines.
@raise Busy_direction exception is raised when another callback is already waiting
for datas on that [connection_info] *)

val read_all :
  t ->
  connection_info ->
  ?read_max:int option->
  ?block_size:int ->
  ?timeout:Time.t ->
  ?buf:FBuffer.t ->
  ?size_max:int ->
  ?err_cont:(exn -> unit) ->
  (int * FBuffer.t -> unit) -> unit
(** Reads until the connection is closed.
It means your callback will be called only at the end of the connection.
See [read] for further information.
@raise Connection_closed exception is raised when the connection on which it was reading is closed.
*)

val write :
  t ->
  connection_info ->
  ?block_size:int ->
  ?timeout:Time.t ->
  string ->
  ?len:int ->
  ?err_cont:(exn -> unit) ->
  (int -> unit) -> unit
(** Writes over a [connection_info]
Several writes on the same connection are permited. In this case,
datas will be written in the same order as the [write] declarations.
*)

val write_to :
  t ->
  connection_info ->
  Unix.sockaddr ->
  ?block_size:int ->
  ?timeout:Time.t ->
  string ->
  ?err_cont:(exn -> unit) ->
  (int -> unit) -> unit
(** Writes over a [connection_info]
Several writes on the same connection are permited. In this case,
datas will be written in the same order as the [write] declarations.
*)

(** {6 Asynchronous calculation} *)

val sleep : t -> Time.t -> (unit -> unit) -> async_key
  (**
[sleep sched x f] schedules the execution of [f] in time interval [x] in [sched]
@return the associated key that can be used with [abort].
*)

val abort : t -> async_key -> unit
  (**
[abort sched key] abort the pending job associated the [key].
@raise Unbound_key if the key is unbound
*)

val timer : t -> Time.t -> (unit -> unit) -> unit
  (** [timer sched x f] schedules the execution of [f] every time interval [x] in [sched].

If you don't define an explicit stop to the timer, it will be call forever (and ever).
Although you might have excellent reasons not to stop the timer, feel free to copy the
following PATTERN:
{v
let f () =
if <condition> then <do_somthing>
else raise Scheduler.StopTimer;
in Scheduler.timer sched x f;

v}
*)


val push : t -> (unit -> unit) -> unit
  (** [push sched f] Push in [sched] the task [f]. *)

val at_exit : t -> (unit -> unit) -> unit
(** [at_exit sched f] Push in [sched] the task [f] to be done at the end of the program. *)

val flush : ?f:(unit -> unit) -> t -> unit
  (** Execute all asynchronous calculation and operation. Optionally given [f] will be evaluated after
performing any scheduled operation. *)

val loop_until : t -> (unit -> bool) -> unit
  (** [flush_until sched cond] Execute all asynchronous calculation and operation until [cond ()] is true
@raise Empty if cond not satisfied and there is nothing queued in the scheduler.
@raise Reentrant_routine if another [wait], [flush] or [flush_until] is yet in progress.
*)

val finalise : t -> ('a -> unit) -> 'a -> unit
  (**
[finalise sched f v] registers f as a finalisation function for v.
It is permited, and only with this finalisation function, to use the
scheduler inside the finalisation function.
WARNING: contrary to [Gc.finalise], the order of calls
to finalisation functions is not guarantee.
*)

val run : t -> unit
  (** Like [flush] but catch all exn *)
Something went wrong with that request. Please try again.