Skip to content

Commit

Permalink
Auto cancel loop watcher on destruct
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 15, 2017
1 parent 1167b31 commit b467d4a
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/Server.php
Expand Up @@ -59,6 +59,17 @@ public function __construct($socket, int $chunkSize = 65536) {
Loop::disable($this->watcher);
}

/**
* Automatically cancels the loop watcher.
*/
public function __destruct() {
if (!$this->socket) {
return;
}

$this->free();
}

/**
* @return \Amp\Promise<ServerSocket|null>
*
Expand Down Expand Up @@ -88,21 +99,28 @@ public function accept(): Promise {
* Closes the server and stops accepting connections. Any socket clients accepted will not be closed.
*/
public function close() {
Loop::cancel($this->watcher);

if ($this->socket) {
\fclose($this->socket);
}

$this->free();
}

/**
* @return string|null
*/
public function getAddress() {
return $this->address;
}

private function free() {
Loop::cancel($this->watcher);

$this->socket = null;

if ($this->acceptor) {
$this->acceptor->resolve();
$this->acceptor = null;
}
}

public function getAddress() {
return $this->address;
}
}

0 comments on commit b467d4a

Please sign in to comment.