Skip to content
Browse files

Docs

  • Loading branch information...
1 parent 02dafcd commit 975b34603b63fdb0929c170991f1bfe5354e4979 @kakserpom committed Mar 19, 2013
Showing with 199 additions and 7 deletions.
  1. +2 −2 lib/Connection.php
  2. +197 −5 lib/IOStream.php
View
4 lib/Connection.php
@@ -260,7 +260,7 @@ public function connectUdp($host, $port) {
DNSClient::getInstance()->resolve($host, function($result) use ($host, $port) {
if ($result === false) {
Daemon::log(get_class($this).'->connectUdp : enable to resolve hostname: '.$host);
- $this->onStateEvent($this->bev, EventBufferEvent::ERROR);
+ $this->onStateEv($this->bev, EventBufferEvent::ERROR);
return;
}
// @todo stack of addrs
@@ -308,7 +308,7 @@ public function connectTcp($host, $port) {
DNSClient::getInstance()->resolve($this->host, function($result) use ($host, $port) {
if ($result === false) {
Daemon::log(get_class($this).'->connectTcp : enable to resolve hostname: '.$host);
- $this->onStateEvent($this->bev, EventBufferEvent::ERROR);
+ $this->onStateEv($this->bev, EventBufferEvent::ERROR);
return;
}
// @todo stack of addrs
View
202 lib/IOStream.php
@@ -7,30 +7,142 @@
* @author Zorin Vasily <kak.serpom.po.yaitsam@gmail.com>
*/
abstract class IOStream {
+
+ /**
+ * Associated pool
+ * @var object ConnectionPool
+ */
public $pool;
+ /**
+ * EOL
+ * @var string "\n"
+ */
protected $EOL = "\n";
+
+ /**
+ * EOLS_* switch
+ * @var integer
+ */
protected $EOLS;
+
+ /**
+ * Number of bytes on each read() in default onRead() implementation
+ * @deprecated Remove in 1.0 or earlier
+ * @var integer 8192
+ */
protected $readPacketSize = 8192;
+
+ /**
+ * EventBufferEvent
+ * @var object EventBufferEvent
+ */
protected $bev;
+
+ /**
+ * File descriptor
+ * @var resource|integer
+ */
protected $fd;
+
+ /**
+ * Finished?
+ * @var boolean
+ */
protected $finished = false;
+
+ /**
+ * Ready?
+ * @var boolean
+ */
protected $ready = false;
+
+ /**
+ * Writing?
+ * @var boolean
+ */
protected $writing = true;
+
+ /**
+ * Reading?
+ * @deprecated May be removed at any moment
+ * @var boolean
+ */
+
protected $reading = false;
- protected $lowMark = 1; // initial value of the minimal amout of bytes in buffer
+
+ /**
+ * Default low mark. Minimum number of bytes in buffer.
+ * @var integer
+ */
+ protected $lowMark = 1;
+
+ /**
+ * Default high mark. Maximum number of bytes in buffer.
+ * @var integer
+ */
protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer
+
+ /**
+ * Priority
+ * @var integer
+ */
protected $priority;
+
+ /**
+ * Initialized?
+ * @var boolean
+ */
protected $inited = false;
+
+ /**
+ * Current state
+ * @var integer
+ */
protected $state = 0; // stream state of the connection (application protocol level)
const STATE_ROOT = 0;
const STATE_STANDBY = 0;
+
+ /**
+ * Stack of callbacks called when writing is done
+ * @var object StackCallbacks
+ */
protected $onWriteOnce;
+
+ /**
+ * Timeout
+ * @var integer
+ */
protected $timeout = null;
+
+ /**
+ * URL
+ * @var string
+ */
protected $url;
- protected $alive = false; // alive?
+
+ /**
+ * Alive?
+ * @var boolean
+ */
+ protected $alive = false;
+
+ /**
+ * Is bevConnect used?
+ * @var boolean
+ */
protected $bevConnect = false;
+
+ /**
+ * Should we can onReadEv() in next onWriteEv()?
+ * @var boolean
+ */
protected $wRead = false;
+
+ /**
+ * Freed?
+ * @var boolean
+ */
protected $freed = false;
/**
@@ -61,14 +173,26 @@ public function __construct($fd = null, $pool = null) {
$this->onWriteOnce = new StackCallbacks;
}
+ /**
+ * Freed?
+ * @return boolean
+ */
public function isFreed() {
return $this->freed;
}
+ /**
+ * Finished?
+ * @return boolean
+ */
public function isFinished() {
return $this->finished;
}
+ /**
+ * Get file descriptor
+ * @return mixed File descriptr
+ */
public function getFd() {
return $this->fd;
}
@@ -83,6 +207,13 @@ public function setReadPacketSize($n) {
return $this;
}
+ /**
+ * Sets fd
+ * @param mixed File descriptor
+ * @param [object EventBufferEvent]
+ * @return void
+ */
+
public function setFd($fd, $bev = null) {
$this->fd = $fd;
if ($this->fd === false) {
@@ -131,6 +262,11 @@ public function setFd($fd, $bev = null) {
}
}
+ /**
+ * Sets timeout
+ * @param integer Timeout
+ * @return void
+ */
public function setTimeout($timeout) {
$this->timeout = $timeout;
if ($this->timeout !== null) {
@@ -140,11 +276,20 @@ public function setTimeout($timeout) {
}
}
+ /* Sets priority
+ * @param integer Priority
+ * @return void
+ */
public function setPriority($p) {
$this->priority = $p;
$this->bev->priority = $p;
}
+ /* Sets watermark
+ * @param integer|null Low
+ * @param integer|null High
+ * @return void
+ */
public function setWatermark($low = null, $high = null) {
if ($low != null) {
$this->lowMark = $low;
@@ -179,13 +324,21 @@ public function gets() { // @TODO: deprecate in favor of readln
return $r;
}
+ /* Reads line from buffer
+ * @param [integer EOLS_*]
+ * @return string|null
+ */
public function readLine($eol = null) {
if (!isset($this->bev)) {
return null;
}
return $this->bev->input->readLine($eol ?: $this->EOLS);
}
+ /* Drains buffer it matches the string
+ * @param string String
+ * @return boolean|null Success
+ */
public function drainIfMatch($str) {
if (!isset($this->bev)) {
return false;
@@ -205,6 +358,11 @@ public function drainIfMatch($str) {
return false;
}
+
+ /* Reads exact $n bytes of buffer without draining
+ * @param integer Number of bytes to read
+ * @return string|false
+ */
public function lookExact($n) {
if (!isset($this->bev)) {
return false;
@@ -217,26 +375,46 @@ public function lookExact($n) {
return $data;
}
+
+ /* Prepends data to input buffer
+ * @param string Data
+ * @return boolean Success
+ */
public function prependInput($str) {
if (!isset($this->bev)) {
return false;
}
return $this->bev->input->prepend($str);
}
+ /* Prepends data to output buffer
+ * @param string Data
+ * @return boolean Success
+ */
public function prependOutput($str) {
if (!isset($this->bev)) {
return false;
}
return $this->bev->output->prepend($str);
}
+ /* Reads maximum $n bytes of buffer without draining
+ * @param integer Number of bytes to read
+ * @return string|false
+ */
public function look($n) {
$data = $this->read($n);
$this->bev->input->prepend($data);
return $data;
}
+
+ /* Searches first occurence of the string in input buffer
+ * @param string Needle
+ * @param [integer Offset start]
+ * @param [integer Offset end]
+ * @return integer Position
+ */
public function search($what, $start = null, $end = null) {
return $this->bev->input->search($what, $start, $end);
}
@@ -254,6 +432,11 @@ public function readFromBufExact($n) { // @TODO: deprecate
}
}
+ /* Reads exact $n bytes from buffer
+ * @param integer Number of bytes to read
+ * @return string|false
+ */
+
public function readExact($n) {
if ($n === 0) {
return '';
@@ -407,7 +590,7 @@ protected function onFinish() {
* @param string New received data
* @return void
*/
- protected function stdin($buf) {} // @TODO: deprecate
+ protected function stdin($buf) {} // @TODO: deprecated, remove in 1.0
/**
* Close the connection
@@ -431,6 +614,11 @@ public function close() {
}
}
+
+ /**
+ * Unsets pointers of associated EventBufferEvent and File descriptr
+ * @return void
+ */
public function unsetFd() {
$this->bev = null;
$this->fd = null;
@@ -455,8 +643,12 @@ public function onReadEv($bev) {
Daemon::uncaughtExceptionHandler($e);
}
}
-
- protected function onRead() {
+
+ /**
+ * Called when new data received
+ * @return void
+ */
+ protected function onRead() { // @todo: remove this default implementation in 1.0
while (($buf = $this->read($this->readPacketSize)) !== false) {
$this->stdin($buf);
}

0 comments on commit 975b346

Please sign in to comment.
Something went wrong with that request. Please try again.