Event

目录

简介

Event class represents and event firing on a file descriptor being ready to read from or write to; a file descriptor becoming ready to read from or write to(edge-triggered I/O only); a timeout expiring; a signal occuring; a user-triggered event.

Every event is associated with EventBase . However, event will never fire until it is added (via Event::add ). An added event remains in pending state until the registered event occurs, thus turning it to active state. To handle events user may register a callback which is called when event becomes active. If event is configured persistent , it remains pending. If it is not persistent, it stops being pending when it's callback runs. Event::del method deletes event, thus making it non-pending. By means of Event::add method it could be added again.

类摘要

Event

final class Event {

/* Constants */

const integer Event::ET = 32 ;

const integer Event::PERSIST = 16 ;

const integer Event::READ = 2 ;

const integer Event::WRITE = 4 ;

const integer Event::SIGNAL = 8 ;

const integer Event::TIMEOUT = 1 ;

/* 属性 */

public readonly bool $pending ;

/* 方法 */

public bool add ([ float $timeout ] )

public bool addSignal ([ float $timeout ] )

public bool addTimer ([ float $timeout ] )

public __construct ( EventBase $base , mixed $fd , int $what , callable $cb [, mixed $arg = NULL ] )

public bool del ( void )

public bool delSignal ( void )

public bool delTimer ( void )

public void free ( void )

public static array getSupportedMethods ( void )

public bool pending ( int $flags )

public bool set ( EventBase $base , mixed $fd [, int $what [, callable $cb [, mixed $arg ]]] )

public bool setPriority ( int $priority )

public bool setTimer ( EventBase $base , callable $cb [, mixed $arg ] )

public static Event signal ( EventBase $base , int $signum , callable $cb [, mixed $arg ] )

public static Event timer ( EventBase $base , callable $cb [, mixed $arg ] )

}

属性

pending
Whether event is pending. See About event persistence .

预定义常量

Event::ET
Indicates that the event should be edge-triggered, if the underlying event base backend supports edge-triggered events. This affects the semantics of Event::READ and Event::WRITE .

Event::PERSIST
Indicates that the event is persistent. See About event persistence .

Event::READ
This flag indicates an event that becomes active when the provided file descriptor(usually a stream resource, or socket) is ready for reading.

Event::WRITE
This flag indicates an event that becomes active when the provided file descriptor(usually a stream resource, or socket) is ready for reading.

Event::SIGNAL
Used to implement signal detection. See "Constructing signal events" below.

Event::TIMEOUT
This flag indicates an event that becomes active after a timeout elapses.

The Event::TIMEOUT flag is ignored when constructing an event: one can either set a timeout when event is added , or not. It is set in the $what argument to the callback function when a timeout has occurred.

Event::add

Makes event pending

说明

public bool Event::add ([ float $timeout ] )

Marks event pending. Non-pending event will never occur, and the event callback will never be called. In conjuction with Event::del an event could be re-scheduled by user at any time.

If Event::add is called on an already pending event, libevent will leave it pending and re-schedule it with the given timeout(if specified). If in this case timeout is not specified, Event::add has no effect.

参数

timeout
Timeout in seconds.

返回值

Returns TRUE on success. Otherwise FALSE

参见

  • Event::addSignal
  • Event::addTimer
  • Event::del

Event::addSignal

Makes signal event pending

说明

public bool Event::addSignal ([ float $timeout ] )

Event::addSignal is an alias of Event::add

范例

示例 #1 Event::addSignal example

<?php
/*
Launch it in a terminal window:

$ php examples/signal.php

In another terminal window find out the pid and send SIGTERM, e.g.:

$ ps aux | grep examp
ruslan    3976  0.2  0.0 139896 11256 pts/1    S+   10:25   0:00 php examples/signal.php
ruslan    3978  0.0  0.0   9572   864 pts/2    S+   10:26   0:00 grep --color=auto examp
$ kill -TERM 3976

At the first terminal window you should catch the following:

Caught signal 15
*/
class MyEventSignal {
    private $base, $ev;

    public function __construct($base) {
        $this->base = $base;
        $this->ev = Event::signal($base, SIGTERM, array($this, 'eventSighandler'));
        $this->ev->addSignal();
    }

    public function eventSighandler($no, $c) {
        echo "Caught signal $no\n";
        $this->base->exit();
    }
}

$base = new EventBase();
$c    = new MyEventSignal($base);

$base->loop();
?>

以上例程的输出类似于:

Caught signal 15

参见

  • Event::add
  • Event::del
  • Event::delSignal
  • Event::signal

Event::addTimer

Makes timer event pending

说明

public bool Event::addTimer ([ float $timeout ] )

Event::addTimer is an alias of Event::add

返回值

范例

示例 #1 Event::addTimer example

<?php
$base = new EventBase();
$n = 2;
$e = Event::timer($base, function($n) use (&$e) {
    echo "$n seconds elapsed\n";
    $e->delTimer();
}, $n);
$e->addTimer($n);
$base->loop();
?>

以上例程的输出类似于:

2 seconds elapsed

参见

  • Event::add
  • Event::del
  • Event::delTimer

Event::__construct

Constructs Event object

说明

public Event::__construct ( EventBase $base , mixed $fd , int $what , callable $cb [, mixed $arg = NULL ] )

Constructs Event object.

参数

base
The event base to associate with.

fd
stream resource, socket resource, or numeric file descriptor. For timer events pass -1 . For signal events pass the signal number, e.g. SIGHUP .

what
Event flags. See Event flags .

cb
The event callback. See Event callbacks .

arg
Custom data. If specified, it will be passed to the callback when event triggers.

返回值

Returns Event object.

参见

  • Event::signal
  • Event::timer

Event::del

Makes event non-pending

说明

public bool Event::del ( void )

Removes an event from the set of monitored events, i.e. makes it non-pending.

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE

参见

  • Event::add

Event::delSignal

Makes signal event non-pending

说明

public bool Event::delSignal ( void )

Event::delSignal is an alias of Event::del

参见

  • Event::del

Event::delTimer

Makes timer event non-pending

说明

public bool Event::delTimer ( void )

Event::delTimer is an alias of Event::del .

参见

  • Event::del

Event::free

Make event non-pending and free resources allocated for this event

说明

public void Event::free ( void )

Removes event from the list of events monitored by libevent, and free resources allocated for the event.

Warning

The Event::free method currently doesn't destruct the object itself. To destruct the object completely call unset , or assign NULL.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • Event::__construct

Event::getSupportedMethods

Returns array with of the names of the methods supported in this version of Libevent

说明

public static array Event::getSupportedMethods ( void )

Returns array with of the names of the methods(backends) supported in this version of Libevent.

参数

此函数没有参数。

返回值

Returns array.

参见

  • EventConfig

Event::pending

Detects whether event is pending or scheduled

说明

public bool Event::pending ( int $flags )

Detects whether event is pending or scheduled

参数

flags
One of, or a composition of the following constants: Event::READ , Event::WRITE , Event::TIMEOUT , Event::SIGNAL .

返回值

Returns TRUE if event is pending or scheduled. Otherwise FALSE.

Event::set

Re-configures event

说明

public bool Event::set ( EventBase $base , mixed $fd [, int $what [, callable $cb [, mixed $arg ]]] )

Re-configures event. Note, this function doesn't invoke obsolete libevent's event_set. It calls event_assign instead.

参数

base
The event base to associate the event with.

fd
Stream resource, socket resource, or numeric file descriptor. For timer events pass -1 . For signal events pass the signal number, e.g. SIGHUP .

what
See Event flags .

cb
The event callback. See Event callbacks .

arg
Custom data associated with the event. It will be passed to the callback when the event becomes active.

返回值

Returns TRUE on success. Otherwise FALSE.

Event::setPriority

Set event priority

说明

public bool Event::setPriority ( int $priority )

Set event priority.

参数

priority
The event priority.

返回值

Returns TRUE on success. Otherwise FALSE.

Event::setTimer

Re-configures timer event

说明

public bool Event::setTimer ( EventBase $base , callable $cb [, mixed $arg ] )

Re-configures timer event. Note, this function doesn't invoke obsolete libevent's event_set . It calls event_assign instead.

参数

base
The event base to associate with.

cb
The timer event callback. See Event callbacks .

arg
Custom data. If specified, it will be passed to the callback when event triggers.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • Event::__construct
  • Event::timer

Event::signal

Constructs signal event object

说明

public static Event Event::signal ( EventBase $base , int $signum , callable $cb [, mixed $arg ] )

Constructs signal event object. This is a straightforward method to create a signal event. Note, the generic Event::__construct method can contruct signal event objects too.

参数

base
The associated event base object.

signum
The signal number.

cb
The signal event callback. See Event callbacks .

arg
Custom data. If specified, it will be passed to the callback when event triggers.

返回值

Returns Event object on success. Otherwise FALSE.

参见

Event::timer

Constructs timer event object

说明

public static Event Event::timer ( EventBase $base , callable $cb [, mixed $arg ] )

Constructs timer event object. This is a straightforward method to create a timer event. Note, the generic Event::__construct method can contruct signal event objects too.

参数

base
The associated event base object.

cb
The signal event callback. See Event callbacks .

arg
Custom data. If specified, it will be passed to the callback when event triggers.

返回值

Returns Event object on success. Otherwise FALSE.

简介

EventBase class represents libevent's event base structure. It holds a set of events and can poll to determine which events are active.

Each event base has a method , or a backend that it uses to determine which events are ready. The recognized methods are: select , poll , epoll , kqueue , devpoll , evport and win32 .

To configure event base to use, or avoid specific backend EventConfig class can be used.

Warning

Do NOT destroy the EventBase object as long as resources of the associated Event objects are not released. Otherwise, it will lead to unpredictable results!

类摘要

EventBase

final class EventBase {

/* Constants */

const integer EventBase::LOOP_ONCE = 1 ;

const integer EventBase::LOOP_NONBLOCK = 2 ;

const integer EventBase::NOLOCK = 1 ;

const integer EventBase::STARTUP_IOCP = 4 ;

const integer EventBase::NO_CACHE_TIME = 8 ;

const integer EventBase::EPOLL_USE_CHANGELIST = 16 ;

/* 方法 */

public __construct ([ EventConfig $cfg ] )

public void dispatch ( void )

public bool exit ([ float $timeout ] )

public void free ( void )

public int getFeatures ( void )

public string getMethod ( void )

public float getTimeOfDayCached ( void )

public bool gotExit ( void )

public bool gotStop ( void )

public bool loop ([ int $flags ] )

public bool priorityInit ( int $n_priorities )

public bool reInit ( void )

public bool stop ( void )

}

预定义常量

EventBase::LOOP_ONCE
Flag used with EventBase::loop method which means: "block until libevent has an active event, then exit once all active events have had their callbacks run".

EventBase::LOOP_NONBLOCK
Flag used with EventBase::loop method which means: "do not block: see which events are ready now, run the callbacks of the highest-priority ones, then exit".

EventBase::NOLOCK
Configuration flag. Do not allocate a lock for the event base, even if we have locking set up".

EventBase::STARTUP_IOCP
Windows-only configuration flag. Enables the IOCP dispatcher at startup.

EventBase::NO_CACHE_TIME
Configuration flag. Instead of checking the current time every time the event loop is ready to run timeout callbacks, check after each timeout callback.

EventBase::EPOLL_USE_CHANGELIST
If we are using the epoll backend, this flag says that it is safe to use Libevent's internal change-list code to batch up adds and deletes in order to try to do as few syscalls as possible.

Setting this flag can make code run faster, but it may trigger a Linux bug: it is not safe to use this flag if one has any fds cloned by dup(), or its variants. Doing so will produce strange and hard-to-diagnose bugs.

This flag can also be activated by settnig the EVENT_EPOLL_USE_CHANGELIST environment variable.

This flag has no effect if one winds up using a backend other than epoll .

EventBase::__construct

Constructs EventBase object

说明

public EventBase::__construct ([ EventConfig $cfg ] )

Constructs EventBase object

参数

cfg
Optional EventConfig object.

返回值

Returns EventBase object.

参见

  • EventConfig

EventBase::dispatch

Dispatch pending events

说明

public void EventBase::dispatch ( void )

Wait for events to become active, and run their callbacks. The same as EventBase::loop with no flags set.

Warning

Do NOT destroy the EventBase object as long as resources of the associated Event objects are not released. Otherwise, it will lead to unpredictable results!

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBase::loop

EventBase::exit

Stop dispatching events

说明

public bool EventBase::exit ([ float $timeout ] )

Tells event base to stop optionally after given number of seconds.

参数

timeout
Optional number of seconds after which the event base should stop dispatching events.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBase::stop

EventBase::free

Free resources allocated for this event base

说明

public void EventBase::free ( void )

Deallocates resources allocated by libevent for the EventBase object.

Warning

The EventBase::free method doesn't destruct the object itself. To destruct the object completely call unset , or assign NULL.

This method does not deallocate or detach any of the events that are currently associated with the EventBase object, or close any of their sockets - beware.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • EventBase::__construct

EventBase::getFeatures

Returns bitmask of features supported

说明

public int EventBase::getFeatures ( void )

Returns bitmask of features supported.

参数

此函数没有参数。

返回值

Returns integer representing a bitmask of supported features. See EventConfig::FEATURE_* constants .

范例

示例 #1 EventBase::getFeatures example

<?php
// Avoiding "select" method
$cfg = new EventConfig();
if ($cfg->avoidMethod("select")) {
    echo "`select' method avoided\n";
}

$base = new EventBase($cfg);

echo "Features:\n";
$features = $base->getFeatures();
($features & EventConfig::FEATURE_ET) and print("ET - edge-triggered IO\n");
($features & EventConfig::FEATURE_O1) and print("O1 - O(1) operation for adding/deletting events\n");
($features & EventConfig::FEATURE_FDS) and print("FDS - arbitrary file descriptor types, and not just sockets\n");
?>

参见

  • EventBase::getMethod
  • EventConfig

EventBase::getMethod

Returns event method in use

说明

public string EventBase::getMethod ( void )

参数

此函数没有参数。

返回值

String representing used event method(backend).

范例

示例 #1 EventBase::getMethod example

<?php
$cfg = new EventConfig();
if ($cfg->avoidMethod("select")) {
    echo "`select' method avoided\n";
}

// Create event_base associated with the config
$base = new EventBase($cfg);
echo "Event method used: ", $base->getMethod(), PHP_EOL;

?>

以上例程的输出类似于:

`select' method avoided
Event method used: epoll

参见

  • EventBase::getFeatures

EventBase::getTimeOfDayCached

Returns the current event base time

说明

public float EventBase::getTimeOfDayCached ( void )

On success returns the current time(as returned by gettimeofday() ), looking at the cached value in base if possible, and calling gettimeofday() or clock_gettime() as appropriate if there is no cached time.

参数

此函数没有参数。

返回值

Returns the current event base time. On failure returns NULL.

EventBase::gotExit

Checks if the event loop was told to exit

说明

public bool EventBase::gotExit ( void )

Checks if the event loop was told to exit by EventBase::exit .

参数

此函数没有参数。

返回值

Returns TRUE, event loop was told to exit by EventBase::exit . Otherwise FALSE.

参见

  • EventBase::exit
  • EventBase::stop
  • EventBase::gotStop

EventBase::gotStop

Checks if the event loop was told to exit

说明

public bool EventBase::gotStop ( void )

Checks if the event loop was told to exit by EventBase::stop .

参数

此函数没有参数。

返回值

Returns TRUE, event loop was told to stop by EventBase::stop . Otherwise FALSE.

参见

  • EventBase::exit
  • EventBase::stop
  • EventBase::gotExit

EventBase::loop

Dispatch pending events

说明

public bool EventBase::loop ([ int $flags ] )

Wait for events to become active, and run their callbacks.

Warning

Do NOT destroy the EventBase object as long as resources of the associated Event objects are not released. Otherwise, it will lead to unpredictable results!

参数

flags
Optional flags. One of EventBase::LOOP_* constants. See EventBase constants .

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBase::dispatch

EventBase::priorityInit

Sets number of priorities per event base

说明

public bool EventBase::priorityInit ( int $n_priorities )

Sets number of priorities per event base.

参数

n_priorities
The number of priorities per event base.

返回值

Returns TRUE on success, otherwise FALSE.

参见

  • Event::setPriority

EventBase::reInit

Re-initialize event base(after a fork)

说明

public bool EventBase::reInit ( void )

Re-initialize event base. Should be called after a fork.

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

EventBase::stop

Tells event_base to stop dispatching events

说明

public bool EventBase::stop ( void )

Tells event_base to stop dispatching events

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBase::exit
  • EventBase::gotStop

简介

EventBuffer represents Libevent's "evbuffer", an utility functionality for buffered I/O.

Event buffers are meant to be generally useful for doing the "buffer" part of buffered network I/O.

类摘要

EventBuffer

class EventBuffer {

/* Constants */

const integer EventBuffer::EOL_ANY = 0 ;

const integer EventBuffer::EOL_CRLF = 1 ;

const integer EventBuffer::EOL_CRLF_STRICT = 2 ;

const integer EventBuffer::EOL_LF = 3 ;

const integer EventBuffer::PTR_SET = 0 ;

const integer EventBuffer::PTR_ADD = 1 ;

/* 属性 */

public readonly int $length ;

public readonly int $contiguous_space ;

/* 方法 */

public bool add ( string $data )

public bool addBuffer ( EventBuffer $buf )

public int appendFrom ( EventBuffer $buf , int $len )

public __construct ( void )

public int copyout ( string &$data , int $max_bytes )

public bool drain ( int $len )

public void enableLocking ( void )

public bool expand ( int $len )

public bool freeze ( bool $at_front )

public void lock ( void )

public bool prepend ( string $data )

public bool prependBuffer ( EventBuffer $buf )

public string pullup ( int $size )

public string read ( int $max_bytes )

public int read ( mixed $fd , int $howmuch )

public string readLine ( int $eol_style )

public mixed search ( string $what [, int $start = -1 [, int $end = -1 ]] )

public mixed searchEol ([ int $start = -1 [, int $eol_style = EventBuffer::EOL_ANY ]] )

public string substr ( int $start [, int $length ] )

public bool unfreeze ( bool $at_front )

public bool unlock ( void )

public int write ( mixed $fd [, int $howmuch ] )

}

属性

length
The number of bytes stored in an event buffer.

contiguous_space
The number of bytes stored contiguously at the front of the buffer. The bytes in a buffer may be stored in multiple separate chunks of memory; the property returns the number of bytes currently stored in the first chunk.

预定义常量

EventBuffer::EOL_ANY
The end of line is any sequence of any number of carriage return and linefeed characters. This format is not very useful; it exists mainly for backward compatibility.

EventBuffer::EOL_CRLF
The end of the line is an optional carriage return, followed by a linefeed. (In other words, it is either a "\r\n" or a "\n" .) This format is useful in parsing text-based Internet protocols, since the standards generally prescribe a "\r\n" line-terminator, but nonconformant clients sometimes say just "\n" .

EventBuffer::EOL_CRLF_STRICT
The end of a line is a single carriage return, followed by a single linefeed. (This is also known as "\r\n" . The ASCII values are 0x0D 0x0A ).

EventBuffer::EOL_LF
The end of a line is a single linefeed character. (This is also known as "\n" . It is ASCII value is 0x0A .)

EventBuffer::PTR_SET
Flag used as argument of EventBuffer::setPosition method. If this flag specified, the position pointer is moved to an absolute position within the buffer.

EventBuffer::PTR_ADD
The same as EventBuffer::PTR_SET , except this flag causes EventBuffer::setPosition method to move position forward up to the specified number of bytes(instead of setting absolute position).

EventBuffer::add

Append data to the end of an event buffer

说明

public bool EventBuffer::add ( string $data )

Append data to the end of an event buffer.

参数

data
String to be appended to the end of the buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::addBuffer

EventBuffer::addBuffer

Move all data from a buffer provided to the current instance of EventBuffer

说明

public bool EventBuffer::addBuffer ( EventBuffer $buf )

Move all data from the buffer provided in buf parameter to the end of current EventBuffer . This is a destructive add. The data from one buffer moves into the other buffer. However, no unnecessary memory copies occur.

参数

buf
The source EventBuffer object.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::add

EventBuffer::appendFrom

Moves the specified number of bytes from a source buffer to the end of the current buffer

说明

public int EventBuffer::appendFrom ( EventBuffer $buf , int $len )

Moves the specified number of bytes from a source buffer to the end of the current buffer. If there are fewer number of bytes, it moves all the bytes available from the source buffer.

参数

buf
Source buffer.

len

返回值

Returns the number of bytes read.

更新日志

版本说明
1.6.0Renamed EventBuffer::appendFrom(the old method name) to EventBuffer::appendFrom.

参见

  • EventBuffer::copyout
  • EventBuffer::drain
  • EventBuffer::pullup
  • EventBuffer::readLine
  • EventBuffer::read

EventBuffer::__construct

Constructs EventBuffer object

说明

public EventBuffer::__construct ( void )

Constructs EventBuffer object

参数

此函数没有参数。

返回值

Returns EventBuffer object.

EventBuffer::copyout

Copies out specified number of bytes from the front of the buffer

说明

public int EventBuffer::copyout ( string &$data , int $max_bytes )

Behaves just like EventBuffer::read , but does not drain any data from the buffer. I.e. it copies the first max_bytes bytes from the front of the buffer into data . If there are fewer than max_bytes bytes available, the function copies all the bytes there are.

参数

data
Output string.

max_bytes
The number of bytes to copy.

返回值

Returns the number of bytes copied, or -1 on failure.

参见

  • EventBuffer::read
  • EventBuffer::appendFrom

EventBuffer::drain

Removes specified number of bytes from the front of the buffer without copying it anywhere

说明

public bool EventBuffer::drain ( int $len )

Behaves as EventBuffer::read , except that it does not copy the data: it just removes it from the front of the buffer.

参数

len
The number of bytes to remove from the buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::read
  • EventBuffer::appendFrom

EventBuffer::enableLocking

说明

public void EventBuffer::enableLocking ( void )

Enable locking on an EventBuffer so that it can safely be used by multiple threads at the same time. When locking is enabled, the lock will be held when callbacks are invoked. This could result in deadlock if you aren't careful. Plan accordingly!

参数

此函数没有参数。

返回值

没有返回值。

参见

EventBuffer::expand

Reserves space in buffer

说明

public bool EventBuffer::expand ( int $len )

Alters the last chunk of memory in the buffer, or adds a new chunk, such that the buffer is now large enough to contain len bytes without any further allocations.

参数

len
The number of bytes to reserve for the buffer

返回值

Returns TRUE on success. Otherwise FALSE.

EventBuffer::freeze

Prevent calls that modify an event buffer from succeeding

说明

public bool EventBuffer::freeze ( bool $at_front )

Prevent calls that modify an event buffer from succeeding

参数

at_front
Whether to disable changes to the front or end of the buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::unfreeze

EventBuffer::lock

Acquires a lock on buffer

说明

public void EventBuffer::lock ( void )

Acquires a lock on buffer. Can be used in pair with EventBuffer::unlock to make a set of operations atomic, i.e. thread-safe. Note, it is not needed to lock buffers for individual operations. When locking is enabled(see EventBuffer::enableLocking ), individual operations on event buffers are already atomic.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • EventBuffer::unlock

EventBuffer::prepend

Prepend data to the front of the buffer

说明

public bool EventBuffer::prepend ( string $data )

Prepend data to the front of the buffer.

参数

data
String to be prepended to the front of the buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::prependBuffer
  • EventBuffer::add
  • EventBuffer::addBuffer

EventBuffer::prependBuffer

Moves all data from source buffer to the front of current buffer

说明

public bool EventBuffer::prependBuffer ( EventBuffer $buf )

Behaves as EventBuffer::addBuffer , except that it moves data to the front of the buffer.

参数

buf
Source buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::add
  • EventBuffer::addBuffer
  • EventBuffer::prepend

EventBuffer::pullup

Linearizes data within buffer and returns it's contents as a string

说明

public string EventBuffer::pullup ( int $size )

"Linearizes" the first size bytes of the buffer, copying or moving them as needed to ensure that they are all contiguous and occupying the same chunk of memory. If size is negative, the function linearizes the entire buffer.

Warning

Calling EventBuffer::pullup with a large size can be quite slow, since it potentially needs to copy the entire buffer's contents.

参数

size
The number of bytes required to be contiguous within the buffer.

返回值

If size is greater than the number of bytes in the buffer, the function returns NULL. Otherwise, EventBuffer::pullup returns string.

参见

  • EventBuffer::copyout
  • EventBuffer::drain
  • EventBuffer::read
  • EventBuffer::readLine
  • EventBuffer::appendFrom

EventBuffer::read

Read data from an evbuffer and drain the bytes read

说明

public string EventBuffer::read ( int $max_bytes )

Read the first max_bytes from the buffer and drain the bytes read. If more max_bytes are requested than are available in the buffer, it only extracts as many bytes as available.

参数

max_bytes
Maxmimum number of bytes to read from the buffer.

返回值

Returns string read, or FALSE on failure.

更新日志

版本说明
1.6.0Renamed EventBuffer::read(the old method name) to EventBuffer::read. EventBuffer::read now takes only max_bytes argument; returns string instead of integer.

参见

  • EventBuffer::copyout
  • EventBuffer::drain
  • EventBuffer::pullup
  • EventBuffer::readLine
  • EventBuffer::appendFrom

EventBuffer::readFrom

Read data from a file onto the end of the buffer

说明

public int EventBuffer::read ( mixed $fd , int $howmuch )

Read data from the file specified by fd onto the end of the buffer.

参数

fd
Socket resource, stream, or numeric file descriptor.

howmuch
Maxmimum number of bytes to read.

返回值

Returns the number of bytes read, or FALSE on failure.

参见

  • EventBuffer::copyout
  • EventBuffer::drain
  • EventBuffer::pullup
  • EventBuffer::readLine
  • EventBuffer::appendFrom

EventBuffer::readLine

Extracts a line from the front of the buffer

说明

public string EventBuffer::readLine ( int $eol_style )

Extracts a line from the front of the buffer and returns it in a newly allocated string. If there is not a whole line to read, the function returns NULL. The line terminator is not included in the copied string.

参数

eol_style
One of EventBuffer:EOL_* constants .

返回值

On success returns the line read from the buffer, otherwise NULL.

参见

  • EventBuffer::copyout
  • EventBuffer::drain
  • EventBuffer::pullup
  • EventBuffer::read
  • EventBuffer::appendFrom

EventBuffer::search

Scans the buffer for an occurrence of a string

说明

public mixed EventBuffer::search ( string $what [, int $start = -1 [, int $end = -1 ]] )

Scans the buffer for an occurrence of the string what . It returns numeric position of the string, or FALSE if the string was not found.

If the start argument is provided, it points to the position at which the search should begin; otherwise, the search is performed from the start of the string. If end argument provided, the search is performed between start and end buffer positions.

参数

what
String to search.

start
Start search position.

end
End search position.

返回值

Returns numeric position of the first occurance of the string in the buffer, or FALSE if string is not found.

Warning

此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

范例

示例 #1 EventBuffer::search example

<?php
// Count total occurances of 'str' in 'buf'
function count_instances($buf, $str) {
    $total = 0;
    $p     = 0;
    $i     = 0;

    while (1) {
        $p = $buf->search($str, $p);
        if ($p === FALSE) {
            break;
        }
        ++$total;
        ++$p;
    }

    return $total;
}

$buf = new EventBuffer();
$buf->add("Some string within a string inside another string");
var_dump(count_instances($buf, "str"));
?>

以上例程的输出类似于:

int(3)

参见

  • EventBuffer::searchEol

EventBuffer::searchEol

Scans the buffer for an occurrence of an end of line

说明

public mixed EventBuffer::searchEol ([ int $start = -1 [, int $eol_style = EventBuffer::EOL_ANY ]] )

Scans the buffer for an occurrence of an end of line specified by eol_style parameter . It returns numeric position of the string, or FALSE if the string was not found.

If the start argument is provided, it represents the position at which the search should begin; otherwise, the search is performed from the start of the string. If end argument provided, the search is performed between start and end buffer positions.

参数

start
Start search position.

eol_style
One of EventBuffer:EOL_* constants .

返回值

Returns numeric position of the first occurance of end-of-line symbol in the buffer, or FALSE if not found.

Warning

此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

参见

  • EventBuffer::search

EventBuffer::substr

Substracts a portion of the buffer data

说明

public string EventBuffer::substr ( int $start [, int $length ] )

Substracts up to length bytes of the buffer data beginning at start position.

参数

start
The start position of data to be substracted.

length
Maximum number of bytes to substract.

返回值

Returns the data substracted as a string on success, or FALSE on failure.

参见

  • EventBuffer::read

EventBuffer::unfreeze

Re-enable calls that modify an event buffer

说明

public bool EventBuffer::unfreeze ( bool $at_front )

Re-enable calls that modify an event buffer.

参数

at_front
Whether to enable events at the front or at the end of the buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::freeze

EventBuffer::unlock

Releases lock acquired by EventBuffer::lock

说明

public bool EventBuffer::unlock ( void )

Releases lock acquired by EventBuffer::lock .

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBuffer::lock

EventBuffer::write

Write contents of the buffer to a file or socket

说明

public int EventBuffer::write ( mixed $fd [, int $howmuch ] )

Write contents of the buffer to a file descriptor. The buffer will be drained after the bytes have been successfully written.

参数

fd
Socket resource, stream or numeric file descriptor associated normally associated with a socket.

howmuch
The maximum number of bytes to write.

返回值

Returns the number of bytes written, or FALSE on error.

参见

  • EventBuffer::read

简介

Represents Libevent's buffer event.

Usually an application wants to perform some amount of data buffering in addition to just responding to events. When we want to write data, for example, the usual pattern looks like:

  1. Decide that we want to write some data to a connection; put that data in a buffer.

  2. Wait for the connection to become writable

  3. Write as much of the data as we can

  4. Remember how much we wrote, and if we still have more data to write, wait for the connection to become writable again.

This buffered I/O pattern is common enough that Libevent provides a generic mechanism for it. A "buffer event" consists of an underlying transport (like a socket), a read buffer, and a write buffer. Instead of regular events, which give callbacks when the underlying transport is ready to be read or written, a buffer event invokes its user-supplied callbacks when it has read or written enough data.

类摘要

EventBufferEvent

final class EventBufferEvent {

/* Constants */

const integer EventBufferEvent::READING = 1 ;

const integer EventBufferEvent::WRITING = 2 ;

const integer EventBufferEvent::EOF = 16 ;

const integer EventBufferEvent::ERROR = 32 ;

const integer EventBufferEvent::TIMEOUT = 64 ;

const integer EventBufferEvent::CONNECTED = 128 ;

const integer EventBufferEvent::OPT_CLOSE_ON_FREE = 1 ;

const integer EventBufferEvent::OPT_THREADSAFE = 2 ;

const integer EventBufferEvent::OPT_DEFER_CALLBACKS = 4 ;

const integer EventBufferEvent::OPT_UNLOCK_CALLBACKS = 8 ;

const integer EventBufferEvent::SSL_OPEN = 0 ;

const integer EventBufferEvent::SSL_CONNECTING = 1 ;

const integer EventBufferEvent::SSL_ACCEPTING = 2 ;

/* 属性 */

public integer $fd ;

public integer $priority ;

public readonly EventBuffer $input ;

public readonly EventBuffer $output ;

/* 方法 */

public void close ( void )

public bool connect ( string $addr )

public bool connectHost ( EventDnsBase $dns_base , string $hostname , int $port [, int $family = EventUtil::AF_UNSPEC ] )

public __construct ( EventBase $base [, mixed $socket = NULL [, int $options = 0 [, callable $readcb = NULL [, callable $writecb = NULL [, callable $eventcb = NULL ]]]]] )

public static array createPair ( EventBase $base [, int $options = 0 ] )

public bool disable ( int $events )

public bool enable ( int $events )

public void free ( void )

public string getDnsErrorString ( void )

public int getEnabled ( void )

public EventBuffer getInput ( void )

public EventBuffer getOutput ( void )

public string read ( int $size )

public bool readBuffer ( EventBuffer $buf )

public void setCallbacks ( callable $readcb , callable $writecb , callable $eventcb [, string $arg ] )

public bool setPriority ( int $priority )

public bool setTimeouts ( float $timeout_read , float $timeout_write )

public void setWatermark ( int $events , int $lowmark , int $highmark )

public string sslError ( void )

public static EventBufferEvent sslFilter ( EventBase $base , EventBufferEvent $underlying , EventSslContext $ctx , int $state [, int $options = 0 ] )

public string sslGetCipherInfo ( void )

public string sslGetCipherName ( void )

public string sslGetCipherVersion ( void )

public string sslGetProtocol ( void )

public void sslRenegotiate ( void )

public static EventBufferEvent sslSocket ( EventBase $base , mixed $socket , EventSslContext $ctx , int $state [, int $options ] )

public bool write ( string $data )

public bool writeBuffer ( EventBuffer $buf )

}

属性

fd
Numeric file descriptor associated with the buffer event. Normally represents a bound socket. Equals to NULL, if there is no file descriptor(socket) associated with the buffer event.

priority
The priority of the events used to implement the buffer event.

input
Underlying input buffer object( EventBuffer )

output
Underlying output buffer object( EventBuffer )

预定义常量

EventBufferEvent::READING
An event occurred during a read operation on the bufferevent. See the other flags for which event it was.

EventBufferEvent::WRITING
An event occurred during a write operation on the bufferevent. See the other flags for which event it was.

EventBufferEvent::EOF
Got an end-of-file indication on the buffer event.

EventBufferEvent::ERROR
An error occurred during a bufferevent operation. For more information on what the error was, call EventUtil::getLastSocketErrno and/or EventUtil::getLastSocketError .

EventBufferEvent::TIMEOUT

EventBufferEvent::CONNECTED
Finished a requested connection on the bufferevent.

EventBufferEvent::OPT_CLOSE_ON_FREE
When the buffer event is freed, close the underlying transport. This will close an underlying socket, free an underlying buffer event, etc.

EventBufferEvent::OPT_THREADSAFE
Automatically allocate locks for the bufferevent, so that it’s safe to use from multiple threads.

EventBufferEvent::OPT_DEFER_CALLBACKS
When this flag is set, the bufferevent defers all of its callbacks. See » Fast portable non-blocking network programming with Libevent, Deferred callbacks .

EventBufferEvent::OPT_UNLOCK_CALLBACKS
By default, when the bufferevent is set up to be threadsafe, the buffer event’s locks are held whenever the any user-provided callback is invoked. Setting this option makes Libevent release the buffer event’s lock when it’s invoking the callbacks.

EventBufferEvent::SSL_OPEN
The SSL handshake is done

EventBufferEvent::SSL_CONNECTING
SSL is currently performing negotiation as a client

EventBufferEvent::SSL_ACCEPTING
SSL is currently performing negotiation as a server

EventBufferEvent::close

Closes file descriptor associated with the current buffer event

说明

public void EventBufferEvent::close ( void )

Closes file descriptor associated with the current buffer event.

This method may be used in cases when the EventBufferEvent::OPT_CLOSE_ON_FREE option is not appropriate.

参数

此函数没有参数。

返回值

没有返回值。

EventBufferEvent::connect

Connect buffer event's file descriptor to given address or UNIX socket

说明

public bool EventBufferEvent::connect ( string $addr )

Connect buffer event's file descriptor to given address(optionally with port), or a UNIX domain socket.

If socket is not assigned to the buffer event, this function allocates a new socket and makes it non-blocking internally.

To resolve DNS names(asyncronously), use EventBufferEvent::connectHost method.

参数

addr
Should contain an IP address with optional port number, or a path to UNIX domain socket. Recognized formats are:

[IPv6Address]:port
[IPv6Address]
IPv6Address
IPv4Address:port
IPv4Address
unix:path

Note, 'unix:' prefix is currently not case sensitive.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventBufferEvent::connect example

<?php
/*
 * 1. Connect to 127.0.0.1 at port 80
 * by means of EventBufferEvent::connect().
 *
 * 2. Request /index.cphp via HTTP/1.0
 * using the output buffer.
 *
 * 3. Asyncronously read the response and print it to stdout.
 */

/* Read callback */
function readcb($bev, $base) {
    $input = $bev->getInput();

    while (($n = $input->remove($buf, 1024)) > 0) {
        echo $buf;
    }
}

/* Event callback */
function eventcb($bev, $events, $base) {
    if ($events & EventBufferEvent::CONNECTED) {
        echo "Connected.\n";
    } elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
        if ($events & EventBufferEvent::ERROR) {
            echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
        }

        echo "Closing\n";
        $base->exit();
        exit("Done\n");
    }
}

$base = new EventBase();

echo "step 1\n";
$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
    EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
if (!$bev) {
    exit("Failed creating bufferevent socket\n");
}

echo "step 2\n";
$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);

echo "step 3\n";
/* Send request */
$output = $bev->getOutput();
if (!$output->add(
    "GET /index.cphp HTTP/1.0\r\n".
    "Connection: Close\r\n\r\n"
)) {
    exit("Failed adding request to output buffer\n");
}

/* Connect to the host syncronously.
 * We know the IP, and don't need to resolve DNS. */
if (!$bev->connect("127.0.0.1:80")) {
    exit("Can't connect to host\n");
}

/* Dispatch pending events */
$base->dispatch();

以上例程的输出类似于:

step 1
step 2
step 3
Connected.
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Sat, 09 Mar 2013 10:06:58 GMT
Content-Type: text/html; charset=utf-8
Connection: close
X-Powered-By: PHP/5.4.11--pl2-gentoo

sdfsdfsf
Closing
Done

示例 #2 Connect to UNIX domain socket which presumably is served by a server, read response from the server and output it to the console

<?php
class MyUnixSocketClient {
    private $base, $bev;

    function __construct($base, $sock_path) {
        $this->base = $base;
        $this->bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE,
            array ($this, "read_cb"), NULL, array ($this, "event_cb"));

        if (!$this->bev->connect("unix:$sock_path")) {
            trigger_error("Failed to connect to socket `$sock_path'", E_USER_ERROR);
        }

        $this->bev->enable(Event::READ);
    }

    function __destruct() {
        if ($this->bev) {
            $this->bev->free();
            $this->bev = NULL;
        }
    }

    function dispatch() {
        $this->base->dispatch();
    }

    function read_cb($bev, $unused) {
        $in = $bev->input;

        printf("Received %ld bytes\n", $in->length);
        printf("----- data ----\n");
        printf("%ld:\t%s\n", (int) $in->length, $in->pullup(-1));

        $this->bev->free();
        $this->bev = NULL;
        $this->base->exit(NULL);
    }

    function event_cb($bev, $events, $unused) {
        if ($events & EventBufferEvent::ERROR) {
            echo "Error from bufferevent\n";
        }

        if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
            $bev->free();
            $bev = NULL;
        } elseif ($events & EventBufferEvent::CONNECTED) {
            $bev->output->add("test\n");
        }
    }
}

if ($argc <= 1) {
    exit("Socket path is not provided\n");
}
$sock_path = $argv[1];

$base = new EventBase();
$cl = new MyUnixSocketClient($base, $sock_path);
$cl->dispatch();
?>

以上例程的输出类似于:

Received 5 bytes
----- data ----
5:  test

参见

  • EventBufferEvent::connectHost

EventBufferEvent::connectHost

Connects to a hostname with optionally asyncronous DNS resolving

说明

public bool EventBufferEvent::connectHost ( EventDnsBase $dns_base , string $hostname , int $port [, int $family = EventUtil::AF_UNSPEC ] )

Resolves the DNS name hostname, looking for addresses of type family ( EventUtil::AF_* constants). If the name resolution fails, it invokes the event callback with an error event. If it succeeds, it launches a connection attempt just as EventBufferEvent::connect would.

dns_base is optional. May be NULL, or an object created with EventDnsBase::__construct . For asyncronous hostname resolving pass a valid event dns base resource. Otherwise the hostname resolving will block.

Note:

EventDnsBase is available only if Event configured --with-event-extra ( event_extra library, libevent protocol-specific functionality support including HTTP, DNS, and RPC ).

Note:

EventBufferEvent::connectHost requires libevent-2.0.3-alpha or greater.

参数

dns_base
Object of EventDnsBase in case if DNS is to be resolved asyncronously. Otherwise NULL.

hostname
Hostname to connect to. Recognized formats are:

www.example.com (hostname)
 1.2.3.4 (ipv4address)
 ::1 (ipv6address)
[::1] ([ipv6address])

port
Port number

family
Address family. EventUtil::AF_UNSPEC , EventUtil::AF_INET , or EventUtil::AF_INET6 . See EventUtil constants .

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventBufferEvent::connectHost example

<?php
/* Read callback */
function readcb($bev, $base) {
    //$input = $bev->input; //$bev->getInput();

    //$pos = $input->search("TTP");
    $pos = $bev->input->search("TTP");

    while (($n = $bev->input->remove($buf, 1024)) > 0) {
        echo $buf;
    }
}

/* Event callback */
function eventcb($bev, $events, $base) {
    if ($events & EventBufferEvent::CONNECTED) {
        echo "Connected.\n";
    } elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
        if ($events & EventBufferEvent::ERROR) {
            echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
        }

        echo "Closing\n";
        $base->exit();
        exit("Done\n");
    }
}

$base = new EventBase();

$dns_base = new EventDnsBase($base, TRUE); // We'll use async DNS resolving
if (!$dns_base) {
    exit("Failed to init DNS Base\n");
}

$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
    EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS,
    "readcb", /* writecb */ NULL, "eventcb", $base
);
if (!$bev) {
    exit("Failed creating bufferevent socket\n");
}

//$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);

$output = $bev->output; //$bev->getOutput();
if (!$output->add(
    "GET {$argv[2]} HTTP/1.0\r\n".
    "Host: {$argv[1]}\r\n".
    "Connection: Close\r\n\r\n"
)) {
    exit("Failed adding request to output buffer\n");
}

if (!$bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC)) {
    exit("Can't connect to host {$argv[1]}\n");
}

$base->dispatch();
?>

以上例程的输出类似于:

Connected.
HTTP/1.0 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sat, 09 Mar 2013 12:21:19 GMT
Expires: Mon, 08 Apr 2013 12:21:19 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/">here</A>.
</BODY></HTML>
Closing
Done

参见

  • EventBufferEvent::connect

EventBufferEvent::__construct

Constructs EventBufferEvent object

说明

public EventBufferEvent::__construct ( EventBase $base [, mixed $socket = NULL [, int $options = 0 [, callable $readcb = NULL [, callable $writecb = NULL [, callable $eventcb = NULL ]]]]] )

Create a buffer event on a socket, stream or a file descriptor. Passing NULL to socket means that the socket should be created later, e.g. by means of EventBufferEvent::connect .

参数

base
Event base that should be associated with the new buffer event.

socket
May be created as a stream(not necessarily by means of sockets extension)

options
One of EventBufferEvent::OPT_* constants , or 0 .

readcb
Read event callback. See About buffer event callbacks .

writecb
Write event callback. See About buffer event callbacks .

eventcb
Status-change event callback. See About buffer event callbacks .

arg
A variable that will be passed to all the callbacks.

返回值

Returns buffer event resource optionally associated with socket resource. */

参见

  • EventBufferEvent::sslFilter
  • EventBufferEvent::sslSocket

EventBufferEvent::createPair

Creates two buffer events connected to each other

说明

public static array EventBufferEvent::createPair ( EventBase $base [, int $options = 0 ] )

Returns array of two EventBufferEvent objects connected to each other. All the usual options are supported, except for EventBufferEvent::OPT_CLOSE_ON_FREE , which has no effect, and EventBufferEvent::OPT_DEFER_CALLBACKS , which is always on.

参数

base
Associated event base

options
EventBufferEvent::OPT_* constants combined with bitwise OR operator.

返回值

Returns array of two EventBufferEvent objects connected to each other.

更新日志

版本说明
1.9.0Method made static.

EventBufferEvent::disable

Disable events read, write, or both on a buffer event

说明

public bool EventBufferEvent::disable ( int $events )

Disable events Event::READ , Event::WRITE , or Event::READ | Event::WRITE on a buffer event.

参数

events

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBufferEvent::enable

EventBufferEvent::enable

Enable events read, write, or both on a buffer event

说明

public bool EventBufferEvent::enable ( int $events )

Enable events Event::READ , Event::WRITE , or Event::READ | Event::WRITE on a buffer event.

参数

events
Event::READ , Event::WRITE , or Event::READ | Event::WRITE on a buffer event.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBufferEvent::disable

EventBufferEvent::free

Free a buffer event

说明

public void EventBufferEvent::free ( void )

Free resources allocated by buffer event.

Usually there is no need to call this method, since normally it is done within internal object destructors. However, sometimes we have a long-time script allocating lots of instances, or a script with a heavy memory usage, where we need to free resources as soon as possible. In such cases EventBufferEvent::free may be used to protect the script against running up to the memory_limit .

参数

此函数没有参数。

返回值

没有返回值。

EventBufferEvent::getDnsErrorString

Returns string describing the last failed DNS lookup attempt

说明

public string EventBufferEvent::getDnsErrorString ( void )

Returns string describing the last failed DNS lookup attempt made by EventBufferEvent::connectHost , or an empty string, if there is no DNS error detected.

参数

此函数没有参数。

返回值

Returns a string describing DNS lookup error, or an empty string for no error.

参见

  • EventBufferEvent::connectHost

EventBufferEvent::getEnabled

Returns bitmask of events currently enabled on the buffer event

说明

public int EventBufferEvent::getEnabled ( void )

Returns bitmask of events currently enabled on the buffer event

参数

此函数没有参数。

返回值

Returns integer representing a bitmask of events currently enabled on the buffer event

参见

  • EventBufferEvent::enable
  • EventBufferEvent::disable

EventBufferEvent::getInput

Returns underlying input buffer associated with current buffer event

说明

public EventBuffer EventBufferEvent::getInput ( void )

Returns underlying input buffer associated with current buffer event. An input buffer is a storage for data to read.

Note, there is also input property of EventBufferEvent class.

参数

此函数没有参数。

返回值

Returns instance of EventBuffer input buffer associated with current buffer event.

范例

示例 #1 Buffer event's read callback

<?php
function readcb($bev, $base) {
    $input = $bev->input; //$bev->getInput();

    while (($n = $input->remove($buf, 1024)) > 0) {
        echo $buf;
    }
}
?>

参见

  • EventBufferEvent::getOutput

EventBufferEvent::getOutput

Returns underlying output buffer associated with current buffer event

说明

public EventBuffer EventBufferEvent::getOutput ( void )

Returns underlying output buffer associated with current buffer event. An output buffer is a storage for data to be written.

Note, there is also output property of EventBufferEvent class.

参数

此函数没有参数。

返回值

Returns instance of EventBuffer output buffer associated with current buffer event.

范例

示例 #1 EventBufferEvent::getOutput example

<?php
$base = new EventBase();

$dns_base = new EventDnsBase($base, TRUE); // Use async DNS resolving
if (!$dns_base) {
    exit("Failed to init DNS Base\n");
}

$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
    EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS,
    "readcb", /* writecb */ NULL, "eventcb", $base
);
if (!$bev) {
    exit("Failed creating bufferevent socket\n");
}

$bev->enable(Event::READ | Event::WRITE);

$output = $bev->getOutput();
if (!$output->add(
    "GET {$argv[2]} HTTP/1.0\r\n".
    "Host: {$argv[1]}\r\n".
    "Connection: Close\r\n\r\n"
)) {
    exit("Failed adding request to output buffer\n");
}

/* ... */
?>

参见

  • EventBufferEvent::getInput

EventBufferEvent::read

Read buffer's data

说明

public string EventBufferEvent::read ( int $size )

Removes up to size bytes from the input buffer. Returns a string of data read from the input buffer.

参数

size
Maximum number of bytes to read

返回值

Returns string of data read from the input buffer.

参见

  • EventBufferEvent::readBuffer

EventBufferEvent::readBuffer

Drains the entire contents of the input buffer and places them into buf

说明

public bool EventBufferEvent::readBuffer ( EventBuffer $buf )

Drains the entire contents of the input buffer and places them into buf .

参数

buf
Target buffer

返回值

Returns TRUE on success; Otherwise FALSE.

参见

  • EventBufferEvent::read

EventBufferEvent::setCallbacks

Assigns read, write and event(status) callbacks

说明

public void EventBufferEvent::setCallbacks ( callable $readcb , callable $writecb , callable $eventcb [, string $arg ] )

Assigns read, write and event(status) callbacks.

参数

readcb
Read event callback. See About buffer event callbacks .

writecb
Write event callback. See About buffer event callbacks .

eventcb
Status-change event callback. See About buffer event callbacks .

arg
A variable that will be passed to all the callbacks.

返回值

没有返回值。

参见

  • EventBufferEvent::__construct

EventBufferEvent::setPriority

Assign a priority to a bufferevent

说明

public bool EventBufferEvent::setPriority ( int $priority )

Assign a priority to a bufferevent

Warning

Only supported for socket buffer events

参数

priority
Priority value.

返回值

Returns TRUE on success. Otherwise FALSE.

EventBufferEvent::setTimeouts

Set the read and write timeout for a buffer event

说明

public bool EventBufferEvent::setTimeouts ( float $timeout_read , float $timeout_write )

Set the read and write timeout for a buffer event

参数

timeout_read
Read timeout

timeout_write
Write timeout

返回值

Returns TRUE on success. Otherwise FALSE.

EventBufferEvent::setWatermark

Adjusts read and/or write watermarks

说明

public void EventBufferEvent::setWatermark ( int $events , int $lowmark , int $highmark )

Adjusts the read watermarks, the write watermarks , or both, of a single buffer event.

A buffer event watermark is an edge, a value specifying number of bytes to be read or written before callback is invoked. By default every read/write event triggers a callback invokation. See » Fast portable non-blocking network programming with Libevent: Callbacks and watermarks

参数

events
Bitmask of Event::READ , Event::WRITE , or both.

lowmark
Minimum watermark value.

highmark
Maximum watermark value. 0 means "unlimited".

返回值

没有返回值。

EventBufferEvent::sslError

Returns most recent OpenSSL error reported on the buffer event

说明

public string EventBufferEvent::sslError ( void )

Returns most recent OpenSSL error reported on the buffer event.

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

此函数没有参数。

返回值

Returns OpenSSL error string reported on the buffer event, or FALSE, if there is no more error to return.

范例

示例 #1 EventBufferEvent::sslError example

<?php
// This callback is invoked when some even occurs on the event listener,
// e.g. connection closed, or an error occured
function ssl_event_cb($bev, $events, $ctx) {
    if ($events & EventBufferEvent::ERROR) {
        // Fetch errors from the SSL error stack
        while ($err = $bev->sslError()) {
            fprintf(STDERR, "Bufferevent error %s.\n", $err);
        }
    }

    if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
        $bev->free();
    }
}
?>

参见

  • EventBufferEvent::sslRenegotiate

EventBufferEvent::sslFilter

Create a new SSL buffer event to send its data over another buffer event

说明

public static EventBufferEvent EventBufferEvent::sslFilter ( EventBase $base , EventBufferEvent $underlying , EventSslContext $ctx , int $state [, int $options = 0 ] )

Create a new SSL buffer event to send its data over another buffer event

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

base
Associated event base.

underlying
A socket buffer event to use for this SSL.

ctx
Object of EventSslContext class.

state
The current state of SSL connection: EventBufferEvent::SSL_OPEN , EventBufferEvent::SSL_ACCEPTING or EventBufferEvent::SSL_CONNECTING .

options
One or more buffer event options.

返回值

Returns a new SSL EventBufferEvent object.

范例

示例 #1 Simple SMTP server

<?php
 /*
 * Author: Andrew Rose <hello at andrewrose dot co dot uk>
 *
 * Usage:
 * 1) Prepare cert.pem certificate and privkey.pem private key files.
 * 2) Launch the server script
 * 3) Open TLS connection, e.g.:
 *      $ openssl s_client -connect localhost:25 -starttls smtp -crlf
 * 4) Start testing the commands listed in `cmd` method below.
 */

class Handler {
    public $domainName = FALSE;
    public $connections = [];
    public $buffers = [];
    public $maxRead = 256000;

    public function __construct() {
        $this->ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, [
            EventSslContext::OPT_LOCAL_CERT  => 'cert.pem',
            EventSslContext::OPT_LOCAL_PK    => 'privkey.pem',
            //EventSslContext::OPT_PASSPHRASE  => '',
            EventSslContext::OPT_VERIFY_PEER => false, // change to true with authentic cert
            EventSslContext::OPT_ALLOW_SELF_SIGNED => true // change to false with authentic cert
        ]);

        $this->base = new EventBase();
        if (!$this->base) {
            exit("Couldn't open event base\n");
        }

        if (!$this->listener = new EventListener($this->base,
            [$this, 'ev_accept'],
            $this->ctx,
            EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE,
            -1,
            '0.0.0.0:25'))
        {
            exit("Couldn't create listener\n");
        }

        $this->listener->setErrorCallback([$this, 'ev_error']);
        $this->base->dispatch();
    }

    public function ev_accept($listener, $fd, $address, $ctx) {
        static $id = 0;
        $id += 1;

        $this->connections[$id]['clientData'] = '';
        $this->connections[$id]['cnx'] = new EventBufferEvent($this->base, $fd,
            EventBufferEvent::OPT_CLOSE_ON_FREE);

        if (!$this->connections[$id]['cnx']) {
            echo "Failed creating buffer\n";
            $this->base->exit(NULL);
            exit(1);
        }

        $this->connections[$id]['cnx']->setCallbacks([$this, "ev_read"], NULL,
            [$this, 'ev_error'], $id);
        $this->connections[$id]['cnx']->enable(Event::READ | Event::WRITE);

        $this->ev_write($id, '220 '.$this->domainName." wazzzap?\r\n");
    }

    function ev_error($listener, $ctx) {
        $errno = EventUtil::getLastSocketErrno();

        fprintf(STDERR, "Got an error %d (%s) on the listener. Shutting down.\n",
            $errno, EventUtil::getLastSocketError());

        if ($errno != 0) {
            $this->base->exit(NULL);
            exit();
        }
    }

    public function ev_close($id) {
        $this->connections[$id]['cnx']->disable(Event::READ | Event::WRITE);
        unset($this->connections[$id]);
    }

    protected function ev_write($id, $string) {
        echo 'S('.$id.'): '.$string;
        $this->connections[$id]['cnx']->write($string);
    }

    public function ev_read($buffer, $id) {
        while($buffer->input->length > 0) {
            $this->connections[$id]['clientData'] .= $buffer->input->read($this->maxRead);
            $clientDataLen = strlen($this->connections[$id]['clientData']);

            if($this->connections[$id]['clientData'][$clientDataLen-1] == "\n"
                && $this->connections[$id]['clientData'][$clientDataLen-2] == "\r")
            {
                // remove the trailing \r\n
                $line = substr($this->connections[$id]['clientData'], 0,
                    strlen($this->connections[$id]['clientData']) - 2);

                $this->connections[$id]['clientData'] = '';
                $this->cmd($buffer, $id, $line);
            }
        }
    }

    protected function cmd($buffer, $id, $line) {
        switch ($line) {
            case strncmp('EHLO ', $line, 4):
                $this->ev_write($id, "250-STARTTLS\r\n");
                $this->ev_write($id, "250 OK ehlo\r\n");
                break;

            case strncmp('HELO ', $line, 4):
                $this->ev_write($id, "250-STARTTLS\r\n");
                $this->ev_write($id, "250 OK helo\r\n");
                break;

            case strncmp('QUIT', $line, 3):
                $this->ev_write($id, "250 OK quit\r\n");
                $this->ev_close($id);
                break;

            case strncmp('STARTTLS', $line, 3):
                $this->ev_write($id, "220 Ready to start TLS\r\n");
                $this->connections[$id]['cnx'] = EventBufferEvent::sslFilter($this->base,
                    $this->connections[$id]['cnx'], $this->ctx,
                    EventBufferEvent::SSL_ACCEPTING,
                    EventBufferEvent::OPT_CLOSE_ON_FREE);
                $this->connections[$id]['cnx']->setCallbacks([$this, "ev_read"], NULL, [$this, 'ev_error'], $id);
                $this->connections[$id]['cnx']->enable(Event::READ | Event::WRITE);
                break;

            default:
                echo 'unknown command: '.$line."\n";
                break;
        }
    }
}

new Handler();

参见

  • EventBufferEvent::sslSocket

EventBufferEvent::sslGetCipherInfo

Returns a textual description of the cipher

说明

public string EventBufferEvent::sslGetCipherInfo ( void )

Retrieves description of the current cipher by means of the SSL_CIPHER_description SSL API function (see SSL_CIPHER_get_name(3) man page).

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

此函数没有参数。

返回值

Returns a textual description of the cipher on success, or FALSE on error.

EventBufferEvent::sslGetCipherName

Returns the current cipher name of the SSL connection

说明

public string EventBufferEvent::sslGetCipherName ( void )

Retrieves name of cipher used by current SSL connection.

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

此函数没有参数。

返回值

Returns the current cipher name of the SSL connection, or FALSE on error.

EventBufferEvent::sslGetCipherVersion

Returns version of cipher used by current SSL connection

说明

public string EventBufferEvent::sslGetCipherVersion ( void )

Retrieves version of cipher used by current SSL connection.

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

此函数没有参数。

返回值

Returns the current cipher version of the SSL connection, or FALSE on error.

EventBufferEvent::sslGetProtocol

Returns the name of the protocol used for current SSL connection

说明

public string EventBufferEvent::sslGetProtocol ( void )

Returns the name of the protocol used for current SSL connection.

Note:

This function is available only if Event is compiled with OpenSSL support.

参数

此函数没有参数。

返回值

Returns the name of the protocol used for current SSL connection.

EventBufferEvent::sslRenegotiate

Tells a bufferevent to begin SSL renegotiation

说明

public void EventBufferEvent::sslRenegotiate ( void )

Tells a bufferevent to begin SSL renegotiation.

Warning

Calling this function tells the SSL to renegotiate, and the buffer event to invoke appropriate callbacks. This is an advanced topic; this should be generally avoided unless one really knows what he/she does, especially since many SSL versions have had known security issues related to renegotiation.

参数

此函数没有参数。

返回值

没有返回值。

EventBufferEvent::sslSocket

Creates a new SSL buffer event to send its data over an SSL on a socket

说明

public static EventBufferEvent EventBufferEvent::sslSocket ( EventBase $base , mixed $socket , EventSslContext $ctx , int $state [, int $options ] )

Creates a new SSL buffer event to send its data over an SSL on a socket.

参数

base
Associated event base.

socket
Socket to use for this SSL. Can be stream or socket resource, numeric file descriptor, or NULL. If socket is NULL, it is assumed that the file descriptor for the socket will be assigned later, for instance, by means of EventBufferEvent::connectHost method.

ctx
Object of EventSslContext class.

state
The current state of SSL connection: EventBufferEvent::SSL_OPEN , EventBufferEvent::SSL_ACCEPTING or EventBufferEvent::SSL_CONNECTING .

options
The buffer event options.

返回值

Returns EventBufferEvent object.

参见

  • EventBufferEvent::sslFilter

EventBufferEvent::write

Adds data to a buffer event's output buffer

说明

public bool EventBufferEvent::write ( string $data )

Adds data to a buffer event's output buffer

参数

data
Data to be added to the underlying buffer.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBufferEvent::writeBuffer

EventBufferEvent::writeBuffer

Adds contents of the entire buffer to a buffer event's output buffer

说明

public bool EventBufferEvent::writeBuffer ( EventBuffer $buf )

Adds contents of the entire buffer to a buffer event's output buffer

参数

buf
Source EventBuffer object.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventBufferEvent::write

简介

Represents configuration structure which could be used in construction of the EventBase .

类摘要

EventConfig

final class EventConfig {

/* Constants */

const integer EventConfig::FEATURE_ET = 1 ;

const integer EventConfig::FEATURE_O1 = 2 ;

const integer EventConfig::FEATURE_FDS = 4 ;

/* 方法 */

public bool avoidMethod ( string $method )

public __construct ( void )

public bool requireFeatures ( int $feature )

public void setMaxDispatchInterval ( int $max_interval , int $max_callbacks , int $min_priority )

}

预定义常量

EventConfig::FEATURE_ET
Requires a backend method that supports edge-triggered I/O.

EventConfig::FEATURE_O1
Requires a backend method where adding or deleting a single event, or having a single event become active, is an O(1) operation.

EventConfig::FEATURE_FDS
Requires a backend method that can support arbitrary file descriptor types, and not just sockets.

EventConfig::avoidMethod

Tells libevent to avoid specific event method

说明

public bool EventConfig::avoidMethod ( string $method )

Tells libevent to avoid specific event method(backend). See » Creating an event base .

参数

method
The backend method to avoid. See EventConfig constants .

返回值

Returns TRUE on success, otherwise FALSE.

范例

示例 #1 EventConfig::avoidMethod example

<?php
$cfg = new EventConfig();
if ($cfg->avoidMethod("select")) {
    echo "`select' method avoided\n";
}
?>

参见

  • EventBase::__construct

EventConfig::__construct

Constructs EventConfig object

说明

public EventConfig::__construct ( void )

Constructs EventConfig object which could be passed to EventBase::__construct constructor.

参数

此函数没有参数。

返回值

Returns EventConfig object.

范例

示例 #1 EventConfig::__construct example

<?php
// Avoiding "select" method
$cfg = new EventConfig();
if ($cfg->avoidMethod("select")) {
    echo "`select' method avoided\n";
}

// Create event_base associated with the config
$base = new EventBase($cfg);

/* Now $base is configured to avoid select backend(method) */
?>

参见

  • EventBase::__construct

EventConfig::requireFeatures

Enters a required event method feature that the application demands

说明

public bool EventConfig::requireFeatures ( int $feature )

Enters a required event method feature that the application demands

参数

feature
Bitmask of required features. See EventConfig::FEATURE_* constants

返回值

范例

示例 #1 EventConfig::requireFeatures example

<?php
$cfg = new EventConfig();

// Create event_base associated with the config
$base = new EventBase($cfg);

// Require FDS feature
if ($cfg->requireFeatures(EventConfig::FEATURE_FDS)) {
    echo "FDS feature is now requried\n";

    $base = new EventBase($cfg);
    ($base->getFeatures() & EventConfig::FEATURE_FDS)
        and print("FDS - arbitrary file descriptor types, and not just sockets\n");
}
?>

以上例程的输出类似于:

FDS feature is now requried
FDS - arbitrary file descriptor types, and not just sockets

参见

  • EventBase::getFeatures

EventConfig::setMaxDispatchInterval

Prevents priority inversion

说明

public void EventConfig::setMaxDispatchInterval ( int $max_interval , int $max_callbacks , int $min_priority )

Prevents priority inversion by limiting how many low-priority event callbacks can be invoked before checking for more high-priority events.

Note:

Available since libevent 2.1.0-alpha .

参数

max_interval
An interval after which Libevent should stop running callbacks and check for more events, or 0 , if there should be no such interval.

max_callbacks
A number of callbacks after which Libevent should stop running callbacks and check for more events, or -1 , if there should be no such limit.

min_priority
A priority below which max_interval and max_callbacks should not be enforced. If this is set to 0 , they are enforced for events of every priority; if it's set to 1 , they're enforced for events of priority 1 and above, and so on.

返回值

Returns TRUE on success, otherwise FALSE.

简介

Represents Libevent's DNS base structure. Used to resolve DNS asyncronously, parse configuration files like resolv.conf etc.

类摘要

EventDnsBase

final class EventDnsBase {

/* Constants */

const integer EventDnsBase::OPTION_SEARCH = 1 ;

const integer EventDnsBase::OPTION_NAMESERVERS = 2 ;

const integer EventDnsBase::OPTION_MISC = 4 ;

const integer EventDnsBase::OPTION_HOSTSFILE = 8 ;

const integer EventDnsBase::OPTIONS_ALL = 15 ;

/* 方法 */

public bool addNameserverIp ( string $ip )

public void addSearch ( string $domain )

public void clearSearch ( void )

public __construct ( EventBase $base , bool $initialize )

public int countNameservers ( void )

public bool loadHosts ( string $hosts )

public bool parseResolvConf ( int $flags , string $filename )

public bool setOption ( string $option , string $value )

public bool setSearchNdots ( int $ndots )

}

预定义常量

EventDnsBase::OPTION_SEARCH
Tells to read the domain and search fields from the resolv.conf file and the ndots option, and use them to decide which domains(if any) to search for hostnames that aren’t fully-qualified.

EventDnsBase::OPTION_NAMESERVERS
Tells to learn the nameservers from the resolv.conf file.

EventDnsBase::OPTION_MISC

EventDnsBase::OPTION_HOSTSFILE
Tells to read a list of hosts from /etc/hosts as part of loading the resolv.conf file.

EventDnsBase::OPTIONS_ALL
Tells to learn as much as it can from the resolv.conf file.

EventDnsBase::addNameserverIp

Adds a nameserver to the DNS base

说明

public bool EventDnsBase::addNameserverIp ( string $ip )

Adds a nameserver to the evdns_base.

参数

ip
The nameserver string, either as an IPv4 address, an IPv6 address, an IPv4 address with a port ( IPv4:Port ), or an IPv6 address with a port ( [IPv6]:Port ).

返回值

Returns TRUE on success. Otherwise FALSE.

EventDnsBase::addSearch

Adds a domain to the list of search domains

说明

public void EventDnsBase::addSearch ( string $domain )

Adds a domain to the list of search domains

参数

domain
Search domain.

返回值

没有返回值。

EventDnsBase::clearSearch

Removes all current search suffixes

说明

public void EventDnsBase::clearSearch ( void )

Removes all current search suffixes from the DNS base; the EventDnsBase::addSearch function adds a suffix.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • EventDnsBase::addSearch

EventDnsBase::__construct

Constructs EventDnsBase object

说明

public EventDnsBase::__construct ( EventBase $base , bool $initialize )

Constructs EventDnsBase object.

参数

base
Event base.

initialize
If the initialize argument is TRUE, it tries to configure the DNS base sensibly given your operating system’s default. Otherwise, it leaves the event DNS base empty, with no nameservers or options configured. In the latter case DNS base should be configured manually, e.g. with EventDnsBase::parseResolvConf .

返回值

Returns EventDnsBase object.

EventDnsBase::countNameservers

Gets the number of configured nameservers

说明

public int EventDnsBase::countNameservers ( void )

Gets the number of configured nameservers

参数

此函数没有参数。

返回值

Returns the number of configured nameservers(not necessarily the number of running nameservers). This is useful for double-checking whether our calls to the various nameserver configuration functions have been successful.

EventDnsBase::loadHosts

Loads a hosts file (in the same format as /etc/hosts) from hosts file

说明

public bool EventDnsBase::loadHosts ( string $hosts )

Loads a hosts file (in the same format as /etc/hosts ) from hosts file.

参数

hosts
Path to the hosts' file.

返回值

Returns TRUE on success. Otherwise FALSE.

EventDnsBase::parseResolvConf

Scans the resolv.conf-formatted file

说明

public bool EventDnsBase::parseResolvConf ( int $flags , string $filename )

Scans the resolv.conf-formatted file stored in filename, and read in all the options from it that are listed in flags

参数

flags
Determines what information is parsed from the resolv.conf file. See the man page for resolv.conf for the format of this file.

The following directives are not parsed from the file: sortlist, rotate, no-check-names, inet6, debug .

If this function encounters an error, the possible return values are:

  • 1 = failed to open file
  • 2 = failed to stat file
  • 3 = file too large
  • 4 = out of memory
  • 5 = short read from file
  • 6 = no nameservers listed in the file

filename
Path to resolv.conf file.

返回值

Returns TRUE on success. Otherwise FALSE.

EventDnsBase::setOption

Set the value of a configuration option

说明

public bool EventDnsBase::setOption ( string $option , string $value )

Set the value of a configuration option.

参数

option
The currently available configuration options are: "ndots" , "timeout" , "max-timeouts" , "max-inflight" , and "attempts" .

value
Option value.

返回值

Returns TRUE on success. Otherwise FALSE.

EventDnsBase::setSearchNdots

Set the 'ndots' parameter for searches

说明

public bool EventDnsBase::setSearchNdots ( int $ndots )

Set the 'ndots' parameter for searches. Sets the number of dots which, when found in a name, causes the first query to be without any search domain.

参数

ndots
The number of dots.

返回值

Returns TRUE on success. Otherwise FALSE.

简介

Represents HTTP server.

类摘要

EventHttp

final class EventHttp {

/* 方法 */

public bool accept ( mixed $socket )

public bool addServerAlias ( string $alias )

public void bind ( string $address , int $port )

public __construct ( EventBase $base [, EventSslContext $ctx = NULL ] )

public bool removeServerAlias ( string $alias )

public void setAllowedMethods ( int $methods )

public void setCallback ( string $path , string $cb [, string $arg ] )

public void setDefaultCallback ( string $cb [, string $arg ] )

public void setMaxBodySize ( int $value )

public void setMaxHeadersSize ( int $value )

public void setTimeout ( int $value )

}

EventHttp::accept

Makes an HTTP server accept connections on the specified socket stream or resource

说明

public bool EventHttp::accept ( mixed $socket )

Makes an HTTP server accept connections on the specified socket stream or resource. The socket should be ready to accept connections.

Can be called multiple times to accept connections on different sockets.

Note:

To bind a socket, listen , and accept connections on the socket in s single call use EventHttp::bind . EventHttp::accept is needed only if one already has a socket ready to accept connections.

参数

socket
Socket resource, stream or numeric file descriptor representing a socket ready to accept connections.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttp::accept example

<?php
$base = new EventBase();
$http = new EventHttp($base);

$addresses = array (
     8091 => "127.0.0.1",
     8092 => "127.0.0.2",
);
$i = 0;

$socket = array();

foreach ($addresses as $port => $ip) {
    echo $ip, " ", $port, PHP_EOL;
    $socket[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!socket_bind($socket[$i], $ip, $port)) {
        exit("socket_bind failed\n");
    }
    socket_listen($socket[$i], 0);
    socket_set_nonblock($socket[$i]);

    if (!$http->accept($socket[$i])) {
        echo "Accept failed\n";
        exit(1);
    }

    ++$i;
}

$http->setCallback("/some-page", function() {
 echo "(some-page)\n";
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
    echo "OK\n";
});

$http->setDefaultCallback(function($req) {
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
    echo "OK\n";
});

$signal = Event::signal($base, SIGINT, function () use ($base) {
    echo "Caught SIGINT. Stopping...\n";
    $base->stop();
});
$signal->add();

$base->dispatch();
echo "END\n";
// We didn't close sockets, since Libevent already sets
// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file 
// descriptor associated with the sockets.
?>

以上例程的输出类似于:

Client:
$ nc 127.0.0.1 8091
GET /about HTTP/1.0
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

Server:
127.0.0.1 8091
127.0.0.2 8092
URI: /about
OK

参见

  • EventHttp::bind

EventHttp::addServerAlias

Adds a server alias to the HTTP server object

说明

public bool EventHttp::addServerAlias ( string $alias )

Adds a server alias to the HTTP server object.

参数

alias
The alias to add.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttp::addServerAlias example

<?php
$base = new EventBase();
$http = new EventHttp($base);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!$http->bind("127.0.0.1", 8088)) {
    exit("bind(1) failed\n");
};

if (!$http->addServerAlias("local.net")) {
    exit("Failed to add server alias\n");
}

$http->setCallback("/about", function($req) {
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
});
$base->dispatch();
?>

参见

  • EventHttp::removeServerAlias

EventHttp::bind

Binds an HTTP server on the specified address and port

说明

public void EventHttp::bind ( string $address , int $port )

Binds an HTTP server on the specified address and port.

Can be called multiple times to bind the same HTTP server to multiple different ports.

参数

address
A string containing the IP address to listen(2) on.

port
The port number to listen on.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttp::bind example

<?php
$base = new EventBase();
$http = new EventHttp($base);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!$http->bind("127.0.0.1", 8088)) {
    exit("bind(1) failed\n");
};
if (!$http->bind("127.0.0.1", 8089)) {
    exit("bind(2) failed\n");
};

$http->setCallback("/about", function($req) {
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
    echo "OK\n";
});

$base->dispatch();
?>

以上例程的输出类似于:

Client:

$ nc 127.0.0.1 8088
GET /about HTTP/1.0
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

$ nc 127.0.0.1 8089
GET /unknown HTTP/1.0
Connection: close

HTTP/1.1 404 Not Found
Content-Type: text/html
Date: Wed, 13 Mar 2013 04:14:41 GMT
Content-Length: 149
Connection: close

<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL /unknown was not found on this server.</p></body></html>

Server:
URI: /about
OK

参见

  • EventHttp::accept

EventHttp::__construct

Constructs EventHttp object(the HTTP server)

说明

public EventHttp::__construct ( EventBase $base [, EventSslContext $ctx = NULL ] )

Constructs the HTTP server object.

参数

base
Associated event base.

ctx
EventSslContext class object. Turns plain HTTP server into HTTPS server. It means that if ctx is configured correctly, then the underlying buffer events will be based on OpenSSL sockets. Thus, all traffic will pass through the SSL or TLS.

Note:

This parameter is available only if Event is compiled with OpenSSL support and only with Libevent 2.1.0-alpha and higher.

返回值

Returns EventHttp object.

更新日志

版本说明
1.9.0OpenSSL support (ctx) added.

范例

示例 #1 Simple HTTP server

<?php
/*
 * Simple HTTP server.
 *
 * To test it:
 * 1) Run it on a port of your choice, e.g.:
 * $ php examples/http.php 8010
 * 2) In another terminal connect to some address on this port
 * and make GET or POST request(others are turned off here), e.g.:
 * $ nc -t 127.0.0.1 8010
 * POST /about HTTP/1.0
 * Content-Type: text/plain
 * Content-Length: 4
 * Connection: close
 * (press Enter)
 *
 * It will output
 * a=12
 * HTTP/1.0 200 OK
 * Content-Type: text/html; charset=ISO-8859-1
 * Connection: close
 *
 * $ nc -t 127.0.0.1 8010
 * GET /dump HTTP/1.0
 * Content-Type: text/plain
 * Content-Encoding: UTF-8
 * Connection: close
 * (press Enter)
 *
 * It will output:
 * HTTP/1.0 200 OK
 * Content-Type: text/html; charset=ISO-8859-1
 * Connection: close
 * (press Enter)
 *
 * $ nc -t 127.0.0.1 8010
 * GET /unknown HTTP/1.0
 * Connection: close
 *
 * It will output:
 * HTTP/1.0 200 OK
 * Content-Type: text/html; charset=ISO-8859-1
 * Connection: close
 *
 * 3) See what the server outputs on the previous terminal window.
 */

function _http_dump($req, $data) {
    static $counter      = 0;
    static $max_requests = 2;

    if (++$counter >= $max_requests)  {
        echo "Counter reached max requests $max_requests. Exiting\n";
        exit();
    }

    echo __METHOD__, " called\n";
    echo "request:"; var_dump($req);
    echo "data:"; var_dump($data);

    echo "\n===== DUMP =====\n";
    echo "Command:", $req->getCommand(), PHP_EOL;
    echo "URI:", $req->getUri(), PHP_EOL;
    echo "Input headers:"; var_dump($req->getInputHeaders());
    echo "Output headers:"; var_dump($req->getOutputHeaders());

    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";

    echo "\n >> Reading input buffer ...\n";
    $buf = $req->getInputBuffer();
    while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
        echo $s, PHP_EOL;
    }
    echo "No more data in the buffer\n";
}

function _http_about($req) {
    echo __METHOD__, PHP_EOL;
    echo "URI: ", $req->getUri(), PHP_EOL;
    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";
}

function _http_default($req, $data) {
    echo __METHOD__, PHP_EOL;
    echo "URI: ", $req->getUri(), PHP_EOL;
    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";
}

$port = 8010;
if ($argc > 1) {
    $port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
    exit("Invalid port");
}

$base = new EventBase();
$http = new EventHttp($base);
$http->setAllowedMethods(EventHttpRequest::CMD_GET | EventHttpRequest::CMD_POST);

$http->setCallback("/dump", "_http_dump", array(4, 8));
$http->setCallback("/about", "_http_about");
$http->setDefaultCallback("_http_default", "custom data value");

$http->bind("0.0.0.0", 8010);
$base->loop();
?>

以上例程的输出类似于:

a=12
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
(press Enter)

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

EventHttp::removeServerAlias

Removes server alias

说明

public bool EventHttp::removeServerAlias ( string $alias )

Removes server alias added with EventHttp::addServerAlias

参数

alias
The alias to remove.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventHttp::addServerAlias

EventHttp::setAllowedMethods

Sets the what HTTP methods are supported in requests accepted by this server, and passed to user callbacks

说明

public void EventHttp::setAllowedMethods ( int $methods )

Sets the what HTTP methods are supported in requests accepted by this server, and passed to user callbacks

If not supported they will generate a "405 Method not allowed" response.

By default this includes the following methods: GET , POST , HEAD , PUT , DELETE . See EventHttpRequest::CMD_* constants.

参数

methods
A bit mask of EventHttpRequest::CMD_* constants .

返回值

没有返回值。

EventHttp::setCallback

Sets a callback for specified URI

说明

public void EventHttp::setCallback ( string $path , string $cb [, string $arg ] )

Sets a callback for specified URI.

参数

path
The path for which to invoke the callback.

cb
The callback callable that gets invoked on requested path . It should match the following prototype:

void callback ([ EventHttpRequest $req = NULL [, mixed $arg = NULL ]] )

req
EventHttpRequest object.

arg
Custom data.

arg
Custom data.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttp::setCallback example

<?php
/*
 * Simple HTTP server.
 *
 * To test it:
 * 1) Run it on a port of your choice, e.g.:
 * $ php examples/http.php 8010
 * 2) In another terminal connect to some address on this port
 * and make GET or POST request(others are turned off here), e.g.:
 * $ nc -t 127.0.0.1 8010
 * POST /about HTTP/1.0
 * Content-Type: text/plain
 * Content-Length: 4
 * Connection: close
 * (press Enter)
 *
 * It will output
 * a=12
 * HTTP/1.0 200 OK
 * Content-Type: text/html; charset=ISO-8859-1
 * Connection: close
 *
 * 3) See what the server outputs on the previous terminal window.
 */

function _http_dump($req, $data) {
    static $counter      = 0;
    static $max_requests = 2;

    if (++$counter >= $max_requests)  {
        echo "Counter reached max requests $max_requests. Exiting\n";
        exit();
    }

    echo __METHOD__, " called\n";
    echo "request:"; var_dump($req);
    echo "data:"; var_dump($data);

    echo "\n===== DUMP =====\n";
    echo "Command:", $req->getCommand(), PHP_EOL;
    echo "URI:", $req->getUri(), PHP_EOL;
    echo "Input headers:"; var_dump($req->getInputHeaders());
    echo "Output headers:"; var_dump($req->getOutputHeaders());

    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";

    echo "\n >> Reading input buffer ...\n";
    $buf = $req->getInputBuffer();
    while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
        echo $s, PHP_EOL;
    }
    echo "No more data in the buffer\n";
}

function _http_about($req) {
    echo __METHOD__, PHP_EOL;
    echo "URI: ", $req->getUri(), PHP_EOL;
    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";
}

function _http_default($req, $data) {
    echo __METHOD__, PHP_EOL;
    echo "URI: ", $req->getUri(), PHP_EOL;
    echo "\n >> Sending reply ...";
    $req->sendReply(200, "OK");
    echo "OK\n";
}

$port = 8010;
if ($argc > 1) {
    $port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
    exit("Invalid port");
}

$base = new EventBase();
$http = new EventHttp($base);
$http->setAllowedMethods(EventHttpRequest::CMD_GET | EventHttpRequest::CMD_POST);

$http->setCallback("/dump", "_http_dump", array(4, 8));
$http->setCallback("/about", "_http_about");
$http->setDefaultCallback("_http_default", "custom data value");

$http->bind("0.0.0.0", 8010);
$base->loop();
?>

以上例程的输出类似于:

a=12
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

参见

  • EventHttp::setDefaultCallback

EventHttp::setDefaultCallback

Sets default callback to handle requests that are not caught by specific callbacks

说明

public void EventHttp::setDefaultCallback ( string $cb [, string $arg ] )

Sets default callback to handle requests that are not caught by specific callbacks

参数

cb
The callback callable . It should match the following prototype:

void callback ([ EventHttpRequest $req = NULL [, mixed $arg = NULL ]] )

req
EventHttpRequest object.

arg
Custom data.

arg
User custom data passed to the callback.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttp::setDefaultCallback example

<?php
$base = new EventBase();
$http = new EventHttp($base);

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!$http->bind("127.0.0.1", 8088)) {
    exit("bind(1) failed\n");
};

$http->setDefaultCallback(function($req) {
    echo "URI: ", $req->getUri(), PHP_EOL;
    $req->sendReply(200, "OK");
});

$base->dispatch();
?>

参见

  • EventHttp::setCallback

EventHttp::setMaxBodySize

Sets maximum request body size

说明

public void EventHttp::setMaxBodySize ( int $value )

Sets maximum request body size.

参数

value
The body size in bytes.

返回值

没有返回值。

参见

  • EventHttp::setMaxHeadersSize

EventHttp::setMaxHeadersSize

Sets maximum HTTP header size

说明

public void EventHttp::setMaxHeadersSize ( int $value )

Sets maximum HTTP header size.

参数

value
The header size in bytes.

返回值

没有返回值。

参见

  • EventHttp::setMaxHeadersSize

EventHttp::setTimeout

Sets the timeout for an HTTP request

说明

public void EventHttp::setTimeout ( int $value )

Sets the timeout for an HTTP request.

参数

value
The timeout in seconds.

返回值

没有返回值。

简介

Represents an HTTP connection.

类摘要

EventHttpConnection

class EventHttpConnection {

/* 方法 */

public __construct ( EventBase $base , EventDnsBase $dns_base , string $address , int $port [, EventSslContext $ctx = NULL ] )

public EventBase getBase ( void )

public void getPeer ( string &$address , int &$port )

public bool makeRequest ( EventHttpRequest $req , int $type , string $uri )

public void setCloseCallback ( callable $callback [, mixed $data ] )

public void setLocalAddress ( string $address )

public void setLocalPort ( int $port )

public void setMaxBodySize ( string $max_size )

public void setMaxHeadersSize ( string $max_size )

public void setRetries ( int $retries )

public void setTimeout ( int $timeout )

}

EventHttpConnection::__construct

Constructs EventHttpConnection object

说明

public EventHttpConnection::__construct ( EventBase $base , EventDnsBase $dns_base , string $address , int $port [, EventSslContext $ctx = NULL ] )

Constructs EventHttpConnection object.

参数

base
Associated event base.

dns_base
If dns_base is NULL, hostname resolution will block.

address
The address to connect to.

port
The port to connect to.

ctx
EventSslContext class object. Enables OpenSSL.

Note:

This parameter is available only if Event is compiled with OpenSSL support and only with Libevent 2.1.0-alpha and higher.

返回值

Returns EventHttpConnection object.

更新日志

版本说明
1.9.0OpenSSL support (ctx) added.

EventHttpConnection::getBase

Returns event base associated with the connection

说明

public EventBase EventHttpConnection::getBase ( void )

Returns event base associated with the connection.

参数

此函数没有参数。

返回值

On success returns EventBase object associated with the connection. Otherwise FALSE.

EventHttpConnection::getPeer

Gets the remote address and port associated with the connection

说明

public void EventHttpConnection::getPeer ( string &$address , int &$port )

Gets the remote address and port associated with the connection

参数

address
Address of the peer.

port
Port of the peer.

返回值

没有返回值。

EventHttpConnection::makeRequest

Makes an HTTP request over the specified connection

说明

public bool EventHttpConnection::makeRequest ( EventHttpRequest $req , int $type , string $uri )

Makes an HTTP request over the specified connection. type is one of EventHttpRequest::CMD_* constants.

参数

req
The connection object over which to send the request.

type
One of EventHttpRequest::CMD_* constants .

uri
The URI associated with the request.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

示例 #1 EventHttpConnection::makeRequest example

<?php
function _request_handler($req, $base) {
    echo __FUNCTION__, PHP_EOL;

    if (is_null($req)) {
        echo "Timed out\n";
    } else {
        $response_code = $req->getResponseCode();

        if ($response_code == 0) {
            echo "Connection refused\n";
        } elseif ($response_code != 200) {
            echo "Unexpected response: $response_code\n";
        } else {
            echo "Success: $response_code\n";
            $buf = $req->getInputBuffer();
            echo "Body:\n";
            while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
                echo $s, PHP_EOL;
            }
        }
    }

    $base->exit(NULL);
}

$address = "127.0.0.1";
$port = 80;

$base = new EventBase();
$conn = new EventHttpConnection($base, NULL, $address, $port);
$conn->setTimeout(5);
$req = new EventHttpRequest("_request_handler", $base);

$req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER);
$req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER);
$conn->makeRequest($req, EventHttpRequest::CMD_GET, "/index.cphp");

$base->loop();
?>

以上例程的输出类似于:

_request_handler
Success: 200
Body:
PHP, date:
2013-03-13T20:27:52+05:00

参见

  • EventHttpRequest::addHeader

EventHttpConnection::setCloseCallback

Set callback for connection close

说明

public void EventHttpConnection::setCloseCallback ( callable $callback [, mixed $data ] )

Sets callback for connection close.

参数

callback
Callback which is called when connection is closed. Should match the following prototype:

void callback ([ EventHttpConnection $conn = NULL [, mixed $arg = NULL ]] )

返回值

没有返回值。

范例

示例 #1 EventHttpConnection::setCloseCallback example

<?php
/*
 * Setting up close-connection callback
 *
 * The script handles closed connections using HTTP API.
 *
 * Usage:
 * 1) Launch the server:
 * $ php examples/http_closecb.php 4242
 *
 * 2) Launch a client in another terminal. Telnet-like
 * session should look like the following:
 *
 * $ nc -t 127.0.0.1 4242
 * GET / HTTP/1.0
 * Connection: close
 *
 * The server will output something similar to the following:
 *
 * HTTP/1.0 200 OK
 * Content-Type: multipart/x-mixed-replace;boundary=boundarydonotcross
 * Connection: close
 *
 * <html>
 *
 * 3) Terminate the client connection abruptly,
 * i.e. kill the process, or just press Ctrl-C.
 *
 * 4) Check if the server called _close_callback.
 * The script should output "_close_callback" string to standard output.
 *
 * 5) Check if the server's process has no orphaned connections,
 * e.g. with `lsof` utility.
 */

function _close_callback($conn)
{
    echo __FUNCTION__, PHP_EOL;
}

function _http_default($req, $dummy)
{
    $conn = $req->getConnection();
    $conn->setCloseCallback('_close_callback', NULL);

    /*
    By enabling Event::READ we protect the server against unclosed conections.
    This is a peculiarity of Libevent. The library disables Event::READ events
     on this connection, and the server is not notified about terminated
    connections.

    So each time client terminates connection abruptly, we get an orphaned
    connection. For instance, the following is a part of `lsof -p $PID | grep TCP`
    command after client has terminated connection:

    57-php     15057 ruslan  6u  unix 0xffff8802fb59c780   0t0  125187 socket
    58:php     15057 ruslan  7u  IPv4             125189   0t0     TCP *:4242 (LISTEN)
    59:php     15057 ruslan  8u  IPv4             124342   0t0     TCP localhost:4242->localhost:37375 (CLOSE_WAIT)

    where $PID is our process ID.

    The following block of code fixes such kind of orphaned connections.
     */
    $bev = $req->getBufferEvent();
    $bev->enable(Event::READ);
    // We have to free it explicitly. See

EventHttpRequest::getConnection

$bev->free(); // we have to free it explicitly

    $req->addHeader(
        'Content-Type',
        'multipart/x-mixed-replace;boundary=boundarydonotcross',
        EventHttpRequest::OUTPUT_HEADER
    );

    $buf = new EventBuffer();
    $buf->add('<html>');

    $req->sendReply(200, "OK");
    $req->sendReplyChunk($buf);
}

$port = 4242;
if ($argc > 1) {
    $port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
    exit("Invalid port");
}

$base = new EventBase();
$http = new EventHttp($base);

$http->setDefaultCallback("_http_default", NULL);
$http->bind("0.0.0.0", $port);
$base->loop();

?>

EventHttpConnection::setLocalAddress

Sets the IP address from which HTTP connections are made

说明

public void EventHttpConnection::setLocalAddress ( string $address )

Sets the IP address from which http connections are made.

参数

address
The IP address from which HTTP connections are made.

返回值

没有返回值。

参见

  • EventHttpConnection::setLocalPort

EventHttpConnection::setLocalPort

Sets the local port from which connections are made

说明

public void EventHttpConnection::setLocalPort ( int $port )

Sets the local port from which connections are made.

参数

port
The port number.

返回值

参见

  • EventHttpConnection::setLocalAddress

EventHttpConnection::setMaxBodySize

Sets maximum body size for the connection

说明

public void EventHttpConnection::setMaxBodySize ( string $max_size )

Sets maximum body size for the connection.

参数

max_size
The maximum body size in bytes.

返回值

没有返回值。

参见

  • EventHttpConnection::setMaxHeadersSize

EventHttpConnection::setMaxHeadersSize

Sets maximum header size

说明

public void EventHttpConnection::setMaxHeadersSize ( string $max_size )

Sets maximum header size for the connection.

参数

max_size
The maximum header size in bytes.

返回值

没有返回值。

参见

  • EventHttpConnection::setMaxBodySize

EventHttpConnection::setRetries

Sets the retry limit for the connection

说明

public void EventHttpConnection::setRetries ( int $retries )

Sets the retry limit for the connection

参数

retries
The retry limit. -1 means infinity.

返回值

没有返回值。

EventHttpConnection::setTimeout

Sets the timeout for the connection

说明

public void EventHttpConnection::setTimeout ( int $timeout )

Sets the timeout for the connection

参数

timeout
Timeout in seconds.

返回值

没有返回值。

简介

Represents an HTTP request.

类摘要

EventHttpRequest

class EventHttpRequest {

/* Constants */

const integer EventHttpRequest::CMD_GET = 1 ;

const integer EventHttpRequest::CMD_POST = 2 ;

const integer EventHttpRequest::CMD_HEAD = 4 ;

const integer EventHttpRequest::CMD_PUT = 8 ;

const integer EventHttpRequest::CMD_DELETE = 16 ;

const integer EventHttpRequest::CMD_OPTIONS = 32 ;

const integer EventHttpRequest::CMD_TRACE = 64 ;

const integer EventHttpRequest::CMD_CONNECT = 128 ;

const integer EventHttpRequest::CMD_PATCH = 256 ;

const integer EventHttpRequest::INPUT_HEADER = 1 ;

const integer EventHttpRequest::OUTPUT_HEADER = 2 ;

/* 方法 */

public bool addHeader ( string $key , string $value , int $type )

public void cancel ( void )

public void clearHeaders ( void )

public void closeConnection ( void )

public __construct ( callable $callback [, mixed $data = NULL ] )

public void findHeader ( string $key , string $type )

public void free ( void )

public EventBufferEvent closeConnection ( void )

public void getCommand ( void )

public EventHttpConnection closeConnection ( void )

public string getHost ( void )

public EventBuffer getInputBuffer ( void )

public array getInputHeaders ( void )

public EventBuffer getOutputBuffer ( void )

public void getOutputHeaders ( void )

public int getResponseCode ( void )

public string getUri ( void )

public void removeHeader ( string $key , string $type )

public void sendError ( int $error [, string $reason = NULL ] )

public void sendReply ( int $code , string $reason [, EventBuffer $buf ] )

public void sendReplyChunk ( EventBuffer $buf )

public void sendReplyEnd ( void )

public void sendReplyStart ( int $code , string $reason )

}

预定义常量

EventHttpRequest::CMD_GET
GET method(command)

EventHttpRequest::CMD_POST
POST method(command)

EventHttpRequest::CMD_HEAD
HEAD method(command)

EventHttpRequest::CMD_PUT
PUT method(command)

EventHttpRequest::CMD_DELETE
DELETE command(method)

EventHttpRequest::CMD_OPTIONS
OPTIONS method(command)

EventHttpRequest::CMD_TRACE
TRACE method(command)

EventHttpRequest::CMD_CONNECT
CONNECT method(command)

EventHttpRequest::CMD_PATCH
PATCH method(command)

EventHttpRequest::INPUT_HEADER
Request input header type.

EventHttpRequest::OUTPUT_HEADER
Request output header type.

EventHttpRequest::addHeader

Adds an HTTP header to the headers of the request

说明

public bool EventHttpRequest::addHeader ( string $key , string $value , int $type )

Adds an HTTP header to the headers of the request.

参数

key
Header name.

value
Header value.

type
One of EventHttpRequest::*_HEADER constants .

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventHttpRequest::removeHeader

EventHttpRequest::cancel

Cancels a pending HTTP request

说明

public void EventHttpRequest::cancel ( void )

Cancels a pending HTTP request.

Cancels an ongoing HTTP request. The callback associated with this request is not executed and the request object is freed. If the request is currently being processed, e.g. it is ongoing, the corresponding EventHttpConnection object is going to get reset.

A request cannot be canceled if its callback has executed already. A request may be canceled reentrantly from its chunked callback.

参数

此函数没有参数。

返回值

没有返回值。

EventHttpRequest::clearHeaders

Removes all output headers from the header list of the request

说明

public void EventHttpRequest::clearHeaders ( void )

Removes all output headers from the header list of the request.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • EventHttpRequest::addHeader

EventHttpRequest::closeConnection

Closes associated HTTP connection

说明

public void EventHttpRequest::closeConnection ( void )

Closes HTTP connection associated with the request.

参数

此函数没有参数。

返回值

没有返回值。

EventHttpRequest::__construct

Constructs EventHttpRequest object

说明

public EventHttpRequest::__construct ( callable $callback [, mixed $data = NULL ] )

Constructs EventHttpRequest object.

参数

callback
Gets invoked on requesting path. Should match the following prototype:

void callback ([ EventHttpRequest $req = NULL [, mixed $arg = NULL ]] )

data
User custom data passed to the callback.

返回值

Returns EventHttpRequest object.

范例

示例 #1 EventHttpRequest::__construct example

<?php

function _request_handler($req, $base) {
    echo __FUNCTION__, PHP_EOL;

    if (is_null($req)) {
        echo "Timed out\n";
    } else {
        $response_code = $req->getResponseCode();

        if ($response_code == 0) {
            echo "Connection refused\n";
        } elseif ($response_code != 200) {
            echo "Unexpected response: $response_code\n";
        } else {
            echo "Success: $response_code\n";
            $buf = $req->getInputBuffer();
            echo "Body:\n";
            while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
                echo $s, PHP_EOL;
            }
        }
    }

    $base->exit(NULL);
}


$address = "127.0.0.1";
$port = 80;

$base = new EventBase();
$conn = new EventHttpConnection($base, NULL, $address, $port);
$conn->setTimeout(5);
$req = new EventHttpRequest("_request_handler", $base);

$req->addHeader("Host", $address, EventHttpRequest::OUTPUT_HEADER);
$req->addHeader("Content-Length", "0", EventHttpRequest::OUTPUT_HEADER);
$conn->makeRequest($req, EventHttpRequest::CMD_GET, "/index.cphp");

$base->loop();
?>

参见

  • EventHttpRequest::cancel
  • EventHttpRequest::addHeader

EventHttpRequest::findHeader

Finds the value belonging a header

说明

public void EventHttpRequest::findHeader ( string $key , string $type )

Finds the value belonging a header.

参数

key
The header name.

type
One of EventHttpRequest::*_HEADER constants .

返回值

Returns NULL if header not found.

参见

  • EventHttpRequest::addHeader

EventHttpRequest::free

Frees the object and removes associated events

说明

public void EventHttpRequest::free ( void )

Frees the object and removes associated events.

参数

此函数没有参数。

返回值

没有返回值。

EventHttpRequest::getBufferEvent

Returns EventBufferEvent object

说明

public EventBufferEvent EventHttpRequest::closeConnection ( void )

Returns EventBufferEvent object which represents buffer event that the connection is using.

Warning

The reference counter of the returned object will be incremented by one to protect internal structures against premature destruction when the method is called from a user callback. So the EventBufferEvent object should be freed explicitly with EventBufferEvent::free method. Otherwise memory will leak.

参数

此函数没有参数。

返回值

Returns EventBufferEvent object.

参见

  • EventHttpRequest::getConnection

EventHttpRequest::getCommand

Returns the request command(method)

说明

public void EventHttpRequest::getCommand ( void )

Returns the request command, one of EventHttpRequest::CMD_* constants.

参数

此函数没有参数。

返回值

Returns the request command, one of EventHttpRequest::CMD_* constants.

EventHttpRequest::getConnection

Returns EventHttpConnection object

说明

public EventHttpConnection EventHttpRequest::closeConnection ( void )

Returns EventHttpConnection object which represents HTTP connection associated with the request.

Warning

Libevent API allows HTTP request objects to be not bound to any HTTP connection. Therefore we can't unambiguously associate EventHttpRequest with EventHttpConnection . Thus, we construct EventHttpConnection object on-the-fly. Having no information about the event base, DNS base and connection-close callback, we just leave these fields unset.

EventHttpRequest::getConnection method is usually useful when we need to set up a callback on connection close. See EventHttpConnection::setCloseCallback .

参数

此函数没有参数。

返回值

Returns EventHttpConnection object.

参见

  • EventHttpConnection::setCloseCallback
  • EventHttpRequest::getBufferEvent

EventHttpRequest::getHost

Returns the request host

说明

public string EventHttpRequest::getHost ( void )

Returns the request host.

参数

此函数没有参数。

返回值

Returns the request host.

参见

  • EventHttpRequest::getUri
  • EventHttpRequest::getCommand

EventHttpRequest::getInputBuffer

Returns the input buffer

说明

public EventBuffer EventHttpRequest::getInputBuffer ( void )

Returns the input buffer.

参数

此函数没有参数。

返回值

Returns the input buffer.

参见

  • EventHttpRequest::getOutputBuffer

EventHttpRequest::getInputHeaders

Returns associative array of the input headers

说明

public array EventHttpRequest::getInputHeaders ( void )

Returns associative array of the input headers.

参数

此函数没有参数。

返回值

Returns associative array of the input headers.

参见

  • EventHttpRequest::getOutputHeaders

EventHttpRequest::getOutputBuffer

Returns the output buffer of the request

说明

public EventBuffer EventHttpRequest::getOutputBuffer ( void )

Returns the output buffer of the request.

参数

此函数没有参数。

返回值

Returns the output buffer of the request.

参见

  • EventHttpRequest::getInputBuffer

EventHttpRequest::getOutputHeaders

Returns associative array of the output headers

说明

public void EventHttpRequest::getOutputHeaders ( void )

Returns associative array of the output headers.

参数

此函数没有参数。

返回值

参见

  • EventHttpRequest::getInputHeaders

EventHttpRequest::getResponseCode

Returns the response code

说明

public int EventHttpRequest::getResponseCode ( void )

Returns the response code.

参数

此函数没有参数。

返回值

Returns the response code of the request.

参见

  • EventHttpRequest::getCommand
  • EventHttpRequest::getHost
  • EventHttpRequest::getUri

EventHttpRequest::getUri

Returns the request URI

说明

public string EventHttpRequest::getUri ( void )

Returns the request URI

参数

此函数没有参数。

返回值

Returns the request URI

参见

  • EventHttpRequest::getCommand
  • EventHttpRequest::getHost
  • EventHttpRequest::getResponseCode

EventHttpRequest::removeHeader

Removes an HTTP header from the headers of the request

说明

public void EventHttpRequest::removeHeader ( string $key , string $type )

Removes an HTTP header from the headers of the request.

参数

key
The header name.

type
type is one of EventHttpRequest::*_HEADER constants.

返回值

Removes an HTTP header from the headers of the request.

参见

  • EventHttpRequest::addHeader

EventHttpRequest::sendError

Send an HTML error message to the client

说明

public void EventHttpRequest::sendError ( int $error [, string $reason = NULL ] )

Send an HTML error message to the client.

参数

error
The HTTP error code.

reason
A brief explanation ofthe error. If NULL, the standard meaning of the error code will be used.

返回值

没有返回值。

范例

示例 #1 EventHttpRequest::sendError example

<?php
function _http_400($req) {
    $req->sendError(400);
}

$base = new EventBase();
$http = new EventHttp($base);

$http->setCallback("/err400", "_http_400");

$http->bind("0.0.0.0", 8010);
$base->loop();
?>

参见

  • EventHttpRequest::sendReply

EventHttpRequest::sendReply

Send an HTML reply to the client

说明

public void EventHttpRequest::sendReply ( int $code , string $reason [, EventBuffer $buf ] )

Send an HTML reply to the client. The body of the reply consists of data in optional buf parameter.

参数

code
The HTTP response code to send.

reason
A brief message to send with the response code.

buf
The body of the response.

返回值

没有返回值。

参见

  • EventHttpRequest::sendError
  • EventHttpRequest::sendReplyChunk

EventHttpRequest::sendReplyChunk

Send another data chunk as part of an ongoing chunked reply

说明

public void EventHttpRequest::sendReplyChunk ( EventBuffer $buf )

Send another data chunk as part of an ongoing chunked reply. After calling this method buf will be empty.

参数

buf
The data chunk to send as part of the reply.

返回值

没有返回值。

参见

  • EventHttpRequest::sendReplyStart
  • EventHttpRequest::sendReplyEnd

EventHttpRequest::sendReplyEnd

Complete a chunked reply, freeing the request as appropriate

说明

public void EventHttpRequest::sendReplyEnd ( void )

Complete a chunked reply, freeing the request as appropriate.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • EventHttpRequest::sendReplyStart
  • EventHttpRequest::sendReplyChunk

EventHttpRequest::sendReplyStart

Initiate a chunked reply

说明

public void EventHttpRequest::sendReplyStart ( int $code , string $reason )

Initiate a reply that uses Transfer-Encoding chunked .

This allows the caller to stream the reply back to the client and is useful when either not all of the reply data is immediately available or when sending very large replies.

The caller needs to supply data chunks with EventHttpRequest::sendReplyChunk and complete the reply by calling EventHttpRequest::sendReplyEnd .

参数

code
The HTTP response code to send.

reason
A brief message to send with the response code.

返回值

没有返回值。

参见

  • EventHttpRequest::sendReplyChunk
  • EventHttpRequest::sendReplyEnd

简介

Represents a connection listener.

类摘要

EventListener

final class EventListener {

/* Constants */

const integer EventListener::OPT_LEAVE_SOCKETS_BLOCKING = 1 ;

const integer EventListener::OPT_CLOSE_ON_FREE = 2 ;

const integer EventListener::OPT_CLOSE_ON_EXEC = 4 ;

const integer EventListener::OPT_REUSEABLE = 8 ;

const integer EventListener::OPT_THREADSAFE = 16 ;

/* 属性 */

public readonly int $fd ;

/* 方法 */

public __construct ( EventBase $base , callable $cb , mixed $data , int $flags , int $backlog , mixed $target )

public bool disable ( void )

public bool enable ( void )

public void getBase ( void )

public static bool getSocketName ( string &$address [, mixed &$port ] )

public void setCallback ( callable $cb [, mixed $arg = NULL ] )

public void setErrorCallback ( string $cb )

}

属性

fd
Numeric file descriptor of the underlying socket. (Added in event-1.6.0 .)

预定义常量

EventListener::OPT_LEAVE_SOCKETS_BLOCKING
By default Libevent turns underlying file descriptors, or sockets, to non-blocking mode. This flag tells Libevent to leave them in blocking mode.

EventListener::OPT_CLOSE_ON_FREE
If this option is set, the connection listener closes its underlying socket when the EventListener object is freed.

EventListener::OPT_CLOSE_ON_EXEC
If this option is set, the connection listener sets the close-on-exec flag on the underlying listener socket. See platform documentation for fcntl and FD_CLOEXEC for more information.

EventListener::OPT_REUSEABLE
By default on some platforms, once a listener socket is closed, no other socket can bind to the same port until a while has passed. Setting this option makes Libevent mark the socket as reusable, so that once it is closed, another socket can be opened to listen on the same port.

EventListener::OPT_THREADSAFE
Allocate locks for the listener, so that it’s safe to use it from multiple threads.

EventListener::__construct

Creates new connection listener associated with an event base

说明

public EventListener::__construct ( EventBase $base , callable $cb , mixed $data , int $flags , int $backlog , mixed $target )

Creates new connection listener associated with an event base.

参数

base
Associated event base.

cb
A callable that will be invoked when new connection received.

data
Custom user data attached to cb .

flags
Bit mask of EventListener::OPT_* constants. See EventListener constants .

backlog
Controls the maximum number of pending connections that the network stack should allow to wait in a not-yet-accepted state at any time; see documentation for your system’s listen function for more details. If backlog is negative, Libevent tries to pick a good value for the backlog ; if it is zero, Event assumes that listen is already called on the socket( target )

target
May be string, socket resource, or a stream associated with a socket. In case if target is a string, the string will be parsed as network address. It will be interpreted as a UNIX domain socket path, if prefixed with 'unix:' , e.g. 'unix:/tmp/my.sock' .

返回值

Returns EventListener object representing the event connection listener.

更新日志

版本说明
1.5.0UNIX domain sockets' support added.

范例

示例 #1 EventListener::__construct example

<?php
/*
 * Simple echo server based on libevent's connection listener.
 *
 * Usage:
 * 1) In one terminal window run:
 *
 * $ php listener.php 9881
 *
 * 2) In another terminal window open up connection, e.g.:
 *
 * $ nc 127.0.0.1 9881
 *
 * 3) start typing. The server should repeat the input.
 */

class MyListenerConnection {
    private $bev, $base;

    public function __destruct() {
        $this->bev->free();
    }

    public function __construct($base, $fd) {
        $this->base = $base;

        $this->bev = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE);

        $this->bev->setCallbacks(array($this, "echoReadCallback"), NULL,
            array($this, "echoEventCallback"), NULL);

        if (!$this->bev->enable(Event::READ)) {
            echo "Failed to enable READ\n";
            return;
        }
    }

    public function echoReadCallback($bev, $ctx) {
        // Copy all the data from the input buffer to the output buffer
        
        // Variant #1
        $bev->output->addBuffer($bev->input);

        /* Variant #2 */
        /*
        $input    = $bev->getInput();
        $output = $bev->getOutput();
        $output->addBuffer($input);
        */
    }

    public function echoEventCallback($bev, $events, $ctx) {
        if ($events & EventBufferEvent::ERROR) {
            echo "Error from bufferevent\n";
        }

        if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
            //$bev->free();
            $this->__destruct();
        }
    }
}

class MyListener {
    public $base,
        $listener,
        $socket;
    private $conn = array();

    public function __destruct() {
        foreach ($this->conn as &$c) $c = NULL;
    }

    public function __construct($port) {
        $this->base = new EventBase();
        if (!$this->base) {
            echo "Couldn't open event base";
            exit(1);
        }

        // Variant #1
        /*
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if (!socket_bind($this->socket, '0.0.0.0', $port)) {
            echo "Unable to bind socket\n";
            exit(1);
        }
        $this->listener = new EventListener($this->base,
            array($this, "acceptConnCallback"), $this->base,
            EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE,
            -1, $this->socket);
         */

        // Variant #2
         $this->listener = new EventListener($this->base,
             array($this, "acceptConnCallback"), $this->base,
             EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE, -1,
             "0.0.0.0:$port");

        if (!$this->listener) {
            echo "Couldn't create listener";
            exit(1);
        }

        $this->listener->setErrorCallback(array($this, "accept_error_cb"));
    }

    public function dispatch() {
        $this->base->dispatch();
    }

    // This callback is invoked when there is data to read on $bev
    public function acceptConnCallback($listener, $fd, $address, $ctx) {
        // We got a new connection! Set up a bufferevent for it. */
        $base = $this->base;
        $this->conn[] = new MyListenerConnection($base, $fd);
    }

    public function accept_error_cb($listener, $ctx) {
        $base = $this->base;

        fprintf(STDERR, "Got an error %d (%s) on the listener. "
            ."Shutting down.\n",
            EventUtil::getLastSocketErrno(),
            EventUtil::getLastSocketError());

        $base->exit(NULL);
    }
}

$port = 9808;

if ($argc > 1) {
    $port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
    exit("Invalid port");
}

$l = new MyListener($port);
$l->dispatch();
?>

EventListener::disable

Disables an event connect listener object

说明

public bool EventListener::disable ( void )

Disables an event connect listener object

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventListener::enable

EventListener::enable

Enables an event connect listener object

说明

public bool EventListener::enable ( void )

Enables an event connect listener object

参数

此函数没有参数。

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • EventListener::disable

EventListener::getBase

Returns event base associated with the event listener

说明

public void EventListener::getBase ( void )

Returns event base associated with the event listener.

参数

此函数没有参数。

返回值

Returns event base associated with the event listener.

EventListener::getSocketName

Retreives the current address to which the listener's socket is bound

说明

public static bool EventListener::getSocketName ( string &$address [, mixed &$port ] )

Retreives the current address to which the listener's socket is bound.

参数

address
Output parameter. IP-address depending on the socket address family.

port
Output parameter. The port the socket is bound to.

返回值

Returns TRUE on success. Otherwise FALSE.

EventListener::setCallback

The setCallback purpose

说明

public void EventListener::setCallback ( callable $cb [, mixed $arg = NULL ] )

Adjust event connect listener's callback and optionally the callback argument.

参数

cb
The new callback for new connections. Ignored if NULL.

Should match the following prototype:

void callback ([ EventListener $listener = NULL [, mixed $fd = NULL [, array $address = NULL [, mixed $arg = NULL ]]]] )

listener
The EventListener object.

fd
The file descriptor or a resource associated with the listener.

address
Array of two elements: IP address and the server port.

arg
User custom data attached to the callback.

arg
Custom user data attached to the callback. Ignored if NULL.

返回值

没有返回值。

EventListener::setErrorCallback

Set event listener's error callback

说明

public void EventListener::setErrorCallback ( string $cb )

Set event listener's error callback

参数

cb
The error callback. Should match the following prototype:

void callback ([ EventListener $listener = NULL [, mixed $data = NULL ]] )

listener
The EventListener object.

data
User custom data attached to the callback.

返回值

参见

  • EventListener::setCallback

简介

Represents SSL_CTX structure. Provides methods and properties to configure the SSL context.

类摘要

EventSslContext

final class EventSslContext {

/* Constants */

const integer EventSslContext::SSLv2_CLIENT_METHOD = 1 ;

const integer EventSslContext::SSLv3_CLIENT_METHOD = 2 ;

const integer EventSslContext::SSLv23_CLIENT_METHOD = 3 ;

const integer EventSslContext::TLS_CLIENT_METHOD = 4 ;

const integer EventSslContext::SSLv2_SERVER_METHOD = 5 ;

const integer EventSslContext::SSLv3_SERVER_METHOD = 6 ;

const integer EventSslContext::SSLv23_SERVER_METHOD = 7 ;

const integer EventSslContext::TLS_SERVER_METHOD = 8 ;

const integer EventSslContext::OPT_LOCAL_CERT = 1 ;

const integer EventSslContext::OPT_LOCAL_PK = 2 ;

const integer EventSslContext::OPT_PASSPHRASE = 3 ;

const integer EventSslContext::OPT_CA_FILE = 4 ;

const integer EventSslContext::OPT_CA_PATH = 5 ;

const integer EventSslContext::OPT_ALLOW_SELF_SIGNED = 6 ;

const integer EventSslContext::OPT_VERIFY_PEER = 7 ;

const integer EventSslContext::OPT_VERIFY_DEPTH = 8 ;

const integer EventSslContext::OPT_CIPHERS = 9 ;

/* 属性 */

public string $local_cert ;

public string $local_pk ;

/* 方法 */

public __construct ( string $method , string $options )

}

属性

local_cert
Path to local certificate file on filesystem. It must be a PEM-encoded file which contains certificate. It can optionally contain the certificate chain of issuers.

local_pk
Path to local private key file

预定义常量

EventSslContext::SSLv2_CLIENT_METHOD
SSLv2 client method. See SSL_CTX_new(3) man page.

EventSslContext::SSLv3_CLIENT_METHOD
SSLv3 client method. See SSL_CTX_new(3) man page.

EventSslContext::SSLv23_CLIENT_METHOD
SSLv23 client method. See SSL_CTX_new(3) man page.

EventSslContext::TLS_CLIENT_METHOD
TLS client method. See SSL_CTX_new(3) man page.

EventSslContext::SSLv2_SERVER_METHOD
SSLv2 server method. See SSL_CTX_new(3) man page.

EventSslContext::SSLv3_SERVER_METHOD
SSLv3 server method. See SSL_CTX_new(3) man page.

EventSslContext::SSLv23_SERVER_METHOD
SSLv23 server method. See SSL_CTX_new(3) man page.

EventSslContext::TLS_SERVER_METHOD
TLS server method. See SSL_CTX_new(3) man page.

EventSslContext::OPT_LOCAL_CERT
Key for an item of the options' array used in EventSslContext::__construct . The option points to path of local certificate.

EventSslContext::OPT_LOCAL_PK
Key for an item of the options' array used in EventSslContext::__construct . The option points to path of the private key.

EventSslContext::OPT_PASSPHRASE
Key for an item of the options' array used in EventSslContext::__construct . Represents passphrase of the certificate.

EventSslContext::OPT_CA_FILE
Key for an item of the options' array used in EventSslContext::__construct . Represents path of the certificate authority file.

EventSslContext::OPT_CA_PATH
Key for an item of the options' array used in EventSslContext::__construct . Represents path where the certificate authority file should be searched for.

EventSslContext::OPT_ALLOW_SELF_SIGNED
Key for an item of the options' array used in EventSslContext::__construct . Represents option that allows self-signed certificates.

EventSslContext::OPT_VERIFY_PEER
Key for an item of the options' array used in EventSslContext::__construct . Represents option that tells Event to verify peer.

EventSslContext::OPT_VERIFY_DEPTH
Key for an item of the options' array used in EventSslContext::__construct . Represents maximum depth for the certificate chain verification that shall be allowed for the SSL context.

EventSslContext::OPT_CIPHERS
Key for an item of the options' array used in EventSslContext::__construct . Represents the cipher list for the SSL context.

EventSslContext::__construct

Constructs an OpenSSL context for use with Event classes

说明

public EventSslContext::__construct ( string $method , string $options )

Creates SSL context holding pointer to SSL_CTX (see the system manual).

参数

method
One of EventSslContext::*_METHOD constants .

options
Associative array of SSL context options One of EventSslContext::OPT_* constants .

返回值

Returns EventSslContext object.

范例

示例 #1 EventSslContext::__construct example

<?php
$ctx = new EventSslContext(EventSslContext::SSLv3_SERVER_METHOD, array(
     EventSslContext::OPT_LOCAL_CERT        => $local_cert,
     EventSslContext::OPT_LOCAL_PK          => $local_pk,
     EventSslContext::OPT_PASSPHRASE        => "echo server",
     EventSslContext::OPT_VERIFY_PEER       => true,
     EventSslContext::OPT_ALLOW_SELF_SIGNED => false,
));
?>

简介

EventUtil is a singleton with supplimentary methods and constants.

类摘要

EventUtil

final class EventUtil {

/* Constants */

const integer EventUtil::AF_INET = 2 ;

const integer EventUtil::AF_INET6 = 10 ;

const integer EventUtil::AF_UNSPEC = 0 ;

const integer EventUtil::LIBEVENT_VERSION_NUMBER = 33559808 ;

const integer EventUtil::SO_DEBUG = 1 ;

const integer EventUtil::SO_REUSEADDR = 2 ;

const integer EventUtil::SO_KEEPALIVE = 9 ;

const integer EventUtil::SO_DONTROUTE = 5 ;

const integer EventUtil::SO_LINGER = 13 ;

const integer EventUtil::SO_BROADCAST = 6 ;

const integer EventUtil::SO_OOBINLINE = 10 ;

const integer EventUtil::SO_SNDBUF = 7 ;

const integer EventUtil::SO_RCVBUF = 8 ;

const integer EventUtil::SO_SNDLOWAT = 19 ;

const integer EventUtil::SO_RCVLOWAT = 18 ;

const integer EventUtil::SO_SNDTIMEO = 21 ;

const integer EventUtil::SO_RCVTIMEO = 20 ;

const integer EventUtil::SO_TYPE = 3 ;

const integer EventUtil::SO_ERROR = 4 ;

const integer EventUtil::SOL_SOCKET = 1 ;

const integer EventUtil::SOL_TCP = 6 ;

const integer EventUtil::SOL_UDP = 17 ;

const integer EventUtil::IPPROTO_IP = 0 ;

const integer EventUtil::IPPROTO_IPV6 = 41 ;

/* 方法 */

abstract public __construct ( void )

public static int getLastSocketErrno ([ mixed $socket = NULL ] )

public static string getLastSocketError ([ mixed $socket ] )

public static int getSocketFd ( mixed $socket )

public static bool getSocketName ( mixed $socket , string &$address [, mixed &$port ] )

public static bool setSocketOption ( mixed $socket , int $level , int $optname , mixed $optval )

public static void sslRandPoll ( void )

}

预定义常量

EventUtil::AF_INET
IPv4 address family

EventUtil::AF_INET6
IPv6 address family

EventUtil::AF_UNSPEC
Unspecified IP address family

EventUtil::SO_DEBUG
Socket option. Enable socket debugging. Only allowed for processes with the CAP_NET_ADMIN capability or an effective user ID of 0 . (Added in event-1.6.0.)

EventUtil::SO_REUSEADDR
Socket option. Indicates that the rules used in validating addresses supplied in a bind(2) call should allow reuse of local addresses. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_KEEPALIVE
Socket option. Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer boolean flag. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_DONTROUTE
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_LINGER
Socket option. When enabled, a close(2) or shutdown(2) will not return until all queued messages for the socket have been successfully sent or the linger timeout has been reached. Otherwise, the call returns immediately and the closing is done in the background. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_BROADCAST
Socket option. Reports whether transmission of broadcast messages is supported. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_OOBINLINE
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_SNDBUF
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_RCVBUF
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_SNDLOWAT
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_RCVLOWAT
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_SNDTIMEO
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_RCVTIMEO
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_TYPE
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SO_ERROR
Socket option. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SOL_SOCKET
Socket option level. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SOL_TCP
Socket option level. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::SOL_UDP
Socket option level. See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::IPPROTO_IP
See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::IPPROTO_IPV6
See the socket(7) manual page. (Added in event-1.6.0.)

EventUtil::LIBEVENT_VERSION_NUMBER
Libevent' version number at the time when Event extension had been compiled with the library.

EventUtil::__construct

The abstract constructor

说明

abstract public EventUtil::__construct ( void )

EventUtil is a singleton. Therefore the constructor is abstract, and it is impossible to create objects based on this class.

参数

此函数没有参数。

返回值

没有返回值。

EventUtil::getLastSocketErrno

Returns the most recent socket error number

说明

public static int EventUtil::getLastSocketErrno ([ mixed $socket = NULL ] )

Returns the most recent socket error number( errno ).

参数

socket
Socket resource, stream or a file descriptor of a socket.

返回值

Returns the most recent socket error number( errno ).

参见

  • EventUtil::getLastSocketError

EventUtil::getLastSocketError

Returns the most recent socket error

说明

public static string EventUtil::getLastSocketError ([ mixed $socket ] )

Returns the most recent socket error.

参数

socket
Socket resource, stream or a file descriptor of a socket.

返回值

Returns the most recent socket error.

参见

  • EventUtil::getLastSocketErrno

EventUtil::getSocketFd

Returns numeric file descriptor of a socket, or stream

说明

public static int EventUtil::getSocketFd ( mixed $socket )

Returns numeric file descriptor of a socket or stream specified by socket argument just like the Event extension does it internally for all methods accepting socket resource or stream.

参数

socket
Socket resource or stream.

返回值

Returns numeric file descriptor of a socket, or stream. EventUtil::getSocketFd returns FALSE in case if it is whether failed to recognize the type of the underlying file, or detected that the file descriptor associated with socket is not valid.

EventUtil::getSocketName

Retreives the current address to which the socket is bound

说明

public static bool EventUtil::getSocketName ( mixed $socket , string &$address [, mixed &$port ] )

Retreives the current address to which the socket is bound.

参数

socket
Socket resource, stream or a file descriptor of a socket.

address
Output parameter. IP-address, or the UNIX domain socket path depending on the socket address family.

port
Output parameter. The port the socket is bound to. Has no meaning for UNIX domain sockets.

返回值

Returns TRUE on success. Otherwise FALSE.

EventUtil::setSocketOption

Sets socket options

说明

public static bool EventUtil::setSocketOption ( mixed $socket , int $level , int $optname , mixed $optval )

Sets socket options.

参数

socket
Socket resource, stream, or numeric file descriptor associated with the socket.

level
One of EventUtil::SOL_* constants. Specifies the protocol level at which the option resides. For example, to retrieve options at the socket level, a level parameter of EventUtil::SOL_SOCKET would be used. Other levels, such as TCP, can be used by specifying the protocol number of that level. Protocol numbers can be found by using the getprotobyname function. See EventUtil constants .

optname
Option name(type). Has the same meaning as corresponding parameter of socket_get_option function. See EventUtil constants .

optval
Accepts the same values as optval parameter of the socket_get_option function.

返回值

Returns TRUE on success. Otherwise FALSE.

参见

  • socket_get_option
  • socket_set_option

EventUtil::sslRandPoll

Generates entropy by means of OpenSSL's RAND_poll()

说明

public static void EventUtil::sslRandPoll ( void )

Generates entropy by means of OpenSSL's RAND_poll() (see the system manual).

参数

此函数没有参数。

返回值

没有返回值。