Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Destroy session object, removing it from the protocol on end event, r…
Browse files Browse the repository at this point in the history
…efs #21
  • Loading branch information
igorw committed Feb 5, 2013
1 parent b729b57 commit cadbd3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/DNode/DNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ public function connect()
throw new \Exception("For now we only support TCP connections to a defined port");
}

$client = @stream_socket_client("tcp://{$params['host']}:{$params['port']}");
$client = stream_socket_client("tcp://{$params['host']}:{$params['port']}");
if (!$client) {
throw new \RuntimeException("No connection to DNode server in tcp://{$params['host']}:{$params['port']}");
$e = new \RuntimeException("No connection to DNode server in tcp://{$params['host']}:{$params['port']}");
$this->emit('error', array($e));

if (!count($this->listeners('error'))) {
trigger_error((string) $e, E_USER_ERROR);
}

var_dump('sleeping');
sleep(1);

return;
}

$conn = new Connection($client, $this->loop);
Expand Down
12 changes: 10 additions & 2 deletions src/DNode/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ public function create()
{
// FIXME: Random ID generation, should be unique
$id = microtime();
$this->sessions[$id] = new Session($id, $this->wrapper);
return $this->sessions[$id];
$session = new Session($id, $this->wrapper);

$that = $this;
$session->on('end', function () use ($that, $id) {
return $that->destroy($id);
});

$this->sessions[$id] = $session;

return $session;
}

public function destroy($id)
Expand Down
6 changes: 6 additions & 0 deletions src/DNode/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public function start()
public function end()
{
$this->emit('end');
$this->removeAllListeners();

$this->callbacks = array();
$this->wrapped = array();
$this->remote = null;
$this->wrapper = null;
}

public function request($method, $args)
Expand Down

0 comments on commit cadbd3a

Please sign in to comment.