Skip to content

Commit

Permalink
Coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
kakserpom committed Jul 1, 2013
1 parent d17a566 commit 3e709ff
Showing 1 changed file with 37 additions and 66 deletions.
103 changes: 37 additions & 66 deletions PHPDaemon/Clients/Mongo/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,18 @@ class Connection extends ClientConnection
public $requests = []; // Pending requests
public $lastReqId = 0; // ID of the last request

public function onReady()
{
if ($this->user===null)
{
public function onReady() {
if ($this->user === null) {
$this->connected = true;
}
if ($this->connected)
{
if ($this->connected) {
parent::onReady();
return;
}
$this->dbname = $this->path;
$this->pool->getNonce(['dbname' => $this->dbname],
function ($result)
{
if (isset($result['$err']))
{
function ($result) {
if (isset($result['$err'])) {
Daemon::log('MongoClient: getNonce() error with '.$this->url.': '.$result['$err']);
$this->finish();
}
Expand All @@ -47,11 +42,9 @@ function ($result)
'nonce' => $result['nonce'],
'dbname' => $this->dbname,
],
function ($result)
{
if (!isset($result['ok']) || !$result['ok'])
{
Daemon::log('MongoClient: authentication error with '.$this->url.': '.$result['errmsg']);
function ($result) {
if (!isset($result['ok']) || !$result['ok']) {
Daemon::log('MongoClient: authentication error with ' . $this->url . ': ' . $result['errmsg']);
$this->finish();
return;
}
Expand All @@ -66,121 +59,99 @@ function ($result)
* Called when new data received
* @return void
*/
public function onRead()
{
public function onRead() {
start:
if ($this->freed)
{
if ($this->freed) {
return;
}
if ($this->state===self::STATE_ROOT)
{
if (false===($hdr = $this->readExact(16)))
{
if ($this->state === self::STATE_ROOT) {
if (false === ($hdr = $this->readExact(16))) {
return; // we do not have a header
}
$this->hdr = unpack('Vlen/VreqId/VresponseTo/VopCode', $hdr);
$this->hdr['plen'] = $this->hdr['len']-16;
$this->hdr['plen'] = $this->hdr['len'] - 16;
$this->setWatermark($this->hdr['plen'], $this->hdr['plen']);
$this->state = self::STATE_PACKET;
}
if ($this->state===self::STATE_PACKET)
if ($this->state === self::STATE_PACKET)
{
if (false===($pct = $this->readExact($this->hdr['plen'])))
if (false === ($pct = $this->readExact($this->hdr['plen'])))
{
return; //we do not have a whole packet
}
$this->state = self::STATE_ROOT;
$this->setWatermark(16, 0xFFFFFF);
if ($this->hdr['opCode']===Pool::OP_REPLY)
{
if ($this->hdr['opCode'] === Pool::OP_REPLY) {
$r = unpack('Vflag/VcursorID1/VcursorID2/Voffset/Vlength', binarySubstr($pct, 0, 20));
$r['cursorId'] = binarySubstr($pct, 4, 8);
$id = (int)$this->hdr['responseTo'];
if (isset($this->requests[$id]))
{
if (isset($this->requests[$id])) {
$req = $this->requests[$id];
}
else
{
else {
$req = false;
}
$flagBits = str_pad(strrev(decbin($r['flag'])), 8, '0', STR_PAD_LEFT);
$curId = ($r['cursorId']!=="\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c'.$r['cursorId'] : 'r'.$this->hdr['responseTo']);
$curId = ($r['cursorId'] !== "\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c'.$r['cursorId'] : 'r'.$this->hdr['responseTo']);

if ($req && isset($req[2]) && ($req[2]===false) && !isset($this->cursors[$curId]))
{
if ($req && isset($req[2]) && ($req[2]===false) && !isset($this->cursors[$curId])) {
$cur = new Cursor($curId, $req[0], $this);
$this->cursors[$curId] = $cur;
$cur->failure = $flagBits[1]==='1';
$cur->await = $flagBits[3]==='1';
$cur->failure = $flagBits[1] === '1';
$cur->await = $flagBits[3] === '1';
$cur->callback = $req[1];
$cur->parseOplog = isset($req[3]) && $req[3];
$cur->tailable = isset($req[4]) && $req[4];
}
else
{
else {
$cur = isset($this->cursors[$curId]) ? $this->cursors[$curId] : false;
}
if ($cur && (($r['length']===0) || (binarySubstr($curId, 0, 1)==='r')))
{
if ($cur->tailable)
{
if ($cur->finished = ($flagBits[0]=='1'))
{
if ($cur && (($r['length']===0) || (binarySubstr($curId, 0, 1) === 'r'))) {
if ($cur->tailable) {
if ($cur->finished = ($flagBits[0] == '1')) {
$cur->destroy();
}
}
else
{
else {
$cur->finished = true;
}
}

$p = 20;
$items = [];
while ($p < $this->hdr['plen'])
{
while ($p < $this->hdr['plen']) {
$dl = unpack('Vlen', binarySubstr($pct, $p, 4));
$doc = bson_decode(binarySubstr($pct, $p, $dl['len']));

if ($cur)
{
if ($cur->parseOplog && isset($doc['ts']))
{
$tsdata = unpack('Vsec/Vinc', binarySubstr($pct, $p+1+4+3, 8));
if ($cur) {
if ($cur->parseOplog && isset($doc['ts'])) {
$tsdata = unpack('Vsec/Vinc', binarySubstr($pct, $p + 8, 8));
$doc['ts'] = $tsdata['sec'].' '.$tsdata['inc'];
}
$cur->items[] = $doc;
}
else
{
else {
$items[] = $doc;
}
$p += $dl['len'];
}
$this->setFree(true);
if (isset($req[2]) && $req[2] && $req[1])
{
if (isset($req[2]) && $req[2] && $req[1]) {
call_user_func(
$req[1],
sizeof($items) ? $items[0] : false
);

if ($cur)
{
if ($cur instanceof Cursor)
{
if ($cur) {
if ($cur instanceof Cursor) {
$cur->destroy();
}
else
{
else {
unset($this->cursors[$curId]);
}
}
}
elseif ($cur)
{
elseif ($cur) {
call_user_func($cur->callback, $cur);
}
unset($this->requests[$id], $req);
Expand Down

0 comments on commit 3e709ff

Please sign in to comment.