1% LIC: GPL 2\documentclass{article} 3\usepackage{epsfig} 4\usepackage[colorlinks]{hyperref} 5\input{style} 6\newcommand{\Le}{\textsf{LibEvent}} 7\newcommand{\Es}{\type{EventSelector}} 8\newcommand{\Eh}{\type{EventHandler}} 9 10\title{\Le{} Programmers Manual} 11\author{David F. Skoll\\\textit{Roaring Penguin Software Inc.}} 12\begin{document} 13\maketitle 14 15\section{Introduction} 16\label{sec:introduction} 17 18Many UNIX programs are event-driven. They spend most of their time 19waiting for an event, such as input from a file descriptor, expiration 20of a timer, or a signal, and then react to that event. 21 22The standard UNIX mechanisms for writing event-driven programs are 23the \name{select} and \name{poll} system calls, which wait for input 24on a set of file descriptors, optionally with a timeout. 25 26While \name{select} and \name{poll} can be used to write event-driven 27programs, their calling interface is awkward and their level of 28abstraction too low. \Le{} is built around \name{select}, but 29provides a more pleasant interface for programmers. 30 31\Le{} provides the following mechanisms: 32\begin{itemize} 33\item \textit{Events}, which trigger under user-specified conditions, 34 such as readability/writability of a file descriptor or expiration of 35 a timer. 36\item \textit{Synchronous signal-handling}, which is the ability to 37 defer signal-handling to a safe point in the event-handling loop. 38\item \textit{Syncronous child cleanup}, which lets you defer calls 39 to \name{wait} or \name{waitpid} to a safe point in the event-handling 40 loop. 41\end{itemize} 42 43\section{Overview} 44\label{sec:overview} 45 46Figure~\ref{fig:flow} indicates the overall flow of programs using 47\Le{}. 48\begin{figure}[htbp] 49 \begin{center} 50 \epsfig{file=flow.\eps,width=3in} 51 \caption{\Le{} Flow} 52 \label{fig:flow} 53 \end{center} 54\end{figure} 55 56\begin{enumerate} 57\item Call \name{Event\_CreateSelector} once to create an \emph{Event 58 Selector}. This is an object which manages event dispatch. 59\item Open file descriptors as required, and call \name{Event\_CreateHandler} 60 to create \emph{Event Handlers} for each descriptor of interest. You 61 can call \name{Event\_CreateTimerHandler} to create timers which are 62 not associated with file descriptors. 63\item Call \name{Event\_HandleEvent} in a loop. Presumably, some event 64 will cause the program to exit out of the infinite loop (unless the 65 program is designed never to exit.) 66\end{enumerate} 67 68To use \Le{}, you should \texttt{\#include} the file 69\incfile{libevent/event.h} 70 71\section{Types} 72 73\Le{} defines the following types: 74\begin{itemize} 75\item \Es{} -- a container object which manages event handlers. 76\item \Eh{} -- an object which triggers a callback function when an event 77 occurs. 78\item \type{EventCallbackFunc} -- a prototype for the callback function 79 called by an \Eh{}. 80\end{itemize} 81 82\section{Basic Functions} 83\label{sec:basic-functions} 84 85This section describes the basic \Le{} functions. Each function is 86described in the following format: 87\function{type}{name}{(\type{type1} \param{arg1}, \type{type2} \param{arg2})} 88{A brief description of the function. \type{type} is the type of the 89return value and \name{name} is the name of the function.} 90{What the function returns} 91\begin{itemize} 92\item \param{arg1} -- A description of the first argument. 93\item \param{arg2} -- A description of the second argument, etc. 94\end{itemize} 95 96\subsection{Event Selector Creation and Destruction} 97\function{EventSelector *}{Event\_CreateSelector}{(\type{void})} 98{Creates an \Es{} object and returns a pointer to it. 99An \Es{} is an object which keeps track of event handlers. 100You should treat it as an opaque type.} 101{A pointer to the \Es{}, or NULL if out of memory.} 102{None.} 103 104\function{void}{Event\_DestroySelector}{(\type{EventSelector *}\name{es})} 105{Destroys an \Es{} and all associated event handlers.} 106{Nothing.} 107\begin{itemize} 108\item \param{es} -- the \Es{} to destroy. 109\end{itemize} 110 111\subsection{Event Handler Creation and Destruction} 112 113An \Eh{} is an opaque object which contains information about an 114event. An event may be \emph{triggered} by one or more of three things: 115 116\begin{enumerate} 117\item A file descriptor becomes readable. That is, \name{select} for 118 readability would return. 119\item A file descriptor becomes writeable. 120\item A timeout elapses. 121\end{enumerate} 122 123When an event triggers, it calls an event callback function. An 124event callback function looks like this: 125 126\function{void}{functionName}{(\=\type{EventSelector *}\param{es},\\ 127\>\type{int} \param{fd},\\ 128\>\type{unsigned int} \param{flags},\\ 129\>\type{void *}\param{data})} 130{Called when an event handler triggers.} 131{Nothing} 132\begin{itemize} 133\item \param{es} -- the \Es{} to which the event handler belongs. 134\item \param{fd} -- the file descriptor (if any) associated with the event. 135\item \param{flags} -- a bitmask of one or more of the following values: 136 \begin{itemize} 137 \item \texttt{EVENT\_FLAG\_READABLE} -- the descriptor is readable. 138 \item \texttt{EVENT\_FLAG\_WRITEABLE} -- the descriptor is writeable. 139 \item \texttt{EVENT\_FLAG\_TIMEOUT} -- a timeout triggered. 140 \end{itemize} 141\item \param{data} -- an opaque pointer which was passed into 142 \name{Event\_AddHandler}. 143\end{itemize} 144 145\function{EventHandler *}{Event\_AddHandler}{(\=\type{EventSelector *}\param{es},\\ 146\>\type{int} \param{fd},\\ 147\>\type{unsigned int} \param{flags},\\ 148\>\type{EventCallbackFunc} \param{fn},\\ 149\>\type{void *}\param{data})} 150{Creates an \Eh{} to handle an event.} 151{An allocated \Eh{}, or NULL if out of memory.} 152\begin{itemize} 153\item \param{es} -- the event selector. 154\item \param{fd} -- the file descriptor to watch. \param{fd} must be 155 a legal file descriptor for use inside \name{select}. 156\item \param{flags} -- a bitmask whose value is one of 157 \texttt{EVENT\_FLAG\_READABLE}, \texttt{EVENT\_FLAG\_WRITEABLE} or 158 \texttt{EVENT\_FLAG\_READABLE~|~EVENT\_FLAG\_WRITEABLE}. \param{flags} 159 specifies the condition(s) under which to trigger the event. 160\item \param{fn} -- the callback function to invoke when the event triggers. 161\item \param{data} -- a pointer which is passed unchanged as the last 162 parameter of \param{fn} when the event triggers. 163\end{itemize} 164 165\function{EventHandler *}{Event\_AddTimerHandler}{(\=\type{EventSelector *}\param{es},\\ 166\>\type{struct timeval} \param{t},\\ 167\>\type{EventCallbackFunc} \param{fn},\\ 168\>\type{void *}\param{data})} 169{Creates an \Eh{} to handle a timeout. After the timeout elapses, the 170 callback function is called once only, and then the \Eh{} is automatically 171 destroyed.} 172{An allocated \Eh{}, or NULL if out of memory.} 173\begin{itemize} 174\item \param{es} -- the event selector. 175\item \param{t} -- the time after which to trigger the event. \param{t} 176 specifies how long \emph{after} the current time to trigger the event. 177\item \param{fn} -- the callback function to invoke when the event triggers. 178 A timer handler function is always called with its \param{flags} set 179 to \texttt{EVENT\_FLAG\_TIMER~|~EVENT\_FLAG\_TIMEOUT}. 180\item \param{data} -- a pointer which is passed unchanged as the last 181 parameter of \param{fn} when the event triggers. 182\end{itemize} 183 184\function{EventHandler *}{Event\_AddHandlerWithTimeout} 185{(\=\type{EventSelector *}\param{es},\\ 186\>\type{int} \param{fd},\\ 187\>\type{unsigned int} \param{flags},\\ 188\>\type{struct timeval} \param{t},\\ 189\>\type{EventCallbackFunc} \param{fn},\\ 190\>\type{void *}\param{data})} 191{Creates an \Eh{} to handle an event. The event is called when a file 192 descriptor is ready or a timeout elapses. This function may be viewed 193 as a combination of \name{Event\_AddHandler} and \name{Event\_AddTimerHandler}.} 194{An allocated \Eh{}, or NULL if out of memory.} 195\begin{itemize} 196\item \param{es} -- the event selector. 197\item \param{fd} -- the file descriptor to watch. \param{fd} must be 198 a legal file descriptor for use inside \name{select}. 199\item \param{flags} -- a bitmask whose value is one of 200 \texttt{EVENT\_FLAG\_READABLE}, \texttt{EVENT\_FLAG\_WRITEABLE} or 201 \texttt{EVENT\_FLAG\_READABLE~|~EVENT\_FLAG\_WRITEABLE}. \param{flags} 202 specifies the condition(s) under which to trigger the event. 203\item \param{t} -- the time after which to trigger the event. If the event 204 is triggered because of a timeout, the callback function's \param{flags} 205 has the \texttt{EVENT\_FLAG\_TIMEOUT} bit set. 206\item \param{fn} -- the callback function to invoke when the event triggers. 207\item \param{data} -- a pointer which is passed unchanged as the last 208 parameter of \param{fn} when the event triggers. 209\end{itemize} 210 211\function{int}{Event\_DelHandler} 212{(\=\type{EventSelector *}\param{es},\\ 213\>\type{EventHandler *}\param{eh})} 214{Deletes an \Eh{} and frees its memory. A handler may be deleted from 215 inside a handler callback; \Le{} defers the actual deallocation of 216 resources to a safe time.} 217{0 if the handler was found and deleted, non-zero otherwise. A non-zero 218 return value indicates a critical internal error.} 219\begin{itemize} 220\item \param{es} -- the event selector which contains \param{eh}. 221\item \param{eh} -- the event handler to delete. 222\end{itemize} 223 224\subsection{Event Handler Access Functions} 225 226The functions in this section access or modify fields in the 227\Eh{} structure. You should \emph{never} access or modify fields 228in an \Eh{} except with these functions. 229 230\function{void}{Event\_ChangeTimeout} 231{(\=\type{EventHandler *}\param{eh},\\ 232 \>\type{struct timeval} \param{t})} 233{Changes the timeout of \param{eh} to be \param{t} seconds from now. If 234 \param{eh} was not created with \name{Event\_AddTimerHandler} or 235 \name{Event\_AddHandlerWithTimeout}, then this function has no effect.} 236{Nothing} 237\begin{itemize} 238\item \param{eh} -- the \Eh{} whose timeout is to be modified. 239\item \param{t} -- new value of timeout, relative to current time. 240\end{itemize} 241 242\function{EventCallbackFunc}{Event\_GetCallback} 243{(\type{EventHandler *}\param{eh})} 244{Returns the callback function associated with \param{eh}.} 245{A pointer to the callback function associated with \param{eh}.} 246\begin{itemize} 247\item \param{eh} -- the \Eh{} whose callback pointer is desired. 248\end{itemize} 249 250\function{void *}{Event\_GetData} 251{(\type{EventHandler *}\param{eh})} 252{Returns the data associated with \param{eh} (the \param{data} argument 253 to the \ldots{}AddHandler\ldots{} function.)} 254{The data pointer associated with \param{eh}.} 255\begin{itemize} 256\item \param{eh} -- the \Eh{} whose data pointer is desired. 257\end{itemize} 258 259\function{void}{Event\_SetCallbackAndData} 260{(\=\type{EventHandler *}\param{eh},\\ 261 \>\type{EventCallbackFunc} \param{fn},\\ 262 \>\type{void *}\param{data})} 263{Sets the callback function and data associated with \param{eh}.} 264{Nothing.} 265\begin{itemize} 266\item \param{eh} -- the \Eh{} whose callback function and data pointer are 267 to be set. 268\item \param{fn} -- the new value for the callback function. 269\item \param{data} -- the new value for the data pointer. 270\end{itemize} 271 272\section{Signal Handling} 273 274In UNIX, signals can arrive asynchronously, and a signal-handler 275function may be called at an unsafe time, leading to race conditions. 276\Le{} has a mechanism to call a handler function during 277\name{Event\_HandleEvent} so that the handler is dispatched just like 278any other event handler. In this way, the signal handler knows that 279it is safe to access shared data without interference from another 280thread of control. 281 282\Le{} implements this \emph{synchronous signal handling} by setting up 283a UNIX pipe, and writing to the write-end inside the asynchronous 284handler. The read end then becomes ready for reading, and triggers 285a normal event. \Le{} encapsulates all the details for you in 286two functions. 287 288\function{int}{Event\_HandleSignal} 289{(\=\type{EventSelector *}\param{es},\\ 290 \>\type{int} \param{sig},\\ 291 \>\type{void (*}\param{handler}\type{)(int} \param{sig}\type{)})} 292{Arranges for the function \param{handler} to be called when signal 293 \param{sig} is received. \param{sig} is typically a constant from 294 \incfile{signal.h}, such as \texttt{SIGHUP}, \texttt{SIGINT}, etc. 295 The \param{handler} function is not called in the context of a UNIX 296 signal handler; rather, it is called soon after the signal has been 297 received as part of the normal \name{Event\_HandleEvent} loop. 298 299 As a side-effect of calling this function, a UNIX signal handler 300 is established for \param{sig}. Any existing signal disposition is 301 forgotten. If \param{sig} is \texttt{SIGCHLD}, then the 302 \texttt{SA\_NOCLDSTOP} flag is set in the \param{struct sigaction} passed 303 to the low-level \name{sigaction} function.} 304{0 on success; -1 on failure. Failure is usually due to a UNIX system 305 call failing or a lack of memory.} 306\begin{itemize} 307\item \param{es} -- the event selector. 308\item \param{sig} -- the signal we wish to handle. 309\item \param{handler} -- the function to call. It is passed a single 310 argument---the signal which is being handled. 311\end{itemize} 312 313\function{int}{Event\_HandleChildExit} 314{(\=\type{EventSelector *}\param{es},\\ 315 \>\type{pid\_t} \param{pid},\\ 316 \>\type{void (*}\param{handler}\type{)(pid\_t} \param{pid}, \type{int} \param{status}, \type{void *}\param{data}\type{)},\\ 317 \>\type{void *}\param{data})} 318{Arranges for \param{handler} to be called when the child process with 319 process-ID \param{pid} exits. \param{pid} must be the return 320 value of a successful call to \name{fork}. 321 322 When the process with process-ID \param{pid} exits, \Le{} catches 323 the \texttt{SIGCHILD} signal and at some point in the event-handling 324 loop, calls \param{handler} with three arguments: \param{pid} is 325 the process-ID of the process which terminated. \param{status} is 326 the exit status as returned by the \name{waitpid} system call. And 327 \param{data} is passed unchanged from the call to \name{Event\_HandleChildExit}.} 328{0 on success; -1 on failure. Failure is the result of lack of memory or 329 the failure of a UNIX system call.} 330\begin{itemize} 331\item \param{es} -- the event selector. 332\item \param{pid} -- process-ID of the child process. 333\item \param{handler} -- the function to call when the process exits. 334\item \param{data} -- a pointer which is passed unchanged to \param{handler} 335 when the process exits. 336\end{itemize} 337 338\section{Stream-Oriented Functions} 339 340The functions presented in the previous sections are appropriate for 341simple events, especially those associated with datagram sockets. A 342higher level of abstraction is required for stream-oriented descriptors. 343It would be nice for \Le{} to invoke a callback function when a certain 344number of bytes or a specific delimiter have been read from a stream, 345or when an entire buffer's worth of data has been written to a stream. 346 347The functions in this section all (unfortunately) have the string 348\texttt{Tcp} in their names, because they were originally used with TCP 349sockets. However, they may be used with any stream-oriented sockets, 350including UNIX-domain sockets. 351 352All of the stream-oriented functions are built on the simpler event 353functions described previously. They simply add an extra layer of 354convenience. To use the stream-oriented functions, 355\texttt{\#include} the file \incfile{libevent/event\_tcp.h}. 356 357\section{Stream-Oriented Data Types} 358 359The stream-oriented functions use the following publicly-accessible type: 360\begin{itemize} 361\item \type{EventTcpState} -- an opaque object which records the state 362 of stream-oriented event handlers. 363\end{itemize} 364 365\section{Stream-Oriented Functions} 366\label{sec:basic-stream-oriented-functions} 367 368The stream-oriented functions may be broken into two main groups: 369Connection establishment, and data transfer. 370 371\subsection{Connection Establishment} 372 373\function{EventHandler *}{EventTcp\_CreateAcceptor} 374{(\=\type{EventSelector *}\param{es},\\ 375 \>\type{int} \param{fd},\\ 376 \>\type{EventTcpAcceptFunc} \param{f})} 377{Creates an event handler to accept incoming connections on the listening 378 descriptor \param{fd}. Each time an incomming connection is accepted, 379 the function \param{f} is called.} 380{An \Eh{} on success; NULL on failure.} 381\begin{itemize} 382\item \param{es} -- the event selector. 383\item \param{fd} -- a listening socket (i.e., one for which the 384 \name{listen}(2) system call has been called.) 385\item \param{f} -- a function which is called each time an incoming 386 connection is accepted. The function \param{f} should look like this: 387 388 \type{void} \name{f}{(\type{EventSelector *}\param{es}, \type{int} \param{fd})} 389 390 In this case, \param{es} is the \Es{}, and \param{fd} is the new file 391 descriptor returned by \name{accept}(2). 392\end{itemize} 393 394\function{void}{EventTcp\_Connect} 395{(\=\type{EventSelector *}\param{es},\\ 396 \>\type{int} \param{fd},\\ 397 \>\type{struct sockaddr const *}\param{addr},\\ 398 \>\type{socklen\_t} \param{addrlen},\\ 399 \>\type{EventTcpConnectFunc} \param{f},\\ 400 \>\type{int} \param{timeout},\\ 401 \>\type{void *}\param{data})} 402{Attempts to connect the socket \param{fd} to \param{addr} using the 403 \name{connect}(2) system call.} 404{Nothing. See below for error-handling notes.} 405\begin{itemize} 406\item \param{es} -- the event selector. 407\item \param{fd} -- a socket which is suitable for passing to 408 \name{connect}(2). 409\item \param{addr} -- the server address to connect to. 410\item \param{addrlen} -- the length of the server address. The 411 three parameters \param{fd}, \param{addr} and \param{addrlen} are passed 412 directly to \name{connect}(2). 413\item \param{f} -- A function which is called when the connection succeeds 414 (or if an error occurs.) The function \param{f} looks like this: 415 416 \type{void} \name{f}{(\type{EventSelector *}\param{es}, \type{int} \param{fd}, \type{int} \param{flag}, \type{void *}\param{data})} 417 418 The parameters of \param{f} have the following meaning: 419 \begin{itemize} 420 \item \param{es} -- the event selector. 421 \item \param{fd} -- the descriptor. 422 \item \param{flag} -- a flag indicating what happened. It may contain 423 one of the following values: 424 \begin{itemize} 425 \item \param{EVENT\_TCP\_FLAG\_IOERROR} -- the \name{connect} system call 426 failed. 427 \item \param{EVENT\_TCP\_FLAG\_COMPLETE} -- the \name{connect} system 428 call succeeded and the descriptor is now connected. 429 \item \param{EVENT\_TCP\_FLAG\_TIMEOUT} -- the \name{connect} system call 430 did not complete within the specified timeout. 431 \end{itemize} 432 \item \param{data} -- a copy of the \param{data} given to 433 \name{EventTcp\_Connect}. 434 \end{itemize} 435\item \param{timeout} -- a timeout value in seconds. If \name{connect} does 436 not complete withing \param{timeout} seconds, the \param{f} is called 437 with a flag of \param{EVENT\_TCP\_FLAG\_TIMEOUT}. 438\item \param{data} -- an opaque pointer passed unchanged to \param{f}. 439\end{itemize} 440 441\subsection{Data Transfer} 442 443There are two stream-oriented functions for data transfer: One for 444reading and one for writing. 445 446\function{EventTcpState *}{EventTcp\_ReadBuf} 447{(\=\type{EventSelector *}\param{es},\\ 448 \>\type{int} \param{fd},\\ 449 \>\type{int} \param{len},\\ 450 \>\type{int} \param{delim},\\ 451 \>\type{EventTcpIOFinishedFunc} \param{f},\\ 452 \>\type{int} \param{timeout},\\ 453 \>\type{void *}\param{data})} 454{Arranges events to read up to \param{len} characters from the file 455 descriptor \param{fd}. If \param{delim} is non-negative, reading stops 456 when the characters \param{delim} is encountered. After \param{len} 457 characters have been read (or \param{delim} has been encountered), or 458 after \param{timeout} seconds have elapsed, the function \param{f} is 459 called.} 460{An \type{EventTcpState} object on success; NULL on failure. Failure 461 is usually due to failure of a UNIX system call or lack of memory.} 462\begin{itemize} 463\item \param{es} -- the event selector. 464\item \param{fd} -- the descriptor to read from. 465\item \param{len} -- the maximum number of bytes to read. 466\item \param{delim} -- if negative, reading continues until exactly 467 \param{len} bytes have been read or the operation times out. If 468 non-negative, reading stops when \param{len} bytes have been read 469 or the characters \param{delim} is encountered, whichever comes first. 470 Note that supplying a non-negative \param{delim} causes \Le{} to 471 invoke the \name{read}(2) system call for \emph{each character}; if 472 you are expecting large amounts of data before the delimiter, this 473 could be inefficient. 474\item \param{f} -- a function which is called when reading has finished, 475 an error occurs, or the operation times out. The function \param{f} 476 looks like this: 477 478 \type{void}~\name{f}{(\type{EventSelector~*}\param{es},~\type{int}~\param{fd},~\type{char~*}\param{int~buf},~\type{int}~\param{len},~\type{int}~\param{flag},~\type{void~*}\param{data})} 479 480 The arguments passed to \param{f} are: 481 \begin{itemize} 482 \item \param{es} -- the event selector. 483 \item \param{fd} -- the file descriptor that was passed to 484 \name{EventTcp\_ReadBuf}. If no more activity on \param{fd} is 485 required, then you should \name{close} it inside \param{f}. 486 \item \param{buf} -- a dynamically-allocated buffer holding the data 487 which were read from \param{fd}. \emph{Do not} free this buffer; 488 \Le{} will take care of it. \emph{Do not} store the pointer value; 489 if you need a copy of the data, you must copy the whole buffer. 490 \item \param{len} -- the number of bytes actually read from \param{fd}. 491 \item \param{flag} -- a flag indicating what happened. It can have 492 one of four values: 493 \begin{itemize} 494 \item \param{EVENT\_TCP\_FLAG\_COMPLETE} -- the operation completed 495 successfully. 496 \item \param{EVENT\_TCP\_FLAG\_IOERROR} -- an error occurred during 497 a \name{read}(2) or some other system call. 498 \item \param{EVENT\_TCP\_FLAG\_EOF} -- EOF was detected before all 499 bytes were read. Nevertheless, \param{len} and \param{buf} have 500 valid contents. 501 \item \param{EVENT\_TCP\_FLAG\_TIMEOUT} -- the operation timed out 502 before all bytes were read. Nevertheless, \param{len} and 503 \param{buf} have valid contents. 504 \end{itemize} 505 \item \param{data} -- a copy of the \param{data} pointer passed to 506 \name{EventTcp\_ReadBuf}. 507 \end{itemize} 508\item \param{timeout} -- if positive, \Le{} times the operation out 509 after \param{timeout} seconds. 510\item \param{data} -- an opaque pointer which is passed as-is to 511 \param{f}. 512\end{itemize} 513 514\function{EventTcpState *}{EventTcp\_WriteBuf} 515{(\=\type{EventSelector *}\param{es},\\ 516 \>\type{int} \param{fd},\\ 517 \>\type{char *}\param{buf},\\ 518 \>\type{int} \param{len},\\ 519 \>\type{EventTcpIOFinishedFunc} \param{f},\\ 520 \>\type{int} \param{timeout},\\ 521 \>\type{void *}\param{data})} 522{Arranges events to write \param{len} characters from the buffer 523 \param{buf} to the file 524 descriptor \param{fd}. After \param{len} 525 characters have been written, an error occurs, or 526 \param{timeout} seconds have elapsed, the function \param{f} is 527 called.} 528{An \type{EventTcpState} object on success; NULL on failure. Failure 529 is usually due to failure of a UNIX system call or lack of memory.} 530\begin{itemize} 531\item \param{es} -- the event selector. 532\item \param{fd} -- the descriptor to write to. 533\item \param{buf} -- buffer containing characters to write. 534 \name{EventTcp\_WriteBuf} allocates its own private copy of the buffer; 535 you may free or reuse the buffer once \name{EventTcp\_WriteBuf} returns. 536\item \param{len} -- the number of bytes to write. 537\item \param{f} -- a function which is called when reading has finished, 538 an error occurs, or the operation times out. The function \param{f} 539 is as described in \name{EventTcp\_ReadBuf}. As a special case, 540 you may supply NULL as the value for \param{f}. In this case, 541 \name{EventTcp\_WriteBuf} calls \name{close}(2) on the descriptor 542 \param{fd} once writing has finished or timed out, or if an error 543 occurs. 544\item \param{timeout} -- if positive, \Le{} times the operation out 545 after \param{timeout} seconds. 546\item \param{data} -- an opaque pointer which is passed as-is to 547 \param{f}. 548\end{itemize} 549\end{document} 550